Thoughts?

I can override CORE::GLOBAL::time() and I've done this before with a 
closure (ala Time::Mock), but how would one implement accelerated time 
for testing a multi-process program?

I'm also dealing with possibly sleep(), alarm() and other timing issues, 
as well as maybe Time::HiRes::time().  I can probably work my way 
around the sleep() and such.

All of the processes are in perl, starting from fork-opens.

If I use Time::HiRes::time() multiplied by some accelerating factor, 
would one process appear to get ahead of the other?  (A quick test 
shows not, at least on this system with that code.)  I don't have any 
particular sync requirements, but I'm curious if there's prior art or 
know pitfalls here.

  use Time::HiRes ();
  BEGIN {
    my $htime = \&Time::HiRes::time;
    my $time_start = $htime->();
    my $timesub = sub () {
      $time_start + ($htime->() - $time_start) * 10_000;
    };
    *Time::HiRes::time = $timesub;

    *CORE::GLOBAL::time = sub () {
      return(sprintf("%0.0f", $timesub->()));
    };
    # TODO override sleep and T::HR::sleep, etc
  }

I was thinking there needs to be a shared filehandle with a stream of 
time on it similar to the below, but with various time() and sleep() 
methods overridden.  Calls to time() or sleep() would peel-off lines, 
thus keeping everyone in sync.  It becomes impossible for two things to 
happen at the same time (and I think I'm seeing the child blocking the 
parent's reads.)

  use Time::HiRes qw(sleep);
  my $pid = open(my $fh, "-|");
  unless($pid) {
    $| = 1;
    while(1) {
      print Time::HiRes::time(), "\n";
      sleep(0.01);
    }
    exit;
  }
  if(fork) {
    while(my $line = <$fh>) {
      warn "parent $line";
    }
  }
  else {
    while(my $line = <$fh>) {
      warn "child $line";
    }
  }

--Eric
-- 
"But as to modern architecture, let us drop it and let us take
modernistic out and shoot it at sunrise."
--F.L. Wright
---------------------------------------------------
    http://scratchcomputing.com
---------------------------------------------------

Reply via email to