forked from amenzhinsky/iothub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmux_test.go
72 lines (65 loc) · 1.39 KB
/
mux_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
package iotdevice
import (
"bytes"
"errors"
"testing"
"gitlab.com/michaeljohn/iothub/common"
)
func TestEventsMuxSub(t *testing.T) {
mux := newEventsMux()
sub := mux.sub()
mux.Dispatch(&common.Message{
Payload: []byte("hello"),
})
msg := <-sub.C()
if !bytes.Equal(msg.Payload, []byte("hello")) {
t.Fatalf("invalid payload = %v, want %v", msg.Payload, []byte("hello"))
}
mux.unsub(sub)
mux.Dispatch(&common.Message{
Payload: []byte("hello"),
})
if !isClosed(sub.C()) {
t.Fatal("C is not closed after unsub")
}
if err := sub.Err(); err != nil {
t.Fatal(err)
}
}
func isClosed(ch <-chan *common.Message) bool {
select {
case _, ok := <-ch:
return !ok
default:
return false
}
}
func TestEventsMuxClose(t *testing.T) {
mux := newEventsMux()
sub := mux.sub()
mux.close()
if err := sub.Err(); !errors.Is(err, ErrClosed) {
t.Fatalf("closed mux sub err = %v, want %v", err, ErrClosed)
}
}
func TestMethodMux(t *testing.T) {
m := methodMux{}
if err := m.handle("add", func(v map[string]interface{}) (int, map[string]interface{}, error) {
v["b"] = 2
return 321, v, nil
}); err != nil {
t.Fatal(err)
}
defer m.remove("add")
rc, data, err := m.Dispatch("add", []byte(`{"a":1}`))
if err != nil {
t.Fatal(err)
}
if rc != 321 {
t.Errorf("rc = %d, want %d", rc, 321)
}
w := []byte(`{"a":1,"b":2}`)
if !bytes.Equal(data, w) {
t.Errorf("data = %q, want %q", data, w)
}
}