-
Notifications
You must be signed in to change notification settings - Fork 0
/
rethinkdb-iterable.test.ts
223 lines (210 loc) · 5.33 KB
/
rethinkdb-iterable.test.ts
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/// <reference types="jest" />
import rethinkdb, { Connection } from 'rethinkdb'
import RethinkdbIterable from '..'
import _assert from 'assert'
function assert(val: any, msg: string): asserts val {
_assert(val, msg)
}
describe('RethinkdbIterable', () => {
let index = 0
const ctx: {
conn: Connection | null
db: string
intervalId: NodeJS.Timeout | null
table: string
} = {
conn: null,
db: 'rethinkdb_iterable_test',
intervalId: null,
table: 'rethinkdb_iterable_test',
}
beforeEach(async () => {
ctx.conn = await rethinkdb.connect({
host: 'localhost',
db: '',
})
await rethinkdb
.dbCreate(ctx.db)
.run(ctx.conn)
.catch((err) => {
if (/exists/.test(err.message)) return
})
await rethinkdb
.db(ctx.db)
.tableCreate(ctx.table)
.run(ctx.conn)
.catch((err) => {
if (/exists/.test(err.message)) return
})
await rethinkdb
.db(ctx.db)
.table(ctx.table)
.indexCreate('value', rethinkdb.row('value'))
.run(ctx.conn)
.catch((err) => {
if (/exists/.test(err.message)) return
})
await rethinkdb.db(ctx.db).table(ctx.table).indexWait('value').run(ctx.conn)
})
afterEach(async () => {
if (ctx.conn) {
await rethinkdb
.dbDrop(ctx.db)
.run(ctx.conn)
.catch((err) => {
if (/exists/.test(err.message)) return
})
await rethinkdb
.db(ctx.db)
.tableDrop(ctx.table)
.run(ctx.conn)
.catch((err) => {
if (/exists/.test(err.message)) return
})
await ctx.conn.close()
}
})
describe('rows being created in table on interval', () => {
const activePromises = new Set()
beforeEach(() => {
assert(ctx.conn != null, 'connection is missing')
const conn = ctx.conn
// insert new rows on interval
ctx.intervalId = setInterval(() => {
const p = rethinkdb
.db(ctx.db)
.table(ctx.table)
.insert({
id: index++,
})
.run(conn)
.finally(() => activePromises.delete(p))
activePromises.add(p)
}, 100)
})
afterEach(async () => {
if (ctx.intervalId) clearInterval(ctx.intervalId)
await Promise.all(Array.from(activePromises))
})
it('should create an async iterable, iterate through it, and eventually close the cursor', async () => {
assert(ctx.conn != null, 'connection is missing')
const conn = ctx.conn
const cursor = await rethinkdb
.db(ctx.db)
.table(ctx.table)
.changes()
.run(conn)
const iterable = new RethinkdbIterable(cursor)
let done = false
setTimeout(() => {
done = true
}, 300)
const results = []
for await (const val of iterable) {
results.push(val)
if (done) break
}
expect(results).toMatchInlineSnapshot(`
Array [
Object {
"new_val": Object {
"id": 0,
},
"old_val": null,
},
Object {
"new_val": Object {
"id": 1,
},
"old_val": null,
},
Object {
"new_val": Object {
"id": 2,
},
"old_val": null,
},
]
`)
})
})
describe('one row exists', () => {
const indexValue = 'indexValue'
beforeEach(async () => {
assert(ctx.conn != null, 'connection is missing')
const conn = ctx.conn
// insert new rows on interval
await rethinkdb
.db(ctx.db)
.table(ctx.table)
.insert({
id: index++,
value: indexValue,
})
.run(conn)
await rethinkdb
.db(ctx.db)
.table(ctx.table)
.insert({
id: index++,
value: indexValue,
})
.run(conn)
await rethinkdb
.db(ctx.db)
.table(ctx.table)
.insert({
id: index++,
value: indexValue,
})
.run(conn)
})
afterEach(() => {
if (ctx.intervalId) clearInterval(ctx.intervalId)
})
it('should create an async iterable that gets row and completes successfully', async () => {
assert(ctx.conn != null, 'connection is missing')
const conn = ctx.conn
// @ts-ignore
const cursor = await rethinkdb
.db(ctx.db)
.table(ctx.table)
.getAll(indexValue, { index: 'value' })
.run(ctx.conn)
const iterable = new RethinkdbIterable(cursor)
const results = []
for await (const val of iterable) {
results.push(val)
}
expect(results).toMatchInlineSnapshot(`
Array [
Object {
"id": 4,
"value": "indexValue",
},
Object {
"id": 5,
"value": "indexValue",
},
Object {
"id": 3,
"value": "indexValue",
},
]
`)
})
})
})
const toPromise = <R>(
task: (cb: (err?: Error, val?: R) => void) => void
): Promise<R> => {
return new Promise((resolve, reject) => {
task((...args) => {
const [err, val] = args
if (err != null) return void reject(err)
if (args.length === 2) return void resolve(val)
resolve()
})
})
}
export default toPromise