Skip to content

Commit

Permalink
misc(server-core): Add test for scheduledRefreshContexts, refs #1904
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Jan 27, 2021
1 parent 13daa00 commit dafbd09
Showing 1 changed file with 68 additions and 6 deletions.
74 changes: 68 additions & 6 deletions packages/cubejs-server-core/test/unit/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-function */

import { withTimeout } from '@cubejs-backend/shared';
import { CubejsServerCore } from '../../src';
import { DatabaseType } from '../../src/core/types';

Expand Down Expand Up @@ -126,7 +127,7 @@ describe('index.test', () => {

test('Should throw error, dbType is required', () => {
delete process.env.CUBEJS_DB_TYPE;

expect(() => {
jest.spyOn(CubejsServerCore.prototype, 'configFileExists').mockImplementation(() => true);
// eslint-disable-next-line
Expand All @@ -135,10 +136,10 @@ describe('index.test', () => {
})
.toThrowError(/driverFactory, apiSecret, dbType are required options/);
});

test('Should not throw when the required options are missing in dev mode and no config file exists', () => {
delete process.env.CUBEJS_DB_TYPE;

expect(() => {
jest.spyOn(CubejsServerCore.prototype, 'configFileExists').mockImplementation(() => false);
// eslint-disable-next-line
Expand All @@ -162,10 +163,8 @@ describe('index.test', () => {
expect(cubejsServerCore).toBeInstanceOf(CubejsServerCore);
expect(cubejsServerCore.detectScheduledRefreshTimer(input)).toBe(output);

await cubejsServerCore.releaseConnections();
await cubejsServerCore.shutdown();
delete process.env.NODE_ENV;

await new Promise((resolve => { setTimeout(resolve, 1000); }));
});
};

Expand All @@ -176,4 +175,67 @@ describe('index.test', () => {
expectRefreshTimerOption('false', false);
expectRefreshTimerOption(undefined, 30000);
expectRefreshTimerOption(undefined, false, true);

test('scheduledRefreshContexts option', async () => {
const cubejsServerCore = new CubejsServerCore({
dbType: 'mysql',
apiSecret: 'secret',
// 250ms
scheduledRefreshTimer: 1,
scheduledRefreshConcurrency: 2,
scheduledRefreshContexts: async () => [
{
authInfo: {
appid: 'test1',
u: {
prop1: 'value1'
}
}
},
{
authInfo: {
appid: 'test2',
u: {
prop2: 'value2'
}
}
},
],
});
expect(cubejsServerCore).toBeInstanceOf(CubejsServerCore);

const timeoutKiller = withTimeout(
() => {
throw new Error('runScheduledRefresh was not called');
},
2 * 1000,
);

const runScheduledRefreshMock = jest.spyOn(cubejsServerCore, 'runScheduledRefresh')
.mockImplementation(async (ctx, query) => {
console.log('test');
await timeoutKiller.cancel();

return {
finished: true,
};
});

await timeoutKiller;

expect(runScheduledRefreshMock.mock.calls.length).toEqual(2);
expect(runScheduledRefreshMock.mock.calls[0]).toEqual([
{ authInfo: { appid: 'test1', u: { prop1: 'value1' } } },
{ concurrency: 2 },
]);
expect(runScheduledRefreshMock.mock.calls[1]).toEqual([
{ authInfo: { appid: 'test2', u: { prop2: 'value2' } } },
{ concurrency: 2 },
]);

await cubejsServerCore.shutdown();

delete process.env.NODE_ENV;
runScheduledRefreshMock.mockRestore();
});
});

0 comments on commit dafbd09

Please sign in to comment.