-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg_test.go
66 lines (49 loc) · 1.15 KB
/
pkg_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
package jumiks
import (
"github.com/raspi/jumiks/pkg/client"
"github.com/raspi/jumiks/pkg/server"
error2 "github.com/raspi/jumiks/pkg/server/error"
"testing"
"time"
)
// TestServerAndClient tests server which sends a message to client
func TestServerAndClient(t *testing.T) {
timeout := time.After(5 * time.Second)
want := []byte(`hello, world!`)
uaddr := `@test-server`
errs := make(chan error2.Error)
srv, err := server.New(uaddr, 100, errs)
if err != nil {
t.Fail()
}
defer srv.Close()
go srv.Listen()
// Wait for setup
time.Sleep(time.Second * 1)
cerrs := make(chan error)
received := make(chan []byte)
clnt, err := client.New(uaddr,
func(b []byte) {
received <- b // send to chan
}, cerrs)
if err != nil {
t.Fail()
}
defer clnt.Close()
go clnt.Listen()
// Wait for setup
time.Sleep(time.Second * 1)
srv.SendToAll(want)
select {
case got := <-received:
if string(got) != string(want) {
t.Fail()
}
case err := <-cerrs: // We shouldn't receive any errors from client
t.Fatal(err)
case err := <-errs: // We shouldn't receive any errors from server
t.Fatal(err)
case <-timeout:
t.Fatalf(`test timed out`)
}
}