5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-10 04:00:37 +00:00

Pass message between nodes

This commit is contained in:
Aleksander Mistewicz 2019-09-25 20:07:36 +02:00
parent d96fb27ab8
commit fffbbbcbd3

View File

@ -1,8 +1,11 @@
package yggdrasil
import (
"bytes"
"math/rand"
"os"
"testing"
"time"
"github.com/gologme/log"
@ -27,7 +30,7 @@ func GetLoggerWithPrefix(prefix string) *log.Logger {
return l
}
func TestCore_Start(t *testing.T) {
func CreateAndConnectTwo(t *testing.T) (*Core, *Core) {
nodeA := Core{}
_, err := nodeA.Start(GenerateConfig(), GetLoggerWithPrefix("A: "))
if err != nil {
@ -44,4 +47,84 @@ func TestCore_Start(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if l := len(nodeA.GetPeers()); l != 1 {
t.Fatal("unexpected number of peers", l)
}
if l := len(nodeB.GetPeers()); l != 1 {
t.Fatal("unexpected number of peers", l)
}
return &nodeA, &nodeB
}
func TestCore_Start_Connect(t *testing.T) {
CreateAndConnectTwo(t)
}
func TestCore_Start_Transfer(t *testing.T) {
nodeA, nodeB := CreateAndConnectTwo(t)
// Listen
listener, err := nodeA.ConnListen()
if err != nil {
t.Fatal(err)
}
defer listener.Close()
done := make(chan struct{})
go func() {
conn, err := listener.Accept()
if err != nil {
t.Error(err)
return
}
defer conn.Close()
buf := make([]byte, 64)
n, err := conn.Read(buf)
if err != nil {
t.Error(err)
return
}
if n != 64 {
t.Error("missing data")
return
}
_, err = conn.Write(buf)
if err != nil {
t.Error(err)
}
done <- struct{}{}
}()
time.Sleep(3 * time.Second) // FIXME
// Dial
dialer, err := nodeB.ConnDialer()
if err != nil {
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())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
msg := make([]byte, 64)
rand.Read(msg)
conn.Write(msg)
if err != nil {
t.Fatal(err)
}
buf := make([]byte, 64)
_, err = conn.Read(buf)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(msg, buf) != 0 {
t.Fatal("expected echo")
}
<-done
}