Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SecuritySolution][Unit Tests] - fix flakey unit test #81239

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import dateMath from '@elastic/datemath';
import moment from 'moment';

import { EqlSearchStrategyResponse } from '../../../../../data_enhanced/common';
Expand All @@ -26,62 +25,62 @@ describe('eql/helpers', () => {
describe('calculateBucketForHour', () => {
test('returns 2 if event occurred within 2 minutes of "now"', () => {
const diff = calculateBucketForHour(
Number(dateMath.parse('now-1m')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T05:56:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
);

expect(diff).toEqual(2);
});

test('returns 10 if event occurred within 8-10 minutes of "now"', () => {
const diff = calculateBucketForHour(
Number(dateMath.parse('now-9m')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T05:48:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
);

expect(diff).toEqual(10);
});

test('returns 16 if event occurred within 10-15 minutes of "now"', () => {
const diff = calculateBucketForHour(
Number(dateMath.parse('now-15m')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T05:42:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
);

expect(diff).toEqual(16);
});

test('returns 60 if event occurred within 58-60 minutes of "now"', () => {
const diff = calculateBucketForHour(
Number(dateMath.parse('now-59m')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T04:58:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
);

expect(diff).toEqual(60);
});

test('returns exact time difference if it is a multiple of 2', () => {
const diff = calculateBucketForHour(
Number(dateMath.parse('now-20m')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T05:37:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
);

expect(diff).toEqual(20);
});

test('returns 0 if times are equal', () => {
const diff = calculateBucketForHour(
Number(dateMath.parse('now')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T05:57:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
);

expect(diff).toEqual(0);
});

test('returns 2 if event occurred within 2 minutes of "now" but arguments are flipped', () => {
const diff = calculateBucketForHour(
Number(dateMath.parse('now')?.format('x')),
Number(dateMath.parse('now-1m')?.format('x'))
Date.parse('2020-02-20T05:57:54.037Z'),
Date.parse('2020-02-20T05:56:54.037Z')
);

expect(diff).toEqual(2);
Expand All @@ -91,53 +90,53 @@ describe('eql/helpers', () => {
describe('calculateBucketForDay', () => {
test('returns 0 if two dates are equivalent', () => {
const diff = calculateBucketForDay(
Number(dateMath.parse('now')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T05:57:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
);

expect(diff).toEqual(0);
});

test('returns 1 if event occurred within 60 minutes of "now"', () => {
const diff = calculateBucketForDay(
Number(dateMath.parse('now-40m')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T05:17:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
);

expect(diff).toEqual(1);
});

test('returns 2 if event occurred 60-120 minutes from "now"', () => {
const diff = calculateBucketForDay(
Number(dateMath.parse('now-120m')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T03:57:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
);

expect(diff).toEqual(2);
});

test('returns 3 if event occurred 120-180 minutes from "now', () => {
const diff = calculateBucketForDay(
Number(dateMath.parse('now-121m')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T03:56:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
);

expect(diff).toEqual(3);
});

test('returns 4 if event occurred 180-240 minutes from "now', () => {
const diff = calculateBucketForDay(
Number(dateMath.parse('now-220m')?.format('x')),
Number(dateMath.parse('now')?.format('x'))
Date.parse('2020-02-20T02:15:54.037Z'),
Date.parse('2020-02-20T05:57:54.037Z')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is 222 minutes... does that matter? Also, is the test description correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the description a bit. That test is just looking for the difference in time to be between 180-240 minutes, so 222 is fine. Let me know if I missed something with the descriptions!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perfect, thank you!

);

expect(diff).toEqual(4);
});

test('returns 2 if event occurred 60-120 minutes of "now" but arguments are flipped', () => {
const diff = calculateBucketForDay(
Number(dateMath.parse('now')?.format('x')),
Number(dateMath.parse('now-118m')?.format('x'))
Date.parse('2020-02-20T05:57:54.037Z'),
Date.parse('2020-02-20T03:59:54.037Z')
);

expect(diff).toEqual(2);
Expand Down