-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverclient_test.go
117 lines (98 loc) · 1.84 KB
/
serverclient_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package osc_test
import (
"github.com/crgimenes/go-osc"
"github.com/stretchr/testify/assert"
"net"
"sync"
"testing"
"time"
)
const (
ping = "/ping"
pong = "/pong"
)
func TestServerAndClient(t *testing.T) {
timeout := time.After(1 * time.Second)
done := make(chan bool)
go func() {
wait := sync.WaitGroup{}
wait.Add(1)
var d float64 = 0.0
var i int32 = 0
addr1, err := net.ResolveUDPAddr("udp", "127.0.0.1:8000")
if err != nil {
t.Error(err)
}
addr2, err := net.ResolveUDPAddr("udp", "127.0.0.1:9000")
if err != nil {
t.Error(err)
}
d1 := osc.NewStandardDispatcher()
app1 := osc.NewServerAndClient(d1)
err = app1.NewConn(addr2, addr1)
if err != nil {
t.Error(err)
}
defer func() {
err := app1.Close()
if err != nil {
t.Error(err)
}
}()
err = d1.AddMsgHandler(ping, func(msg *osc.Message) {
d = msg.Arguments[0].(float64)
err = app1.SendMsg(pong, 2)
if err != nil {
t.Error(err)
}
})
if err != nil {
t.Error(err)
}
go func() {
err := app1.ListenAndServe()
if err != nil {
t.Error(err)
}
}()
d2 := osc.NewStandardDispatcher()
err = d2.AddMsgHandler(pong, func(msg *osc.Message) {
i = msg.Arguments[0].(int32)
wait.Done()
})
if err != nil {
t.Error(err)
}
app2 := osc.NewServerAndClient(d2)
err = app2.NewConn(addr1, addr2)
if err != nil {
t.Error(err)
}
defer func() {
err := app2.Close()
if err != nil {
t.Error(err)
}
}()
go func() {
err := app2.ListenAndServe()
if err != nil {
t.Error(err)
}
}()
err = app2.SendMsg(ping, 1.0)
if err != nil {
t.Error(err)
}
wait.Wait()
// check if send and receive are the same
assert.Equal(t, 1.0, d)
assert.Equal(t, int32(2), i)
done <- true
}()
select {
case <-timeout:
t.Fatal("test didn't finish in time")
case <-done:
}
}