Skip to content

Commit

Permalink
fix: return error if compression client is used for incr operation
Browse files Browse the repository at this point in the history
  • Loading branch information
rishtigupta committed Apr 16, 2024
1 parent 72edd17 commit b05a0bb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
12 changes: 8 additions & 4 deletions src/momento-redis-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface MomentoIORedis {
nx: 'NX'
): Promise<'OK' | null>;

incr(key: RedisKey): Promise<number | string>;
incr(key: RedisKey): Promise<number | null>;

ttl(key: RedisKey): Promise<number | null>;

Expand Down Expand Up @@ -398,17 +398,21 @@ export class MomentoRedisAdapter
return null;
}

async incr(key: RedisKey): Promise<number | string> {
async incr(key: RedisKey): Promise<number | null> {
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(
Expand Down
28 changes: 28 additions & 0 deletions test/increment-compression.test.ts
Original file line number Diff line number Diff line change
@@ -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');
}
});
});
2 changes: 1 addition & 1 deletion test/increment.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down
12 changes: 4 additions & 8 deletions test/integration-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 () => {
Expand All @@ -82,7 +78,7 @@ function setupIntegrationTestWithMomento() {
momentoClient,
cacheName,
{
useCompression: useCompression(),
useCompression: useCompression,
}
);

Expand Down

0 comments on commit b05a0bb

Please sign in to comment.