-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdispatcher_test.go
117 lines (110 loc) · 2.35 KB
/
dispatcher_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
import (
"testing"
"time"
"github.com/pkg/errors"
)
// Test a successful method invocation.
func TestDispatcherDispatchOK(t *testing.T) {
c := make(chan struct{})
d := PatternMatching{
"/bar": Method(func(msg Message) error {
close(c)
return nil
}),
}
later := time.Now().Add(20 * time.Millisecond)
b := Bundle{
Timetag: FromTime(later),
Packets: []Packet{
Message{Address: "/bar"},
},
}
if err := d.Dispatch(b, false); err != nil {
t.Fatal(err)
}
<-c
}
// Test a method that returns an error.
func TestDispatcherDispatchError(t *testing.T) {
d := PatternMatching{
"/foo": Method(func(msg Message) error {
return errors.New("oops")
}),
}
later := time.Now().Add(20 * time.Millisecond)
b := Bundle{
Timetag: FromTime(later),
Packets: []Packet{
Message{Address: "/foo"},
},
}
if err := d.Dispatch(b, false); err == nil {
t.Fatal("expected error, got nil")
}
}
func TestDispatcherDispatchNestedBundle(t *testing.T) {
c := make(chan struct{})
d := PatternMatching{
"/foo": Method(func(msg Message) error {
close(c)
return nil
}),
}
later := time.Now().Add(20 * time.Millisecond)
b := Bundle{
Timetag: FromTime(later),
Packets: []Packet{
Bundle{
Timetag: FromTime(later.Add(20 * time.Millisecond)),
Packets: []Packet{
Message{Address: "/foo"},
},
},
},
}
if err := d.Dispatch(b, false); err != nil {
t.Fatal(err)
}
<-c
}
func TestDispatcherMiss(t *testing.T) {
d := PatternMatching{
"/foo": Method(func(msg Message) error {
return nil
}),
}
b := Bundle{
Timetag: FromTime(time.Now()),
}
if err := d.Dispatch(b, false); err != nil {
t.Fatal(err)
}
}
func TestDispatcherInvoke(t *testing.T) {
d := PatternMatching{
"/foo": Method(func(msg Message) error {
return errors.New("foo error")
}),
"/bar": Method(func(msg Message) error {
return nil
}),
}
msg := Message{Address: "/foo"}
if err := d.Invoke(msg, false); err == nil {
t.Fatal("expected error, got nil")
}
badMsg := Message{Address: "/["}
if err := d.Invoke(badMsg, false); err == nil {
t.Fatal("expected error, got nil")
}
if err := d.Invoke(Message{Address: "/bar"}, false); err != nil {
t.Fatal(err)
}
if err := d.Invoke(Message{Address: "/baz"}, false); err != nil {
t.Fatal(err)
}
if err := d.invoke(badPacket{}, false); err == nil {
t.Fatal("expected error, got nil")
}
}