trpc.group/trpc-go/trpc-go@v1.0.3/transport/transport_stream_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 "testing" 19 20 "github.com/stretchr/testify/assert" 21 22 "trpc.group/trpc-go/trpc-go/transport" 23 ) 24 25 type mockSvrStreamTransport struct{} 26 27 // ListenAndServe mocks stream Transport listener interface. 28 func (t *mockSvrStreamTransport) ListenAndServe(ctx context.Context, opts ...transport.ListenServeOption) error { 29 return nil 30 } 31 32 // Send mocks stream Transport send method. 33 func (t *mockSvrStreamTransport) Send(ctx context.Context, req []byte) error { 34 return nil 35 } 36 37 func (t *mockSvrStreamTransport) Close(ctx context.Context) { 38 return 39 } 40 41 type mockClientStreamTransport struct{} 42 43 // RoundTrip mocks ClientStreamTransport roundtrip method. 44 func (c *mockClientStreamTransport) RoundTrip(ctx context.Context, req []byte, 45 opts ...transport.RoundTripOption) ([]byte, error) { 46 return nil, nil 47 } 48 49 // Send is a noop implementation which mocks ClientStreamTransport.Send. 50 func (c *mockClientStreamTransport) Send(ctx context.Context, req []byte, opts ...transport.RoundTripOption) error { 51 return nil 52 } 53 54 // Recv is a noop implementation which mocks ClientStreamTransport.Recv. 55 func (c *mockClientStreamTransport) Recv(ctx context.Context, opts ...transport.RoundTripOption) ([]byte, error) { 56 return nil, nil 57 } 58 59 // Init is a noop implementation which mocks ClientStreamTransport.Init. 60 func (c *mockClientStreamTransport) Init(ctx context.Context, opts ...transport.RoundTripOption) error { 61 return nil 62 } 63 64 // Close is a noop implementation which mocks ClientStreamTransport.Close. 65 func (c *mockClientStreamTransport) Close(ctx context.Context) { 66 return 67 } 68 69 // TestGetServerStreamTransport tests register and get of server stream transport. 70 func TestGetServerStreamTransport(t *testing.T) { 71 transport.RegisterServerStreamTransport("mock", &mockSvrStreamTransport{}) 72 ts := transport.GetServerStreamTransport("mock") 73 assert.NotNil(t, ts) 74 assert.Equal(t, &mockSvrStreamTransport{}, ts) 75 } 76 77 // TestGetClientStreamTransport tests register and get client stream transport. 78 func TestGetClientStreamTransport(t *testing.T) { 79 transport.RegisterClientStreamTransport("mock", &mockClientStreamTransport{}) 80 tc := transport.GetClientStreamTransport("mock") 81 assert.NotNil(t, tc) 82 assert.Equal(t, &mockClientStreamTransport{}, tc) 83 84 defer func() { 85 err := recover() 86 assert.NotNil(t, err) 87 }() 88 transport.RegisterClientStreamTransport("mock", nil) 89 } 90 91 // TestClientStreamTransportNilPointer tests register nil client stream transport. 92 func TestClientStreamTransportNilPointer(t *testing.T) { 93 // Test ClientTransport nil. 94 defer func() { 95 err := recover() 96 assert.NotNil(t, err) 97 }() 98 99 var c *mockClientStreamTransport 100 transport.RegisterClientStreamTransport("mock", c) 101 } 102 103 // TestRegisterClientTransport_NameNil tests register of an empty named client stream transport. 104 func TestRegisterClientStreamTransport_NameNil(t *testing.T) { 105 // Test name nil. 106 defer func() { 107 err := recover() 108 assert.NotNil(t, err) 109 }() 110 111 transport.RegisterClientStreamTransport("", &mockClientStreamTransport{}) 112 } 113 114 // TestRegisterServerTransport_Nil tests register a nil server stream transport. 115 func TestRegisterServerStreamTransport_Nil(t *testing.T) { 116 defer func() { 117 err := recover() 118 assert.NotNil(t, err) 119 }() 120 transport.RegisterServerStreamTransport("mock", nil) 121 } 122 123 // TestRegisterServerTransport_NilPointer tests register a nil server stream transport. 124 func TestRegisterServerStreamTransport_NilPointer(t *testing.T) { 125 defer func() { 126 err := recover() 127 assert.NotNil(t, err) 128 }() 129 130 var ts *mockSvrStreamTransport 131 transport.RegisterServerStreamTransport("mock", ts) 132 } 133 134 // TestRegisterServerStreamTransport_EmptyName tests register of a empty named server stream 135 // transport. 136 func TestRegisterServerStreamTransport_EmptyName(t *testing.T) { 137 defer func() { 138 err := recover() 139 assert.NotNil(t, err) 140 }() 141 transport.RegisterServerStreamTransport("", &mockSvrStreamTransport{}) 142 } 143 144 // TestRegisterNilSvrStreamTransport tests nil stream transport. 145 func TestRegisterNilSvrStreamTransport(t *testing.T) { 146 defer func() { 147 err := recover() 148 assert.NotNil(t, err) 149 }() 150 transport.RegisterServerStreamTransport("mock", nil) 151 }