Skip to content

Commit

Permalink
Add some fix to the setup node test
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Aug 19, 2019
1 parent 1f3272e commit 78eb7e6
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions pkg/setup/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/skycoin/skywire/pkg/snet"

"github.com/skycoin/dmsg"

"github.com/google/uuid"
Expand All @@ -24,7 +28,7 @@ import (
"github.com/skycoin/skycoin/src/util/logging"
)

/*func TestMain(m *testing.M) {
func TestMain(m *testing.M) {
loggingLevel, ok := os.LookupEnv("TEST_LOGGING_LEVEL")
if ok {
lvl, err := logging.LevelFromString(loggingLevel)
Expand All @@ -37,7 +41,7 @@ import (
}

os.Exit(m.Run())
}*/
}

func TestNode(t *testing.T) {
// Prepare mock dmsg discovery.
Expand All @@ -60,7 +64,13 @@ func TestNode(t *testing.T) {
prepClients := func(n int) ([]clientWithDMSGAddrAndListener, func()) {
clients := make([]clientWithDMSGAddrAndListener, n)
for i := 0; i < n; i++ {
port := uint16(15678 + i)
var port uint16
// setup node
if i == 0 {
port = snet.SetupPort
} else {
port = snet.AwaitSetupPort
}
pk, sk, err := cipher.GenerateDeterministicKeyPair([]byte{byte(i)})
require.NoError(t, err)
t.Logf("client[%d] PK: %s\n", i, pk)
Expand Down Expand Up @@ -156,28 +166,38 @@ func TestNode(t *testing.T) {
conn, err := clients[client].Listener.Accept()
require.NoError(t, err)

fmt.Printf("client %v:%v accepted\n", client, clients[client].Addr)

proto := NewSetupProtocol(conn)

pt, _, err := proto.ReadPacket()
require.NoError(t, err)
require.Equal(t, PacketRequestRegistrationID, pt)

fmt.Printf("client %v:%v got PacketRequestRegistrationID\n", client, clients[client].Addr)

routeID := atomic.AddUint32(&nextRouteID, 1)

err = proto.WritePacket(RespSuccess, []routing.RouteID{routing.RouteID(routeID)})
require.NoError(t, err)

fmt.Printf("client %v:%v responded to with registration ID: %v\n", client, clients[client].Addr, routeID)

require.NoError(t, conn.Close())

conn, err = clients[client].Listener.Accept()
require.NoError(t, err)

fmt.Printf("client %v:%v accepted 2nd time\n", client, clients[client].Addr)

proto = NewSetupProtocol(conn)

pt, pp, err := proto.ReadPacket()
require.NoError(t, err)
require.Equal(t, PacketAddRules, pt)

fmt.Printf("client %v:%v got PacketAddRules\n", client, clients[client].Addr)

var rs []routing.Rule
require.NoError(t, json.Unmarshal(pp, &rs))

Expand All @@ -188,6 +208,8 @@ func TestNode(t *testing.T) {
// TODO: This error is not checked due to a bug in dmsg.
_ = proto.WritePacket(RespSuccess, nil) //nolint:errcheck

fmt.Printf("client %v:%v responded for PacketAddRules\n", client, clients[client].Addr)

require.NoError(t, conn.Close())

addRuleDone.Done()
Expand Down Expand Up @@ -231,12 +253,14 @@ func TestNode(t *testing.T) {
go expectAddRules(2, routing.RuleForward)
go expectAddRules(1, routing.RuleForward)
addRuleDone.Wait()
fmt.Println("FORWARD ROUTE DONE")
addRuleDone.Add(4)
go expectAddRules(1, routing.RuleApp)
go expectAddRules(2, routing.RuleForward)
go expectAddRules(3, routing.RuleForward)
go expectAddRules(4, routing.RuleForward)
addRuleDone.Wait()
fmt.Println("REVERSE ROUTE DONE")
expectConfirmLoop(1)
expectConfirmLoop(4)
})
Expand Down

0 comments on commit 78eb7e6

Please sign in to comment.