Skip to content

Commit

Permalink
[Flaky tests] Metric's server collector's integration tests (#127139) (
Browse files Browse the repository at this point in the history
…#127264)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 6bcce44)

# Conflicts:
#	src/core/server/metrics/integration_tests/server_collector.test.ts

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
afharo and kibanamachine authored Mar 9, 2022
1 parent 747a746 commit cfa084a
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/core/server/metrics/integration_tests/server_collector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { executionContextServiceMock } from '../../execution_context/execution_c
import { ServerMetricsCollector } from '../collectors/server';
import { setTimeout as setTimeoutPromise } from 'timers/promises';

const requestWaitDelay = 25;

describe('ServerMetricsCollector', () => {
let server: HttpService;
let collector: ServerMetricsCollector;
Expand Down Expand Up @@ -78,30 +76,32 @@ describe('ServerMetricsCollector', () => {

it('collect disconnects requests infos', async () => {
const never = new Promise((resolve) => undefined);
const hitSubject = new BehaviorSubject(0);
const disconnectRequested$ = new Subject(); // Controls the number of requests in the /disconnect endpoint
const disconnectAborted$ = new Subject(); // Controls the abort event in the /disconnect endpoint

router.get({ path: '/', validate: false }, async (ctx, req, res) => {
return res.ok({ body: '' });
});
router.get({ path: '/disconnect', validate: false }, async (ctx, req, res) => {
hitSubject.next(hitSubject.value + 1);
await never;
disconnectRequested$.next();
req.events.aborted$.subscribe(() => {
disconnectAborted$.next();
});
await never; // Never resolve the request
return res.ok({ body: '' });
});
await server.start();

await sendGet('/');
// superTest.get(path).end needs to be called with a callback to actually send the request.
const discoReq1 = sendGet('/disconnect').end(() => null);
const discoReq2 = sendGet('/disconnect').end(() => null);

await hitSubject
.pipe(
filter((count) => count >= 2),
take(1)
)
.toPromise();
await delay(requestWaitDelay); // wait for the requests to send

// Subscribe to expect 2 requests to /disconnect
const waitFor2Requests = disconnectRequested$.pipe(take(2)).toPromise();

const discoReq1 = sendGet('/disconnect').end();
const discoReq2 = sendGet('/disconnect').end();

// Wait for 2 requests to /disconnect
await waitFor2Requests;

let metrics = await collector.collect();
expect(metrics.requests).toEqual(
Expand All @@ -112,8 +112,13 @@ describe('ServerMetricsCollector', () => {
})
);

// Subscribe to the aborted$ event
const waitFor1stAbort = disconnectAborted$.pipe(take(1)).toPromise();

discoReq1.abort();
await delay(requestWaitDelay);

// Wait for the aborted$ event
await waitFor1stAbort;

metrics = await collector.collect();
expect(metrics.requests).toEqual(
Expand All @@ -123,8 +128,13 @@ describe('ServerMetricsCollector', () => {
})
);

// Subscribe to the aborted$ event
const waitFor2ndAbort = disconnectAborted$.pipe(take(1)).toPromise();

discoReq2.abort();
await delay(requestWaitDelay);

// Wait for the aborted$ event
await waitFor2ndAbort;

metrics = await collector.collect();
expect(metrics.requests).toEqual(
Expand Down

0 comments on commit cfa084a

Please sign in to comment.