Skip to content

Commit

Permalink
[ResponseOps][Rules] Fix rules list item enabled state updates causin…
Browse files Browse the repository at this point in the history
…g test flakyness (elastic#183805)

## Summary

Removes optimistic updates from rules list item when enabling/disabling
rules to avoid out-of-sync states (the likely cause of
[this](elastic#157623) flaky test).

Fixes elastic#157623
  • Loading branch information
umbopepato authored May 21, 2024
1 parent ab3a272 commit 46c8a99
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { useState, useEffect, useCallback } from 'react';
import React, { useState, useCallback } from 'react';
import moment from 'moment';
import { i18n } from '@kbn/i18n';
import type { RuleSnooze } from '@kbn/alerting-plugin/common';
Expand All @@ -29,7 +29,6 @@ import { Rule, SnoozeSchedule, BulkOperationResponse } from '../../../../types';
import { ToastWithCircuitBreakerContent } from '../../../components/toast_with_circuit_breaker_content';
import { UntrackAlertsModal } from '../../common/components/untrack_alerts_modal';

export type SnoozeUnit = 'm' | 'h' | 'd' | 'w' | 'M';
const SNOOZE_END_TIME_FORMAT = 'LL @ LT';

type DropdownRuleRecord = Pick<
Expand Down Expand Up @@ -60,24 +59,16 @@ export const RuleStatusDropdown: React.FunctionComponent<ComponentOpts> = ({
hideSnoozeOption = false,
direction = 'column',
}: ComponentOpts) => {
const [isEnabled, setIsEnabled] = useState<boolean>(rule.enabled);
const [isSnoozed, setIsSnoozed] = useState<boolean>(!hideSnoozeOption && isRuleSnoozed(rule));

const {
notifications: { toasts },
i18n: i18nStart,
theme,
} = useKibana().services;

useEffect(() => {
setIsEnabled(rule.enabled);
}, [rule.enabled]);
useEffect(() => {
if (!hideSnoozeOption) setIsSnoozed(isRuleSnoozed(rule));
}, [rule, hideSnoozeOption]);
const [isUpdating, setIsUpdating] = useState<boolean>(false);
const [isPopoverOpen, setIsPopoverOpen] = useState<boolean>(false);
const [isUntrackAlertsModalOpen, setIsUntrackAlertsModalOpen] = useState<boolean>(false);
const isSnoozed = !hideSnoozeOption && isRuleSnoozed(rule);

const onClickBadge = useCallback(() => setIsPopoverOpen((isOpen) => !isOpen), [setIsPopoverOpen]);
const onClosePopover = useCallback(() => setIsPopoverOpen(false), [setIsPopoverOpen]);
Expand Down Expand Up @@ -106,7 +97,6 @@ export const RuleStatusDropdown: React.FunctionComponent<ComponentOpts> = ({
setIsUpdating(true);
try {
await enableRuleInternal();
setIsEnabled(true);
onRuleChanged();
} finally {
setIsUpdating(false);
Expand All @@ -118,7 +108,6 @@ export const RuleStatusDropdown: React.FunctionComponent<ComponentOpts> = ({
setIsUpdating(true);
try {
await disableRule(untrack);
setIsEnabled(false);
onRuleChanged();
} finally {
setIsUpdating(false);
Expand Down Expand Up @@ -181,11 +170,11 @@ export const RuleStatusDropdown: React.FunctionComponent<ComponentOpts> = ({
[unsnoozeRule, onRuleChanged, onClosePopover]
);

const badgeColor = !isEnabled ? 'default' : isSnoozed ? 'warning' : 'primary';
const badgeMessage = !isEnabled ? DISABLED : isSnoozed ? SNOOZED : ENABLED;
const badgeColor = !rule.enabled ? 'default' : isSnoozed ? 'warning' : 'primary';
const badgeMessage = !rule.enabled ? DISABLED : isSnoozed ? SNOOZED : ENABLED;

const remainingSnoozeTime =
isEnabled && isSnoozed ? (
rule.enabled && isSnoozed ? (
<EuiToolTip
content={
rule.muteAll
Expand Down Expand Up @@ -246,7 +235,7 @@ export const RuleStatusDropdown: React.FunctionComponent<ComponentOpts> = ({
<RuleStatusMenu
onClosePopover={onClosePopover}
onChangeEnabledStatus={onChangeEnabledStatus}
isEnabled={isEnabled}
isEnabled={rule.enabled}
isSnoozed={isSnoozed}
snoozeEndTime={rule.isSnoozedUntil}
hideSnoozeOption={hideSnoozeOption}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ export default ({ getPageObjects, getPageObject, getService }: FtrProviderContex
return summary;
};

// FLAKY: https://github.com/elastic/kibana/issues/157623
describe.skip('rules list', function () {
describe('rules list', function () {
const assertRulesLength = async (length: number) => {
return await retry.try(async () => {
const rules = await pageObjects.triggersActionsUI.getAlertsList();
Expand Down

0 comments on commit 46c8a99

Please sign in to comment.