Skip to content

Commit

Permalink
feat: cache tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prc5 committed Dec 9, 2024
1 parent aaac03d commit c1e89e9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .yarn/releases/yarn-1.19.0.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -102029,7 +102029,7 @@ wercker.yml
// I know what you're about to say. But literally everything about
// signal-exit is a compromise with evil. Get used to it.
if (!emitter.infinite) {
emitter.setMaxListeners(20000);
emitter.setMaxListeners(1000);
emitter.infinite = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,30 @@ describe("Cache [ Garbage Collector ]", () => {
});
it("should schedule garbage collection on mount", async () => {
const storage = new Map();
storage.set(cacheKey, cacheData);
const cacheInstance = createCache(client, {
storage.set("cacheKey", cacheData);

const cacheInstance = createCache(new Client({ url: "shared-base-url" }), {
storage,
version,
});

expect(Array.from(cacheInstance.garbageCollectors.keys())).toHaveLength(1);
});
it("should schedule lazy storage garbage collection on mount", async () => {
lazyStorage.set(cacheKey, cacheData);
const cacheInstance = createCache(client, {
lazyStorage: createLazyCacheAdapter(lazyStorage),
version,
});

await waitFor(() => {
expect(Array.from(cacheInstance.garbageCollectors.keys())).toHaveLength(1);
});
});
// it("should schedule lazy storage garbage collection on mount", async () => {
// lazyStorage.set(cacheKey, cacheData);
// const cacheInstance = createCache(client, {
// lazyStorage: createLazyCacheAdapter(lazyStorage),
// version,
// });

// await waitFor(() => {
// expect(Array.from(cacheInstance.garbageCollectors.keys())).toHaveLength(1);
// });
// });
it("should schedule garbage collection when resource is added", async () => {
const spy = jest.spyOn(cache, "scheduleGarbageCollector");
cache.set(request, cacheData);
await waitFor(() => {
expect(spy).toHaveBeenCalledTimes(1);
});
expect(spy).toHaveBeenCalledTimes(1);
});
it("should remove resource with not matching lazy version", async () => {
const data = { ...cacheData, cacheTime: Time.MIN };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe("Cache [ Lazy Storage ]", () => {
const otherKey = "otherKey";
await cache.options?.lazyStorage?.set(cacheKey, cacheData);
await cache.storage.set(otherKey, cacheData);
const keys = await cache.getLazyKeys();
const keys = await cache.getAllKeys();
await sleep(50);
expect(keys).toStrictEqual([cacheKey, otherKey]);
});
Expand Down
37 changes: 29 additions & 8 deletions packages/core/src/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,31 @@ export class Cache<C extends ClientInstance> {
public client: C,
public options?: CacheOptionsType,
) {
this.emitter?.setMaxListeners(20000);
this.storage = this.options?.storage || (new Map<string, CacheValueType>() as typeof this.storage);
const { storage = new Map<string, CacheValueType>() } = this.options || {};

this.storage = storage;
this.emitter?.setMaxListeners(1000);
this.events = getCacheEvents(this.emitter);
this.options?.onInitialization?.(this);

this.version = this.options?.version || "0.0.1";
this.lazyStorage = this.options?.lazyStorage;
this.logger = this.client.loggerManager.init("Cache");

[...this.storage.keys()].forEach(this.scheduleGarbageCollector);
const scheduleGarbageCollector = (keys: string[]) => {
keys.forEach(this.scheduleGarbageCollector);

// Going back from offline should re-trigger garbage collection
this.client.appManager.events.onOnline(() => {
[...this.storage.keys()].forEach(this.scheduleGarbageCollector);
});
// Going back from offline should re-trigger garbage collection
this.client.appManager.events.onOnline(() => {
keys.forEach(this.scheduleGarbageCollector);
});
};

scheduleGarbageCollector([...this.storage.keys()]);

// Schedule garbage collection for lazy storage
// To make sure we do not store some data that is no longer needed
this.getLazyKeys().then(scheduleGarbageCollector);
}

/**
Expand Down Expand Up @@ -178,7 +188,7 @@ export class Cache<C extends ClientInstance> {
*/
invalidate = async (invalidateKeys: string | RegExp | RequestInstance | Array<string | RegExp | RequestInstance>) => {
this.logger.debug("Revalidating cache element", { invalidateKeys });
const keys = await this.getLazyKeys();
const keys = await this.getAllKeys();

const invalidate = (key: string | RegExp | RequestInstance) => {
const handleInvalidation = (cacheKey: string) => {
Expand Down Expand Up @@ -249,6 +259,16 @@ export class Cache<C extends ClientInstance> {
* @param cacheKey
*/
getLazyKeys = async () => {
const keys = (await this.lazyStorage?.keys()) || [];

return [...new Set(keys)];
};

/**
* Used to receive keys from sync storage and lazy storage
* @param cacheKey
*/
getAllKeys = async () => {
const keys = await this.lazyStorage?.keys();
const asyncKeys = Array.from(keys || []);
const syncKeys = Array.from(this.storage.keys());
Expand Down Expand Up @@ -294,6 +314,7 @@ export class Cache<C extends ClientInstance> {
* Clear cache storages
*/
clear = async (): Promise<void> => {
this.garbageCollectors.forEach((timeout) => clearTimeout(timeout));
this.storage.clear();
};
}
2 changes: 1 addition & 1 deletion packages/core/src/dispatcher/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Dispatcher {
public client: ClientInstance,
public options?: DispatcherOptionsType,
) {
this.emitter?.setMaxListeners(20000);
this.emitter?.setMaxListeners(1000);
this.logger = client.loggerManager.init("Dispatcher");

if (this.options?.storage) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/managers/app/app.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class AppManager {
isFocused: boolean;

constructor(public options?: AppManagerOptionsType) {
this.emitter?.setMaxListeners(20000);
this.emitter?.setMaxListeners(1000);
const {
focusEvent = appManagerInitialOptions.focusEvent,
onlineEvent = appManagerInitialOptions.onlineEvent,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/managers/logger/logger.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class LoggerManager {
private client: Pick<ClientInstance, "debug">,
private options?: LoggerOptionsType,
) {
this.emitter?.setMaxListeners(20000);
this.emitter?.setMaxListeners(1000);
this.logger = this.options?.logger || logger;
this.severity = this.options?.severity || 2;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/managers/request/request.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class RequestManager {
events = getRequestManagerEvents(this.emitter);

constructor() {
this.emitter?.setMaxListeners(20000);
this.emitter?.setMaxListeners(1000);
}

abortControllers = new Map<string, Map<string, AbortController>>();
Expand Down
2 changes: 1 addition & 1 deletion packages/sockets/src/socket/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Socket<Adapter extends SocketAdapterInstance = WebsocketAdapterType
constructor(public options: SocketOptionsType<Adapter>) {
const { url, adapter, queryParams, reconnect, reconnectTime, queryParamsConfig, queryParamsStringify } =
this.options;
this.emitter?.setMaxListeners(20000);
this.emitter?.setMaxListeners(1000);
this.url = url;
this.queryParams = queryParams;
this.debug = false;
Expand Down

0 comments on commit c1e89e9

Please sign in to comment.