Skip to content

Commit 957de1f

Browse files
Add ttl and tests for ttl (#160)
1 parent 74d84f9 commit 957de1f

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

schemaregistry/rules/encryption/dekregistry/dekregistry-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class DekRegistryClient implements DekClient {
6666
constructor(config: ClientConfig) {
6767
const cacheOptions = {
6868
max: config.cacheCapacity !== undefined ? config.cacheCapacity : 1000,
69-
...(config.cacheLatestTtlSecs !== undefined && { maxAge: config.cacheLatestTtlSecs * 1000 }),
69+
...(config.cacheLatestTtlSecs !== undefined && { ttl: config.cacheLatestTtlSecs * 1000 }),
7070
};
7171

7272

schemaregistry/schemaregistry-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export class SchemaRegistryClient implements Client {
198198
this.clientConfig = config
199199
const cacheOptions = {
200200
max: config.cacheCapacity !== undefined ? config.cacheCapacity : 1000,
201-
...(config.cacheLatestTtlSecs !== undefined && { maxAge: config.cacheLatestTtlSecs * 1000 })
201+
...(config.cacheLatestTtlSecs !== undefined && { ttl: config.cacheLatestTtlSecs * 1000 })
202202
};
203203

204204
this.restService = new RestService(config.baseURLs, config.isForward, config.createAxiosDefaults,

test/schemaregistry/schemaregistry-client.spec.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { RestService } from '../../schemaregistry/rest-service';
1010
import { AxiosResponse } from 'axios';
1111
import stringify from "json-stringify-deterministic";
1212
import { beforeEach, afterEach, describe, expect, it, jest } from '@jest/globals';
13-
import { mockClientConfig } from '../../test/schemaregistry/test-constants';
13+
import { mockClientConfig, mockTtlClientConfig } from '../../test/schemaregistry/test-constants';
1414

1515
jest.mock('../../schemaregistry/rest-service');
1616

@@ -76,6 +76,10 @@ const schemaInfoMetadata2 = {
7676
const subjects: string[] = [mockSubject, mockSubject2];
7777
const versions: number[] = [1, 2, 3];
7878

79+
async function sleep(ms: number) {
80+
return new Promise(resolve => setTimeout(resolve, ms));
81+
}
82+
7983
describe('SchemaRegistryClient-Register', () => {
8084

8185
beforeEach(() => {
@@ -630,3 +634,33 @@ describe('SchemaRegistryClient-Config', () => {
630634
expect(restService.handleRequest).toHaveBeenCalledTimes(1);
631635
});
632636
});
637+
638+
describe('SchemaRegistryClient-Cache', () => {
639+
beforeEach(() => {
640+
restService = new RestService(mockClientConfig.baseURLs) as jest.Mocked<RestService>;
641+
client = new SchemaRegistryClient(mockTtlClientConfig);
642+
(client as any).restService = restService;
643+
});
644+
afterEach(() => {
645+
jest.clearAllMocks();
646+
});
647+
648+
it('Should delete cached item after expiry', async () => {
649+
const expectedResponse = {
650+
id: 1,
651+
version: 1,
652+
schema: schemaString,
653+
metadata: metadata,
654+
};
655+
656+
restService.handleRequest.mockResolvedValue({ data: expectedResponse } as AxiosResponse);
657+
658+
await client.register(mockSubject, schemaInfo);
659+
660+
await sleep(2000);
661+
662+
await client.register(mockSubject, schemaInfo);
663+
664+
expect(restService.handleRequest).toHaveBeenCalledTimes(2);
665+
});
666+
});

test/schemaregistry/test-constants.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,13 @@ const mockClientConfig: ClientConfig = {
3232
basicAuthCredentials: basicAuthCredentials
3333
};
3434

35-
export { clientConfig, mockClientConfig };
35+
const mockTtlClientConfig: ClientConfig = {
36+
baseURLs: mockBaseUrls,
37+
createAxiosDefaults: createAxiosDefaults,
38+
isForward: false,
39+
cacheCapacity: 512,
40+
cacheLatestTtlSecs: 1,
41+
basicAuthCredentials: basicAuthCredentials
42+
};
43+
44+
export { clientConfig, mockClientConfig, mockTtlClientConfig };

0 commit comments

Comments
 (0)