5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-22 23:41:35 +00:00

Wait for nodes to negotiate

This commit is contained in:
Aleksander Mistewicz 2019-09-28 14:10:17 +02:00
parent fffbbbcbd3
commit 8677a042cf

View File

@ -58,35 +58,45 @@ func CreateAndConnectTwo(t *testing.T) (*Core, *Core) {
return &nodeA, &nodeB return &nodeA, &nodeB
} }
// WaitConnected blocks until either nodes negotiated DHT or 5 seconds passed.
func WaitConnected(nodeA, nodeB *Core) bool {
// It may take up to 3 seconds, but let's wait 5.
for i := 0; i < 50; i++ {
time.Sleep(100 * time.Millisecond)
if len(nodeA.GetSwitchPeers()) > 0 && len(nodeB.GetSwitchPeers()) > 0 {
return true
}
}
return false
}
func TestCore_Start_Connect(t *testing.T) { func TestCore_Start_Connect(t *testing.T) {
CreateAndConnectTwo(t) CreateAndConnectTwo(t)
} }
func TestCore_Start_Transfer(t *testing.T) { func CreateEchoListener(t *testing.T, nodeA *Core, bufLen int) chan struct{} {
nodeA, nodeB := CreateAndConnectTwo(t)
// Listen // Listen
listener, err := nodeA.ConnListen() listener, err := nodeA.ConnListen()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer listener.Close()
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
defer listener.Close()
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
} }
defer conn.Close() defer conn.Close()
buf := make([]byte, 64) buf := make([]byte, bufLen)
n, err := conn.Read(buf) n, err := conn.Read(buf)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
} }
if n != 64 { if n != bufLen {
t.Error("missing data") t.Error("missing data")
return return
} }
@ -97,28 +107,36 @@ func TestCore_Start_Transfer(t *testing.T) {
done <- struct{}{} done <- struct{}{}
}() }()
time.Sleep(3 * time.Second) // FIXME return done
}
func TestCore_Start_Transfer(t *testing.T) {
nodeA, nodeB := CreateAndConnectTwo(t)
msgLen := 1500
done := CreateEchoListener(t, nodeA, msgLen)
if !WaitConnected(nodeA, nodeB) {
t.Fatal("nodes did not connect")
}
// Dial // Dial
dialer, err := nodeB.ConnDialer() dialer, err := nodeB.ConnDialer()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Log(nodeA.GetSwitchPeers())
t.Log(nodeB.GetSwitchPeers())
t.Log(nodeA.GetSessions())
t.Log(nodeB.GetSessions())
conn, err := dialer.Dial("nodeid", nodeA.NodeID().String()) conn, err := dialer.Dial("nodeid", nodeA.NodeID().String())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer conn.Close() defer conn.Close()
msg := make([]byte, 64) msg := make([]byte, msgLen)
rand.Read(msg) rand.Read(msg)
conn.Write(msg) conn.Write(msg)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
buf := make([]byte, 64) buf := make([]byte, msgLen)
_, err = conn.Read(buf) _, err = conn.Read(buf)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)