forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.test.ts
180 lines (158 loc) · 4.75 KB
/
db.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
import { once } from 'events'
import { wait } from '@atproto/common'
import { Database } from '../src'
import { Leader } from '../src/db/leader'
import { runTestServer, CloseFn } from './_util'
describe('db', () => {
let close: CloseFn
let db: Database
beforeAll(async () => {
const server = await runTestServer({
dbPostgresSchema: 'db',
})
close = server.close
db = server.ctx.db
})
afterAll(async () => {
if (close) {
await close()
}
})
describe('transaction()', () => {
it('commits changes', async () => {
const result = await db.transaction(async (dbTxn) => {
return await dbTxn.db
.insertInto('actor')
.values({
did: 'x',
handle: 'x',
indexedAt: 'bad-date',
})
.returning('did')
.executeTakeFirst()
})
if (!result) {
return expect(result).toBeTruthy()
}
expect(result.did).toEqual('x')
const row = await db.db
.selectFrom('actor')
.selectAll()
.where('did', '=', 'x')
.executeTakeFirst()
expect(row).toEqual({
did: 'x',
handle: 'x',
indexedAt: 'bad-date',
takedownId: null,
})
})
it('rolls-back changes on failure', async () => {
const promise = db.transaction(async (dbTxn) => {
await dbTxn.db
.insertInto('actor')
.values({
did: 'y',
handle: 'y',
indexedAt: 'bad-date',
})
.returning('did')
.executeTakeFirst()
throw new Error('Oops!')
})
await expect(promise).rejects.toThrow('Oops!')
const row = await db.db
.selectFrom('actor')
.selectAll()
.where('did', '=', 'y')
.executeTakeFirst()
expect(row).toBeUndefined()
})
it('indicates isTransaction', async () => {
expect(db.isTransaction).toEqual(false)
await db.transaction(async (dbTxn) => {
expect(db.isTransaction).toEqual(false)
expect(dbTxn.isTransaction).toEqual(true)
})
expect(db.isTransaction).toEqual(false)
})
it('asserts transaction', async () => {
expect(() => db.assertTransaction()).toThrow('Transaction required')
await db.transaction(async (dbTxn) => {
expect(() => dbTxn.assertTransaction()).not.toThrow()
})
})
})
describe('Leader', () => {
it('allows leaders to run sequentially.', async () => {
const task = async () => {
await wait(25)
return 'complete'
}
const leader1 = new Leader(707, db)
const leader2 = new Leader(707, db)
const leader3 = new Leader(707, db)
const result1 = await leader1.run(task)
await wait(5) // Short grace period for pg to close session
const result2 = await leader2.run(task)
await wait(5)
const result3 = await leader3.run(task)
await wait(5)
const result4 = await leader3.run(task)
expect([result1, result2, result3, result4]).toEqual([
{ ran: true, result: 'complete' },
{ ran: true, result: 'complete' },
{ ran: true, result: 'complete' },
{ ran: true, result: 'complete' },
])
})
it('only allows one leader at a time.', async () => {
const task = async () => {
await wait(75)
return 'complete'
}
const results = await Promise.all([
new Leader(717, db).run(task),
new Leader(717, db).run(task),
new Leader(717, db).run(task),
])
const byRan = (a, b) => Number(a.ran) - Number(b.ran)
expect(results.sort(byRan)).toEqual([
{ ran: false },
{ ran: false },
{ ran: true, result: 'complete' },
])
})
it('leaders with different ids do not conflict.', async () => {
const task = async () => {
await wait(75)
return 'complete'
}
const results = await Promise.all([
new Leader(727, db).run(task),
new Leader(728, db).run(task),
new Leader(729, db).run(task),
])
expect(results).toEqual([
{ ran: true, result: 'complete' },
{ ran: true, result: 'complete' },
{ ran: true, result: 'complete' },
])
})
it('supports abort.', async () => {
const task = async (ctx: { signal: AbortSignal }) => {
wait(10).then(abort)
return await Promise.race([
wait(50),
once(ctx.signal, 'abort').then(() => ctx.signal.reason),
])
}
const leader = new Leader(737, db)
const abort = () => {
leader.session?.abortController.abort(new Error('Oops!'))
}
const result = await leader.run(task)
expect(result).toEqual({ ran: true, result: new Error('Oops!') })
})
})
})