Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: time tests only use faketimers once #634

Merged
merged 3 commits into from Sep 29, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/generated/changelog.html
Expand Up @@ -16,13 +16,11 @@ <h2>Version 0.14.0</h2>
Adds retry logic to HttpAgent. By default, retries three times before throwing an error,
to offer a more cohesive workflow
</li>
<li>fixes flaky tests for syncTime</li>
<li>
Adds a top-level <code>fetchCandid()</code> function which retrieves the Candid interface
for a given canister id.
</li>
</ul>
<h2>Version 0.13.4</h2>
<ul>
<li>chore: auth-client expose storage constant keys</li>
<li>
bug: auth-client resolves window.open issue in login function in safari due to async
Expand Down
182 changes: 86 additions & 96 deletions packages/agent/src/agent/http/http.test.ts
Expand Up @@ -550,120 +550,110 @@ describe('retry failures', () => {
expect(mockFetch.mock.calls.length).toBe(4);
});
});
jest.useFakeTimers({ legacyFakeTimers: true });
test('should change nothing if time is within 30 seconds of replica', async () => {
const systemTime = new Date('August 19, 1975 23:15:30');
// jest.setSystemTime(systemTime);
const mockFetch = jest.fn();

describe('reconcile time', () => {
it('should change nothing if time is within 30 seconds of replica', async () => {
const systemTime = new Date('August 19, 1975 23:15:30');
jest.setSystemTime(systemTime);
const mockFetch = jest.fn();
const agent = new HttpAgent({ host: 'http://localhost:8000', fetch: mockFetch });

const agent = new HttpAgent({ host: 'http://localhost:8000', fetch: mockFetch });

await agent.syncTime();
await agent.syncTime();

agent
.call(Principal.managementCanister(), {
methodName: 'test',
arg: new Uint8Array().buffer,
})
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
.catch(function (_) {});
agent
.call(Principal.managementCanister(), {
methodName: 'test',
arg: new Uint8Array().buffer,
})
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
.catch(function (_) {});

const requestBody = cbor.decode(mockFetch.mock.calls[0][1].body);
expect((requestBody as unknown as any).content.ingress_expiry).toMatchInlineSnapshot(
`1240000000000`,
);
});
test('should adjust the Expiry if the clock is more than 30 seconds behind', async () => {
const mockFetch = jest.fn();

const requestBody = cbor.decode(mockFetch.mock.calls[0][1].body);
expect((requestBody as unknown as any).content.ingress_expiry).toMatchInlineSnapshot(
`1240000000000`,
);
const replicaTime = new Date(Date.now() + 31_000);
jest.mock('../../canisterStatus', () => {
return {
request: () => {
return {
// 31 seconds ahead
get: () => replicaTime,
};
},
};
});
// TODO - fix broken test
it.skip('should adjust the Expiry if the clock is more than 30 seconds behind', async () => {
const systemTime = new Date('August 19, 1975 23:15:30');
jest.useFakeTimers({ legacyFakeTimers: true });
const mockFetch = jest.fn();

const replicaTime = new Date(Number(systemTime) + 31_000);
jest.mock('../../canisterStatus', () => {
return {
request: () => {
return {
// 31 seconds ahead
get: () => replicaTime,
};
},
};
});
await import('../../canisterStatus');
const { HttpAgent } = await import('../index');
await import('../../canisterStatus');
const { HttpAgent } = await import('../index');

const agent = new HttpAgent({ host: 'http://localhost:8000', fetch: mockFetch });
const agent = new HttpAgent({ host: 'http://localhost:8000', fetch: mockFetch });

await agent.syncTime();
await agent.syncTime();

await agent
.call(Principal.managementCanister(), {
methodName: 'test',
arg: new Uint8Array().buffer,
})
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
.catch(function (_) {});
await agent
.call(Principal.managementCanister(), {
methodName: 'test',
arg: new Uint8Array().buffer,
})
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
.catch(function (_) {});

const requestBody: any = cbor.decode(mockFetch.mock.calls[0][1].body);

// Expiry should be: ingress expiry + replica time
const expiryInMs = requestBody.content.ingress_expiry / NANOSECONDS_PER_MILLISECONDS;

const requestBody: any = cbor.decode(mockFetch.mock.calls[0][1].body);
const delay = expiryInMs + REPLICA_PERMITTED_DRIFT_MILLISECONDS - Number(replicaTime);

// Expiry should be: ingress expiry + replica time
const expiryInMs = requestBody.content.ingress_expiry / NANOSECONDS_PER_MILLISECONDS;
expect(requestBody.content.ingress_expiry).toMatchInlineSnapshot(`1271000000000`);

const delay = expiryInMs + REPLICA_PERMITTED_DRIFT_MILLISECONDS - Number(replicaTime);
expect(delay).toBe(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);
jest.resetModules();
});

expect(requestBody.content.ingress_expiry).toMatchInlineSnapshot(`"177747601000000000"`);
// TODO - fix broken test
test('should adjust the Expiry if the clock is more than 30 seconds ahead', async () => {
const mockFetch = jest.fn();

expect(delay).toBe(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);
jest.autoMockOff();
const replicaTime = new Date(Date.now() - 31_000);
jest.mock('../../canisterStatus', () => {
return {
request: () => {
return {
// 31 seconds behind
get: () => replicaTime,
};
},
};
});
// TODO - fix broken test
it.skip('should adjust the Expiry if the clock is more than 30 seconds ahead', async () => {
jest.useFakeTimers();
const systemTime = new Date('August 19, 1975 23:15:30');
jest.setSystemTime(systemTime);
const mockFetch = jest.fn();
jest.useFakeTimers();

const replicaTime = new Date(Number(systemTime) - 31_000);
// jest.mock('../../canisterStatus', () => {
// return {
// request: () => {
// return {
// // 31 seconds ahead
// get: () => replicaTime,
// };
// },
// };
// });
await import('../../canisterStatus');
const { HttpAgent } = await import('../index');

const agent = new HttpAgent({ host: 'https://ic0.app', fetch: fetch });

await agent.syncTime();
await import('../../canisterStatus');
const { HttpAgent } = await import('../index');

await agent
.call(Principal.managementCanister(), {
methodName: 'test',
arg: new Uint8Array().buffer,
})
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
.catch(function (_) {
console.error(_);
});
const agent = new HttpAgent({ host: 'http://localhost:8000', fetch: mockFetch });

// const requestBody: any = cbor.decode(mockFetch.mock.calls[0][1].body);
await agent.syncTime();

// Expiry should be: ingress expiry + replica time
// const expiryInMs = requestBody.content.ingress_expiry / NANOSECONDS_PER_MILLISECONDS;
await agent
.call(Principal.managementCanister(), {
methodName: 'test',
arg: new Uint8Array().buffer,
})
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
.catch(function (_) {});

// const delay = expiryInMs + REPLICA_PERMITTED_DRIFT_MILLISECONDS - Number(replicaTime);
const requestBody: any = cbor.decode(mockFetch.mock.calls[0][1].body);

// expect(expiryInMs).toMatchInlineSnapshot(`177747539000`);
// Expiry should be: replica time - ingress expiry
const expiryInMs = requestBody.content.ingress_expiry / NANOSECONDS_PER_MILLISECONDS;

// expect(delay).toBe(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);
});
jest.autoMockOff();
const delay = Number(replicaTime) - (expiryInMs + REPLICA_PERMITTED_DRIFT_MILLISECONDS);

expect(requestBody.content.ingress_expiry).toMatchInlineSnapshot(`1209000000000`);

expect(delay).toBe(-1 * DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);
jest.resetModules();
});