3

I am trying to test a logging module which asynchronously writes to a file... the unit test tries to read the log to make sure the written message matches expected. However, I am finding that the asynchronous writes by the module don't reach the file until after the unit tests are finished, even if I sleep to wait on the file for an arbitrary length of time. I verified that the files aren't getting closed until the very end by adding a print statement next to the aio_close. What can I do to test this?

        #approximately the way this works:
        aio_open($pathname,
             $flags,
             $mode,
             sub
             {
                 my $fh = shift;
                 aio_write($fh,
                             0,
                             length($log),
                             $log,
                             0,
                             sub
                             {
                                 print "here";
                                 aio_close($fh, sub {});
                             });
             });
tbischel
  • 6,337
  • 11
  • 51
  • 73

2 Answers2

2

Ok, having dug around a bit, I found I can call...

IO::AIO::flush;

That causes all asynchronous buffers to flush prior to execution of my test.

tbischel
  • 6,337
  • 11
  • 51
  • 73
  • Not exactly. `flush` causes your program to wait for all pending asynchronous actions to be completed. In your case, that includes executing `aio_close`, which is what ultimately flushes your output buffers. – mob Aug 30 '11 at 21:58
1

Have your file writer accept a callback "done_writing" sub.

If supplied, call it.

Have a unit test supply a callback which either creates a touch file, or simply populates a "ready" flag in the unit test.

As an alternate version, have a callback issue a signal, and set up the unit test to sleep with a signal handler for that signal

DVK
  • 126,886
  • 32
  • 213
  • 327
  • I think if I make it automatically flush, it would block (and thus no longer be asynchronous), right? I have considered modifying the logger for the callback... I think that would work. I ended up finding the answer I added. – tbischel Aug 30 '11 at 20:25