-
Notifications
You must be signed in to change notification settings - Fork 382
/
lcm_test.go
102 lines (86 loc) · 1.78 KB
/
lcm_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package lcm
import (
"testing"
"time"
)
const (
// The amount of messages we want to exchange here.
messageGoal = 10000
// Max time we want to wait for executing to be done.
timeout = 1 * time.Second
)
var (
// Some sample data.
someBytes = []byte("abcdefghijklmnopqrstuvwxyz")
)
func runTest(t *testing.T, provider string) {
// The amount of messages that we received.
messages := 1
var lcm LCM
var err error
// New LCM
if len(provider) != 0 {
lcm, err = NewProvider(provider)
} else {
lcm, err = New()
}
if err != nil {
t.Fatal(err)
}
defer func() {
err = lcm.Destroy()
if err != nil {
t.Fatal(err)
}
}()
// Subscribe to "TEST" LCM channel using Go
subscription, err := lcm.Subscribe("TEST", messageGoal)
if err != nil {
t.Fatal(err)
}
defer func() {
if err = lcm.Unsubscribe(subscription); err != nil {
t.Error(err)
}
}()
// Send messageGoal messages
go func() {
t.Log("sending", "amount", messageGoal)
publishChan, _ := lcm.Publisher("TEST")
for i := 1; i < messageGoal; i++ {
publishChan <- someBytes
}
close(publishChan)
}()
FOR_SELECT:
for {
select {
case <-time.After(timeout):
t.Log("timed out after", timeout)
break FOR_SELECT
case _, ok := <-subscription.ReceiveChan:
if !ok {
break FOR_SELECT
}
messages++
}
// Stop, if we are already there...
if messages == messageGoal {
break
}
}
// Did we receive messageGoal messages?
if messages != messageGoal {
t.Fatalf("Expected %d but received %d messages.", messageGoal, messages)
}
t.Log("received", "amount", messages)
}
func TestLCMDefaultProvider(t *testing.T) {
runTest(t, "")
}
func TestLCMProviderUDPM(t *testing.T) {
runTest(t, "udpm://239.255.76.67:7667?ttl=1")
}
func TestLCMProviderMemQ(t *testing.T) {
runTest(t, "memq://")
}