-
Notifications
You must be signed in to change notification settings - Fork 9
/
worker_test.go
63 lines (55 loc) · 1.25 KB
/
worker_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
package osc
import (
"testing"
"time"
"github.com/pkg/errors"
)
func TestWorkerRun(t *testing.T) {
var (
data = make(chan Incoming)
errch = make(chan error)
ready = make(chan worker)
)
wrk := worker{
DataChan: data,
Dispatcher: errorDispatcher{},
ErrChan: errch,
Ready: ready,
}
// Worker exits when the data chan is closed.
defer close(data)
// Run the worker goroutine.
go wrk.run()
// Wait for the worker to signal that it is ready.
select {
case <-ready:
case <-time.After(1 * time.Second):
t.Fatal("timeout receiving on ready chan")
}
// Send some data.
incoming := Incoming{
Data: Message{Address: "/foo"}.Bytes(),
}
select {
case data <- incoming:
case <-time.After(1 * time.Second):
t.Fatal("timeout sending on data chan")
}
// Dispatcher will generate an error.
select {
case err := <-errch:
if err == nil {
t.Fatal("expected an error, got nil")
}
case <-time.After(1 * time.Second):
t.Fatal("timeout receiving on error chan")
}
}
type errorDispatcher struct {
}
func (d errorDispatcher) Dispatch(bundle Bundle, exactMatch bool) error {
return errors.New("fake Dispatch error")
}
func (d errorDispatcher) Invoke(msg Message, exactMatch bool) error {
return errors.New("fake Invoke error")
}