Test-More / Test2-Harness

Alternative to Test::Harness
Other
23 stars 26 forks source link

dropped diag in subtest + fork #94

Closed aeruder closed 4 years ago

aeruder commented 6 years ago

Still haven't managed to figure out the right fix here, but writing it down so I don't forget about the details or maybe someone else has an idea.

sample test:

use Test2::V0;
use Test2::Tools::Subtest;

ok(1, "before subtest");

my $fork_and_diag = sub {
  my $pid = fork;
  if ($pid) {
    waitpid $pid, 0;
  } else {
    diag "test diag";
    exit(0);
  }
  ok(1, "in subtest");
};

$fork_and_diag->();
subtest_streamed "subtest" => $fork_and_diag;

ok(1, "after subtest");

done_testing;

Output when running test:

% yath test -v t2/diag_in_fork.t
( LAUNCH )  job  1    t2/diag_in_fork.t
(  NOTE  )  job  1    Seeded srand with seed '20181002' from local date.
[  PASS  ]  job  1  + before subtest
(  DIAG  )  job  1    test diag
[  PASS  ]  job  1  + in subtest
(  NOTE  )  job  1    Subtest: subtest
[  PASS  ]  job  1  +~Subtest: subtest
[  PASS  ]  job  1    + in subtest
[  PLAN  ]  job  1    | Expected assertions: 1
            job  1    ^
[  PASS  ]  job  1  + after subtest
[  PLAN  ]  job  1    Expected assertions: 4
[  TIME  ]  job  1    0.12714s on wallclock (0.09 usr 0.02 sys + 0.00 cusr 0.01 csys = 0.12 CPU)
( PASSED )  job  1    t2/diag_in_fork.t

Note that the test diag line only shows up once - it gets skipped when run in a fork + subtest.

exodist commented 4 years ago

The bug here is that the test did not load Test2::IPC which si required for this to work. Add the use Test2::IPC to the test file and it works fine.

exodist commented 4 years ago

To be clear, there is a race condition in the test that almost always produces the result you see, Test2::IPC makes test2 multi-process aware and fixes the race.