-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40c0fc1
commit 4386056
Showing
2 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package broadcast | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCausalBroadcast(t *testing.T) { | ||
peer_addrs := []string{"localhost:9991", "localhost:9992"} | ||
messages := [][]byte{randStringBytes(2 << 23), []byte("hello gofret!")} | ||
incoming_channels := []chan []byte{make(chan []byte), make(chan []byte)} | ||
done_signal := make(chan bool) | ||
ready_signal := make(chan bool) | ||
|
||
emitter_routine := func(address string, messages [][]byte, incoming_channel chan []byte, done chan bool) { | ||
broadcaster := CausalBroadcast(Configuration{ | ||
SelfAddress: address, | ||
PeerAddresses: peer_addrs, | ||
}) | ||
|
||
incoming_messages, err := broadcaster.Init() | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
|
||
go func() { | ||
for { | ||
incoming_channel <- <-incoming_messages | ||
} | ||
}() | ||
|
||
for _, message := range messages { | ||
if err := broadcaster.Broadcast(message); err != nil { | ||
t.Errorf("cannot broadcast: %v", err) | ||
} | ||
} | ||
|
||
done <- true | ||
} | ||
|
||
receiver_routine := func(address string, incoming_channel chan []byte, ready chan bool) { | ||
broadcaster := CausalBroadcast(Configuration{ | ||
SelfAddress: address, | ||
PeerAddresses: peer_addrs, | ||
}) | ||
|
||
incoming_messages, err := broadcaster.Init() | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
|
||
go func() { | ||
for { | ||
incoming_channel <- <-incoming_messages | ||
} | ||
}() | ||
ready_signal <- true | ||
} | ||
|
||
go receiver_routine(peer_addrs[0], incoming_channels[0], ready_signal) | ||
<-ready_signal | ||
|
||
go emitter_routine(peer_addrs[1], messages, incoming_channels[1], done_signal) | ||
<-done_signal | ||
|
||
timeout := time.After(30000 * time.Millisecond) | ||
select { | ||
case incoming_message := <-incoming_channels[0]: | ||
if string(incoming_message[:len(messages[1])-1]) == string(messages[1]) { | ||
t.Errorf("order is not correct") | ||
return | ||
} | ||
case incoming_message := <-incoming_channels[1]: | ||
if string(incoming_message[:len(messages[1])-1]) == string(messages[1]) { | ||
t.Errorf("order is not correct") | ||
return | ||
} | ||
case <-timeout: | ||
t.Errorf("timed out") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters