help icon indicating copy to clipboard operation
help copied to clipboard

any showcase for node:test `concurrency` options?

Open atian25 opened this issue 2 years ago • 5 comments

Details

as https://nodejs.org/dist/latest-v18.x/docs/api/test.html#testname-options-fn docs says, subtests could run concurrency.

when try as:

test('multi level test', { concurrency: 2 }, async (t) => {
  await t.test('subtest 1', async () => {
    console.log('1');
    await setTimeout(2000);
    console.log('11');
  });

  await t.test('subtest 2', async () => {
    console.log('2');
    await setTimeout(1000);
    console.log('22');
  });
});

itdon't work, since each subtests is awaited.


but if write test like below:

test('multi level test', { concurrency: 2 }, async (t) => {
  t.test('subtest 1', async () => {
    console.log('1');
    await setTimeout(2000);
    console.log('11');
  });

  t.test('subtest 2', async () => {
    console.log('2');
    await setTimeout(1000);
    console.log('22');
  });
});

got error:

not ok 1 - subtest 1
      ---
      duration_ms: 0.001728458
      failureType: 'cancelledByParent'
      error: "'test did not finish before its parent and was cancelled'"
      code: ERR_TEST_FAILURE
      ...

Node.js version

18.0.0

Example code

No response

Operating system

macOS

Scope

test

Module and version

Not applicable.

atian25 avatar Apr 22 '22 15:04 atian25

@targos

atian25 avatar Jul 19 '22 07:07 atian25

Hi. concurrency can be used with Promise.all:

test('multi level test', { concurrency: 2 }, async (t) => {
	await Promise.all([
		t.test('subtest 1', async () => {
	      console.log('1');
	      await setTimeout(2000);
	      console.log('11');
	    });
	  
	    t.test('subtest 2', async () => {
	      console.log('2');
	      await setTimeout(1000);
	      console.log('22');
	    });
	]);
});

or with describe:

descrie('multi level test', { concurrency: 2 }, async () => {
	it('subtest 1', async () => {
      console.log('1');
      await setTimeout(2000);
      console.log('11');
    });
  
    it('subtest 2', async () => {
      console.log('2');
      await setTimeout(1000);
      console.log('22');
    });
});

MoLow avatar Jul 20 '22 07:07 MoLow

I cannot get it work, with the following code:

const test = require('node:test')
const assert = require('node:assert/strict')

const wait = (ms) => new Promise((resolve) => setTimeout(() => resolve(), ms))

const input = [{
  expected: 2,
  actual: 2
}, {
  expected: 'ola',
  actual: 'ciao'
}]

input.forEach((el, index) => {
  test('should work', { concurrency: true }, async (t) => {
    console.log(`start-${index}`)
    await wait(3000)

    assert.strictEqual(el.actual, el.expected)
    console.log(`end-${index}`)
  })
})

node version: v20.9.0

I'm running it with

const { junit } = require('node:test/reporters')
const { run } = require('node:test')
const path = require('node:path')

run({ concurrency: true, files: [path.resolve(__dirname, './test-suite-1.js')] })
  .compose(junit)
  .pipe(process.stdout)

bitliner avatar Nov 16 '23 13:11 bitliner

Hi. concurrency can be used with Promise.all:

test('multi level test', { concurrency: 2 }, async (t) => {
	await Promise.all([
		t.test('subtest 1', async () => {
	      console.log('1');
	      await setTimeout(2000);
	      console.log('11');
	    });
	  
	    t.test('subtest 2', async () => {
	      console.log('2');
	      await setTimeout(1000);
	      console.log('22');
	    });
	]);
});

or with describe:

descrie('multi level test', { concurrency: 2 }, async () => {
	it('subtest 1', async () => {
      console.log('1');
      await setTimeout(2000);
      console.log('11');
    });
  
    it('subtest 2', async () => {
      console.log('2');
      await setTimeout(1000);
      console.log('22');
    });
});

with the first showcase, { concurrency: 2 } seems to be useless due to it can't affect Promise.all()

atian25 avatar Nov 17 '23 02:11 atian25

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar May 16 '24 01:05 github-actions[bot]

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar Jun 15 '24 01:06 github-actions[bot]