Skip to content

Commit

Permalink
chore(test): add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Aug 16, 2024
1 parent 6925014 commit 5babc3c
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 3 deletions.
132 changes: 131 additions & 1 deletion servers/cu/src/domain/client/ao-process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import bytes from 'bytes'
import { createLogger } from '../logger.js'
import { findLatestProcessMemorySchema, findProcessSchema, saveLatestProcessMemorySchema, saveProcessSchema } from '../dal.js'
import { PROCESSES_TABLE } from './sqlite.js'
import { LATEST, createProcessMemoryCache, findCheckpointFileBeforeWith, findLatestProcessMemoryWith, findProcessWith, saveCheckpointWith, saveLatestProcessMemoryWith, saveProcessWith } from './ao-process.js'
import { LATEST, createProcessMemoryCache, findCheckpointFileBeforeWith, findLatestProcessMemoryWith, findProcessWith, saveCheckpointWith, saveLatestProcessMemoryWith, saveHistoricalProcessMemoryWith, saveProcessWith } from './ao-process.js'

const gzipP = promisify(gzip)
const logger = createLogger('ao-cu:ao-process')
Expand Down Expand Up @@ -1400,6 +1400,136 @@ describe('ao-process', () => {
})
})

describe('saveHistoricalProcessMemoryWith', () => {
const PROCESS = 'process-123'
const now = new Date().getTime()
const tenSecondsAgo = now - 10000
const Memory = Buffer.from('hello world')
const cachedEval = {
processId: PROCESS,
moduleId: 'module-123',
epoch: 0,
nonce: 11,
timestamp: tenSecondsAgo,
blockHeight: 123,
ordinate: '11',
encoding: 'gzip',
gasUsed: BigInt(100)
}
const cachedEvalFuture = {
processId: PROCESS,
moduleId: 'module-123',
epoch: 0,
nonce: 11,
timestamp: now + 1000,
blockHeight: 123,
ordinate: '11',
encoding: 'gzip',
gasUsed: BigInt(100)
}
const targetWithGasUsed = {
processId: PROCESS,
Memory: Buffer.from('Hello World'),
timestamp: now - 1000,
ordinate: '13',
cron: undefined,
evalCount: 15,
gasUsed: BigInt(50)
}

describe('updating the cache', () => {
const deps = {
cache: {
get: () => ({
Memory,
evaluation: cachedEvalFuture
}),
set: () => assert.fail('should not call if found in cache')
},
logger,
saveCheckpoint: () => assert.fail('should not call if found in cache'),
EAGER_CHECKPOINT_ACCUMULATED_GAS_THRESHOLD: BigInt(200)
}

test('should not update if the cache entry is ahead of provided save', async () => {
const saveHistoricalProcessMemory = saveHistoricalProcessMemoryWith(deps)
const res = await saveHistoricalProcessMemory(targetWithGasUsed)
assert.equal(res, undefined)
})

test('should update if the cache entry is ahead of provided save', async () => {
let cacheUpdated = false
const saveHistoricalProcessMemory = saveHistoricalProcessMemoryWith({
...deps,
cache: {
get: () => ({
Memory,
evaluation: cachedEval
}),
set: () => { cacheUpdated = true }
}
})
await saveHistoricalProcessMemory(targetWithGasUsed)
assert.ok(cacheUpdated)
})

test('should update if the cache entry is equal to provided save, AND cache does not contain the Memory', async () => {
let cacheUpdated = false
const saveHistoricalProcessMemory = saveHistoricalProcessMemoryWith({
...deps,
cache: {
get: () => ({
Memory: undefined,
evaluation: cachedEval
}),
set: () => { cacheUpdated = true }
}
})
await saveHistoricalProcessMemory({
...targetWithGasUsed,
timestamp: cachedEval.timestamp,
ordinate: cachedEval.ordinate,
cron: cachedEval.cron
})
assert.ok(cacheUpdated)
})

test('should NOT update if the cache entry is equal to provided save, AND contains the Memory', async () => {
let cacheUpdated = false
const saveHistoricalProcessMemory = saveHistoricalProcessMemoryWith({
...deps,
cache: {
get: () => ({
Memory,
evaluation: cachedEval
}),
set: () => { cacheUpdated = true }
}
})
await saveHistoricalProcessMemory({
...targetWithGasUsed,
timestamp: cachedEval.timestamp,
oridnate: cachedEval.oridnate,
cron: cachedEval.cron
})
assert.ok(!cacheUpdated)
})

test('should update if there is no cache entry', async () => {
let cacheUpdated = false
const saveHistoricalProcessMemory = saveHistoricalProcessMemoryWith({
...deps,
cache: {
get: () => null,
set: () => { cacheUpdated = true }
}
})
await saveHistoricalProcessMemory(targetWithGasUsed)
assert.ok(cacheUpdated)
})
})
})

