Skip to content

Commit ffd354d

Browse files
committed
test: updated
1 parent c5963f8 commit ffd354d

File tree

3 files changed

+92
-108
lines changed

3 files changed

+92
-108
lines changed

packages/collector/test/tracing/protocols/apollo_subgraph/client.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,21 @@ function runQuery(req, res) {
5656
return runQueryViaHttp(query, res);
5757
}
5858

59-
function runQueryViaHttp(query, res) {
60-
return fetch(serverGraphQLEndpoint, {
61-
method: 'POST',
62-
headers: {
63-
'Content-Type': 'application/json'
64-
},
65-
body: JSON.stringify({
66-
query
67-
})
68-
})
69-
.then(response => response.json())
70-
.then(data => {
71-
res.send(data);
72-
})
73-
.catch(error => {
74-
log(error);
75-
res.sendStatus(500);
59+
async function runQueryViaHttp(query, res) {
60+
try {
61+
const response = await fetch(serverGraphQLEndpoint, {
62+
method: 'POST',
63+
headers: {
64+
'Content-Type': 'application/json'
65+
},
66+
body: JSON.stringify({ query })
7667
});
68+
const data = await response.json();
69+
res.send(data);
70+
} catch (error) {
71+
log(error);
72+
res.sendStatus(500);
73+
}
7774
}
7875

7976
app.listen(port, () => {

packages/collector/test/tracing/protocols/graphql/client.js

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -99,28 +99,27 @@ app.post('/mutation', (req, res) =>
9999

100100
app.post('/subscription', (req, res) => establishSubscription(req, res));
101101

102-
app.post('/publish-update-via-http', (req, res) =>
103-
fetch(`${serverBaseUrl}/publish-update`, {
104-
method: 'POST',
105-
url: `${serverBaseUrl}/publish-update`,
106-
body: JSON.stringify({
107-
id: req.body.id || 1,
108-
name: req.body.name || 'Updated Name',
109-
profession: req.body.profession || 'Updated Profession'
110-
}),
111-
headers: {
112-
'Content-Type': 'application/json'
113-
}
114-
})
115-
.then(response => response.json())
116-
.then(response => {
117-
res.send(response);
118-
})
119-
.catch(e => {
120-
log(e);
121-
res.sendStatus(500);
122-
})
123-
);
102+
app.post('/publish-update-via-http', async (req, res) => {
103+
try {
104+
const response = await fetch(`${serverBaseUrl}/publish-update`, {
105+
method: 'POST',
106+
headers: {
107+
'Content-Type': 'application/json'
108+
},
109+
body: JSON.stringify({
110+
id: req.body.id || 1,
111+
name: req.body.name || 'Updated Name',
112+
profession: req.body.profession || 'Updated Profession'
113+
})
114+
});
115+
116+
const data = await response.json();
117+
res.send(data);
118+
} catch (e) {
119+
log(e);
120+
res.sendStatus(500);
121+
}
122+
});
124123

125124
app.post('/publish-update-via-graphql', (req, res) =>
126125
runMutation(req, res, {
@@ -159,25 +158,22 @@ function runQuery(req, res, resolverType) {
159158
}
160159
}
161160

162-
function runQueryViaHttp(query, res) {
163-
return fetch(serverGraphQLEndpoint, {
164-
method: 'POST',
165-
url: serverGraphQLEndpoint,
166-
body: JSON.stringify({
167-
query
168-
}),
169-
headers: {
170-
'Content-Type': 'application/json'
171-
}
172-
})
173-
.then(response => response.json())
174-
.then(response => {
175-
res.send(response);
176-
})
177-
.catch(e => {
178-
log(e);
179-
res.sendStatus(500);
161+
async function runQueryViaHttp(query, res) {
162+
try {
163+
const response = await fetch(serverGraphQLEndpoint, {
164+
method: 'POST',
165+
headers: {
166+
'Content-Type': 'application/json'
167+
},
168+
body: JSON.stringify({ query })
180169
});
170+
171+
const data = await response.json();
172+
res.send(data);
173+
} catch (e) {
174+
log(e);
175+
res.sendStatus(500);
176+
}
181177
}
182178

183179
function runQueryViaAmqp(query, res) {
@@ -216,7 +212,7 @@ function runQueryViaAmqp(query, res) {
216212
});
217213
}
218214

219-
function runMutation(req, res, input) {
215+
async function runMutation(req, res, input) {
220216
const mutation = `
221217
mutation UpdateCharacter($id: ID!, $name: String, $profession: String) {
222218
updateCharacter(input: { id: $id, name: $name, profession: $profession }) {
@@ -226,25 +222,24 @@ function runMutation(req, res, input) {
226222
}
227223
`;
228224

229-
return fetch(serverGraphQLEndpoint, {
230-
method: 'POST',
231-
url: serverGraphQLEndpoint,
232-
body: JSON.stringify({
233-
query: mutation,
234-
variables: input
235-
}),
236-
headers: {
237-
'Content-Type': 'application/json'
238-
}
239-
})
240-
.then(response => response.json())
241-
.then(response => {
242-
res.send(response);
243-
})
244-
.catch(e => {
245-
log(e);
246-
res.sendStatus(500);
225+
try {
226+
const response = await fetch(serverGraphQLEndpoint, {
227+
method: 'POST',
228+
headers: {
229+
'Content-Type': 'application/json'
230+
},
231+
body: JSON.stringify({
232+
query: mutation,
233+
variables: input
234+
})
247235
});
236+
237+
const data = await response.json();
238+
res.send(data);
239+
} catch (e) {
240+
log(e);
241+
res.sendStatus(500);
242+
}
248243
}
249244

250245
function establishSubscription(req, res) {

packages/collector/test/tracing/protocols/http/server/test.js

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -508,39 +508,31 @@ function registerTests(agentControls, appUsesHttps, useHttp2CompatApi) {
508508
});
509509
});
510510

511-
it(`must capture an HTTP entry when the client closes the connection (HTTPS: ${appUsesHttps})`, () =>
512-
controls
513-
.sendRequest({
514-
path: '/dont-respond',
515-
timeout: 100,
516-
simple: false
517-
})
518-
.then(() => {
519-
fail('Expected the HTTP call to time out.');
511+
it(`must capture an HTTP entry when the client closes the connection (HTTPS: ${appUsesHttps})`, async () => {
512+
const requestPromise = controls.sendRequest({
513+
path: '/dont-respond',
514+
simple: false
515+
});
516+
await delay(200);
517+
518+
// Verify that an entry span has been created even though the request hasn't completed yet
519+
// The span is captured when the client closes the connection via the req.on('close') event
520+
await retry(() =>
521+
agentControls.getSpans().then(spans => {
522+
// Note: For HTTP 1, the captured HTTP status will be 200 even for a client timeout, because the we take
523+
// the status from the response object which is created before the request is processed by user code. The
524+
// default for the status attribute is 200 and so this is what we capture (or whatever the user code sets
525+
// on the response object before running the request is aborted due to the timeout). For HTTP 2, the
526+
// situation is different because we inspect a response header of the stream (HTTP2_HEADER_STATUS), which
527+
// does not exist until a response is actually sent. Thus, for HTTP 2, span.data.http.status will be
528+
// undefined.
529+
verifyThereIsExactlyOneHttpEntry(spans, controls, '/dont-respond', 'GET', 200, false, false);
520530
})
521-
.catch(err => {
522-
if (
523-
(err.error && (err.error.code === 'ESOCKETTIMEDOUT' || err.error.code === 'ETIMEDOUT')) ||
524-
err.type === 'request-timeout'
525-
) {
526-
// We actually expect the request to time out. But we still want to verify that an entry span has been created
527-
// for it.
528-
return retry(() =>
529-
agentControls.getSpans().then(spans => {
530-
// Note: For HTTP 1, the captured HTTP status will be 200 even for a client timeout, because the we take
531-
// the status from the response object which is created before the request is processed by user code. The
532-
// default for the status attribute is 200 and so this is what we capture (or whatever the user code sets
533-
// on the response object before running the request is aborted due to the timeout). For HTTP 2, the
534-
// situation is different because we inspect a response header of the stream (HTTP2_HEADER_STATUS), which
535-
// does not exist until a response is actually sent. Thus, for HTTP 2, span.data.http.status will be
536-
// undefined.
537-
verifyThereIsExactlyOneHttpEntry(spans, controls, '/dont-respond', 'GET', undefined, false, false);
538-
})
539-
);
540-
} else {
541-
throw err;
542-
}
543-
}));
531+
);
532+
533+
// The request will eventually complete after 4 seconds (as defined in app.js)
534+
return requestPromise.catch(() => {});
535+
});
544536

545537
it(`must capture an HTTP entry when the server destroys the socket (HTTPS: ${appUsesHttps})`, () =>
546538
controls

0 commit comments

Comments
 (0)