@@ -11,7 +11,6 @@ import (
1111 "github.com/pion/logging"
1212 "github.com/pion/stun"
1313 "github.com/pion/transport/v2"
14- "github.com/pion/transport/v2/stdnet"
1514 "github.com/pion/turn/v2/internal/proto"
1615 "github.com/stretchr/testify/assert"
1716)
@@ -41,52 +40,11 @@ func (c dummyTCPConn) Read(b []byte) (int, error) {
4140 return len (msg .Raw ), nil
4241}
4342
44- type dummyConnObserver struct {
45- net transport.Net
46- turnServerAddr net.Addr
47- username stun.Username
48- realm stun.Realm
49- _writeTo func (data []byte , to net.Addr ) (int , error )
50- _performTransaction func (msg * stun.Message , to net.Addr , dontWait bool ) (TransactionResult , error )
51- _onDeallocated func (relayedAddr net.Addr )
52- }
53-
54- func (obs * dummyConnObserver ) Net () transport.Net {
55- if obs .net == nil {
56- n , err := stdnet .NewNet ()
57- if err != nil {
58- return nil
59- }
60- obs .net = n
61- }
62- return obs .net
63- }
64-
65- func (obs * dummyConnObserver ) WriteTo (data []byte , to net.Addr ) (int , error ) {
66- if obs ._writeTo != nil {
67- return obs ._writeTo (data , to )
68- }
69- return 0 , nil
70- }
71-
72- func (obs * dummyConnObserver ) PerformTransaction (msg * stun.Message , to net.Addr , dontWait bool ) (TransactionResult , error ) {
73- if obs ._performTransaction != nil {
74- return obs ._performTransaction (msg , to , dontWait )
75- }
76- return TransactionResult {}, nil
77- }
78-
79- func (obs * dummyConnObserver ) OnDeallocated (relayedAddr net.Addr ) {
80- if obs ._onDeallocated != nil {
81- obs ._onDeallocated (relayedAddr )
82- }
83- }
84-
8543func TestTCPConn (t * testing.T ) {
8644 t .Run ("Connect()" , func (t * testing.T ) {
8745 var cid proto.ConnectionID = 5
88- obs := & dummyConnObserver {
89- _performTransaction : func (msg * stun.Message , to net.Addr , dontWait bool ) (TransactionResult , error ) {
46+ client := & mockClient {
47+ performTransaction : func (msg * stun.Message , to net.Addr , dontWait bool ) (TransactionResult , error ) {
9048 if msg .Type .Class == stun .ClassRequest && msg .Type .Method == stun .MethodConnect {
9149 msg , err := stun .Build (
9250 stun .TransactionID ,
@@ -114,7 +72,7 @@ func TestTCPConn(t *testing.T) {
11472 log := loggerFactory .NewLogger ("test" )
11573 alloc := TCPAllocation {
11674 allocation : allocation {
117- client : obs ,
75+ client : client ,
11876 permMap : pm ,
11977 log : log ,
12078 },
@@ -124,8 +82,8 @@ func TestTCPConn(t *testing.T) {
12482 assert .NoError (t , err )
12583 assert .Equal (t , cid , actualCid )
12684
127- obs = & dummyConnObserver {
128- _performTransaction : func (msg * stun.Message , to net.Addr , dontWait bool ) (TransactionResult , error ) {
85+ client = & mockClient {
86+ performTransaction : func (msg * stun.Message , to net.Addr , dontWait bool ) (TransactionResult , error ) {
12987 if msg .Type .Class == stun .ClassRequest && msg .Type .Method == stun .MethodConnect {
13088 msg , err = stun .Build (
13189 stun .TransactionID ,
@@ -140,7 +98,7 @@ func TestTCPConn(t *testing.T) {
14098 }
14199 alloc = TCPAllocation {
142100 allocation : allocation {
143- client : obs ,
101+ client : client ,
144102 permMap : pm ,
145103 log : log ,
146104 },
@@ -155,9 +113,9 @@ func TestTCPConn(t *testing.T) {
155113 assert .NoError (t , err )
156114
157115 loggerFactory := logging .NewDefaultLoggerFactory ()
158- obs := & dummyConnObserver {}
116+
159117 alloc := NewTCPAllocation (& AllocationConfig {
160- Client : obs ,
118+ Client : & mockClient {} ,
161119 Lifetime : time .Second ,
162120 Log : loggerFactory .NewLogger ("test" ),
163121 RelayedAddr : relayedAddr ,
@@ -176,9 +134,8 @@ func TestTCPConn(t *testing.T) {
176134 assert .NoError (t , err )
177135
178136 loggerFactory := logging .NewDefaultLoggerFactory ()
179- obs := & dummyConnObserver {}
180137 alloc := NewTCPAllocation (& AllocationConfig {
181- Client : obs ,
138+ Client : & mockClient {} ,
182139 Lifetime : time .Second ,
183140 Log : loggerFactory .NewLogger ("test" ),
184141 RelayedAddr : relayedAddr ,
@@ -201,8 +158,8 @@ func TestTCPConn(t *testing.T) {
201158
202159 var cid proto.ConnectionID = 5
203160 loggerFactory := logging .NewDefaultLoggerFactory ()
204- obs := & dummyConnObserver {
205- _performTransaction : func (msg * stun.Message , to net.Addr , dontWait bool ) (TransactionResult , error ) {
161+ client := & mockClient {
162+ performTransaction : func (msg * stun.Message , to net.Addr , dontWait bool ) (TransactionResult , error ) {
206163 typ := stun .NewType (stun .MethodConnect , stun .ClassSuccessResponse )
207164 if msg .Type .Method == stun .MethodCreatePermission {
208165 typ = stun .NewType (stun .MethodCreatePermission , stun .ClassSuccessResponse )
@@ -218,7 +175,7 @@ func TestTCPConn(t *testing.T) {
218175 },
219176 }
220177 alloc := NewTCPAllocation (& AllocationConfig {
221- Client : obs ,
178+ Client : client ,
222179 Lifetime : time .Second ,
223180 Log : loggerFactory .NewLogger ("test" ),
224181 RelayedAddr : relayedAddr ,
0 commit comments