-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwatchable-resolver_test.go
95 lines (82 loc) · 2.58 KB
/
watchable-resolver_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
package directive_test
import (
"context"
"testing"
"github.com/aperturerobotics/controllerbus/bus/inmem"
"github.com/aperturerobotics/controllerbus/controller"
"github.com/aperturerobotics/controllerbus/controller/callback"
"github.com/aperturerobotics/controllerbus/directive"
cdc "github.com/aperturerobotics/controllerbus/directive/controller"
boilerplate_v1 "github.com/aperturerobotics/controllerbus/example/boilerplate/v1"
"github.com/aperturerobotics/util/ccontainer"
"github.com/blang/semver/v4"
"github.com/sirupsen/logrus"
)
func TestWatchableResolver(t *testing.T) {
ctx := context.Background()
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
dc := cdc.NewController(ctx, le)
b := inmem.NewBus(dc)
ctr := ccontainer.NewCContainer(0)
testCtrl := callback.NewCallbackController(
controller.NewInfo("test", semver.MustParse("0.0.0"), "test controller"),
nil,
func(ctx context.Context, di directive.Instance) ([]directive.Resolver, error) {
switch di.GetDirective().(type) {
case *boilerplate_v1.Boilerplate:
return directive.R(directive.NewWatchableResolver(ctr), nil)
}
return nil, nil
},
nil,
)
relCtrl, err := b.AddController(ctx, testCtrl, nil)
if err != nil {
t.Fatal(err.Error())
}
defer relCtrl()
dir := &boilerplate_v1.Boilerplate{MessageText: "test"}
type cbValues struct {
added int
removed int
}
cbChan := make(chan cbValues, 1)
handler := directive.NewCallbackHandler(func(addedValue directive.AttachedValue) {
cbChan <- cbValues{added: addedValue.GetValue().(int)}
}, func(removedValue directive.AttachedValue) {
cbChan <- cbValues{removed: removedValue.GetValue().(int)}
}, nil)
_, dirRef, err := dc.AddDirective(dir, directive.NewLogHandler(le, handler))
if err != nil {
t.Fatal(err.Error())
}
defer dirRef.Release()
// Set value and expect it to be added
ctr.SetValue(42)
values := <-cbChan
if values.added != 42 {
t.Fatalf("Expected added value to be 42, got %d", values.added)
}
if values.removed != 0 {
t.Fatalf("Expected removed value to be 0, got %d", values.removed)
}
// Change value and expect old value removed, new value added
ctr.SetValue(99)
values = <-cbChan
if values.removed != 42 {
t.Fatalf("Expected removed value to be 42, got %d", values.removed)
}
values = <-cbChan
if values.added != 99 {
t.Fatalf("Expected added value to be 99, got %d", values.added)
}
// Clear value and expect it to be removed
var empty int
ctr.SetValue(empty)
values = <-cbChan
if values.removed != 99 {
t.Fatalf("Expected removed value to be 99, got %d", values.removed)
}
}