Skip to content

Commit

Permalink
fix: stabilize clock creation and deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Aug 29, 2022
1 parent 685dd87 commit bae41d8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 133 deletions.
32 changes: 15 additions & 17 deletions test/gateway/application-hooks.js
@@ -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({
shouldClearNativeTimers: true,
shouldAdvanceTime: true,
advanceTimeDelta: 100
})

const resolvers = {
Query: {
me: (root, args, context, info) => user
Expand All @@ -42,7 +48,6 @@ test('onGatewayReplaceSchema - polling interval with a new schema should trigger
t.teardown(async () => {
await gateway.close()
await userService.close()
clock.uninstall()
})

userService.register(GQL, {
Expand Down Expand Up @@ -97,7 +102,7 @@ test('onGatewayReplaceSchema - polling interval with a new schema should trigger
userService.graphql.defineResolvers(resolvers)

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

// We need the event loop to actually spin twice to
Expand All @@ -109,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({
shouldClearNativeTimers: true,
shouldAdvanceTime: true,
advanceTimeDelta: 100
})

const resolvers = {
Query: {
me: (root, args, context, info) => user
Expand All @@ -135,7 +134,6 @@ test('onGatewayReplaceSchema - should log an error should any errors occur in th
t.teardown(async () => {
await gateway.close()
await userService.close()
clock.uninstall()
})

userService.register(GQL, {
Expand Down Expand Up @@ -199,7 +197,7 @@ test('onGatewayReplaceSchema - should log an error should any errors occur in th
userService.graphql.defineResolvers(resolvers)

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

// We need the event loop to actually spin twice to
Expand Down
64 changes: 17 additions & 47 deletions test/gateway/pollingInterval.js
@@ -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.afterEach(({ context }) => {
context.clock.uninstall()
})

test('Polling schemas with disable cache', async (t) => {
const resolvers = {
Query: {
me: (root, args, context, info) => user
Expand All @@ -40,7 +46,6 @@ test('Polling schemas with disable cache', async (t) => {
t.teardown(async () => {
await gateway.close()
await userService.close()
clock.uninstall()
})

userService.register(GQL, {
Expand Down Expand Up @@ -104,12 +109,6 @@ test('Polling schemas with disable cache', async (t) => {
})

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

const resolvers = {
Query: {
me: (root, args, context, info) => user
Expand All @@ -130,7 +129,6 @@ test('Polling schemas', async (t) => {
t.teardown(async () => {
await gateway.close()
await userService.close()
clock.uninstall()
})

userService.register(GQL, {
Expand Down Expand Up @@ -237,7 +235,7 @@ test('Polling schemas', async (t) => {
userService.graphql.defineResolvers(resolvers)

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

// We need the event loop to actually spin twice to
Expand Down Expand Up @@ -340,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({
shouldClearNativeTimers: true,
shouldAdvanceTime: true,
advanceTimeDelta: 100
})

const resolvers = {
Query: {
me: (root, args, context, info) => user
Expand All @@ -367,7 +359,6 @@ test("Polling schemas (if service is down, schema shouldn't be changed)", async
t.teardown(async () => {
await gateway.close()
await userService.close()
clock.uninstall()
})

userService.register(GQL, {
Expand All @@ -386,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 @@ -402,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 @@ -423,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 @@ -468,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 @@ -627,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({
shouldClearNativeTimers: true,
shouldAdvanceTime: true,
advanceTimeDelta: 100
})

const user = {
id: 'u1',
name: 'John',
Expand All @@ -644,7 +629,6 @@ test('Polling schemas (cache should be cleared)', async (t) => {
t.teardown(async () => {
await gateway.close()
await userService.close()
clock.uninstall()
})

userService.register(GQL, {
Expand Down Expand Up @@ -736,7 +720,7 @@ test('Polling schemas (cache should be cleared)', async (t) => {
})

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

// We need the event loop to actually spin twice to
Expand Down Expand Up @@ -801,12 +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({
shouldClearNativeTimers: true,
shouldAdvanceTime: true,
advanceTimeDelta: 100
})

const oldSchema = `
type Query {
me: User
Expand Down Expand Up @@ -889,7 +867,6 @@ test('Polling schemas (should properly regenerate the schema when a downstream s
await gateway.close()
await userService.close()
await restartedUserService.close()
clock.uninstall()
})

const refreshedSchema = `
Expand Down Expand Up @@ -927,7 +904,7 @@ test('Polling schemas (should properly regenerate the schema when a downstream s
await restartedUserService.listen({ port: userServicePort })

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

// We need the event loop to actually spin twice to
Expand Down Expand Up @@ -994,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({
shouldClearNativeTimers: true,
shouldAdvanceTime: true,
advanceTimeDelta: 100
})

const user = {
id: 'u1',
name: 'John',
Expand Down Expand Up @@ -1039,7 +1010,6 @@ test('Polling schemas (subscriptions should be handled)', async (t) => {
t.teardown(async () => {
await gateway.close()
await userService.close()
clock.uninstall()
})

userService.register(GQL, {
Expand Down Expand Up @@ -1185,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

0 comments on commit bae41d8

Please sign in to comment.