Skip to content

Commit 521e5ad

Browse files
committed
Remove duplicate dummyConnObserver
1 parent b6c8547 commit 521e5ad

File tree

3 files changed

+53
-106
lines changed

3 files changed

+53
-106
lines changed

internal/client/client_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
2+
// SPDX-License-Identifier: MIT
3+
4+
package client
5+
6+
import (
7+
"net"
8+
9+
"github.com/pion/stun"
10+
)
11+
12+
type mockClient struct {
13+
writeTo func(data []byte, to net.Addr) (int, error)
14+
performTransaction func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error)
15+
onDeallocated func(relayedAddr net.Addr)
16+
}
17+
18+
func (c *mockClient) WriteTo(data []byte, to net.Addr) (int, error) {
19+
if c.writeTo != nil {
20+
return c.writeTo(data, to)
21+
}
22+
return 0, nil
23+
}
24+
25+
func (c *mockClient) PerformTransaction(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
26+
if c.performTransaction != nil {
27+
return c.performTransaction(msg, to, dontWait)
28+
}
29+
return TransactionResult{}, nil
30+
}
31+
32+
func (c *mockClient) OnDeallocated(relayedAddr net.Addr) {
33+
if c.onDeallocated != nil {
34+
c.onDeallocated(relayedAddr)
35+
}
36+
}

internal/client/tcp_conn_test.go

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
8543
func 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,

internal/client/udp_conn_test.go

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,13 @@ import (
88
"testing"
99

1010
"github.com/pion/stun"
11-
"github.com/pion/transport/v2"
1211
"github.com/stretchr/testify/assert"
1312
)
1413

15-
type dummyClient struct {
16-
turnServerAddr net.Addr
17-
username stun.Username
18-
realm stun.Realm
19-
_writeTo func(data []byte, to net.Addr) (int, error)
20-
_performTransaction func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error)
21-
_onDeallocated func(relayedAddr net.Addr)
22-
}
23-
24-
func (obs *dummyClient) TURNServerAddr() net.Addr {
25-
return obs.turnServerAddr
26-
}
27-
28-
func (obs *dummyClient) Username() stun.Username {
29-
return obs.username
30-
}
31-
32-
func (obs *dummyClient) Realm() stun.Realm {
33-
return obs.realm
34-
}
35-
36-
func (obs *dummyClient) Net() transport.Net {
37-
return nil
38-
}
39-
40-
func (obs *dummyClient) WriteTo(data []byte, to net.Addr) (int, error) {
41-
if obs._writeTo != nil {
42-
return obs._writeTo(data, to)
43-
}
44-
return 0, nil
45-
}
46-
47-
func (obs *dummyClient) PerformTransaction(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
48-
if obs._performTransaction != nil {
49-
return obs._performTransaction(msg, to, dontWait)
50-
}
51-
return TransactionResult{}, nil
52-
}
53-
54-
func (obs *dummyClient) OnDeallocated(relayedAddr net.Addr) {
55-
if obs._onDeallocated != nil {
56-
obs._onDeallocated(relayedAddr)
57-
}
58-
}
59-
6014
func TestUDPConn(t *testing.T) {
6115
t.Run("bind()", func(t *testing.T) {
62-
client := &dummyClient{
63-
_performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
16+
client := &mockClient{
17+
performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
6418
return TransactionResult{}, errFake
6519
},
6620
}
@@ -85,11 +39,11 @@ func TestUDPConn(t *testing.T) {
8539
})
8640

8741
t.Run("WriteTo()", func(t *testing.T) {
88-
client := &dummyClient{
89-
_performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
42+
client := &mockClient{
43+
performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
9044
return TransactionResult{}, errFake
9145
},
92-
_writeTo: func(data []byte, to net.Addr) (int, error) {
46+
writeTo: func(data []byte, to net.Addr) (int, error) {
9347
return len(data), nil
9448
},
9549
}

0 commit comments

Comments
 (0)