Skip to content

Commit

Permalink
ADD test to ensure all remote message channels have been closed
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Apr 8, 2023
1 parent 4166b53 commit ebb4aaa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/plugins/storage-remote/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './rx-storage-remote';
export * from './storage-remote-types';
export * from './storage-remote-helpers';
export * from './message-channel-cache';
export * from './remote';
5 changes: 5 additions & 0 deletions src/plugins/storage-remote/message-channel-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export type RemoteMessageChannelCacheItem = {
export const MESSAGE_CHANNEL_CACHE_BY_IDENTIFIER = new Map<string, Map<string, RemoteMessageChannelCacheItem>>();
const CACHE_ITEM_BY_MESSAGE_CHANNEL = new WeakMap<RemoteMessageChannel, RemoteMessageChannelCacheItem>();


export const OPEN_REMOTE_MESSAGE_CHANNELS = new Set<RemoteMessageChannel>();

function getMessageChannelCache(
identifier: string
) {
Expand Down Expand Up @@ -46,6 +49,7 @@ export function getMessageChannel(
refCount: 1,
messageChannel: settings.messageChannelCreator()
.then((messageChannel) => {
OPEN_REMOTE_MESSAGE_CHANNELS.add(messageChannel);
CACHE_ITEM_BY_MESSAGE_CHANNEL.set(messageChannel, newCacheItem);
return messageChannel;
}),
Expand All @@ -67,6 +71,7 @@ export function closeMessageChannel(
cacheItem.refCount = cacheItem.refCount - 1;
if (cacheItem.refCount === 0 && !cacheItem.keepAlive) {
getMessageChannelCache(cacheItem.identifier).delete(cacheItem.cacheKey);
OPEN_REMOTE_MESSAGE_CHANNELS.delete(messageChannel);
return messageChannel.close();
} else {
return PROMISE_RESOLVE_VOID;
Expand Down
1 change: 0 additions & 1 deletion test/unit/custom-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ config.parallel('custom-index.test.ts', () => {
const time = endTime - startTime;
assert.ok(time);
console.log('time: ' + time);
// process.exit();
});
});
});
Expand Down
19 changes: 19 additions & 0 deletions test/unit/last.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import config from './config';
import {
GRAPHQL_WEBSOCKET_BY_URL
} from '../../plugins/replication-graphql';
import {
OPEN_REMOTE_MESSAGE_CHANNELS,
CACHE_ITEM_BY_MESSAGE_CHANNEL
} from '../../plugins/storage-remote';


describe('last.test.ts (' + config.storage.name + ')', () => {
Expand All @@ -31,6 +35,21 @@ describe('last.test.ts (' + config.storage.name + ')', () => {
throw new Error('not all broadcast channels have been closed (' + openChannelKeys.length + ')');
}
});
it('ensure all RemoteMessageChannels have been closed', async () => {
try {
await waitUntil(() => {
return OPEN_REMOTE_MESSAGE_CHANNELS.size === 0;
}, 5 * 1000);
} catch (err) {
const stillOpen = Array.from(OPEN_REMOTE_MESSAGE_CHANNELS);
stillOpen.forEach(messageChannel => {
console.log('open graphql webRemoteMessageChannelssockets:');
console.dir(CACHE_ITEM_BY_MESSAGE_CHANNEL.get(messageChannel));
});
console.log(stillOpen);
throw new Error('not all RemoteMessageChannels have been closed (' + stillOpen.length + ')');
}
});
it('ensure all websockets have been closed', async () => {
try {
await waitUntil(() => {
Expand Down

0 comments on commit ebb4aaa

Please sign in to comment.