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.
This commit is contained in:
Chang Wang 2019-02-13 08:09:38 -05:00 committed by Jesse Rosenberger
parent 4d017c0447
commit 5918e188d9

View file

@ -37,6 +37,7 @@ import {
ApolloServerBase,
} from 'apollo-server-core';
import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions';
import { TracingFormat } from 'apollo-tracing';
export function createServerInfo<AS extends ApolloServerBase>(
server: AS,
@ -1260,5 +1261,67 @@ export function testApolloServer<AS extends ApolloServerBase>(
}, 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);
});
});
});
}