-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_test.go
More file actions
106 lines (90 loc) · 2.9 KB
/
Copy pathdynamic_test.go
File metadata and controls
106 lines (90 loc) · 2.9 KB
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
package proteus_test
import (
"bytes"
"sync/atomic"
"testing"
"time"
"github.com/simplesurance/proteus"
"github.com/simplesurance/proteus/internal/assert"
"github.com/simplesurance/proteus/plog"
"github.com/simplesurance/proteus/sources/cfgtest"
"github.com/simplesurance/proteus/types"
"github.com/simplesurance/proteus/xtypes"
)
// TestDynamic asserts that the expected behavior is being observed when
// a provider updates a value regarding the result of the Value() function
// and the callback function. The test is written in a specific way to
// maximize the chance of possible race conditions to be detected by
// "go test -race".
func TestDynamic(t *testing.T) {
wantedValues := []string{
"initial value",
"updated value",
"second updated value",
"final updated value",
}
var callbackInvoked int32
params := struct {
X *xtypes.String
Y string
}{
X: &xtypes.String{
UpdateFn: func(s string) {
// Each time this callback function is called we expect
// that is called with one of the values from the wanted
// values, in order.
callIx := atomic.AddInt32(&callbackInvoked, 1) - 1
t.Logf("callback invoked callIx=%d value=%s", callIx, s)
assert.SmallerNow(t, int(callIx), len(wantedValues))
assert.Equal(t, wantedValues[callIx], s)
},
},
}
provider := cfgtest.New(types.ParamValues{
"": map[string]string{
"x": wantedValues[0],
"y": wantedValues[0],
},
})
parsed, err := proteus.MustParse(¶ms,
proteus.WithLogger(plog.TestLogger(t)),
proteus.WithProviders(provider))
if err != nil {
buffer := bytes.Buffer{}
parsed.WriteError(&buffer, err)
t.Error(buffer.String())
}
defer parsed.Stop()
assert.EqualNow(t, wantedValues[0], params.X.Value())
assert.EqualNow(t, wantedValues[0], params.Y)
time.Sleep(time.Second)
for ix, value := range wantedValues[1:] {
t.Logf("Done waiting for callback; requesting dynamic value update with value %q", value)
// Change the value in one routine and assert that the new value is
// visible on another. The read happens on a busy loop. This is to
// maximize the chance of the go race detector to find issues with the
// code.
go func(value string) {
time.Sleep(100 * time.Millisecond)
provider.Update("", "x", &value)
}(value)
start := time.Now()
for i := 0; ; i++ {
if params.Y != wantedValues[0] {
t.Fatalf("non-xtype value has unexpected value %q", params.Y)
}
if params.X.Value() == value {
t.Logf("Got new value with i=%d", i)
break
}
if time.Since(start) > 2*time.Second {
t.Fatalf("timeout waiting for Value() to return test value ix=%d value=%q", ix+1, value)
}
}
// give time for any spurious invocation of the callback function to happen
time.Sleep(time.Second)
}
// each value the parameter ever had must result in exactly one call to
// the callback function
assert.Equal(t, len(wantedValues), int(atomic.LoadInt32(&callbackInvoked)))
}