Skip to content

Commit

Permalink
fix(backend-shared): Rename convertTimeStrToSeconds to convertTimeStr…
Browse files Browse the repository at this point in the history
…ToSeconds

It returns input number (which should be seconds) as it is, and 5 for '5s'
  • Loading branch information
mcheshkov committed Nov 19, 2024
1 parent 0278d70 commit 0438e83
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions packages/cubejs-backend-shared/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class InvalidConfiguration extends Error {
}
}

export function convertTimeStrToMs(
export function convertTimeStrToSeconds(
input: string,
envName: string,
description: string = 'Must be a number in seconds or duration string (1s, 1m, 1h).',
Expand Down Expand Up @@ -126,7 +126,7 @@ function asBoolOrTime(input: string, envName: string): number | boolean {
return false;
}

return convertTimeStrToMs(
return convertTimeStrToSeconds(
input,
envName,
'Should be boolean or number (in seconds) or string in time format (1s, 1m, 1h)'
Expand Down Expand Up @@ -510,7 +510,7 @@ const variables: Record<string, (...args: any) => any> = {
}) => {
const key = keyByDataSource('CUBEJS_DB_POLL_MAX_INTERVAL', dataSource);
const value = process.env[key] || '5s';
return convertTimeStrToMs(value, key);
return convertTimeStrToSeconds(value, key);
},

/**
Expand All @@ -525,7 +525,7 @@ const variables: Record<string, (...args: any) => any> = {
const key = keyByDataSource('CUBEJS_DB_POLL_TIMEOUT', dataSource);
const value = process.env[key];
if (value) {
return convertTimeStrToMs(value, key);
return convertTimeStrToSeconds(value, key);
} else {
return null;
}
Expand All @@ -546,7 +546,7 @@ const variables: Record<string, (...args: any) => any> = {
} = {}) => {
const key = keyByDataSource('CUBEJS_DB_QUERY_TIMEOUT', dataSource);
const value = process.env[key] || '10m';
return convertTimeStrToMs(value, key);
return convertTimeStrToSeconds(value, key);
},

/**
Expand Down
18 changes: 9 additions & 9 deletions packages/cubejs-backend-shared/test/env.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { getEnv, convertTimeStrToMs } from '../src/env';
import { getEnv, convertTimeStrToSeconds } from '../src/env';

test('convertTimeStrToMs', () => {
expect(convertTimeStrToMs('1', 'VARIABLE_ENV')).toBe(1);
expect(convertTimeStrToMs('1s', 'VARIABLE_ENV')).toBe(1);
expect(convertTimeStrToMs('5s', 'VARIABLE_ENV')).toBe(5);
expect(convertTimeStrToMs('1m', 'VARIABLE_ENV')).toBe(1 * 60);
expect(convertTimeStrToMs('10m', 'VARIABLE_ENV')).toBe(10 * 60);
expect(convertTimeStrToMs('1h', 'VARIABLE_ENV')).toBe(60 * 60);
expect(convertTimeStrToMs('2h', 'VARIABLE_ENV')).toBe(2 * 60 * 60);
expect(convertTimeStrToSeconds('1', 'VARIABLE_ENV')).toBe(1);
expect(convertTimeStrToSeconds('1s', 'VARIABLE_ENV')).toBe(1);
expect(convertTimeStrToSeconds('5s', 'VARIABLE_ENV')).toBe(5);
expect(convertTimeStrToSeconds('1m', 'VARIABLE_ENV')).toBe(1 * 60);
expect(convertTimeStrToSeconds('10m', 'VARIABLE_ENV')).toBe(10 * 60);
expect(convertTimeStrToSeconds('1h', 'VARIABLE_ENV')).toBe(60 * 60);
expect(convertTimeStrToSeconds('2h', 'VARIABLE_ENV')).toBe(2 * 60 * 60);
});

test('convertTimeStrToMs(exception)', () => {
expect(() => convertTimeStrToMs('', 'VARIABLE_ENV')).toThrowError(
expect(() => convertTimeStrToSeconds('', 'VARIABLE_ENV')).toThrowError(
`Value "" is not valid for VARIABLE_ENV. Must be a number in seconds or duration string (1s, 1m, 1h).`
);
});
Expand Down

0 comments on commit 0438e83

Please sign in to comment.