Skip to content

Commit

Permalink
Merge pull request #569 from zextras/SHELL-270-Polling-Interval
Browse files Browse the repository at this point in the history
fix: update parsing of polling interval
  • Loading branch information
CataldoMazzilli authored Jan 17, 2025
2 parents 19056d1 + 8386e14 commit d807f63
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/network/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('Fetch', () => {
}
);

it.each<Duration>(['500', '500ms'])(
it.each<Duration>(['500', '500ms', '500s'])(
'should send limitToOneBlocked and wait if zimbraPrefMailPollingInterval is %s',
async (pollingPref) => {
useAccountStore.setState(
Expand Down Expand Up @@ -186,10 +186,10 @@ describe('Fetch', () => {
}
);

it('should not send limitToOneBlocked and wait if zimbraPrefMailPollingInterval is not 500ms', async () => {
it('should not send limitToOneBlocked and wait if zimbraPrefMailPollingInterval is not either 500, 500ms or 500s', async () => {
useAccountStore.setState(
produce<AccountState>((state) => {
state.settings.prefs.zimbraPrefMailPollingInterval = '500s';
state.settings.prefs.zimbraPrefMailPollingInterval = '60s';
})
);

Expand Down
102 changes: 74 additions & 28 deletions src/store/network/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,72 @@ describe('Utils', () => {
expect(result).toBe(30000);
});

it('should return 500 if zimbraPrefMailPollingInterval is "500" without a duration unit', () => {
useNetworkStore.setState({ pollingInterval: 123456789 });
useAccountStore.setState((state) => ({
...state,
settings: {
...state.settings,
prefs: { ...state.settings.prefs, zimbraPrefMailPollingInterval: '500' }
}
}));
const response = {
Header: {
context: {}
},
Body: {}
} satisfies RawSoapResponse<Record<string, unknown>>;
const result = getPollingInterval(response);
expect(result).toBe(500);
describe('long polling cases', () => {
it('should return 500 if zimbraPrefMailPollingInterval is "500" without a duration unit', () => {
useNetworkStore.setState({ pollingInterval: 123456789 });
useAccountStore.setState((state) => ({
...state,
settings: {
...state.settings,
prefs: { ...state.settings.prefs, zimbraPrefMailPollingInterval: '500' }
}
}));
const response = {
Header: {
context: {}
},
Body: {}
} satisfies RawSoapResponse<Record<string, unknown>>;
const result = getPollingInterval(response);
expect(result).toBe(500);
});

it('should return 500 if zimbraPrefMailPollingInterval is "500ms"', () => {
useNetworkStore.setState({ pollingInterval: 123456789 });
useAccountStore.setState((state) => ({
...state,
settings: {
...state.settings,
prefs: {
...state.settings.prefs,
zimbraPrefMailPollingInterval: '500ms' satisfies Duration
}
}
}));
const response = {
Header: {
context: {}
},
Body: {}
} satisfies RawSoapResponse<Record<string, unknown>>;
const result = getPollingInterval(response);
expect(result).toBe(500);
});

it('should return 500 if zimbraPrefMailPollingInterval is "500s"', () => {
useNetworkStore.setState({ pollingInterval: 123456789 });
useAccountStore.setState((state) => ({
...state,
settings: {
...state.settings,
prefs: {
...state.settings.prefs,
zimbraPrefMailPollingInterval: '500s' satisfies Duration
}
}
}));
const response = {
Header: {
context: {}
},
Body: {}
} satisfies RawSoapResponse<Record<string, unknown>>;
const result = getPollingInterval(response);
expect(result).toBe(500);
});
});

it('should return the number if zimbraPrefMailPollingInterval is set without a duration unit', () => {
it('should return the number * 1000 if zimbraPrefMailPollingInterval is set without a duration unit(so are handled as seconds)', () => {
useNetworkStore.setState({ pollingInterval: 123456789 });
useAccountStore.setState((state) => ({
...state,
Expand All @@ -136,18 +182,18 @@ describe('Utils', () => {
Body: {}
} satisfies RawSoapResponse<Record<string, unknown>>;
const result = getPollingInterval(response);
expect(result).toBe(753);
expect(result).toBe(753_000);
});

it('should return the number * 60 * 1000 if zimbraPrefMailPollingInterval duration is set with the duration unit m (minutes)', () => {
it('should return the number if zimbraPrefMailPollingInterval is set with the duration unit ms (milliseconds)', () => {
useNetworkStore.setState({ pollingInterval: 123456789 });
useAccountStore.setState((state) => ({
...state,
settings: {
...state.settings,
prefs: {
...state.settings.prefs,
zimbraPrefMailPollingInterval: '50m' satisfies Duration
zimbraPrefMailPollingInterval: '284ms' satisfies Duration
}
}
}));
Expand All @@ -158,18 +204,18 @@ describe('Utils', () => {
Body: {}
} satisfies RawSoapResponse<Record<string, unknown>>;
const result = getPollingInterval(response);
expect(result).toBe(50 * 60 * 1000);
expect(result).toBe(284);
});

it('should return the number if zimbraPrefMailPollingInterval is set with the duration unit ms (milliseconds)', () => {
it('should return the number * 1000 if zimbraPrefMailPollingInterval is set with the duration unit s (seconds)', () => {
useNetworkStore.setState({ pollingInterval: 123456789 });
useAccountStore.setState((state) => ({
...state,
settings: {
...state.settings,
prefs: {
...state.settings.prefs,
zimbraPrefMailPollingInterval: '284ms' satisfies Duration
zimbraPrefMailPollingInterval: '753s' satisfies Duration
}
}
}));
Expand All @@ -180,18 +226,18 @@ describe('Utils', () => {
Body: {}
} satisfies RawSoapResponse<Record<string, unknown>>;
const result = getPollingInterval(response);
expect(result).toBe(284);
expect(result).toBe(753000);
});

it('should return the number * 1000 if zimbraPrefMailPollingInterval is set with the duration unit s (seconds)', () => {
it('should return the number * 60 * 1000 if zimbraPrefMailPollingInterval duration is set with the duration unit m (minutes)', () => {
useNetworkStore.setState({ pollingInterval: 123456789 });
useAccountStore.setState((state) => ({
...state,
settings: {
...state.settings,
prefs: {
...state.settings.prefs,
zimbraPrefMailPollingInterval: '753s' satisfies Duration
zimbraPrefMailPollingInterval: '50m' satisfies Duration
}
}
}));
Expand All @@ -202,7 +248,7 @@ describe('Utils', () => {
Body: {}
} satisfies RawSoapResponse<Record<string, unknown>>;
const result = getPollingInterval(response);
expect(result).toBe(753000);
expect(result).toBe(50 * 60 * 1000);
});

it('should return the number * 60 * 60 * 1000 if zimbraPrefMailPollingInterval is set with the duration unit h (hours)', () => {
Expand Down
11 changes: 10 additions & 1 deletion src/store/network/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,26 @@ const POLLING_RETRY_INTERVAL = 60_000;

const POLLING_INVALID_DURATION = 30_000;

const LONG_POLLING_MARKER_VALUE = 500;

export const parsePollingInterval = (settings: AccountSettings): number => {
const pollingPref = settings.prefs?.zimbraPrefMailPollingInterval ?? '';
const [value, durationUnit] = pollingPref.split(/([a-z]+)/g);
const pollingValue = parseInt(value, 10);
if (Number.isNaN(pollingValue)) {
return POLLING_INVALID_DURATION;
}

if (
pollingValue === LONG_POLLING_MARKER_VALUE &&
(durationUnit === undefined || durationUnit === 'ms' || durationUnit === 's')
) {
return LONG_POLLING_MARKER_VALUE;
}
switch (durationUnit) {
case undefined:
case 'ms':
return pollingValue;
case undefined:
case 's':
return pollingValue * 1000;
case 'm':
Expand Down

0 comments on commit d807f63

Please sign in to comment.