Skip to content

Commit

Permalink
[Usage] Fix flaky UI Counters test (#100979)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
Bamieh and kibanamachine committed Jun 3, 2021
1 parent 5f6cf01 commit 72d2272
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions test/api_integration/apis/ui_counters/ui_counters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { UsageCountersSavedObject } from '../../../../src/plugins/usage_collecti
export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const retry = getService('retry');

const createUiCounterEvent = (eventName: string, type: UiCounterMetricType, count = 1) => ({
eventName,
Expand All @@ -23,16 +24,24 @@ export default function ({ getService }: FtrProviderContext) {
count,
});

const sendReport = async (report: Report) => {
const fetchUsageCountersObjects = async (): Promise<UsageCountersSavedObject[]> => {
const {
body: { saved_objects: savedObjects },
} = await supertest
.get('/api/saved_objects/_find?type=usage-counters')
.set('kbn-xsrf', 'kibana')
.expect(200);

return savedObjects;
};

const sendReport = async (report: Report): Promise<void> => {
await supertest
.post('/api/ui_counters/_report')
.set('kbn-xsrf', 'kibana')
.set('content-type', 'application/json')
.send({ report })
.expect(200);

// wait for SO to index data into ES
await new Promise((res) => setTimeout(res, 5 * 1000));
};

const getCounterById = (
Expand All @@ -47,8 +56,7 @@ export default function ({ getService }: FtrProviderContext) {
return savedObject;
};

// FLAKY: https://github.com/elastic/kibana/issues/98240
describe.skip('UI Counters API', () => {
describe('UI Counters API', () => {
const dayDate = moment().format('DDMMYYYY');
before(async () => await esArchiver.emptyKibanaIndex());

Expand All @@ -61,18 +69,15 @@ export default function ({ getService }: FtrProviderContext) {

await sendReport(report);

const {
body: { saved_objects: savedObjects },
} = await supertest
.get('/api/saved_objects/_find?type=usage-counters')
.set('kbn-xsrf', 'kibana')
.expect(200);

const countTypeEvent = getCounterById(
savedObjects,
`uiCounter:${dayDate}:${METRIC_TYPE.COUNT}:myApp:my_event`
);
expect(countTypeEvent.attributes.count).to.eql(1);
await retry.waitForWithTimeout('reported events to be stored into ES', 8000, async () => {
const savedObjects = await fetchUsageCountersObjects();
const countTypeEvent = getCounterById(
savedObjects,
`uiCounter:${dayDate}:${METRIC_TYPE.COUNT}:myApp:my_event`
);
expect(countTypeEvent.attributes.count).to.eql(1);
return true;
});
});

it('supports multiple events', async () => {
Expand All @@ -87,31 +92,27 @@ export default function ({ getService }: FtrProviderContext) {
]);

await sendReport(report);

const {
body: { saved_objects: savedObjects },
} = await supertest
.get('/api/saved_objects/_find?type=usage-counters&fields=count')
.set('kbn-xsrf', 'kibana')
.expect(200);

const countTypeEvent = getCounterById(
savedObjects,
`uiCounter:${dayDate}:${METRIC_TYPE.COUNT}:myApp:${uniqueEventName}`
);
expect(countTypeEvent.attributes.count).to.eql(1);

const clickTypeEvent = getCounterById(
savedObjects,
`uiCounter:${dayDate}:${METRIC_TYPE.CLICK}:myApp:${uniqueEventName}`
);
expect(clickTypeEvent.attributes.count).to.eql(2);

const secondEvent = getCounterById(
savedObjects,
`uiCounter:${dayDate}:${METRIC_TYPE.COUNT}:myApp:${uniqueEventName}_2`
);
expect(secondEvent.attributes.count).to.eql(1);
await retry.waitForWithTimeout('reported events to be stored into ES', 8000, async () => {
const savedObjects = await fetchUsageCountersObjects();
const countTypeEvent = getCounterById(
savedObjects,
`uiCounter:${dayDate}:${METRIC_TYPE.COUNT}:myApp:${uniqueEventName}`
);
expect(countTypeEvent.attributes.count).to.eql(1);

const clickTypeEvent = getCounterById(
savedObjects,
`uiCounter:${dayDate}:${METRIC_TYPE.CLICK}:myApp:${uniqueEventName}`
);
expect(clickTypeEvent.attributes.count).to.eql(2);

const secondEvent = getCounterById(
savedObjects,
`uiCounter:${dayDate}:${METRIC_TYPE.COUNT}:myApp:${uniqueEventName}_2`
);
expect(secondEvent.attributes.count).to.eql(1);
return true;
});
});
});
}

0 comments on commit 72d2272

Please sign in to comment.