Skip to content

Commit

Permalink
Add tests for timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
theref committed Aug 3, 2023
1 parent d6bbedf commit 311b755
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 8 deletions.
12 changes: 5 additions & 7 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,16 @@ export class DkgClient {
private static performRitual = async (
web3Provider: ethers.providers.Web3Provider,
ritualId: number
): Promise<void> => {
): Promise<void> => {
const isSuccessful = await DkgClient.waitUntilRitualEnd(
web3Provider,
ritualId
);

if (!isSuccessful) {
throw new Error(
`Ritual initialization failed. Ritual id ${ritualId}`
);
throw new Error(`Ritual initialization failed. Ritual id ${ritualId}`);
}
}
};

private static waitForBlockTime = async (
web3Provider: ethers.providers.Web3Provider,
Expand All @@ -181,7 +179,7 @@ export class DkgClient {
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second before checking again
}
} while (currentBlockTime < endTimestamp);
}
};

private static waitUntilRitualEnd = async (
web3Provider: ethers.providers.Web3Provider,
Expand Down
100 changes: 99 additions & 1 deletion test/integration/dkg-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { SecretKey } from '@nucypher/nucypher-core';

import { DkgCoordinatorAgent } from '../../src/agents/coordinator';
import {
DkgCoordinatorAgent,
DkgRitualState,
} from '../../src/agents/coordinator';
import { DkgClient } from '../../src/dkg';
import {
fakeCoordinatorRitual,
Expand Down Expand Up @@ -81,4 +84,99 @@ describe('DkgClient', () => {
);
expect(getParticipantPublicKeysSpy).toHaveBeenCalled();
});

it('waits until the ritual end time during initialization', async () => {
jest.useFakeTimers();
const fakeProvider = fakeWeb3Provider(SecretKey.random().toBEBytes());
const fakeUrsulas = ['ursula1', 'ursula2', 'ursula3'];
const fakeRitualId = 123;
const initTimestamp = Math.floor(Date.now() / 1000);
const timeout = 10;

jest
.spyOn(DkgCoordinatorAgent, 'initializeRitual')
.mockResolvedValue(fakeRitualId);

jest
.spyOn(DkgCoordinatorAgent, 'getRitualInitTime')
.mockResolvedValue(initTimestamp);

jest.spyOn(DkgCoordinatorAgent, 'getTimeout').mockResolvedValue(timeout);

jest.spyOn(DkgClient as any, 'performRitual').mockResolvedValue(undefined);

Check warning on line 106 in test/integration/dkg-client.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Unexpected any. Specify a different type

const promise = DkgClient.initializeRitual(fakeProvider, fakeUrsulas, true);

jest.advanceTimersByTime(timeout * 1000);

await expect(promise).resolves.toBe(fakeRitualId);

expect(DkgCoordinatorAgent.initializeRitual).toHaveBeenCalledWith(
fakeProvider,
fakeUrsulas
);

expect(DkgCoordinatorAgent.getRitualInitTime).toHaveBeenCalledWith(
fakeProvider,
fakeRitualId
);

expect(DkgCoordinatorAgent.getTimeout).toHaveBeenCalledWith(fakeProvider);

expect((DkgClient as any).performRitual).toHaveBeenCalledWith(

Check warning on line 126 in test/integration/dkg-client.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Unexpected any. Specify a different type
fakeProvider,
fakeRitualId
);
});

it('throws an error when initialization times out', async () => {
jest.useFakeTimers();
const fakeProvider = fakeWeb3Provider(SecretKey.random().toBEBytes());
const fakeUrsulas = ['ursula1', 'ursula2', 'ursula3'];
const fakeRitualId = 123;
const initTimestamp = Math.floor(Date.now() / 1000);
const timeout = 10;

jest
.spyOn(DkgCoordinatorAgent, 'initializeRitual')
.mockResolvedValue(fakeRitualId);

jest
.spyOn(DkgCoordinatorAgent, 'getRitualInitTime')
.mockResolvedValue(initTimestamp);

jest.spyOn(DkgCoordinatorAgent, 'getTimeout').mockResolvedValue(timeout);

const performRitualSpy = jest
.spyOn(DkgClient as any, 'performRitual')

Check warning on line 151 in test/integration/dkg-client.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Unexpected any. Specify a different type
.mockRejectedValue(
new Error(`Ritual initialization failed. Ritual id ${fakeRitualId}`)
);

jest
.spyOn(DkgCoordinatorAgent, 'getRitualState')
.mockResolvedValue(DkgRitualState.TIMEOUT);

const promise = DkgClient.initializeRitual(fakeProvider, fakeUrsulas, true);

jest.advanceTimersByTime(timeout * 1000);

await expect(promise).rejects.toThrow(
`Ritual initialization failed. Ritual id ${fakeRitualId} is in state TIMEOUT`
);

expect(DkgCoordinatorAgent.initializeRitual).toHaveBeenCalledWith(
fakeProvider,
fakeUrsulas
);

expect(DkgCoordinatorAgent.getRitualInitTime).toHaveBeenCalledWith(
fakeProvider,
fakeRitualId
);

expect(DkgCoordinatorAgent.getTimeout).toHaveBeenCalledWith(fakeProvider);

expect(performRitualSpy).toHaveBeenCalledWith(fakeProvider, fakeRitualId);
});
});

0 comments on commit 311b755

Please sign in to comment.