forked from brutella/can
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bus_test.go
77 lines (58 loc) · 1.32 KB
/
bus_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
package can
import (
"reflect"
"testing"
"time"
)
var testFrame = Frame{
ID: 0x5FAF,
Length: 0x8,
Flags: 0x0,
Res0: 0x0,
Res1: 0x0,
Data: [MaxFrameDataLength]uint8{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8},
}
type testHandler struct {
frame Frame
}
func newTestHandler() *testHandler {
return &testHandler{}
}
func (h *testHandler) Handle(frame Frame) {
h.frame = frame
}
func TestPublish(t *testing.T) {
rwc := NewEchoReadWriteCloser()
bus := NewBus(rwc)
handler := newTestHandler()
bus.Subscribe(handler)
go bus.publishNextFrame()
rwc.WriteFrame(testFrame)
<-time.After(time.Millisecond * 50)
if is, want := handler.frame, testFrame; reflect.DeepEqual(is, want) == false {
t.Fatalf("is=% X want=% X", is, want)
}
}
func TestSubscribe(t *testing.T) {
rwc := NewEchoReadWriteCloser()
bus := NewBus(rwc)
handler := newTestHandler()
bus.Subscribe(handler)
bus.publish(testFrame)
if is, want := handler.frame, testFrame; reflect.DeepEqual(is, want) == false {
t.Fatalf("is=% X want=% X", is, want)
}
}
func TestUnsubscribe(t *testing.T) {
rwc := NewEchoReadWriteCloser()
bus := NewBus(rwc)
handler := newTestHandler()
bus.Subscribe(handler)
if x := bus.contains(handler); x != true {
t.Fatal(x)
}
bus.Unsubscribe(handler)
if x := bus.contains(handler); x != false {
t.Fatal(x)
}
}