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: clear native timers on tests #856

Merged
merged 5 commits into from
Aug 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions test/gateway/application-hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test, t } = require('tap')
const FakeTimers = require('@sinonjs/fake-timers')
const { GraphQLSchema } = require('graphql')
const { promisify } = require('util')
Expand All @@ -10,18 +10,24 @@ const buildFederationSchema = require('../../lib/federation')

const immediate = promisify(setImmediate)

t.beforeEach(({ context }) => {
context.clock = FakeTimers.install({
shouldClearNativeTimers: true,
shouldAdvanceTime: true,
advanceTimeDelta: 100
})
})

t.afterEach(({ context }) => {
context.clock.uninstall()
})

// ----------------------
// onGatewayReplaceSchema
// ----------------------
test('onGatewayReplaceSchema - polling interval with a new schema should trigger onGatewayReplaceSchema hook', async (t) => {
t.plan(2)

const clock = FakeTimers.install({
shouldAdvanceTime: true,
advanceTimeDelta: 40
})
t.teardown(() => clock.uninstall())

const resolvers = {
Query: {
me: (root, args, context, info) => user
Expand Down Expand Up @@ -95,7 +101,9 @@ test('onGatewayReplaceSchema - polling interval with a new schema should trigger
)
userService.graphql.defineResolvers(resolvers)

await clock.tickAsync(2000)
for (let i = 0; i < 10; i++) {
await t.context.clock.tickAsync(200)
}

// We need the event loop to actually spin twice to
// be able to propagate the change
Expand All @@ -106,12 +114,6 @@ test('onGatewayReplaceSchema - polling interval with a new schema should trigger
test('onGatewayReplaceSchema - should log an error should any errors occur in the hook', async (t) => {
t.plan(2)

const clock = FakeTimers.install({
shouldAdvanceTime: true,
advanceTimeDelta: 40
})
t.teardown(() => clock.uninstall())

const resolvers = {
Query: {
me: (root, args, context, info) => user
Expand Down Expand Up @@ -194,7 +196,9 @@ test('onGatewayReplaceSchema - should log an error should any errors occur in th
)
userService.graphql.defineResolvers(resolvers)

await clock.tickAsync(2000)
for (let i = 0; i < 10; i++) {
await t.context.clock.tickAsync(200)
}

// We need the event loop to actually spin twice to
// be able to propagate the change
Expand Down
65 changes: 24 additions & 41 deletions test/gateway/pollingInterval.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test, t } = require('tap')

const FakeTimers = require('@sinonjs/fake-timers')

Expand All @@ -13,13 +13,19 @@ const WebSocket = require('ws')
const buildFederationSchema = require('../../lib/federation')
const GQL = require('../..')

test('Polling schemas with disable cache', async (t) => {
const clock = FakeTimers.install({
t.beforeEach(({ context }) => {
context.clock = FakeTimers.install({
shouldClearNativeTimers: true,
shouldAdvanceTime: true,
advanceTimeDelta: 40
})
t.teardown(() => clock.uninstall())
})

t.afterEach(({ context }) => {
context.clock.uninstall()
})

test('Polling schemas with disable cache', async (t) => {
const resolvers = {
Query: {
me: (root, args, context, info) => user
Expand Down Expand Up @@ -103,12 +109,6 @@ test('Polling schemas with disable cache', async (t) => {
})

test('Polling schemas', async (t) => {
const clock = FakeTimers.install({
shouldAdvanceTime: true,
advanceTimeDelta: 40
})
t.teardown(() => clock.uninstall())

const resolvers = {
Query: {
me: (root, args, context, info) => user
Expand Down Expand Up @@ -234,7 +234,9 @@ test('Polling schemas', async (t) => {
)
userService.graphql.defineResolvers(resolvers)

await clock.tickAsync(2000)
for (let i = 0; i < 10; i++) {
await t.context.clock.tickAsync(200)
}

// We need the event loop to actually spin twice to
// be able to propagate the change
Expand Down Expand Up @@ -336,12 +338,6 @@ test('Polling schemas (gateway.polling interval is not a number)', async (t) =>
})

test("Polling schemas (if service is down, schema shouldn't be changed)", async (t) => {
const clock = FakeTimers.install({
shouldAdvanceTime: true,
advanceTimeDelta: 40
})
t.teardown(() => clock.uninstall())

const resolvers = {
Query: {
me: (root, args, context, info) => user
Expand Down Expand Up @@ -381,7 +377,7 @@ test("Polling schemas (if service is down, schema shouldn't be changed)", async
})

await userService.listen({ port: 0 })
await clock.tickAsync()
await t.context.clock.tickAsync()

const userServicePort = userService.server.address().port

Expand All @@ -397,7 +393,7 @@ test("Polling schemas (if service is down, schema shouldn't be changed)", async
}
})

await clock.tickAsync()
await t.context.clock.tickAsync()

{
const { body } = await gateway.inject({
Expand All @@ -418,7 +414,7 @@ test("Polling schemas (if service is down, schema shouldn't be changed)", async
})
})

await clock.tickAsync()
await t.context.clock.tickAsync()

t.same(JSON.parse(body), {
data: {
Expand Down Expand Up @@ -463,7 +459,7 @@ test("Polling schemas (if service is down, schema shouldn't be changed)", async
}

await userService.close()
await clock.tickAsync(500)
await t.context.clock.tickAsync(500)

{
const { body } = await gateway.inject({
Expand Down Expand Up @@ -622,12 +618,6 @@ test('Polling schemas (if service is mandatory, exception should be thrown)', as
})

test('Polling schemas (cache should be cleared)', async (t) => {
const clock = FakeTimers.install({
shouldAdvanceTime: true,
advanceTimeDelta: 40
})
t.teardown(() => clock.uninstall())

const user = {
id: 'u1',
name: 'John',
Expand Down Expand Up @@ -729,7 +719,9 @@ test('Polling schemas (cache should be cleared)', async (t) => {
}
})

await clock.tickAsync(10000)
for (let i = 0; i < 100; i++) {
await t.context.clock.tickAsync(100)
}

// We need the event loop to actually spin twice to
// be able to propagate the change
Expand Down Expand Up @@ -793,11 +785,6 @@ test('Polling schemas (cache should be cleared)', async (t) => {
})

test('Polling schemas (should properly regenerate the schema when a downstream service restarts)', async (t) => {
const clock = FakeTimers.install({
shouldAdvanceTime: true,
advanceTimeDelta: 40
})
t.teardown(() => clock.uninstall())
const oldSchema = `
type Query {
me: User
Expand Down Expand Up @@ -916,7 +903,9 @@ test('Polling schemas (should properly regenerate the schema when a downstream s

await restartedUserService.listen({ port: userServicePort })

await clock.tickAsync(10000)
for (let i = 0; i < 100; i++) {
await t.context.clock.tickAsync(100)
}

// We need the event loop to actually spin twice to
// be able to propagate the change
Expand Down Expand Up @@ -982,12 +971,6 @@ test('Polling schemas (should properly regenerate the schema when a downstream s
test('Polling schemas (subscriptions should be handled)', async (t) => {
t.plan(12)

const clock = FakeTimers.install({
shouldAdvanceTime: true,
advanceTimeDelta: 40
})
t.teardown(() => clock.uninstall())

const user = {
id: 'u1',
name: 'John',
Expand Down Expand Up @@ -1172,7 +1155,7 @@ test('Polling schemas (subscriptions should be handled)', async (t) => {

userService.graphql.defineResolvers(resolvers)

await clock.tickAsync(10000)
await t.context.clock.tickAsync(10000)

// We need the event loop to actually spin twice to
// be able to propagate the change
Expand Down
Loading