-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from nkryuchkov/feature/nettest-testconn
Test stcp with nettest
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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 | ||
done := make(chan struct{}) | ||
go func() { | ||
b, respErr = newConn(bConn, time.Now().Add(HandshakeTimeout), rhs, nil) | ||
close(done) | ||
}() | ||
|
||
a, err := newConn(aConn, time.Now().Add(HandshakeTimeout), ihs, nil) | ||
require.NoError(t, err) | ||
|
||
<-done | ||
require.NoError(t, respErr) | ||
|
||
closeFunc := func() { | ||
require.NoError(t, a.Close()) | ||
require.NoError(t, b.Close()) | ||
} | ||
|
||
return a, b, closeFunc | ||
} |