add FakeCacheManagementService testing utility
This commit is contained in:
parent
4142788989
commit
dac1a3d20f
1 changed files with 73 additions and 0 deletions
73
packages/backend/test/misc/FakeCacheManagementService.ts
Normal file
73
packages/backend/test/misc/FakeCacheManagementService.ts
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import * as Redis from 'ioredis';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { GodOfTimeService } from './GodOfTimeService.js';
|
||||
import { MockInternalEventService } from './MockInternalEventService.js';
|
||||
import { MockRedis } from './MockRedis.js';
|
||||
import type { QuantumKVOpts } from '@/misc/QuantumKVCache.js';
|
||||
import type { RedisKVCacheOpts, RedisSingleCacheOpts } from '@/misc/cache.js';
|
||||
import type { TimeService } from '@/core/TimeService.js';
|
||||
import type { InternalEventService } from '@/core/InternalEventService.js';
|
||||
import {
|
||||
CacheManagementService,
|
||||
type ManagedMemoryKVCache,
|
||||
type ManagedMemorySingleCache,
|
||||
type ManagedRedisKVCache,
|
||||
type ManagedRedisSingleCache,
|
||||
type ManagedQuantumKVCache,
|
||||
} from '@/core/CacheManagementService.js';
|
||||
|
||||
/**
|
||||
* Fake implementation of cache management that suppresses all caching behavior.
|
||||
* The returned cache instances are real and fully functional, but expiration is negative to ensure that data is immediately discarded and nothing is cached.
|
||||
* Essentially, it strips out the caching behavior and converts caches into pure data accessors.
|
||||
*/
|
||||
@Injectable()
|
||||
export class FakeCacheManagementService extends CacheManagementService {
|
||||
constructor(opts?: {
|
||||
redisClient?: Redis.Redis;
|
||||
timeService?: TimeService;
|
||||
internalEventService?: InternalEventService;
|
||||
}) {
|
||||
const timeService = opts?.timeService ?? new GodOfTimeService();
|
||||
const redisClient = opts?.redisClient ?? new MockRedis(timeService);
|
||||
const internalEventService = opts?.internalEventService ?? new MockInternalEventService();
|
||||
|
||||
super(redisClient, timeService, internalEventService);
|
||||
}
|
||||
|
||||
createMemoryKVCache<T>(): ManagedMemoryKVCache<T> {
|
||||
return super.createMemoryKVCache(-1);
|
||||
}
|
||||
|
||||
createMemorySingleCache<T>(): ManagedMemorySingleCache<T> {
|
||||
return super.createMemorySingleCache(-1);
|
||||
}
|
||||
|
||||
createRedisKVCache<T>(name: string, opts: RedisKVCacheOpts<T>): ManagedRedisKVCache<T> {
|
||||
return super.createRedisKVCache(name, {
|
||||
...opts,
|
||||
lifetime: -1,
|
||||
memoryCacheLifetime: -1,
|
||||
});
|
||||
}
|
||||
|
||||
createRedisSingleCache<T>(name: string, opts: RedisSingleCacheOpts<T>): ManagedRedisSingleCache<T> {
|
||||
return super.createRedisSingleCache(name, {
|
||||
...opts,
|
||||
lifetime: -1,
|
||||
memoryCacheLifetime: -1,
|
||||
});
|
||||
}
|
||||
|
||||
createQuantumKVCache<T>(name: string, opts: QuantumKVOpts<T>): ManagedQuantumKVCache<T> {
|
||||
return super.createQuantumKVCache(name, {
|
||||
...opts,
|
||||
lifetime: -1,
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue