Skip to content

Commit f95aa73

Browse files
committed
Fix race condition in flaky test
1 parent a8e33a8 commit f95aa73

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

x-pack/test/alerting_api_integration/common/lib/task_manager_utils.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class TaskManagerUtils {
1313
this.retry = retry;
1414
}
1515

16-
async waitForIdle(taskRunAtFilter: Date) {
16+
async waitForEmpty(taskRunAtFilter: Date) {
1717
return await this.retry.try(async () => {
1818
const searchResult = await this.es.search({
1919
index: '.kibana_task_manager',
@@ -44,6 +44,44 @@ export class TaskManagerUtils {
4444
});
4545
}
4646

47+
async waitForAllTasksIdle(taskRunAtFilter: Date) {
48+
return await this.retry.try(async () => {
49+
const searchResult = await this.es.search({
50+
index: '.kibana_task_manager',
51+
body: {
52+
query: {
53+
bool: {
54+
must: [
55+
{
56+
terms: {
57+
'task.scope': ['actions', 'alerting'],
58+
},
59+
},
60+
{
61+
range: {
62+
'task.scheduledAt': {
63+
gte: taskRunAtFilter,
64+
},
65+
},
66+
},
67+
],
68+
must_not: [
69+
{
70+
term: {
71+
'task.status': 'idle',
72+
},
73+
},
74+
],
75+
},
76+
},
77+
},
78+
});
79+
if (searchResult.hits.total.value) {
80+
throw new Error(`Expected 0 non-idle tasks but received ${searchResult.hits.total.value}`);
81+
}
82+
});
83+
}
84+
4785
async waitForActionTaskParamsToBeCleanedUp(createdAtFilter: Date): Promise<void> {
4886
return await this.retry.try(async () => {
4987
const searchResult = await this.es.search({

x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/alerts.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ export default function alertTests({ getService }: FtrProviderContext) {
2626
const esTestIndexTool = new ESTestIndexTool(es, retry);
2727
const taskManagerUtils = new TaskManagerUtils(es, retry);
2828

29-
// FLAKY: https://github.com/elastic/kibana/issues/58643
30-
// FLAKY: https://github.com/elastic/kibana/issues/58991
31-
describe.skip('alerts', () => {
29+
describe('alerts', () => {
3230
const authorizationIndex = '.kibana-test-authorization';
3331
const objectRemover = new ObjectRemover(supertest);
3432

@@ -99,9 +97,11 @@ export default function alertTests({ getService }: FtrProviderContext) {
9997
// Wait for the action to index a document before disabling the alert and waiting for tasks to finish
10098
await esTestIndexTool.waitForDocs('action:test.index-record', reference);
10199

100+
await taskManagerUtils.waitForAllTasksIdle(testStart);
101+
102102
const alertId = response.body.id;
103103
await alertUtils.disable(alertId);
104-
await taskManagerUtils.waitForIdle(testStart);
104+
await taskManagerUtils.waitForEmpty(testStart);
105105

106106
// Ensure only 1 alert executed with proper params
107107
const alertSearchResult = await esTestIndexTool.search(
@@ -166,6 +166,7 @@ instanceStateValue: true
166166
});
167167

168168
it('should pass updated alert params to executor', async () => {
169+
const testStart = new Date();
169170
// create an alert
170171
const reference = alertUtils.generateReference();
171172
const overwrites = {
@@ -197,6 +198,9 @@ instanceStateValue: true
197198

198199
// make sure alert info passed to executor is correct
199200
await esTestIndexTool.waitForDocs('alert:test.always-firing', reference2);
201+
202+
await taskManagerUtils.waitForAllTasksIdle(testStart);
203+
200204
await alertUtils.disable(alertId);
201205
const alertSearchResult = await esTestIndexTool.search(
202206
'alert:test.always-firing',
@@ -359,7 +363,7 @@ instanceStateValue: true
359363
// Wait for test.authorization to index a document before disabling the alert and waiting for tasks to finish
360364
await esTestIndexTool.waitForDocs('alert:test.authorization', reference);
361365
await alertUtils.disable(response.body.id);
362-
await taskManagerUtils.waitForIdle(testStart);
366+
await taskManagerUtils.waitForEmpty(testStart);
363367

364368
// Ensure only 1 document exists with proper params
365369
searchResult = await esTestIndexTool.search('alert:test.authorization', reference);
@@ -387,7 +391,7 @@ instanceStateValue: true
387391
// Wait for test.authorization to index a document before disabling the alert and waiting for tasks to finish
388392
await esTestIndexTool.waitForDocs('alert:test.authorization', reference);
389393
await alertUtils.disable(response.body.id);
390-
await taskManagerUtils.waitForIdle(testStart);
394+
await taskManagerUtils.waitForEmpty(testStart);
391395

392396
// Ensure only 1 document exists with proper params
393397
searchResult = await esTestIndexTool.search('alert:test.authorization', reference);
@@ -467,7 +471,7 @@ instanceStateValue: true
467471
// Ensure test.authorization indexed 1 document before disabling the alert and waiting for tasks to finish
468472
await esTestIndexTool.waitForDocs('action:test.authorization', reference);
469473
await alertUtils.disable(response.body.id);
470-
await taskManagerUtils.waitForIdle(testStart);
474+
await taskManagerUtils.waitForEmpty(testStart);
471475

472476
// Ensure only 1 document with proper params exists
473477
searchResult = await esTestIndexTool.search('action:test.authorization', reference);
@@ -495,7 +499,7 @@ instanceStateValue: true
495499
// Ensure test.authorization indexed 1 document before disabling the alert and waiting for tasks to finish
496500
await esTestIndexTool.waitForDocs('action:test.authorization', reference);
497501
await alertUtils.disable(response.body.id);
498-
await taskManagerUtils.waitForIdle(testStart);
502+
await taskManagerUtils.waitForEmpty(testStart);
499503

500504
// Ensure only 1 document with proper params exists
501505
searchResult = await esTestIndexTool.search('action:test.authorization', reference);
@@ -544,7 +548,7 @@ instanceStateValue: true
544548
// Wait until alerts scheduled actions 3 times before disabling the alert and waiting for tasks to finish
545549
await esTestIndexTool.waitForDocs('alert:test.always-firing', reference, 3);
546550
await alertUtils.disable(response.body.id);
547-
await taskManagerUtils.waitForIdle(testStart);
551+
await taskManagerUtils.waitForEmpty(testStart);
548552

549553
// Ensure actions only executed once
550554
const searchResult = await esTestIndexTool.search(
@@ -610,7 +614,7 @@ instanceStateValue: true
610614
// Wait for actions to execute twice before disabling the alert and waiting for tasks to finish
611615
await esTestIndexTool.waitForDocs('action:test.index-record', reference, 2);
612616
await alertUtils.disable(response.body.id);
613-
await taskManagerUtils.waitForIdle(testStart);
617+
await taskManagerUtils.waitForEmpty(testStart);
614618

615619
// Ensure only 2 actions with proper params exists
616620
const searchResult = await esTestIndexTool.search(
@@ -660,7 +664,7 @@ instanceStateValue: true
660664
// Actions should execute twice before widning things down
661665
await esTestIndexTool.waitForDocs('action:test.index-record', reference, 2);
662666
await alertUtils.disable(response.body.id);
663-
await taskManagerUtils.waitForIdle(testStart);
667+
await taskManagerUtils.waitForEmpty(testStart);
664668

665669
// Ensure only 2 actions are executed
666670
const searchResult = await esTestIndexTool.search(
@@ -705,7 +709,7 @@ instanceStateValue: true
705709
// execution once before disabling the alert and waiting for tasks to finish
706710
await esTestIndexTool.waitForDocs('alert:test.always-firing', reference, 2);
707711
await alertUtils.disable(response.body.id);
708-
await taskManagerUtils.waitForIdle(testStart);
712+
await taskManagerUtils.waitForEmpty(testStart);
709713

710714
// Should not have executed any action
711715
const executedActionsResult = await esTestIndexTool.search(
@@ -750,7 +754,7 @@ instanceStateValue: true
750754
// once before disabling the alert and waiting for tasks to finish
751755
await esTestIndexTool.waitForDocs('alert:test.always-firing', reference, 2);
752756
await alertUtils.disable(response.body.id);
753-
await taskManagerUtils.waitForIdle(testStart);
757+
await taskManagerUtils.waitForEmpty(testStart);
754758

755759
// Should not have executed any action
756760
const executedActionsResult = await esTestIndexTool.search(
@@ -796,7 +800,7 @@ instanceStateValue: true
796800
// Ensure actions are executed once before disabling the alert and waiting for tasks to finish
797801
await esTestIndexTool.waitForDocs('action:test.index-record', reference, 1);
798802
await alertUtils.disable(response.body.id);
799-
await taskManagerUtils.waitForIdle(testStart);
803+
await taskManagerUtils.waitForEmpty(testStart);
800804

801805
// Should have one document indexed by the action
802806
const searchResult = await esTestIndexTool.search(

0 commit comments

Comments
 (0)