From 5babc3c99b3c91bc7d13496413d02d9b9598d430 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Fri, 16 Aug 2024 11:42:15 -0600 Subject: [PATCH] chore(test): add unit tests --- .../cu/src/domain/client/ao-process.test.js | 132 +++++++++++++++++- servers/cu/src/domain/lib/evaluate.js | 5 +- servers/cu/src/domain/lib/evaluate.test.js | 5 + 3 files changed, 139 insertions(+), 3 deletions(-) diff --git a/servers/cu/src/domain/client/ao-process.test.js b/servers/cu/src/domain/client/ao-process.test.js index 1e56d0e3e..cfc2dc579 100644 --- a/servers/cu/src/domain/client/ao-process.test.js +++ b/servers/cu/src/domain/client/ao-process.test.js @@ -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') @@ -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 () => { diff --git a/servers/cu/src/domain/lib/evaluate.js b/servers/cu/src/domain/lib/evaluate.js index f39e28414..3313e0df4 100644 --- a/servers/cu/src/domain/lib/evaluate.js +++ b/servers/cu/src/domain/lib/evaluate.js @@ -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 @@ -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) @@ -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, diff --git a/servers/cu/src/domain/lib/evaluate.test.js b/servers/cu/src/domain/lib/evaluate.test.js index 00129af78..0224615f3 100644 --- a/servers/cu/src/domain/lib/evaluate.test.js +++ b/servers/cu/src/domain/lib/evaluate.test.js @@ -45,6 +45,7 @@ describe('evaluate', () => { findMessageBefore: async () => { throw { status: 404 } }, loadEvaluator: evaluateHappyMessage, saveLatestProcessMemory: async () => {}, + saveHistoricalProcessMemory: async () => {}, evaluationCounter: { inc: () => undefined }, @@ -178,6 +179,7 @@ describe('evaluate', () => { } }, saveLatestProcessMemory: async () => {}, + saveHistoricalProcessMemory: async () => {}, evaluationCounter: { inc: () => undefined }, @@ -292,6 +294,7 @@ describe('evaluate', () => { } }, saveLatestProcessMemory: async () => {}, + saveHistoricalProcessMemory: async () => {}, evaluationCounter: { inc: () => undefined }, @@ -440,6 +443,7 @@ describe('evaluate', () => { } }, saveLatestProcessMemory: async () => {}, + saveHistoricalProcessMemory: async () => {}, evaluationCounter: { inc: () => undefined }, @@ -552,6 +556,7 @@ describe('evaluate', () => { return { Memory: Buffer.from('Hello world') } }, saveLatestProcessMemory: async () => {}, + saveHistoricalProcessMemory: async () => {}, evaluationCounter: { inc: () => undefined },