github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/server_conn_test.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package nphttp2
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/cloudwego/kitex/internal/test"
    24  	"github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/grpc"
    25  )
    26  
    27  func TestServerConn(t *testing.T) {
    28  	// init
    29  	npConn := newMockNpConn(mockAddr0)
    30  	npConn.mockSettingFrame()
    31  	tr, err := newMockServerTransport(npConn)
    32  	test.Assert(t, err == nil, err)
    33  	s := grpc.CreateStream(1, func(i int) {})
    34  	serverConn := newServerConn(tr, s)
    35  	defer serverConn.Close()
    36  
    37  	// test LocalAddr()
    38  	la := serverConn.LocalAddr()
    39  	test.Assert(t, la == nil)
    40  
    41  	// test RemoteAddr()
    42  	ra := serverConn.RemoteAddr()
    43  	test.Assert(t, ra.String() == mockAddr0)
    44  
    45  	// test Read()
    46  	testStr := "1234567890"
    47  	testByte := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    48  	mockStreamRecv(s, testStr)
    49  	n, err := serverConn.Read(testByte)
    50  	test.Assert(t, err == nil, err)
    51  	test.Assert(t, n == len(testStr))
    52  	test.Assert(t, string(testByte) == testStr)
    53  
    54  	// test write()
    55  	testByte = []byte(testStr)
    56  	n, err = serverConn.Write(testByte)
    57  	test.Assert(t, err == nil, err)
    58  	test.Assert(t, n == len(testStr))
    59  
    60  	// test write() short write error
    61  	n, err = serverConn.Write([]byte(""))
    62  	test.Assert(t, err != nil, err)
    63  	test.Assert(t, n == 0)
    64  
    65  	// test SetReadDeadline()
    66  	err = serverConn.SetReadDeadline(time.Now())
    67  	test.Assert(t, err == nil, err)
    68  
    69  	// test SetDeadline()
    70  	err = serverConn.SetDeadline(time.Now())
    71  	test.Assert(t, err == nil, err)
    72  
    73  	// test SetWriteDeadline()
    74  	err = serverConn.SetWriteDeadline(time.Now())
    75  	test.Assert(t, err == nil, err)
    76  }