Skip to content

Commit

Permalink
Remove duplicate dummyConnObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
stv0g committed May 19, 2023
1 parent b6c8547 commit 521e5ad
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 106 deletions.
36 changes: 36 additions & 0 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package client

import (
"net"

"github.com/pion/stun"
)

type mockClient struct {
writeTo func(data []byte, to net.Addr) (int, error)
performTransaction func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error)
onDeallocated func(relayedAddr net.Addr)
}

func (c *mockClient) WriteTo(data []byte, to net.Addr) (int, error) {
if c.writeTo != nil {
return c.writeTo(data, to)
}
return 0, nil
}

func (c *mockClient) PerformTransaction(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
if c.performTransaction != nil {
return c.performTransaction(msg, to, dontWait)
}
return TransactionResult{}, nil
}

func (c *mockClient) OnDeallocated(relayedAddr net.Addr) {
if c.onDeallocated != nil {
c.onDeallocated(relayedAddr)
}
}
67 changes: 12 additions & 55 deletions internal/client/tcp_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/pion/logging"
"github.com/pion/stun"
"github.com/pion/transport/v2"
"github.com/pion/transport/v2/stdnet"
"github.com/pion/turn/v2/internal/proto"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -41,52 +40,11 @@ func (c dummyTCPConn) Read(b []byte) (int, error) {
return len(msg.Raw), nil
}

type dummyConnObserver struct {
net transport.Net
turnServerAddr net.Addr
username stun.Username
realm stun.Realm
_writeTo func(data []byte, to net.Addr) (int, error)
_performTransaction func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error)
_onDeallocated func(relayedAddr net.Addr)
}

func (obs *dummyConnObserver) Net() transport.Net {
if obs.net == nil {
n, err := stdnet.NewNet()
if err != nil {
return nil
}
obs.net = n
}
return obs.net
}

func (obs *dummyConnObserver) WriteTo(data []byte, to net.Addr) (int, error) {
if obs._writeTo != nil {
return obs._writeTo(data, to)
}
return 0, nil
}

func (obs *dummyConnObserver) PerformTransaction(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
if obs._performTransaction != nil {
return obs._performTransaction(msg, to, dontWait)
}
return TransactionResult{}, nil
}

func (obs *dummyConnObserver) OnDeallocated(relayedAddr net.Addr) {
if obs._onDeallocated != nil {
obs._onDeallocated(relayedAddr)
}
}

func TestTCPConn(t *testing.T) {
t.Run("Connect()", func(t *testing.T) {
var cid proto.ConnectionID = 5
obs := &dummyConnObserver{
_performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
client := &mockClient{
performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
if msg.Type.Class == stun.ClassRequest && msg.Type.Method == stun.MethodConnect {
msg, err := stun.Build(
stun.TransactionID,
Expand Down Expand Up @@ -114,7 +72,7 @@ func TestTCPConn(t *testing.T) {
log := loggerFactory.NewLogger("test")
alloc := TCPAllocation{
allocation: allocation{
client: obs,
client: client,
permMap: pm,
log: log,
},
Expand All @@ -124,8 +82,8 @@ func TestTCPConn(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, cid, actualCid)

obs = &dummyConnObserver{
_performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
client = &mockClient{
performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
if msg.Type.Class == stun.ClassRequest && msg.Type.Method == stun.MethodConnect {
msg, err = stun.Build(
stun.TransactionID,
Expand All @@ -140,7 +98,7 @@ func TestTCPConn(t *testing.T) {
}
alloc = TCPAllocation{
allocation: allocation{
client: obs,
client: client,
permMap: pm,
log: log,
},
Expand All @@ -155,9 +113,9 @@ func TestTCPConn(t *testing.T) {
assert.NoError(t, err)

loggerFactory := logging.NewDefaultLoggerFactory()
obs := &dummyConnObserver{}

alloc := NewTCPAllocation(&AllocationConfig{
Client: obs,
Client: &mockClient{},
Lifetime: time.Second,
Log: loggerFactory.NewLogger("test"),
RelayedAddr: relayedAddr,
Expand All @@ -176,9 +134,8 @@ func TestTCPConn(t *testing.T) {
assert.NoError(t, err)

loggerFactory := logging.NewDefaultLoggerFactory()
obs := &dummyConnObserver{}
alloc := NewTCPAllocation(&AllocationConfig{
Client: obs,
Client: &mockClient{},
Lifetime: time.Second,
Log: loggerFactory.NewLogger("test"),
RelayedAddr: relayedAddr,
Expand All @@ -201,8 +158,8 @@ func TestTCPConn(t *testing.T) {

var cid proto.ConnectionID = 5
loggerFactory := logging.NewDefaultLoggerFactory()
obs := &dummyConnObserver{
_performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
client := &mockClient{
performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
typ := stun.NewType(stun.MethodConnect, stun.ClassSuccessResponse)
if msg.Type.Method == stun.MethodCreatePermission {
typ = stun.NewType(stun.MethodCreatePermission, stun.ClassSuccessResponse)
Expand All @@ -218,7 +175,7 @@ func TestTCPConn(t *testing.T) {
},
}
alloc := NewTCPAllocation(&AllocationConfig{
Client: obs,
Client: client,
Lifetime: time.Second,
Log: loggerFactory.NewLogger("test"),
RelayedAddr: relayedAddr,
Expand Down
56 changes: 5 additions & 51 deletions internal/client/udp_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,13 @@ import (
"testing"

"github.com/pion/stun"
"github.com/pion/transport/v2"
"github.com/stretchr/testify/assert"
)

type dummyClient struct {
turnServerAddr net.Addr
username stun.Username
realm stun.Realm
_writeTo func(data []byte, to net.Addr) (int, error)
_performTransaction func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error)
_onDeallocated func(relayedAddr net.Addr)
}

func (obs *dummyClient) TURNServerAddr() net.Addr {
return obs.turnServerAddr
}

func (obs *dummyClient) Username() stun.Username {
return obs.username
}

func (obs *dummyClient) Realm() stun.Realm {
return obs.realm
}

func (obs *dummyClient) Net() transport.Net {
return nil
}

func (obs *dummyClient) WriteTo(data []byte, to net.Addr) (int, error) {
if obs._writeTo != nil {
return obs._writeTo(data, to)
}
return 0, nil
}

func (obs *dummyClient) PerformTransaction(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
if obs._performTransaction != nil {
return obs._performTransaction(msg, to, dontWait)
}
return TransactionResult{}, nil
}

func (obs *dummyClient) OnDeallocated(relayedAddr net.Addr) {
if obs._onDeallocated != nil {
obs._onDeallocated(relayedAddr)
}
}

func TestUDPConn(t *testing.T) {
t.Run("bind()", func(t *testing.T) {
client := &dummyClient{
_performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
client := &mockClient{
performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
return TransactionResult{}, errFake
},
}
Expand All @@ -85,11 +39,11 @@ func TestUDPConn(t *testing.T) {
})

t.Run("WriteTo()", func(t *testing.T) {
client := &dummyClient{
_performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
client := &mockClient{
performTransaction: func(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error) {
return TransactionResult{}, errFake
},
_writeTo: func(data []byte, to net.Addr) (int, error) {
writeTo: func(data []byte, to net.Addr) (int, error) {
return len(data), nil
},
}
Expand Down

0 comments on commit 521e5ad

Please sign in to comment.