This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
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 #29 from wetterj/master
Improved test coverage
- Loading branch information
Showing
3 changed files
with
169 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
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,123 @@ | ||
package net | ||
|
||
import ( | ||
"testing" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func TestListen(T *testing.T) { | ||
var notifee NotifyBundle | ||
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234") | ||
if err != nil { | ||
T.Fatal("unexpected multiaddr error") | ||
} | ||
notifee.Listen(nil, addr) | ||
|
||
called := false | ||
notifee.ListenF = func(Network, ma.Multiaddr) { | ||
called = true | ||
} | ||
if called { | ||
T.Fatal("called should be false") | ||
} | ||
|
||
notifee.Listen(nil, addr) | ||
if !called { | ||
T.Fatal("Listen should have been called") | ||
} | ||
} | ||
|
||
func TestListenClose(T *testing.T) { | ||
var notifee NotifyBundle | ||
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234") | ||
if err != nil { | ||
T.Fatal("unexpected multiaddr error") | ||
} | ||
notifee.ListenClose(nil, addr) | ||
|
||
called := false | ||
notifee.ListenCloseF = func(Network, ma.Multiaddr) { | ||
called = true | ||
} | ||
if called { | ||
T.Fatal("called should be false") | ||
} | ||
|
||
notifee.ListenClose(nil, addr) | ||
if !called { | ||
T.Fatal("ListenClose should have been called") | ||
} | ||
} | ||
|
||
func TestConnected(T *testing.T) { | ||
var notifee NotifyBundle | ||
notifee.Connected(nil, nil) | ||
|
||
called := false | ||
notifee.ConnectedF = func(Network, Conn) { | ||
called = true | ||
} | ||
if called { | ||
T.Fatal("called should be false") | ||
} | ||
|
||
notifee.Connected(nil, nil) | ||
if !called { | ||
T.Fatal("Connected should have been called") | ||
} | ||
} | ||
|
||
func TestDisconnected(T *testing.T) { | ||
var notifee NotifyBundle | ||
notifee.Disconnected(nil, nil) | ||
|
||
called := false | ||
notifee.DisconnectedF = func(Network, Conn) { | ||
called = true | ||
} | ||
if called { | ||
T.Fatal("called should be false") | ||
} | ||
|
||
notifee.Disconnected(nil, nil) | ||
if !called { | ||
T.Fatal("Disconnected should have been called") | ||
} | ||
} | ||
|
||
func TestOpenedStream(T *testing.T) { | ||
var notifee NotifyBundle | ||
notifee.OpenedStream(nil, nil) | ||
|
||
called := false | ||
notifee.OpenedStreamF = func(Network, Stream) { | ||
called = true | ||
} | ||
if called { | ||
T.Fatal("called should be false") | ||
} | ||
|
||
notifee.OpenedStream(nil, nil) | ||
if !called { | ||
T.Fatal("OpenedStream should have been called") | ||
} | ||
} | ||
|
||
func TestClosedStream(T *testing.T) { | ||
var notifee NotifyBundle | ||
notifee.ClosedStream(nil, nil) | ||
|
||
called := false | ||
notifee.ClosedStreamF = func(Network, Stream) { | ||
called = true | ||
} | ||
if called { | ||
T.Fatal("called should be false") | ||
} | ||
|
||
notifee.ClosedStream(nil, nil) | ||
if !called { | ||
T.Fatal("ClosedStream should have been called") | ||
} | ||
} |
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,40 @@ | ||
package net | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDefaultTimeout(t *testing.T) { | ||
ctx := context.Background() | ||
dur := GetDialPeerTimeout(ctx) | ||
if dur != DialPeerTimeout { | ||
t.Fatal("expected default peer timeout") | ||
} | ||
} | ||
|
||
func TestNonDefaultTimeout(t *testing.T) { | ||
customTimeout := time.Duration(1) | ||
ctx := context.WithValue( | ||
context.Background(), | ||
dialPeerTimeoutCtxKey{}, | ||
customTimeout, | ||
) | ||
dur := GetDialPeerTimeout(ctx) | ||
if dur != customTimeout { | ||
t.Fatal("peer timeout doesn't match set timeout") | ||
} | ||
} | ||
|
||
func TestSettingTimeout(t *testing.T) { | ||
customTimeout := time.Duration(1) | ||
ctx := WithDialPeerTimeout( | ||
context.Background(), | ||
customTimeout, | ||
) | ||
dur := GetDialPeerTimeout(ctx) | ||
if dur != customTimeout { | ||
t.Fatal("peer timeout doesn't match set timeout") | ||
} | ||
} |