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

How to fast forward days for testing purpose #1288

Closed
snawaz opened this issue Jun 19, 2022 · 1 comment
Closed

How to fast forward days for testing purpose #1288

snawaz opened this issue Jun 19, 2022 · 1 comment

Comments

@snawaz
Copy link
Contributor

snawaz commented Jun 19, 2022

In our tests, we have some jobs to be executed, say 7d later. How do we fast forward the jobs in the queue so that we can test such scenarios?

Using bull: 3.22.0, we used to do this (written by someone before I joined the team):

async function accelerate(q: Queue<unknown>, deltaMs: number): Promise<void> {
  await new Promise((resolve) => setTimeout(resolve, 300));
  try {
    jest.useFakeTimers("modern").setSystemTime(new Date().getTime() + deltaMs);
    // @ts-expect-error Internal method.
    // eslint-disable-next-line @typescript-eslint/no-unsafe-call
    q.updateDelayTimer();
  } finally {
    jest.useRealTimers();
  }
  await new Promise((resolve) => setTimeout(resolve, 300));
}

This used to work.

Now we're migrating to bullmq: ^1.86.1, so how do we accomplish the same using bullmq?

I"m trying this. But I'm not sure if it's even close to what the above is trying to accomplish! 🤔

async function accelerate(q: Queue<unknown>, deltaMs: number): Promise<void> {
  await new Promise((resolve) => setTimeout(resolve, 300));  // how about this?
  const jobs = await q.getDelayed();                                                                                                                         
  await Promise.all(jobs.map(async (job) => await job.changeDelay(deltaMs)));
  await new Promise((resolve) => setTimeout(resolve, 300));  //how about this?
}
@snawaz
Copy link
Contributor Author

snawaz commented Jun 19, 2022

Turned out. I was almost there. So instead of using deltaMs, I used -deltaMs (i.e use negative value).

async function accelerate(q: Queue<unknown>, deltaMs: number): Promise<void> {
  await new Promise((resolve) => setTimeout(resolve, 300)); // give bullmq time to move jobs 
  const jobs = await q.getDelayed();
  await Promise.all(jobs.map(async (job) => await job.changeDelay(-deltaMs))); // NOTE
  await new Promise((resolve) => setTimeout(resolve, 300)); // give bullmq time to move jobs
}

Well, the sign of deltaMs actually depends on how we pass this value. So here it's:

await accelerate(myQueue, ms("8d")); // I use 1d more so that by this time the job will be executed

@snawaz snawaz closed this as completed Jun 19, 2022
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

No branches or pull requests

1 participant