Skip to content

Commit

Permalink
test(channel): specify expected states
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Sep 4, 2024
1 parent e6e954a commit d048a89
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
3 changes: 2 additions & 1 deletion test/integration/channel-other.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,14 @@ describe('Channel other', () => {
const channelId = initiatorCh.id();
const fsmId = initiatorCh.fsmId();
initiatorCh.disconnect();
await waitForChannel(initiatorCh, ['disconnected']);
const ch = await Channel.initialize({
...sharedParams,
...initiatorParams,
existingChannelId: channelId,
existingFsmId: fsmId,
});
await waitForChannel(ch);
await waitForChannel(ch, ['open']);
expect(ch.fsmId()).to.be.equal(fsmId);
expect(ch.round()).to.be.equal(2);
const state = await ch.state();
Expand Down
27 changes: 15 additions & 12 deletions test/integration/channel-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import {
} from '../../src';
import { ChannelOptions, SignTxWithTag } from '../../src/channel/internal';

export async function waitForChannel(channel: Channel): Promise<void> {
export async function waitForChannel(channel: Channel, statuses: string[]): Promise<void> {
return new Promise((resolve, reject) => {
channel.on('statusChanged', (status: string) => {
switch (status) {
case 'open':
resolve();
break;
case 'disconnected':
reject(new Error('Unexpected SC status: disconnected'));
break;
default:
function handler(status: string): void {
const expectedStatus = statuses.shift();
if (status !== expectedStatus) {
reject(new Error(`Expected SC status ${expectedStatus}, got ${status} instead`));
channel.off('statusChanged', handler);
} else if (statuses.length === 0) {
resolve();
channel.off('statusChanged', handler);
}
});
}
channel.on('statusChanged', handler);
});
}

Expand Down Expand Up @@ -46,7 +46,10 @@ export async function initializeChannels(
...sharedParams,
...responderParams,
});
await Promise.all([waitForChannel(initiatorCh), waitForChannel(responderCh)]);
await Promise.all([
waitForChannel(initiatorCh, ['accepted', 'signed', 'open']),
waitForChannel(responderCh, ['halfSigned', 'open']),
]);
return [initiatorCh, responderCh];
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ describe('Channel', () => {
existingFsmId: existingChannelId,
offchainTx,
});
await waitForChannel(initiatorCh);
await waitForChannel(initiatorCh, ['open']);
// TODO: why node doesn't return signed_tx when channel is reestablished?
// initiatorCh.round().should.equal(existingChannelRound)
sinon.assert.notCalled(initiatorSignTag);
Expand Down

0 comments on commit d048a89

Please sign in to comment.