Skip to content

Commit 4bbfb51

Browse files
committed
remove setTimeout race from tests
1 parent 3947c69 commit 4bbfb51

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/kernels/helpers.unit.test.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -664,22 +664,27 @@ suite('Kernel Connection Helpers', () => {
664664
function createMockKernel(options: MockKernelOptions) {
665665
return {
666666
requestExecute: () => {
667-
let iopubCallback: ((msg: any) => void) | undefined;
667+
let resolvePromise: (value: any) => void;
668668

669-
// Create a promise that resolves after IOPub messages are dispatched
669+
// Create a promise that will be resolved after IOPub messages are dispatched
670670
const donePromise = new Promise<any>((resolve) => {
671-
// Dispatch messages asynchronously to preserve async behavior
672-
setTimeout(() => {
673-
if (iopubCallback && options.messages && options.messages.length > 0) {
671+
resolvePromise = resolve;
672+
});
673+
674+
return {
675+
done: donePromise,
676+
set onIOPub(cb: (msg: any) => void) {
677+
// Invoke IOPub callback synchronously with all messages
678+
if (options.messages && options.messages.length > 0) {
674679
options.messages.forEach((msg) => {
675-
iopubCallback!({
680+
cb({
676681
header: { msg_type: msg.msg_type },
677682
content: msg.content
678683
});
679684
});
680685
}
681686
// Resolve the done promise after messages are dispatched
682-
resolve({
687+
resolvePromise({
683688
content:
684689
options.status === 'ok'
685690
? { status: 'ok' as const }
@@ -688,13 +693,6 @@ suite('Kernel Connection Helpers', () => {
688693
...options.errorContent
689694
}
690695
});
691-
}, 0);
692-
});
693-
694-
return {
695-
done: donePromise,
696-
set onIOPub(cb: (msg: any) => void) {
697-
iopubCallback = cb;
698696
}
699697
};
700698
}

0 commit comments

Comments
 (0)