Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #29 from wetterj/master
Browse files Browse the repository at this point in the history
Improved test coverage
  • Loading branch information
Stebalien authored Sep 8, 2018
2 parents 674eaa7 + 1a6e408 commit ca76418
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
6 changes: 6 additions & 0 deletions notifiee.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,42 @@ type NotifyBundle struct {

var _ Notifiee = (*NotifyBundle)(nil)

// Listen calls ListenF if it is not null.
func (nb *NotifyBundle) Listen(n Network, a ma.Multiaddr) {
if nb.ListenF != nil {
nb.ListenF(n, a)
}
}

// ListenClose calls ListenCloseF if it is not null.
func (nb *NotifyBundle) ListenClose(n Network, a ma.Multiaddr) {
if nb.ListenCloseF != nil {
nb.ListenCloseF(n, a)
}
}

// Connected calls ConnectedF if it is not null.
func (nb *NotifyBundle) Connected(n Network, c Conn) {
if nb.ConnectedF != nil {
nb.ConnectedF(n, c)
}
}

// Disconnected calls DisconnectedF if it is not null.
func (nb *NotifyBundle) Disconnected(n Network, c Conn) {
if nb.DisconnectedF != nil {
nb.DisconnectedF(n, c)
}
}

// OpenedStream calls OpenedStreamF if it is not null.
func (nb *NotifyBundle) OpenedStream(n Network, s Stream) {
if nb.OpenedStreamF != nil {
nb.OpenedStreamF(n, s)
}
}

// ClosedStream calls ClosedStreamF if it is not null.
func (nb *NotifyBundle) ClosedStream(n Network, s Stream) {
if nb.ClosedStreamF != nil {
nb.ClosedStreamF(n, s)
Expand Down
123 changes: 123 additions & 0 deletions notifiee_test.go
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")
}
}
40 changes: 40 additions & 0 deletions timeouts_test.go
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")
}
}

0 comments on commit ca76418

Please sign in to comment.