Skip to content

Commit

Permalink
Create nettest.TestConn test for stcp
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Oct 23, 2019
1 parent d0ef5a3 commit 5a9c5be
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/snet/stcp/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package stcp

import (
"net"
"testing"
"time"

"github.com/SkycoinProject/dmsg"
"github.com/SkycoinProject/dmsg/cipher"
"github.com/stretchr/testify/require"
"golang.org/x/net/nettest"
)

func TestConn(t *testing.T) {
mp := func() (c1, c2 net.Conn, stop func(), err error) {
c1, c2, stop = prepareConns(t)
return
}
nettest.TestConn(t, mp)
}

func prepareConns(t *testing.T) (*Conn, *Conn, func()) {
aPK, aSK := cipher.GenerateKeyPair()
bPK, _ := cipher.GenerateKeyPair()

aConn, bConn := net.Pipe()

ihs := InitiatorHandshake(aSK, dmsg.Addr{PK: aPK, Port: 1}, dmsg.Addr{PK: bPK, Port: 1})

rhs := ResponderHandshake(func(f2 Frame2) error {
return nil
})

var b *Conn
var respErr error
go func() {
b, respErr = newConn(bConn, time.Now().Add(HandshakeTimeout), rhs, nil)
}()

a, err := newConn(aConn, time.Now().Add(HandshakeTimeout), ihs, nil)
require.NoError(t, err)
require.NoError(t, respErr)

closeFunc := func() {
require.NoError(t, a.Close())
require.NoError(t, b.Close())
}

return a, b, closeFunc
}

0 comments on commit 5a9c5be

Please sign in to comment.