trpc.group/trpc-go/trpc-go@v1.0.3/transport/transport_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package transport_test
    15  
    16  import (
    17  	"context"
    18  	"io"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"trpc.group/trpc-go/trpc-go/codec"
    24  	"trpc.group/trpc-go/trpc-go/transport"
    25  )
    26  
    27  type mockSvrTransport struct{}
    28  
    29  func (t *mockSvrTransport) ListenAndServe(ctx context.Context, opts ...transport.ListenServeOption) error {
    30  	return nil
    31  }
    32  
    33  type mockClientTransport struct{}
    34  
    35  func (c *mockClientTransport) RoundTrip(ctx context.Context, req []byte,
    36  	opts ...transport.RoundTripOption) ([]byte, error) {
    37  	return nil, nil
    38  }
    39  
    40  type mockFramerBuilder struct{}
    41  
    42  func (f *mockFramerBuilder) New(reader io.Reader) codec.Framer {
    43  	return nil
    44  }
    45  
    46  func TestListenAndServe(t *testing.T) {
    47  	var err error
    48  	err = transport.ListenAndServe()
    49  	assert.NotNil(t, err)
    50  }
    51  
    52  func TestRoundTrip(t *testing.T) {
    53  	_, err := transport.RoundTrip(context.Background(), nil)
    54  	assert.NotNil(t, err)
    55  }
    56  
    57  func TestGetFramerBuilder(t *testing.T) {
    58  	transport.RegisterFramerBuilder("mock", &mockFramerBuilder{})
    59  	f := transport.GetFramerBuilder("mock")
    60  	assert.NotNil(t, f)
    61  	assert.Equal(t, &mockFramerBuilder{}, f)
    62  }
    63  
    64  func TestRegisterFramerBuilder_BuilderNil(t *testing.T) {
    65  	defer func() {
    66  		err := recover()
    67  		assert.NotNil(t, err)
    68  	}()
    69  
    70  	transport.RegisterFramerBuilder("mock", nil)
    71  }
    72  
    73  func TestRegisterFramerBuilder_BuilderNilPointer(t *testing.T) {
    74  	defer func() {
    75  		err := recover()
    76  		assert.NotNil(t, err)
    77  	}()
    78  
    79  	var fb *mockFramerBuilder
    80  	transport.RegisterFramerBuilder("mock", fb)
    81  }
    82  
    83  func TestRegisterFramerBuilder_NameNil(t *testing.T) {
    84  	defer func() {
    85  		err := recover()
    86  		assert.NotNil(t, err)
    87  	}()
    88  
    89  	transport.RegisterFramerBuilder("", &mockFramerBuilder{})
    90  }
    91  
    92  func TestGetServerTransport(t *testing.T) {
    93  	transport.RegisterServerTransport("mock", &mockSvrTransport{})
    94  	ts := transport.GetServerTransport("mock")
    95  	assert.NotNil(t, ts)
    96  	assert.Equal(t, &mockSvrTransport{}, ts)
    97  }
    98  
    99  func TestGetClientTransport(t *testing.T) {
   100  	transport.RegisterClientTransport("mock", &mockClientTransport{})
   101  	tc := transport.GetClientTransport("mock")
   102  	assert.NotNil(t, tc)
   103  	assert.Equal(t, &mockClientTransport{}, tc)
   104  
   105  	// Test ClientTransport nil.
   106  	defer func() {
   107  		err := recover()
   108  		assert.NotNil(t, err)
   109  	}()
   110  	transport.RegisterClientTransport("mock", nil)
   111  }
   112  
   113  func TestClientTransportNilPointer(t *testing.T) {
   114  	// Test ClientTransport nil.
   115  	defer func() {
   116  		err := recover()
   117  		assert.NotNil(t, err)
   118  	}()
   119  
   120  	var c *mockClientTransport
   121  	transport.RegisterClientTransport("mock", c)
   122  }
   123  
   124  func TestRegisterClientTransport_NameNil(t *testing.T) {
   125  	// Test name nil.
   126  	defer func() {
   127  		err := recover()
   128  		assert.NotNil(t, err)
   129  	}()
   130  
   131  	transport.RegisterClientTransport("", &mockClientTransport{})
   132  }
   133  
   134  func TestRegisterServerTransport_Nil(t *testing.T) {
   135  	defer func() {
   136  		err := recover()
   137  		assert.NotNil(t, err)
   138  	}()
   139  	transport.RegisterServerTransport("mock", nil)
   140  }
   141  
   142  func TestRegisterServerTransport_NilPointer(t *testing.T) {
   143  	defer func() {
   144  		err := recover()
   145  		assert.NotNil(t, err)
   146  	}()
   147  
   148  	var ts *mockSvrTransport
   149  	transport.RegisterServerTransport("mock", ts)
   150  }
   151  
   152  func TestRegisterServerTransport_EmptyName(t *testing.T) {
   153  	defer func() {
   154  		err := recover()
   155  		assert.NotNil(t, err)
   156  	}()
   157  	transport.RegisterServerTransport("", &mockSvrTransport{})
   158  }
   159  
   160  func TestRegisterNilSvrTransport(t *testing.T) {
   161  	defer func() {
   162  		err := recover()
   163  		assert.NotNil(t, err)
   164  	}()
   165  	transport.RegisterServerTransport("mock", nil)
   166  }
   167  
   168  func TestRemoteAddrFromContext(t *testing.T) {
   169  	addr := transport.RemoteAddrFromContext(context.Background())
   170  	assert.Nil(t, addr)
   171  }