mirror of
https://github.com/yggdrasil-network/yggdrasil-go
synced 2024-11-13 00:40:24 +03:00
Add more comments to explain helper functions
This commit is contained in:
parent
8053766092
commit
783959208c
@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GenerateConfig is modification
|
// GenerateConfig produces default configuration with suitable modifications for tests.
|
||||||
func GenerateConfig() *config.NodeConfig {
|
func GenerateConfig() *config.NodeConfig {
|
||||||
cfg := config.GenerateConfig()
|
cfg := config.GenerateConfig()
|
||||||
cfg.AdminListen = "none"
|
cfg.AdminListen = "none"
|
||||||
@ -22,6 +22,8 @@ func GenerateConfig() *config.NodeConfig {
|
|||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLoggerWithPrefix creates a new logger instance wih prefix.
|
||||||
|
// If verbose is set to true, three log levels are enabled: "info", "warn", "error".
|
||||||
func GetLoggerWithPrefix(prefix string, verbose bool) *log.Logger {
|
func GetLoggerWithPrefix(prefix string, verbose bool) *log.Logger {
|
||||||
l := log.New(os.Stderr, prefix, log.Flags())
|
l := log.New(os.Stderr, prefix, log.Flags())
|
||||||
if !verbose {
|
if !verbose {
|
||||||
@ -33,14 +35,16 @@ func GetLoggerWithPrefix(prefix string, verbose bool) *log.Logger {
|
|||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateAndConnectTwo(t testing.TB, verbose bool) (*Core, *Core) {
|
// CreateAndConnectTwo creates two nodes. nodeB connects to nodeA.
|
||||||
nodeA := Core{}
|
// Verbosity flag is passed to logger.
|
||||||
|
func CreateAndConnectTwo(t testing.TB, verbose bool) (nodeA *Core, nodeB *Core) {
|
||||||
|
nodeA = new(Core)
|
||||||
_, err := nodeA.Start(GenerateConfig(), GetLoggerWithPrefix("A: ", verbose))
|
_, err := nodeA.Start(GenerateConfig(), GetLoggerWithPrefix("A: ", verbose))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeB := Core{}
|
nodeB = new(Core)
|
||||||
_, err = nodeB.Start(GenerateConfig(), GetLoggerWithPrefix("B: ", verbose))
|
_, err = nodeB.Start(GenerateConfig(), GetLoggerWithPrefix("B: ", verbose))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -58,7 +62,7 @@ func CreateAndConnectTwo(t testing.TB, verbose bool) (*Core, *Core) {
|
|||||||
t.Fatal("unexpected number of peers", l)
|
t.Fatal("unexpected number of peers", l)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &nodeA, &nodeB
|
return nodeA, nodeB
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitConnected blocks until either nodes negotiated DHT or 5 seconds passed.
|
// WaitConnected blocks until either nodes negotiated DHT or 5 seconds passed.
|
||||||
@ -73,17 +77,16 @@ func WaitConnected(nodeA, nodeB *Core) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCore_Start_Connect(t *testing.T) {
|
// CreateEchoListener creates a routine listening on nodeA. It expects repeats messages of length bufLen.
|
||||||
CreateAndConnectTwo(t, true)
|
// It returns a channel used to synchronize the routine with caller.
|
||||||
}
|
|
||||||
|
|
||||||
func CreateEchoListener(t testing.TB, nodeA *Core, bufLen int, repeats int) chan struct{} {
|
func CreateEchoListener(t testing.TB, nodeA *Core, bufLen int, repeats int) chan struct{} {
|
||||||
// Listen
|
// Listen. Doing it here guarantees that there will be something to try to connect when it returns.
|
||||||
listener, err := nodeA.ConnListen()
|
listener, err := nodeA.ConnListen()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start routine
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
@ -116,6 +119,12 @@ func CreateEchoListener(t testing.TB, nodeA *Core, bufLen int, repeats int) chan
|
|||||||
return done
|
return done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestCore_Start_Connect checks if two nodes can connect together.
|
||||||
|
func TestCore_Start_Connect(t *testing.T) {
|
||||||
|
CreateAndConnectTwo(t, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCore_Start_Transfer checks that messages can be passed between nodes (in both directions).
|
||||||
func TestCore_Start_Transfer(t *testing.T) {
|
func TestCore_Start_Transfer(t *testing.T) {
|
||||||
nodeA, nodeB := CreateAndConnectTwo(t, true)
|
nodeA, nodeB := CreateAndConnectTwo(t, true)
|
||||||
|
|
||||||
@ -153,6 +162,7 @@ func TestCore_Start_Transfer(t *testing.T) {
|
|||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BenchmarkCore_Start_Transfer estimates the possible transfer between nodes (in MB/s).
|
||||||
func BenchmarkCore_Start_Transfer(b *testing.B) {
|
func BenchmarkCore_Start_Transfer(b *testing.B) {
|
||||||
nodeA, nodeB := CreateAndConnectTwo(b, false)
|
nodeA, nodeB := CreateAndConnectTwo(b, false)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user