Skip to content

Commit

Permalink
Finally fix test conn?
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jan 30, 2020
1 parent 42b9559 commit 58787e5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
19 changes: 16 additions & 3 deletions pkg/router/route_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,27 @@ func (rg *RouteGroup) Write(p []byte) (n int, err error) {
}

rg.mu.Lock()
defer rg.mu.Unlock()
//defer rg.mu.Unlock()

tp, err := rg.tp()
if err != nil {
rg.mu.Unlock()
return 0, err
}

rule, err := rg.rule()
if err != nil {
rg.mu.Unlock()
return 0, err
}

packet := routing.MakeDataPacket(rule.KeyRouteID(), p)
rg.mu.Unlock()

return rg.write(p, tp, rule)
}

func (rg *RouteGroup) write(data []byte, tp *transport.ManagedTransport, rule routing.Rule) (int, error) {
packet := routing.MakeDataPacket(rule.KeyRouteID(), data)

ctx, cancel := context.WithCancel(context.Background())

Expand All @@ -221,9 +229,11 @@ func (rg *RouteGroup) Write(p []byte) (n int, err error) {
return 0, err
}

fmt.Println()

atomic.StoreInt64(&rg.lastSent, time.Now().UnixNano())

return len(p), nil
return len(data), nil
}
}

Expand Down Expand Up @@ -282,8 +292,10 @@ func (rg *RouteGroup) Close() error {

atomic.StoreInt32(&rg.closeInitiated, 1)

fmt.Println("BEFORE MU")
rg.mu.Lock()
defer rg.mu.Unlock()
fmt.Println("MU ACQUIRED")

return rg.close(routing.CloseRequested)
}
Expand Down Expand Up @@ -372,6 +384,7 @@ func (rg *RouteGroup) sendKeepAlive() error {
// - Delete all rules (ForwardRules and ConsumeRules) from routing table.
// - Close all go channels.
func (rg *RouteGroup) close(code routing.CloseCode) error {
fmt.Println("GOT IN close")
if rg.isClosed() {
return nil
}
Expand Down
20 changes: 7 additions & 13 deletions pkg/router/route_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,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 @@ -698,7 +698,7 @@ func TestRouteGroup_TestConn(t *testing.T) {
require.NotNil(t, tp1.Entry)
require.NotNil(t, tp2.Entry)

// because some subtests of `TestConn` are highly specific in their behaviour,
// because some subtests of `TestConn` are highly specific in their behavior,
// it's best to exceed the `readCh` size
rgCfg := &RouteGroupConfig{
ReadChBufSize: defaultReadChBufSize * 3,
Expand Down Expand Up @@ -739,14 +739,8 @@ func TestRouteGroup_TestConn(t *testing.T) {
go pushPackets(ctx, m1, rg0)

stop = func() {
if err := rg0.Close(); err != nil {
//panic(err)
}
fmt.Printf("CLOSED 0 %s\n", rg0.LocalAddr().String())
if err := rg1.Close(); err != nil {
//panic(err)
}
fmt.Printf("CLOSED 1 %s\n", rg1.LocalAddr().String())
_ = rg0.Close() // nolint:errcheck
_ = rg1.Close() // nolint:errcheck
cancel()
nEnv.Teardown()
}
Expand Down Expand Up @@ -780,7 +774,6 @@ func pushPackets(ctx context.Context, from *transport.Manager, to *RouteGroup) {
fmt.Printf("GOT CLOSE PACKET ON %s\n", to.LocalAddr().String())
if to.isClosed() {
panic(io.ErrClosedPipe)
return
}

if err := to.handleClosePacket(routing.CloseCode(packet.Payload()[0])); err != nil {
Expand All @@ -789,6 +782,7 @@ func pushPackets(ctx context.Context, from *transport.Manager, to *RouteGroup) {

return
case routing.DataPacket:
//fmt.Println("GOT DATA PACKET")
if !safeSend(ctx, to, payload) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/golang.org/x/net/nettest/conntest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58787e5

Please sign in to comment.