Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import { OperationStatus, Operation } from '@amzn/dex-internal-sdk';
describe('waitBeforeContinue', () => {
let mockContext: jest.Mocked<ExecutionContext>;
let mockHasRunningOperations: jest.Mock;
let timers: NodeJS.Timeout[] = [];

beforeEach(() => {
mockContext = {
getStepData: jest.fn(),
} as any;
mockHasRunningOperations = jest.fn();
timers = [];
});

afterEach(() => {
// Clean up any remaining timers
timers.forEach(timer => clearTimeout(timer));
timers = [];
});

test('should resolve when operations complete', async () => {
Expand All @@ -28,9 +36,10 @@ describe('waitBeforeContinue', () => {
});

// Complete operations after 50ms
setTimeout(() => {
const timer = setTimeout(() => {
operationsRunning = false;
}, 50);
timers.push(timer);

const result = await resultPromise;
expect(result.reason).toBe('operations');
Expand Down Expand Up @@ -68,9 +77,10 @@ describe('waitBeforeContinue', () => {
});

// Change status after 50ms
setTimeout(() => {
const timer = setTimeout(() => {
stepStatus = OperationStatus.SUCCEEDED;
}, 50);
timers.push(timer);

const result = await resultPromise;
expect(result.reason).toBe('status');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,20 @@ export async function waitBeforeContinue(
} = options;

const promises: Promise<WaitBeforeContinueResult>[] = [];
const timers: NodeJS.Timeout[] = [];

// Cleanup function to clear all timers
const cleanup = () => {
timers.forEach(timer => clearTimeout(timer));
};

// Timer promise - resolves when scheduled time is reached
if (checkTimer && scheduledTimestamp) {
const timerPromise = new Promise<WaitBeforeContinueResult>(resolve => {
const timeLeft = Number(scheduledTimestamp) - Date.now();
if (timeLeft > 0) {
setTimeout(() => resolve({ reason: 'timer', timerExpired: true }), timeLeft);
const timer = setTimeout(() => resolve({ reason: 'timer', timerExpired: true }), timeLeft);
timers.push(timer);
} else {
resolve({ reason: 'timer', timerExpired: true });
}
Expand All @@ -75,7 +82,8 @@ export async function waitBeforeContinue(
if (!hasRunningOperations()) {
resolve({ reason: 'operations' });
} else {
setTimeout(checkOperations, pollingInterval);
const timer = setTimeout(checkOperations, pollingInterval);
timers.push(timer);
}
};
checkOperations();
Expand All @@ -92,7 +100,8 @@ export async function waitBeforeContinue(
if (originalStatus !== currentStatus) {
resolve({ reason: 'status' });
} else {
setTimeout(checkStepStatus, pollingInterval);
const timer = setTimeout(checkStepStatus, pollingInterval);
timers.push(timer);
}
};
checkStepStatus();
Expand All @@ -105,8 +114,9 @@ export async function waitBeforeContinue(
return { reason: 'timeout' };
}

// Wait for any condition to be met
// Wait for any condition to be met, then cleanup timers
const result = await Promise.race(promises);
cleanup();

// If timer expired, force checkpoint to get fresh data from API
if (result.reason === 'timer' && result.timerExpired && checkpoint) {
Expand Down
Loading