-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathloadProcess.test.js
More file actions
214 lines (193 loc) Β· 6.87 KB
/
loadProcess.test.js
File metadata and controls
214 lines (193 loc) Β· 6.87 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
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
/* eslint-disable no-throw-literal */
import { describe, test } from 'node:test'
import * as assert from 'node:assert'
import { createTestLogger } from '../logger.js'
import { loadProcessWith } from './loadProcess.js'
const PROCESS = 'process-123-9HdeqeuYQOgMgWucro'
const logger = createTestLogger({ name: 'ao-cu:readState' })
describe('loadProcess', () => {
test('appends result, from, fromCron, fromBlockHeight and evaluatedAt to ctx', async () => {
const loadProcess = loadProcessWith({
findEvaluation: async () => { throw { status: 404 } },
findLatestProcessMemory: async () => ({
src: 'cold_start',
Memory: null,
moduleId: undefined,
timestamp: undefined,
epoch: undefined,
nonce: undefined,
blockHeight: undefined,
cron: undefined,
ordinate: '0'
}),
saveLatestProcessMemory: async () => assert.fail('should not be called on cold_start'),
logger
})
const res = await loadProcess({ id: PROCESS, to: '1697574792000' }).toPromise()
assert.deepStrictEqual(res.result, { Memory: null })
assert.equal(res.from, undefined)
assert.equal(res.fromCron, undefined)
assert.equal(res.ordinate, '0')
assert.equal(res.fromBlockHeight, undefined)
assert.equal(res.evaluatedAt, undefined)
assert.equal(res.id, PROCESS)
})
test('use exact match from db', async () => {
const cachedEvaluation = {
processId: PROCESS,
messageId: 'message-123',
cron: '1-10-minutes',
timestamp: 1697574792000,
ordinate: '1',
blockHeight: 1234,
evaluatedAt: new Date(),
output: {
Messages: [
{
target: 'foobar',
tags: [
{ name: 'foo', value: 'bar' }
]
}
],
Output: [],
Spawns: []
}
}
const loadProcess = loadProcessWith({
findEvaluation: async () => cachedEvaluation,
findLatestProcessMemory: async ({ processId, timestamp }) => {
assert.fail('should not be called when exact match is found')
},
saveLatestProcessMemory: async () => assert.fail('should not be called if exact match if found'),
logger
})
const res = await loadProcess({ id: PROCESS, to: 1697574792000, messageId: 'message-123' }).toPromise()
assert.deepStrictEqual(res.result, cachedEvaluation.output)
assert.deepStrictEqual(res.from, cachedEvaluation.timestamp)
assert.deepStrictEqual(res.ordinate, cachedEvaluation.ordinate)
assert.deepStrictEqual(res.fromCron, cachedEvaluation.cron)
assert.deepStrictEqual(res.fromBlockHeight, cachedEvaluation.blockHeight)
assert.equal(res.id, PROCESS)
})
test('use latest in cache', async () => {
const cached = {
src: 'memory',
Memory: Buffer.from('hello world'),
moduleId: 'module-123',
timestamp: 1697574792000,
epoch: 0,
nonce: 11,
blockHeight: 123,
cron: undefined,
ordinate: '11'
}
const loadProcess = loadProcessWith({
findEvaluation: async () => { throw { status: 404 } },
findLatestProcessMemory: async ({ processId, timestamp }) => cached,
saveLatestProcessMemory: async () => assert.fail('should not be called if memory'),
logger
})
const res = await loadProcess({ id: PROCESS, to: 1697574792000 }).toPromise()
assert.deepStrictEqual(res.from, cached.timestamp)
assert.deepStrictEqual(res.ordinate, cached.ordinate)
assert.deepStrictEqual(res.fromCron, cached.cron)
assert.deepStrictEqual(res.fromBlockHeight, cached.blockHeight)
assert.equal(res.id, PROCESS)
})
test('backfill cache if latest from arweave or file', async () => {
const cached = {
Memory: Buffer.from('hello world'),
moduleId: 'module-123',
timestamp: 1697574792000,
epoch: 0,
nonce: 11,
blockHeight: 123,
cron: undefined,
ordinate: '11'
}
const loadProcess = loadProcessWith({
findEvaluation: async () => { throw { status: 404 } },
findLatestProcessMemory: async ({ processId, timestamp }) => cached,
saveLatestProcessMemory: async (args) => {
assert.deepStrictEqual(args, {
processId: PROCESS,
evalCount: 0,
Memory: cached.Memory,
moduleId: cached.moduleId,
timestamp: cached.timestamp,
epoch: cached.epoch,
nonce: cached.nonce,
ordinate: cached.ordinate,
blockHeight: cached.blockHeight,
cron: cached.cron
})
},
logger
})
cached.src = 'record'
await loadProcess({ id: PROCESS, to: 1697574792000 }).toPromise()
cached.src = 'arweave'
await loadProcess({ id: PROCESS, to: 1697574792000 }).toPromise()
})
test('backfill cache if latest from memory drained to file', async () => {
const cached = {
Memory: Buffer.from('hello world'),
moduleId: 'module-123',
timestamp: 1697574792000,
epoch: 0,
nonce: 11,
blockHeight: 123,
cron: undefined,
ordinate: '11'
}
const loadProcess = loadProcessWith({
findEvaluation: async () => { throw { status: 404 } },
findLatestProcessMemory: async ({ processId, timestamp }) => cached,
saveLatestProcessMemory: async (args) => {
assert.deepStrictEqual(args, {
processId: PROCESS,
evalCount: 0,
Memory: cached.Memory,
moduleId: cached.moduleId,
timestamp: cached.timestamp,
epoch: cached.epoch,
nonce: cached.nonce,
ordinate: cached.ordinate,
blockHeight: cached.blockHeight,
cron: cached.cron
})
},
logger
})
cached.src = 'memory'
cached.fromFile = 'state-process-123.dat'
await loadProcess({ id: PROCESS, to: 1697574792000 }).toPromise()
})
test('bubble 425 if latestProcessMemory is later than requested message', async () => {
const loadProcess = loadProcessWith({
findEvaluation: async () => { throw { status: 404 } },
findLatestProcessMemory: async ({ processId, timestamp }) => {
throw { status: 425, ordinate: '12', message: 'foobar' }
},
saveLatestProcessMemory: async () => assert.fail('should not be called if memory'),
logger
})
// timestamp
await loadProcess({ id: PROCESS, to: 1697574792000 })
.toPromise()
.then(() => assert.fail('should have rejected'))
.catch((err) => assert.deepStrictEqual(err, {
status: 425,
message: 'message at timestamp 1697574792000 not found cached, and earlier than latest known nonce 12'
}))
// ordinate
await loadProcess({ id: PROCESS, ordinate: 11 })
.toPromise()
.then(() => assert.fail('should have rejected'))
.catch((err) => assert.deepStrictEqual(err, {
status: 425,
message: 'message at nonce 11 not found cached, and earlier than latest known nonce 12'
}))
})
})