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

chore: use graceful timeout mechanism #1841

Merged
merged 13 commits into from
Feb 15, 2024
Merged

Conversation

fbarbu15
Copy link
Collaborator

@fbarbu15 fbarbu15 commented Feb 12, 2024

Problem

Looking at the flaky tests from the last months it seems most are failing at beforeEach and afterEach hooks, usually with a Timeout error when staring or stopping the nwaku container.
After investigating it seems that mocha retry mechanism only retries the test if the failure was in the test itself (inside it ) and it fails immediately, without any retry, if the failure is in those beforeEach/afterEach hooks.

Solution

To address those failures I've increased the timeouts for all beforeEach and afterEach hooks
Also added a function to gracefully handle timeouts in the staring/stopping of the nwaku containers, and making the hook pass in such cases, so the failure gets detected in the test itself and hence triggering the retry mechanism.

Copy link

github-actions bot commented Feb 12, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
Waku core 36.2 KB (0%) 724 ms (0%) 1.8 s (-10.01% 🔽) 2.5 s
Waku Simple Light Node 278.28 KB (0%) 5.6 s (0%) 4.3 s (+51.26% 🔺) 9.9 s
ECIES encryption 32.01 KB (0%) 641 ms (0%) 1.7 s (+26.44% 🔺) 2.4 s
Symmetric encryption 32 KB (0%) 641 ms (0%) 1.8 s (+91.98% 🔺) 2.5 s
DNS discovery 107.71 KB (0%) 2.2 s (0%) 1.4 s (-29.06% 🔽) 3.6 s
Privacy preserving protocols 128.92 KB (0%) 2.6 s (0%) 2.3 s (+16.55% 🔺) 4.8 s
Light protocols 34.27 KB (0%) 686 ms (0%) 831 ms (-31.12% 🔽) 1.6 s
History retrieval protocols 32.71 KB (0%) 655 ms (0%) 1.3 s (+30.09% 🔺) 1.9 s
Deterministic Message Hashing 5.96 KB (0%) 120 ms (0%) 297 ms (+54.53% 🔺) 416 ms

@fbarbu15 fbarbu15 marked this pull request as ready for review February 12, 2024 18:38
@fbarbu15 fbarbu15 requested a review from a team as a code owner February 12, 2024 18:38

export function withGracefulTimeout(
asyncOperation: () => Promise<void>,
timeoutDuration: number,
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's make it same as MOCHA_HOOK_MAX_TIMEOUT global config and optional to pass

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, makes sense

import { Logger } from "@waku/utils";
const log = new Logger("test:mocha-hook");

export function withGracefulTimeout(
Copy link
Collaborator

Choose a reason for hiding this comment

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

to decrease amount of lines changed - I want to propose following:

export const beforeEachCustom = function (self, cb: Promise<void>, setter: any) {
  self.beforeEach((done) => {
    this.timeout(MOCHA_HOOK_MAX_TIMEOUT);
    withGracefulTimeout(async () => {
      const [nwaku, waku] = await runMultipleNodes(
        this,
        [DefaultPubsubTopic],
        strictCheckNodes
      );
      subscription = await waku.filter.createSubscription();
      
      setter({ nwaku, waku/*, nwaku1, ...etc */});
    }, 20000, done);
  });
});

do you think it would work, @fbarbu15 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did made it work but there are multiple functions that we need to wrap:

runMultipleNodes 
runNodes (2 of them)
tearDownNodes
teardownNodesWithRedundancy

and direct usage of
nwaku.start + other commands like subscribe

So we would probably need to create multiple customFunctions (we will complicate stuff even further) or unify the mentioned methods to have only one general runNodes function and only one general tearDownNodes function (this one looks like a big refactoring but it may be worth it)
Wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is solvable the same way:

in utils file:

export const beforeEachCustom = function (self, cb: Promise<void>) {
  self.beforeEach((done) => {
    this.timeout(MOCHA_HOOK_MAX_TIMEOUT);
    withGracefulTimeout(cb, 20000, done);
  });
});

export const afterEachCustom = function (self, cb: Promise<void>, setter: any) {
  self.afterEach((done) => {
    this.timeout(MOCHA_HOOK_MAX_TIMEOUT);
    withGracefulTimeout(cb, 20000, done);
  });
});

in test file:

describe("test", () => {
  let nwaku, waku;

  beforeEachCustom(async () => {
    [nwaku, waku] = await runMultipleNodes(
      this,
      [DefaultPubsubTopic],
      strictCheckNodes
    );
    subscription = await waku.filter.createSubscription();
  });

  afterEachCustom(async () => {
    await teardownNodesWithRedundancy(serviceNodes, waku);
  });

  test("test case", () => {});
});

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

worked with some change regarding global timeout restoring

@fbarbu15 fbarbu15 requested a review from weboko February 14, 2024 13:47
Copy link
Collaborator

@danisharora099 danisharora099 left a comment

Choose a reason for hiding this comment

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

LGTM

@fbarbu15 fbarbu15 merged commit de5be44 into master Feb 15, 2024
10 checks passed
@fbarbu15 fbarbu15 deleted the chore/fix-mocha-hooks-timeouts branch February 15, 2024 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants