开发者

how to wait on async files in perl

开发者 https://www.devze.com 2023-04-01 09:06 出处:网络
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 async

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 c开发者_StackOverflowlosed 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 {});
                             });
             });


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号