Fix Test::SharedFork #64

schwern opened this Issue Sep 11, 2010 · 5 comments

2 participants

Test-More member

Test::SharedFork allows you to fork but retain shared information between processes. It does this by various hacks, mostly freezing and restoring objects in a temp directory.

The TB2 way would be to write a handler which stores

  • the parent pid
  • a temp directory

When it sees an event, the handler acts in one of two ways.

If it's in the child process...

  • writes each event to the temp directory with a millisecond timestamp to keep order

If it's in the parent process...

  • checks the temp directory for events, reads and deletes them, and posts them only to history

Here's a rough sketch of the parent process handler.

sub _parent_event {
    my $self = shift;
    my $ec = shift;

    my @events = and delete the events...

    my $history = $ec->history;
    for my $event (@events) {
        $history->accept_event($event, $ec);


In this way, events in children can be communicated back to the parent.

#190 will have to be done before this will work. All state must reside in the history object for this to work.

Test-More member

(Pulling in @tokuhirom)

@geistteufel and I have been working on a patch for Test::SharedFork using the technique outlined above which more completely stores the test state. It uses the same basic technique as 0.20 is currently using, but uses event handlers to retrieve and store the TB2::History object rather than wrapping methods. This eliminates a small pile of hacky code in SharedFork and makes it much more reliable. You can see the work here:

We're finishing up making it work with Test::Builder::Tester, which swaps out the test environment, as well as proper subtest support. And I'm likely to make it use the existing SharedFork Store object, though the lexical locking is a problem as the lock must span across subroutines.

I'm leaning towards the idea that this should be in TB2 itself, because not just the History but the EventCoordinator and probably the entire TestState should be shared, just like with threads. EventHandlers can't do that. Fork coordination is probably the biggest reason for test authors needing to monkey with the TB2 internals, and it would be nice if they didn't have to do that.

Test-More member

The issue for implementing this inside TB2::TestState is #334

Test-More member

A fix using coordinated_forks has been sent upstream. tokuhirom/Test-SharedFork#5

Test-More member

@tokuhirom merged the pull request. We still need to release Test::More with the shared forking as an alpha and a new Test::SharedFork, but I just want to close this ticket triumphantly right now so I will!

@schwern schwern closed this Apr 20, 2013
@schwernbot schwernbot referenced this issue in Test-More/TB2 Mar 16, 2014

Fix Test::SharedFork #106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment