Skip to content

Commit

Permalink
Merge pull request #4257 from Agoric/sam-deflake
Browse files Browse the repository at this point in the history
Deflake wallet-connection tests
  • Loading branch information
mergify[bot] authored Jan 11, 2022
2 parents ac23367 + c5b8bb4 commit 6087647
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
8 changes: 5 additions & 3 deletions packages/web-components/src/AgoricWalletConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const makeAgoricWalletConnection = (makeCapTP = defaultMakeCapTP) =>
// This state machine integration is much like lit-robot, but also raises
// state events.
const machine = makeConnectionMachine();
const onState = service => {
const onState = (service, requestUpdate = true) => {
this.machine = service.machine;
const ev = new CustomEvent('state', {
detail: {
Expand All @@ -122,13 +122,15 @@ export const makeAgoricWalletConnection = (makeCapTP = defaultMakeCapTP) =>
},
});
this.dispatchEvent(ev);
this.requestUpdate();
if (requestUpdate) {
this.requestUpdate();
}
};
this.service = interpret(machine, onState);
this.machine = this.service.machine;

// Wait until we load before sending the first state.
this.firstUpdated = () => onState(this.service);
this.firstUpdated = () => onState(this.service, false);

this._nextEpoch = 0;
this._bridgePK = makePromiseKit();
Expand Down
31 changes: 21 additions & 10 deletions packages/web-components/test/agoric-wallet-connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ describe('AgoricWalletConnection', () => {
describe('on getAdminBootstrap', () => {
let el;
let adminBootstrap;
let stateResolvers;

const onState = ev => {
if (ev.detail.state in stateResolvers) {
stateResolvers[ev.detail.state]();
}
switch (ev.detail.state) {
case 'idle': {
adminBootstrap = E(ev.detail.walletConnection).getAdminBootstrap(
Expand All @@ -123,7 +127,19 @@ describe('AgoricWalletConnection', () => {
}
};

const createStatePromise = state => {
const statePromise = new Promise(
resolve => (stateResolvers[state] = resolve),
);

return Promise.race([
statePromise,
new Promise(resolve => setTimeout(() => resolve('timeout'), 2000)),
]);
};

beforeEach(async () => {
stateResolvers = {};
el = await fixture(
html`
<agoric-wallet-connection
Expand All @@ -146,45 +162,40 @@ describe('AgoricWalletConnection', () => {
it('goes to admin state once connected', async () => {
iframeOnMessage('http://localhost:8000');

// Connecting happens instantly with the mock socket,
// just need to let the event loop run once.
await new Promise(resolve => setTimeout(resolve, 30));
await createStatePromise('bridged');
await new Promise(resolve => setTimeout(resolve, 100));

expect(el.state).to.equal('bridged');
});

it('lets the websocket dispatch messages through capTP', async () => {
iframeOnMessage('http://localhost:8000');
await new Promise(resolve => setTimeout(resolve, 30));

socket.send(JSON.stringify({ foo: 'bar' }));
await new Promise(resolve => setTimeout(resolve, 30));

expect(captpInnards.lastDispatched).to.deep.equal({ foo: 'bar' });
});

it('lets capTP send messages through the websocket', async () => {
iframeOnMessage('http://localhost:8000');
await new Promise(resolve => setTimeout(resolve, 30));
await new Promise(resolve => setTimeout(resolve, 100));

captpInnards.send({ foo: 'bar2' });
await new Promise(resolve => setTimeout(resolve, 30));
await new Promise(resolve => setTimeout(resolve, 100));

expect(lastMessage).to.equal(JSON.stringify({ foo: 'bar2' }));
});

it('aborts capTP when the socket disconnects', async () => {
iframeOnMessage('http://localhost:8000');
await new Promise(resolve => setTimeout(resolve, 30));

socket.close();

await new Promise(resolve => setTimeout(resolve, 30));
expect(captpInnards.isAborted).to.equal(true);
});

it('returns the admin bootstrap', async () => {
iframeOnMessage('http://localhost:8000');
await new Promise(resolve => setTimeout(resolve, 30));

expect(await adminBootstrap).to.deep.equal({ isAdmin: true });
});
Expand Down

0 comments on commit 6087647

Please sign in to comment.