describe('createProcessMemoryCache', () => {
const THIRTY_TWO_DAYS = 1000 * 60 * 60 * 24 * 30
test('can set cache TTL greater than int32 max value without overflowing', async () => {
Expand Down
5 changes: 3 additions & 2 deletions servers/cu/src/domain/lib/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { LRUCache } from 'lru-cache'
import { evaluatorSchema, findMessageBeforeSchema, saveLatestProcessMemorySchema } from '../dal.js'
import { evaluationSchema } from '../model.js'
import { removeTagsByNameMaybeValue } from '../utils.js'
import { saveHistoricalProcessMemoryWith } from '../client/ao-process.js'

/**
* The result that is produced from this step
Expand Down Expand Up @@ -85,6 +84,8 @@ export function evaluateWith (env) {
const loadEvaluator = evaluatorWith(env)

const saveLatestProcessMemory = saveLatestProcessMemorySchema.implement(env.saveLatestProcessMemory)
// TODO: add zod schema for saveHistoricalProcessMemory
const saveHistoricalProcessMemory = env.saveHistoricalProcessMemory

return (ctx) =>
of(ctx)
Expand Down Expand Up @@ -314,7 +315,7 @@ export function evaluateWith (env) {
gasUsed: totalGasUsed
})
// for historical purposes
await saveHistoricalProcessMemoryWith({
await saveHistoricalProcessMemory({
processId: ctx.id,
moduleId: ctx.moduleId,
messageId: message.Id,
Expand Down
5 changes: 5 additions & 0 deletions servers/cu/src/domain/lib/evaluate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('evaluate', () => {
findMessageBefore: async () => { throw { status: 404 } },
loadEvaluator: evaluateHappyMessage,
saveLatestProcessMemory: async () => {},
saveHistoricalProcessMemory: async () => {},
evaluationCounter: {
inc: () => undefined
},
Expand Down Expand Up @@ -178,6 +179,7 @@ describe('evaluate', () => {
}
},
saveLatestProcessMemory: async () => {},
saveHistoricalProcessMemory: async () => {},
evaluationCounter: {
inc: () => undefined
},
Expand Down Expand Up @@ -292,6 +294,7 @@ describe('evaluate', () => {
}
},
saveLatestProcessMemory: async () => {},
saveHistoricalProcessMemory: async () => {},
evaluationCounter: {
inc: () => undefined
},
Expand Down Expand Up @@ -440,6 +443,7 @@ describe('evaluate', () => {
}
},
saveLatestProcessMemory: async () => {},
saveHistoricalProcessMemory: async () => {},
evaluationCounter: {
inc: () => undefined
},
Expand Down Expand Up @@ -552,6 +556,7 @@ describe('evaluate', () => {
return { Memory: Buffer.from('Hello world') }
},
saveLatestProcessMemory: async () => {},
saveHistoricalProcessMemory: async () => {},
evaluationCounter: {
inc: () => undefined
},
Expand Down

0 comments on commit 5babc3c

Please sign in to comment.