From 5918e188d9601b2695e03407f991b1ba50e05ed6 Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Wed, 13 Feb 2019 08:09:38 -0500 Subject: [PATCH] Follow-up #2298 with regression test (#2308) * test: ensure "total duration" > "duration of resolvers" Add test for https://github.com/apollographql/apollo-server/pull/2298 * Update tracing duration test to be more comprehensive Previously we were only guaranteeing that individual resolvers didn't have a duration longer than the total duration. The updated test guarantees that the duration (from when the first resolver started to when the last resolver ended) should be less than total duration * Add another resolver to tracing test * Prefer `const` over `let` for variables which aren't mutated. --- .../src/ApolloServer.ts | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/packages/apollo-server-integration-testsuite/src/ApolloServer.ts b/packages/apollo-server-integration-testsuite/src/ApolloServer.ts index 750e4a5b..819245d8 100644 --- a/packages/apollo-server-integration-testsuite/src/ApolloServer.ts +++ b/packages/apollo-server-integration-testsuite/src/ApolloServer.ts @@ -37,6 +37,7 @@ import { ApolloServerBase, } from 'apollo-server-core'; import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions'; +import { TracingFormat } from 'apollo-tracing'; export function createServerInfo( server: AS, @@ -1260,5 +1261,67 @@ export function testApolloServer( }, done.fail); }); }); + + describe('Tracing', () => { + const typeDefs = gql` + type Book { + title: String + author: String + } + + type Movie { + title: String + } + + type Query { + books: [Book] + movies: [Movie] + } + `; + + const resolvers = { + Query: { + books: () => + new Promise(resolve => + setTimeout(() => resolve([{ title: 'H', author: 'J' }]), 10), + ), + movies: () => + new Promise(resolve => + setTimeout(() => resolve([{ title: 'H' }]), 12), + ), + }, + }; + + it('reports a total duration that is longer than the duration of its resolvers', async () => { + const { url: uri } = await createApolloServer({ + typeDefs, + resolvers, + tracing: true, + }); + + const apolloFetch = createApolloFetch({ uri }); + const result = await apolloFetch({ + query: `{ books { title author } }`, + }); + + const tracing: TracingFormat = result.extensions.tracing; + + const earliestStartOffset = tracing.execution.resolvers + .map(resolver => resolver.startOffset) + .reduce((currentEarliestOffset, nextOffset) => + Math.min(currentEarliestOffset, nextOffset), + ); + + const latestEndOffset = tracing.execution.resolvers + .map(resolver => resolver.startOffset + resolver.duration) + .reduce((currentLatestEndOffset, nextEndOffset) => + Math.min(currentLatestEndOffset, nextEndOffset), + ); + + const resolverDuration = latestEndOffset - earliestStartOffset; + + expect(resolverDuration).not.toBeGreaterThan(tracing.duration); + }); + }); }); }