From b05a0bba028a6d583fa0d018d169a2b274c618bc Mon Sep 17 00:00:00 2001 From: rishtigupta Date: Tue, 16 Apr 2024 16:23:10 -0700 Subject: [PATCH] fix: return error if compression client is used for incr operation --- src/momento-redis-adapter.ts | 12 ++++++++---- test/increment-compression.test.ts | 28 ++++++++++++++++++++++++++++ test/increment.test.ts | 2 +- test/integration-setup.ts | 12 ++++-------- 4 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 test/increment-compression.test.ts diff --git a/src/momento-redis-adapter.ts b/src/momento-redis-adapter.ts index 671ebdd..6b7064c 100644 --- a/src/momento-redis-adapter.ts +++ b/src/momento-redis-adapter.ts @@ -92,7 +92,7 @@ export interface MomentoIORedis { nx: 'NX' ): Promise<'OK' | null>; - incr(key: RedisKey): Promise; + incr(key: RedisKey): Promise; ttl(key: RedisKey): Promise; @@ -398,17 +398,21 @@ export class MomentoRedisAdapter return null; } - async incr(key: RedisKey): Promise { + async incr(key: RedisKey): Promise { + if (this.useCompression) { + this.emitError('incr', 'compression-not-supported'); + return null; + } + const rsp = await this.momentoClient.increment(this.cacheName, key); if (rsp instanceof CacheIncrement.Success) { return rsp.value(); } else if (rsp instanceof CacheIncrement.Error) { this.emitError('incr', rsp.message(), rsp.errorCode()); - return rsp.message(); } else { this.emitError('incr', `unexpected-response ${rsp.toString()}`); - return rsp.toString(); } + return null; } async hset( diff --git a/test/increment-compression.test.ts b/test/increment-compression.test.ts new file mode 100644 index 0000000..c3b9adc --- /dev/null +++ b/test/increment-compression.test.ts @@ -0,0 +1,28 @@ +import {v4} from 'uuid'; + +import {SetupIntegrationTest} from './integration-setup'; + +const {client} = SetupIntegrationTest(); + +describe('increment, with compression client', () => { + it('should return error saying compression not supported', async () => { + const key = v4(); + const value = 5; + + // Set initial key value + await client.set(key, value); + + // Increment the value of the key + try { + await client.incr(key); + } catch (error) { + const momentoError = error as { + code: string; + context: {code: string; msg: string; op: string; platform: string}; + }; + expect(momentoError.context.op).toBe('incr'); + expect(momentoError.context.platform).toBe('momento'); + expect(momentoError.context.msg).toBe('compression-not-supported'); + } + }); +}); diff --git a/test/increment.test.ts b/test/increment.test.ts index 60f5c22..e5ea4c8 100644 --- a/test/increment.test.ts +++ b/test/increment.test.ts @@ -1,7 +1,7 @@ import {SetupIntegrationTest} from './integration-setup'; import {v4} from 'uuid'; -const {client} = SetupIntegrationTest(); +const {client} = SetupIntegrationTest(false); describe('increment', () => { it('should increment the value of the key by 1 if the key exists', async () => { diff --git a/test/integration-setup.ts b/test/integration-setup.ts index fd941c3..fe9f103 100644 --- a/test/integration-setup.ts +++ b/test/integration-setup.ts @@ -16,10 +16,6 @@ export function testCacheName(): string { return name + v4(); } -function useCompression(): boolean { - return process.env.COMPRESSION === 'true'; -} - const deleteCacheIfExists = async (momento: CacheClient, cacheName: string) => { const deleteResponse = await momento.deleteCache(cacheName); if (deleteResponse instanceof DeleteCache.Error) { @@ -45,17 +41,17 @@ export function isRedisBackedTest() { return process.env.MOMENTO_ENABLED !== 'true'; } -export function SetupIntegrationTest(): { +export function SetupIntegrationTest(useCompression = true): { client: MomentoIORedis; } { if (isRedisBackedTest()) { return setupIntegrationTestWithRedis(); } else { - return setupIntegrationTestWithMomento(); + return setupIntegrationTestWithMomento(useCompression); } } -function setupIntegrationTestWithMomento() { +function setupIntegrationTestWithMomento(useCompression = true) { const cacheName = testCacheName(); beforeAll(async () => { @@ -82,7 +78,7 @@ function setupIntegrationTestWithMomento() { momentoClient, cacheName, { - useCompression: useCompression(), + useCompression: useCompression, } );