Skip to content

Commit

Permalink
Fixing route group test conn
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jan 6, 2020
1 parent f21db0e commit 66b3f6b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
8 changes: 4 additions & 4 deletions pkg/router/route_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ func NewRouteGroup(cfg *RouteGroupConfig, rt routing.Table, desc routing.RouteDe
// The Router, via transport.Manager, is responsible for reading incoming packets and pushing it
// to the appropriate RouteGroup via (*RouteGroup).readCh.
func (rg *RouteGroup) Read(p []byte) (n int, err error) {
if rg.isClosed() {
/*if rg.isClosed() {
return 0, io.ErrClosedPipe
}
}*/

if rg.readDeadline.Closed() {
rg.logger.Infoln("TIMEOUT ERROR?")
Expand All @@ -145,10 +145,10 @@ func (rg *RouteGroup) Read(p []byte) (n int, err error) {
// In case the read buffer is short.
rg.mu.Lock()
if rg.readBuf.Len() > 0 {
data, err := rg.readBuf.Read(p)
n, err := rg.readBuf.Read(p)
rg.mu.Unlock()

return data, err
return n, err
}
rg.mu.Unlock()

Expand Down
60 changes: 53 additions & 7 deletions pkg/router/route_group_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package router

import (
"bytes"
"context"
"fmt"
"io"
"math/rand"
"net"
Expand All @@ -11,15 +13,15 @@ import (
"testing"
"time"

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

"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/skywire-mainnet/pkg/routing"
"github.com/SkycoinProject/skywire-mainnet/pkg/snet/snettest"
"github.com/SkycoinProject/skywire-mainnet/pkg/snet/stcp"
"github.com/SkycoinProject/skywire-mainnet/pkg/transport"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewRouteGroup(t *testing.T) {
Expand Down Expand Up @@ -616,7 +618,7 @@ func TestRouteGroup_TestConn(t *testing.T) {
default:
}

packet, err := m1.ReadPacket()
packet, err := m2.ReadPacket()
if err != nil {
panic(err)
}
Expand All @@ -638,7 +640,7 @@ func TestRouteGroup_TestConn(t *testing.T) {
return
}
} else {
panic("wrong packet type")
panic(fmt.Sprintf("wrong packet type %v", packet.Type()))
}
}
}
Expand All @@ -653,7 +655,7 @@ func TestRouteGroup_TestConn(t *testing.T) {
default:
}

packet, err := m2.ReadPacket()
packet, err := m1.ReadPacket()
if err != nil {
panic(err)
}
Expand All @@ -675,7 +677,7 @@ func TestRouteGroup_TestConn(t *testing.T) {
return
}
} else {
panic("wrong packet type")
panic(fmt.Sprintf("wrong packet type %v", packet.Type()))
}
}
}
Expand All @@ -693,6 +695,50 @@ func TestRouteGroup_TestConn(t *testing.T) {
}

nettest.TestConn(t, mp)
/*c1, c2, stop, err := mp()
require.NoError(t, err)
defer stop()
testBasicIO(t, c1, c2)*/
}

func testBasicIO(t *testing.T, c1, c2 net.Conn) {
want := make([]byte, 1<<20)
rand.New(rand.NewSource(0)).Read(want)

dataCh := make(chan []byte)
go func() {
rd := bytes.NewReader(want)
if err := chunkedCopy(c1, rd); err != nil {
t.Errorf("unexpected c1.Write error: %v", err)
}
if err := c1.Close(); err != nil {
t.Errorf("unexpected c1.Close error: %v", err)
}
}()

//time.Sleep(10 * time.Second)

go func() {
wr := new(bytes.Buffer)
if err := chunkedCopy(wr, c2); err != nil {
t.Errorf("unexpected c2.Read error: %v", err)
}
/*if err := c2.Close(); err != nil {
t.Errorf("unexpected c2.Close error: %v", err)
}*/
dataCh <- wr.Bytes()
}()

if got := <-dataCh; !bytes.Equal(got, want) {
t.Error("transmitted data differs")
}
}

func chunkedCopy(w io.Writer, r io.Reader) error {
b := make([]byte, 1024)
_, err := io.CopyBuffer(struct{ io.Writer }{w}, struct{ io.Reader }{r}, b)
return err
}

func pushPackets(ctx context.Context, t *testing.T, from *transport.Manager, to *RouteGroup) {
Expand Down

0 comments on commit 66b3f6b

Please sign in to comment.