-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatch_test.go
76 lines (59 loc) · 2.21 KB
/
watch_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
package whatnot
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestWatchNotifications(t *testing.T) {
t.Run("Create new watch subscription on PathElement", createNewWatchOnElement)
t.Run("Changing a path element creates notification", changeElementAndNotify)
}
func createNewWatchOnElement(t *testing.T) {
t.Log("creating a new watch on existing path element")
gns := createTestNamespace(t)
testPathString := PathString("/path/to/test/data")
testpath := testPathString.ToAbsolutePath()
err := gns.RegisterAbsolutePath(testpath)
if !assert.Nil(t, err, "registerabsolute path returned error") {
t.Error(err.Error())
return
}
elem := gns.FetchAbsolutePath(testPathString)
sub := elem.SubscribeToEvents(false)
select {
case <-sub.Events():
default:
// we instantly pass here, because there is no waiting message on the channel
}
}
func changeElementAndNotify(t *testing.T) {
t.Log("testing that a notification is received when modifying a watched element")
gns := createTestNamespace(t)
testPathString := PathString("/path/to/test/data")
testpath := testPathString.ToAbsolutePath()
err := gns.RegisterAbsolutePath(testpath)
if !assert.Nil(t, err, "registerabsolute path returned error") {
t.Error(err.Error())
return
}
t.Log("Creating a subscription to change notifications on the test element")
elem := gns.FetchAbsolutePath(testPathString)
// create a local subscription to the element itself
elemsub := elem.SubscribeToEvents(false)
parentelement := elem.Parent()
// create a prefix subscription to the parent element, which should also receive the same notification
parsub := parentelement.SubscribeToEvents(true)
go func() {
t.Log("waiting 1 second for notifier channel to attach")
time.Sleep(time.Second)
t.Log("locking the element to create a change notification")
elem.Lock()
}()
e := <-elemsub.Events()
t.Logf("received update event %d from element subscription", e.id)
assert.Equal(t, elem, e.OnElement(), "watch event did not indicate original element")
e = <-parsub.Events()
t.Logf("received update event %d from parent element subscription", e.id)
assert.Equal(t, elem, e.OnElement(), "watch event did not indicate original element")
time.Sleep(time.Second)
}