-
Notifications
You must be signed in to change notification settings - Fork 16
/
secured_subscriptions_basic_test.go
192 lines (154 loc) · 4.5 KB
/
secured_subscriptions_basic_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package tests
import (
"fmt"
"reflect"
"testing"
"time"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func securedSubscriptionsBasic_shouldStreamAllDocumentsAfterSubscriptionCreation(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getSecuredDocumentStoreMust(t)
defer store.Close()
var users []*User
{
session := openSessionMust(t, store)
user1 := &User{
Age: 31,
}
err = session.StoreWithID(user1, "users/1")
assert.NoError(t, err)
users = append(users, user1)
user2 := &User{
Age: 27,
}
err = session.StoreWithID(user2, "users/12")
assert.NoError(t, err)
users = append(users, user2)
user3 := &User{
Age: 25,
}
err = session.StoreWithID(user3, "users/3")
assert.NoError(t, err)
users = append(users, user3)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
id, err := store.Subscriptions().CreateForType(reflect.TypeOf(&User{}), nil, "")
assert.NoError(t, err)
{
opts := ravendb.NewSubscriptionWorkerOptions(id)
clazz := reflect.TypeOf(&User{})
subscription, err := store.Subscriptions().GetSubscriptionWorker(clazz, opts, "")
assert.NoError(t, err)
results := make(chan *ravendb.SubscriptionBatch, 16)
cb := func(batch *ravendb.SubscriptionBatch) error {
results <- batch
return nil
}
err = subscription.Run(cb)
assert.NoError(t, err)
select {
case batch := <-results:
for i, item := range batch.Items {
userExp := users[i]
assert.Equal(t, item.ID, userExp.ID)
var u *User
err := item.GetResult(&u)
assert.NoError(t, err)
assert.Equal(t, u.Age, userExp.Age)
}
case <-time.After(_reasonableWaitTime):
assert.Fail(t, "timed out waiting for batch")
}
err = subscription.Close()
assert.NoError(t, err)
}
}
func securedSubscriptionsBasic_shouldSendAllNewAndModifiedDocs(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getSecuredDocumentStoreMust(t)
defer store.Close()
id, err := store.Subscriptions().CreateForType(reflect.TypeOf(&User{}), nil, "")
assert.NoError(t, err)
{
opts := ravendb.NewSubscriptionWorkerOptions(id)
clazz := reflect.TypeOf(map[string]interface{}{})
subscription, err := store.Subscriptions().GetSubscriptionWorker(clazz, opts, "")
assert.NoError(t, err)
results := make(chan *ravendb.SubscriptionBatch, 16)
cb := func(batch *ravendb.SubscriptionBatch) error {
results <- batch
return nil
}
err = subscription.Run(cb)
assert.NoError(t, err)
{
session := openSessionMust(t, store)
user := &User{}
user.setName("James")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
assertNextNameIs := func(results chan *ravendb.SubscriptionBatch, nameExp string) {
select {
case batch := <-results:
assert.Equal(t, len(batch.Items), 1)
item := batch.Items[0]
var m map[string]interface{}
err := item.GetResult(&m)
assert.NoError(t, err)
name := m["name"].(string)
assert.Equal(t, nameExp, name)
case <-time.After(_reasonableWaitTime):
assert.Fail(t, "timed out waiting for batch")
}
}
assertNextNameIs(results, "James")
{
session := openSessionMust(t, store)
user := &User{}
user.setName("Adam")
err = session.StoreWithID(user, "users/12")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
assertNextNameIs(results, "Adam")
//Thread.sleep(15000); // test with sleep - let few heartbeats come to us - commented out for CI
{
session := openSessionMust(t, store)
user := &User{}
user.setName("David")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
assertNextNameIs(results, "David")
err = subscription.Close()
assert.NoError(t, err)
}
}
func TestSecuredSubscriptionsBasic(t *testing.T) {
// self-signing cert on windows is not added as root ca so
// we can't run https tests
if isWindows() {
fmt.Printf("Skipping TestSecuredSubscriptionsBasic on windows\n")
t.Skip("Skipping TestSecuredSubscriptionsBasic on windows")
return
}
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches order of Java tests
securedSubscriptionsBasic_shouldStreamAllDocumentsAfterSubscriptionCreation(t, driver)
securedSubscriptionsBasic_shouldSendAllNewAndModifiedDocs(t, driver)
}