-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_test.go
50 lines (42 loc) · 1003 Bytes
/
controller_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
package chik
import (
"context"
"errors"
"testing"
"time"
)
type testHandler struct {
BaseHandler
trigger chan interface{}
currentValue string
}
func (t *testHandler) Setup(controller *Controller) (Interrupts, error) {
return Interrupts{Timer: NewEmptyTimer(), Event: t.trigger}, nil
}
func (t *testHandler) HandleChannelEvent(event interface{}, controller *Controller) error {
val, ok := event.(string)
if !ok {
return errors.New("Conversion error")
}
t.currentValue = val
return nil
}
func TestHandlerError(t *testing.T) {
cont := NewController()
trigger := make(chan interface{}, 0)
handler := testHandler{trigger: trigger}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go cont.Start(
ctx,
[]Handler{&handler},
)
trigger <- false
time.Sleep(6 * time.Second)
word := "works"
trigger <- word
time.Sleep(10 * time.Millisecond)
if handler.currentValue != word {
t.Fatal("Handler error recovery failed: ", handler.currentValue)
}
}