Skip to content

Commit

Permalink
Improve Router and RouteGroup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Dec 12, 2019
1 parent 7c97a2b commit d9d9288
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
6 changes: 5 additions & 1 deletion pkg/router/route_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ func (r *RouteGroup) Write(p []byte) (n int, err error) {
return 0, timeoutError{}
}

if len(p) == 0 {
return 0, nil
}

r.mu.Lock()
defer r.mu.Unlock()

Expand Down Expand Up @@ -252,7 +256,7 @@ func (r *RouteGroup) Close() error {

r.once.Do(func() {
close(r.done)
// close(r.readCh) // TODO: uncomment
// close(r.readCh) // TODO(nkryuchkov): uncomment
})

return nil
Expand Down
33 changes: 31 additions & 2 deletions pkg/router/route_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func TestRouteGroup_Read(t *testing.T) {
rg1.readCh <- msg1
rg2.readCh <- msg2

n, err := rg1.Read(buf1)
n, err := rg1.Read([]byte{})
require.Equal(t, 0, n)
require.NoError(t, err)

n, err = rg1.Read(buf1)
require.NoError(t, err)
require.Equal(t, msg1, buf1)
require.Equal(t, len(msg1), n)
Expand All @@ -60,6 +64,9 @@ func TestRouteGroup_Read(t *testing.T) {
require.NoError(t, err)
require.Equal(t, msg2, buf2)
require.Equal(t, len(msg2), n)

require.NoError(t, rg1.Close())
require.NoError(t, rg2.Close())
}

func TestRouteGroup_Write(t *testing.T) {
Expand All @@ -79,6 +86,14 @@ func TestRouteGroup_Write(t *testing.T) {
m1, m2, teardown := createTransports(t, rg1, rg2, stcp.Type)
defer teardown()

n, err := rg1.Write([]byte{})
require.Equal(t, 0, n)
require.NoError(t, err)

n, err = rg2.Write([]byte{})
require.Equal(t, 0, n)
require.NoError(t, err)

_, err = rg1.Write(msg1)
require.NoError(t, err)

Expand All @@ -92,6 +107,9 @@ func TestRouteGroup_Write(t *testing.T) {
recv, err = m2.ReadPacket()
require.NoError(t, err)
require.Equal(t, msg1, recv.Payload())

require.NoError(t, rg1.Close())
require.NoError(t, rg2.Close())
}

func TestRouteGroup_ReadWrite(t *testing.T) {
Expand Down Expand Up @@ -121,6 +139,9 @@ func testReadWrite(t *testing.T, iterations int) {
assert.NoError(t, rg2.Close())

teardownEnv()

require.NoError(t, rg1.Close())
require.NoError(t, rg2.Close())
}

func testRouteGroupReadWrite(t *testing.T, iterations int, rg1, rg2 io.ReadWriter) {
Expand Down Expand Up @@ -378,19 +399,25 @@ func testArbitrarySizeOneMessage(t *testing.T, size int) {
require.Equal(t, io.EOF, err)
require.Equal(t, 0, n)
require.Equal(t, make([]byte, size), buf)

require.NoError(t, rg1.Close())
require.NoError(t, rg2.Close())
}

func TestRouteGroup_LocalAddr(t *testing.T) {
rg := createRouteGroup()
require.Equal(t, rg.desc.Dst(), rg.LocalAddr())

require.NoError(t, rg.Close())
}

func TestRouteGroup_RemoteAddr(t *testing.T) {
rg := createRouteGroup()
require.Equal(t, rg.desc.Src(), rg.RemoteAddr())

require.NoError(t, rg.Close())
}

// TODO: fix hangs
func TestRouteGroup_TestConn(t *testing.T) {
mp := func() (c1, c2 net.Conn, stop func(), err error) {
rg1 := createRouteGroup()
Expand All @@ -408,6 +435,8 @@ func TestRouteGroup_TestConn(t *testing.T) {
stop = func() {
cancel()
teardownEnv()
require.NoError(t, rg1.Close())
require.NoError(t, rg2.Close())
}

return
Expand Down
1 change: 0 additions & 1 deletion pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ func (r *router) AcceptRoutes(ctx context.Context) (*RouteGroup, error) {
case <-ctx.Done():
return nil, ctx.Err()
case rules, ok = <-r.accept:
break
}
if !ok {
return nil, &net.OpError{Op: "accept", Net: "skynet", Source: nil, Err: errors.New("use of closed network connection")}
Expand Down
17 changes: 14 additions & 3 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ func Test_router_AcceptRoutes(t *testing.T) {
require.Len(t, rg.tps, 1)

allRules := rg.rt.AllRules()
assert.Len(t, allRules, 2)
assert.Contains(t, allRules, fwdRule)
assert.Contains(t, allRules, cnsmRule)
require.Len(t, allRules, 2)
require.Contains(t, allRules, fwdRule)
require.Contains(t, allRules, cnsmRule)

require.NoError(t, r0.Close())
}

// Ensure that received packets are handled properly in `(*Router).Serve()`.
Expand Down Expand Up @@ -122,6 +124,11 @@ func TestRouter_Serve(t *testing.T) {

// go r1.Serve(context.TODO())

defer func() {
require.NoError(t, r0.Close())
require.NoError(t, r1.Close())
}()

// Create dmsg transport between two `snet.Network` entities.
tp1, err := rEnv.TpMngrs[1].SaveTransport(context.TODO(), keys[0].PK, dmsg.Type)
require.NoError(t, err)
Expand Down Expand Up @@ -324,6 +331,10 @@ func TestRouter_Rules(t *testing.T) {
r, ok := rIfc.(*router)
require.True(t, ok)

defer func() {
require.NoError(t, r.Close())
}()

r.rt = rt

// CLOSURE: Delete all routing rules.
Expand Down

0 comments on commit d9d9288

Please sign in to comment.