Skip to content

Commit 44190c3

Browse files
committed
Cluster UnitTests
1 parent b383e56 commit 44190c3

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ga
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestCluster(t *testing.T) {
9+
recvBestChan := make(chan Individual, 100)
10+
11+
peer1 := MakeCluster(9998, 100, nil, []string{})
12+
peer2 := MakeCluster(9999, 100, recvBestChan, []string{"127.0.0.1:9998"})
13+
14+
// Launch First peer and wait for it to be ready
15+
go peer1.Start(NodeMetadata{})
16+
time.Sleep(2 * time.Second)
17+
18+
// Peer2 will connect to peer1
19+
go peer2.Start(NodeMetadata{})
20+
time.Sleep(2 * time.Second)
21+
22+
if peer1.GetNumNodes() != 2 {
23+
t.Errorf("Peer1 could not discover peer2")
24+
}
25+
26+
if peer2.GetNumNodes() != 2 {
27+
t.Errorf("Peer2 could not discover peer2")
28+
}
29+
30+
// Test individual broadcast
31+
peer1.BroadcastBestIndividual <- Individual{
32+
IndividualID: "Test",
33+
}
34+
35+
// Wait for gossip the individual
36+
time.Sleep(5 * time.Second)
37+
38+
indiv := <-peer2.ReceiveBestIndividual
39+
t.Logf("Got indiv broadcasted with ID: %s", indiv.IndividualID)
40+
41+
if indiv.IndividualID != "Test" {
42+
t.Errorf("Error on broadcast")
43+
}
44+
45+
peer1.Shutdown()
46+
peer2.Shutdown()
47+
}

0 commit comments

Comments
 (0)