Future-IO-0.11000755001750001750 014165715533 11745 5ustar00leoleo000000000000Future-IO-0.11/Build.PL000444001750001750 102014165715533 13367 0ustar00leoleo000000000000use strict; use warnings; use Module::Build; my $build = Module::Build->new( module_name => 'Future::IO', requires => { 'Future' => 0, 'Struct::Dumb' => 0, 'Time::HiRes' => 0, 'perl' => '5.010', # // }, test_requires => { 'Test::Identity' => 0, 'Test::More' => '0.88', # done_testing }, configure_requires => { 'Module::Build' => '0.4004', # test_requires }, license => 'perl', create_license => 1, create_readme => 1, ); $build->create_build_script; Future-IO-0.11/Changes000444001750001750 422214165715533 13375 0ustar00leoleo000000000000Revision history for Future-IO 0.11 2022-01-07 [CHANGES] * Added ->connect and ->accept for socket-based code * Provide ->HAVE_MULTIPLE_FILEHANDLES for user code to check if a real impl has been loaded * Simplify the default implementation so it can reuse most of the ImplBase class * Wording fix in docs for ->alarm 0.10 2021-08-26 [BUGFIXES] * Skip the unit test about filling pipes with ->syswrite on MSWin32 because the test just hangs there (RT138985) 0.09 2021-08-24 [CHANGES] * Added ->waitpid that real event loop implementations can provide (though built-in default implementation does not provide it) * Extracted impl tests to shareable Test::Future::IO::Impl module * Add links to existing CPAN implementations and test modules 0.08 2020-12-31 [CHANGES] * Added Future::IO->alarm [BUGFIXES] * Avoid CPU-spin in the single-filehandle case on nonblocking filehandles 0.07 2020-03-24 [CHANGES] * Make the ->await method wait until the future instance is ready * Document ->await 0.06 2019-06-26 18:11:54 [CHANGES] * Added Future::IO::ImplBase to assist implementation authors 0.05 2019-05-07 01:00:19 [CHANGES] * Misc small docs fixes [BUGFIXES] * Expect EINVAL on MSWin32 for pipe close write test, rather than EPIPE 0.04 2019-04-27 19:08:40 [CHANGES] * Added ->syswrite and ->syswrite_exactly [BUGFIXES] * Implement ->sysread_exactly test a better way that shouldn't break on Windows due to select() on pipes 0.03 2019-04-25 22:39:55 [CHANGES] * Add ->sysread_exactly 0.02 2019-04-10 14:01:17 [CHANGES] * Provide and document the $IMPL package variable mechanism [BUGFIXES] * Skip t/10sleep+sysread.t on MSWin32 because Windows doesn't support select() on pipes (RT129108) 0.01 2019-04-10 01:44:18 First version, released on an unsuspecting world. Future-IO-0.11/LICENSE000444001750001750 4376214165715533 13143 0ustar00leoleo000000000000This software is copyright (c) 2022 by Paul Evans . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2022 by Paul Evans . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2022 by Paul Evans . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Future-IO-0.11/MANIFEST000444001750001750 53314165715533 13214 0ustar00leoleo000000000000Build.PL Changes lib/Future/IO.pm lib/Future/IO/ImplBase.pm lib/Test/Future/IO/Impl.pm MANIFEST This list of files t/00use.t t/01sleep.t t/02sysread.t t/03sysread_exactly.t t/04syswrite.t t/05syswrite_exactly.t t/06connect.t t/07accept.t t/10sleep+sysread.t t/20impl-override.t t/21impl-before-load.t t/99pod.t README LICENSE META.yml META.json Future-IO-0.11/META.json000444001750001750 253714165715533 13532 0ustar00leoleo000000000000{ "abstract" : "Future-returning IO methods", "author" : [ "Paul Evans " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4231", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Future-IO", "prereqs" : { "configure" : { "requires" : { "Module::Build" : "0.4004" } }, "runtime" : { "requires" : { "Future" : "0", "Struct::Dumb" : "0", "Time::HiRes" : "0", "perl" : "5.010" } }, "test" : { "requires" : { "Test::Identity" : "0", "Test::More" : "0.88" } } }, "provides" : { "Future::IO" : { "file" : "lib/Future/IO.pm", "version" : "0.11" }, "Future::IO::ImplBase" : { "file" : "lib/Future/IO/ImplBase.pm", "version" : "0.11" }, "Test::Future::IO::Impl" : { "file" : "lib/Test/Future/IO/Impl.pm", "version" : "0.11" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ] }, "version" : "0.11", "x_serialization_backend" : "JSON::PP version 4.04" } Future-IO-0.11/META.yml000444001750001750 153614165715533 13360 0ustar00leoleo000000000000--- abstract: 'Future-returning IO methods' author: - 'Paul Evans ' build_requires: Test::Identity: '0' Test::More: '0.88' configure_requires: Module::Build: '0.4004' dynamic_config: 1 generated_by: 'Module::Build version 0.4231, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Future-IO provides: Future::IO: file: lib/Future/IO.pm version: '0.11' Future::IO::ImplBase: file: lib/Future/IO/ImplBase.pm version: '0.11' Test::Future::IO::Impl: file: lib/Test/Future/IO/Impl.pm version: '0.11' requires: Future: '0' Struct::Dumb: '0' Time::HiRes: '0' perl: '5.010' resources: license: http://dev.perl.org/licenses/ version: '0.11' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' Future-IO-0.11/README000444001750001750 2074514165715533 13012 0ustar00leoleo000000000000NAME Future::IO - Future-returning IO methods SYNOPSIS use Future::IO; my $delay = Future::IO->sleep( 5 ); # $delay will become done in 5 seconds time my $input = Future::IO->sysread( \*STDIN, 4096 ); # $input will yield some input from the STDIN IO handle DESCRIPTION This package provides a few basic methods that behave similarly to the same-named core perl functions relating to IO operations, but yield their results asynchronously via Future instances. This is provided primarily as a decoupling mechanism, to allow modules to be written that perform IO in an asynchronous manner to depend directly on this, while allowing asynchronous event systems to provide an implementation of these operations. Default Implementation If the override_impl method is not invoked, a default implementation of these operations is provided. This implementation allows a single queue of sysread or syswrite calls on a single filehandle only, combined with sleep calls. It does not support waitpid. It is provided for the simple cases where modules only need one filehandle (most likely a single network socket or hardware device handle), allowing such modules to work without needing a better event system. If there are both read/write and sleep futures pending, the implementation will use select() to wait for either. This may be problematic on MSWin32, depending on what type of filehandle is involved. For cases where multiple filehandles are required, or for doing more involved IO operations, a real implementation based on an actual event loop should be provided. The following are known to exist; CPAN may provide others: * Future::IO::Impl::Glib * Future::IO::Impl::IOAsync * Future::IO::Impl::UV Unit Testing The replaceable implementation is also useful for writing unit test scripts. If the implementation is set to an instance of some sort of test fixture or mocking object, a unit test can check that the appropriate IO operations happen as part of the test. A testing module which does this is provided by Test::Future::IO. METHODS accept $f = Future::IO->accept( $fh ) Since version 0.11. Returns a Future that will become done when a new connection has been accepted on the given filehandle, which should represent a listen-mode socket. alarm $f = Future::IO->alarm( $epoch ) Since version 0.08. Returns a Future that will become done at a fixed point in the future, given as an epoch timestamp (such as returned by time()). This value may be fractional. connect $f = Future::IO->connect( $fh, $name ) Since version 0.11. Returns a Future that will become done when a connect() has succeeded on the given filehandle to the given sockname address. sleep $f = Future::IO->sleep( $secs ) Returns a Future that will become done a fixed delay from now, given in seconds. This value may be fractional. sysread $f = Future::IO->sysread( $fh, $length ) $bytes = $f->get Returns a Future that will become done when at least one byte can be read from the given filehandle. It may return up to $length bytes. On EOF, the returned future will yield an empty list (or undef in scalar context). On any error (other than EAGAIN / EWOULDBLOCK which are ignored), the future fails with a suitable error message. Note specifically this may perform only a single sysread() call, and thus is not guaranteed to actually return the full length. sysread_exactly $f = Future::IO->sysread_exactly( $fh, $length ) $bytes = $f->get Since version 0.03. Returns a Future that will become done when exactly the given number of bytes have been read from the given filehandle. It returns exactly $length bytes. On EOF, the returned future will yield an empty list (or undef in scalar context), even if fewer bytes have already been obtained. These bytes will be lost. On any error (other than EAGAIN / EWOULDBLOCK which are ignored), the future fails with a suitable error message. This may make more than one sysread() call. syswrite $f = Future::IO->syswrite( $fh, $bytes ) $written_len = $f->get Since version 0.04. Returns a Future that will become done when at least one byte has been written to the given filehandle. It may write up to all of the bytes. On any error (other than EAGAIN / EWOULDBLOCK which are ignored) the future fails with a suitable error message. Note specifically this may perform only a single syswrite() call, and thus is not guaranteed to actually return the full length. syswrite_exactly $f = Future::IO->syswrite_exactly( $fh, $bytes ) $written_len = $f->get Since version 0.04. Returns a Future that will become done when exactly the given bytes have been written to the given filehandle. On any error (other than EAGAIN / EWOULDBLOCK which are ignored) the future fails with a suitable error message. This may make more than one syswrite() call. waitpid $f = Future::IO->waitpid( $pid ) $wstatus = $f->get Since version 0.09. Returns a Future that will become done when the given child process terminates. The future will yield the wait status of the child process. This can be inspected by the usual bitshifting operations as per $?: if( my $termsig = ($wstatus & 0x7f) ) { say "Terminated with signal $termsig"; } else { my $exitcode = ($wstatus >> 8); say "Terminated with exit code $exitcode"; } override_impl Future::IO->override_impl( $impl ) Sets a new implementation for Future::IO, replacing the minimal default internal implementation. This can either be a package name or an object instance reference, but must provide the methods named above. This method is intended to be called by event loops and other similar places, to provide a better integration. Another way, which doesn't involve directly depending on Future::IO or loading it, is to use the $IMPL variable; see below. Can only be called once, and only if the default implementation is not in use, therefore a module that wishes to override this ought to invoke it as soon as possible on program startup, before any of the main Future::IO methods may have been called. HAVE_MULTIPLE_FILEHANDLES $has = Future::IO->HAVE_MULTIPLE_FILEHANDLES Since version 0.11. Returns true if the underlying IO implementation actually supports multiple filehandles. Most real support modules will return true here, but this returns false for the internal minimal implementation. await $f = $f->await Since version 0.07. Blocks until this future is ready (either by success or failure). Does not throw an exception if failed. THE $IMPL VARIABLE Since version 0.02. As an alternative to setting an implementation by using override_impl, a package variable is also available that allows modules such as event systems to opportunistically provide an implementation without needing to depend on the module, or loading it require. Simply directly set that package variable to the name of an implementing package or an object instance. Additionally, implementors may use a name within the Future::IO::Impl:: namespace, suffixed by the name of their event system. For example, something like the following code arrangement is recommended. package Future::IO::Impl::BananaLoop; { no warnings 'once'; ( $Future::IO::IMPL //= __PACKAGE__ ) eq __PACKAGE__ or warn "Unable to set Future::IO implementation to " . __PACKAGE__ . " as it is already $Future::IO::IMPL\n"; } sub sleep { ... } sub sysread { ... } sub syswrite { ... } sub waitpid { ... } Optionally, you can also implement "sysread_exactly" and "syswrite_exactly": sub sysread_exactly { ... } sub syswrite_exactly { ... } If not, they will be emulated by Future::IO itself, making multiple calls to the non-_exactly versions. AUTHOR Paul Evans Future-IO-0.11/lib000755001750001750 014165715533 12513 5ustar00leoleo000000000000Future-IO-0.11/lib/Future000755001750001750 014165715533 13765 5ustar00leoleo000000000000Future-IO-0.11/lib/Future/IO.pm000444001750001750 3473014165715533 15016 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2019-2021 -- leonerd@leonerd.org.uk package Future::IO; use strict; use warnings; use 5.010; # // our $VERSION = '0.11'; use Carp; # These need to be visible to sub override_impl my @alarms; my @readers; my @writers; our $IMPL; =head1 NAME C - Future-returning IO methods =head1 SYNOPSIS use Future::IO; my $delay = Future::IO->sleep( 5 ); # $delay will become done in 5 seconds time my $input = Future::IO->sysread( \*STDIN, 4096 ); # $input will yield some input from the STDIN IO handle =head1 DESCRIPTION This package provides a few basic methods that behave similarly to the same-named core perl functions relating to IO operations, but yield their results asynchronously via L instances. This is provided primarily as a decoupling mechanism, to allow modules to be written that perform IO in an asynchronous manner to depend directly on this, while allowing asynchronous event systems to provide an implementation of these operations. =head2 Default Implementation If the C method is not invoked, a default implementation of these operations is provided. This implementation allows a single queue of C or C calls on a single filehandle only, combined with C calls. It does not support C. It is provided for the simple cases where modules only need one filehandle (most likely a single network socket or hardware device handle), allowing such modules to work without needing a better event system. If there are both read/write and C futures pending, the implementation will use C to wait for either. This may be problematic on MSWin32, depending on what type of filehandle is involved. For cases where multiple filehandles are required, or for doing more involved IO operations, a real implementation based on an actual event loop should be provided. The following are known to exist; CPAN may provide others: =over 4 =item * L =item * L =item * L =back =head2 Unit Testing The replaceable implementation is also useful for writing unit test scripts. If the implementation is set to an instance of some sort of test fixture or mocking object, a unit test can check that the appropriate IO operations happen as part of the test. A testing module which does this is provided by L. =cut =head1 METHODS =cut =head2 accept $f = Future::IO->accept( $fh ) I Returns a L that will become done when a new connection has been accepted on the given filehandle, which should represent a listen-mode socket. =cut sub accept { shift; my ( $fh ) = @_; return ( $IMPL //= "Future::IO::_DefaultImpl" )->accept( $fh ); } =head2 alarm $f = Future::IO->alarm( $epoch ) I Returns a L that will become done at a fixed point in the future, given as an epoch timestamp (such as returned by C). This value may be fractional. =cut sub alarm { shift; my ( $epoch ) = @_; $IMPL //= "Future::IO::_DefaultImpl"; if( $IMPL->can( "alarm" ) ) { return $IMPL->alarm( $epoch ); } else { return $IMPL->sleep( $epoch - Time::HiRes::time() ); } } =head2 connect $f = Future::IO->connect( $fh, $name ) I Returns a L that will become done when a C has succeeded on the given filehandle to the given sockname address. =cut sub connect { shift; my ( $fh, $name ) = @_; return ( $IMPL //= "Future::IO::_DefaultImpl" )->connect( $fh, $name ); } =head2 sleep $f = Future::IO->sleep( $secs ) Returns a L that will become done a fixed delay from now, given in seconds. This value may be fractional. =cut sub sleep { shift; my ( $secs ) = @_; return ( $IMPL //= "Future::IO::_DefaultImpl" )->sleep( $secs ); } =head2 sysread $f = Future::IO->sysread( $fh, $length ) $bytes = $f->get Returns a L that will become done when at least one byte can be read from the given filehandle. It may return up to C<$length> bytes. On EOF, the returned future will yield an empty list (or C in scalar context). On any error (other than C / C which are ignored), the future fails with a suitable error message. Note specifically this may perform only a single C call, and thus is not guaranteed to actually return the full length. =cut sub sysread { shift; my ( $fh, $length ) = @_; return ( $IMPL //= "Future::IO::_DefaultImpl" )->sysread( $fh, $length ); } =head2 sysread_exactly $f = Future::IO->sysread_exactly( $fh, $length ) $bytes = $f->get I Returns a L that will become done when exactly the given number of bytes have been read from the given filehandle. It returns exactly C<$length> bytes. On EOF, the returned future will yield an empty list (or C in scalar context), even if fewer bytes have already been obtained. These bytes will be lost. On any error (other than C / C which are ignored), the future fails with a suitable error message. This may make more than one C call. =cut sub sysread_exactly { shift; my ( $fh, $length ) = @_; $IMPL //= "Future::IO::_DefaultImpl"; if( my $code = $IMPL->can( "sysread_exactly" ) ) { return $IMPL->$code( $fh, $length ); } return _sysread_into_buffer( $IMPL, $fh, $length, '' ); } sub _sysread_into_buffer { my ( $IMPL, $fh, $length, $bytes ) = @_; $IMPL->sysread( $fh, $length - length $bytes )->then( sub { my ( $more ) = @_; return Future->done() if !defined $more; # EOF $bytes .= $more; return Future->done( $bytes ) if length $bytes >= $length; return _sysread_into_buffer( $IMPL, $fh, $length, $bytes ); }); } =head2 syswrite $f = Future::IO->syswrite( $fh, $bytes ) $written_len = $f->get I Returns a L that will become done when at least one byte has been written to the given filehandle. It may write up to all of the bytes. On any error (other than C / C which are ignored) the future fails with a suitable error message. Note specifically this may perform only a single C call, and thus is not guaranteed to actually return the full length. =cut sub syswrite { shift; my ( $fh, $bytes ) = @_; return ( $IMPL //= "Future::IO::_DefaultImpl" )->syswrite( $fh, $bytes ); } =head2 syswrite_exactly $f = Future::IO->syswrite_exactly( $fh, $bytes ) $written_len = $f->get I Returns a L that will become done when exactly the given bytes have been written to the given filehandle. On any error (other than C / C which are ignored) the future fails with a suitable error message. This may make more than one C call. =cut sub syswrite_exactly { shift; my ( $fh, $bytes ) = @_; $IMPL //= "Future::IO::_DefaultImpl"; if( my $code = $IMPL->can( "syswrite_exactly" ) ) { return $IMPL->$code( $fh, $bytes ); } return _syswrite_from_buffer( $IMPL, $fh, $bytes, length $bytes ); } sub _syswrite_from_buffer { my ( $IMPL, $fh, $bytes, $len ) = @_; $IMPL->syswrite( $fh, $bytes )->then( sub { my ( $written_len ) = @_; substr $bytes, 0, $written_len, ""; return Future->done( $len ) if !length $bytes; return _syswrite_from_buffer( $IMPL, $fh, $bytes, $len ); }); } =head2 waitpid $f = Future::IO->waitpid( $pid ) $wstatus = $f->get I Returns a L that will become done when the given child process terminates. The future will yield the wait status of the child process. This can be inspected by the usual bitshifting operations as per C<$?>: if( my $termsig = ($wstatus & 0x7f) ) { say "Terminated with signal $termsig"; } else { my $exitcode = ($wstatus >> 8); say "Terminated with exit code $exitcode"; } =cut sub waitpid { shift; my ( $pid ) = @_; return ( $IMPL //= "Future::IO::_DefaultImpl" )->waitpid( $pid ); } =head2 override_impl Future::IO->override_impl( $impl ) Sets a new implementation for C, replacing the minimal default internal implementation. This can either be a package name or an object instance reference, but must provide the methods named above. This method is intended to be called by event loops and other similar places, to provide a better integration. Another way, which doesn't involve directly depending on C or loading it, is to use the C<$IMPL> variable; see below. Can only be called once, and only if the default implementation is not in use, therefore a module that wishes to override this ought to invoke it as soon as possible on program startup, before any of the main C methods may have been called. =cut my $overridden; sub override_impl { shift; croak "Future::IO implementation is already overridden" if defined $IMPL; croak "Future::IO implementation cannot be set once default is already in use" if @alarms or @readers; ( $IMPL ) = @_; } =head2 HAVE_MULTIPLE_FILEHANDLES $has = Future::IO->HAVE_MULTIPLE_FILEHANDLES I Returns true if the underlying IO implementation actually supports multiple filehandles. Most real support modules will return true here, but this returns false for the internal minimal implementation. =cut sub HAVE_MULTIPLE_FILEHANDLES { return ( $IMPL //= "Future::IO::_DefaultImpl" )->HAVE_MULTIPLE_FILEHANDLES; } package Future::IO::_DefaultImpl; use base qw( Future::IO::ImplBase ); use Carp; use Struct::Dumb qw( readonly_struct ); use Time::HiRes qw( time ); readonly_struct Alarm => [qw( time f )]; readonly_struct Reader => [qw( fh f )]; readonly_struct Writer => [qw( fh f )]; use constant HAVE_MULTIPLE_FILEHANDLES => 0; sub alarm { my $class = shift; return $class->_done_at( shift ); } sub sleep { my $class = shift; return $class->_done_at( time() + shift ); } sub ready_for_read { my $class = shift; my ( $fh ) = @_; croak "This implementation can only cope with a single pending filehandle in ->syread" if @readers and $readers[-1]->fh != $fh; my $f = Future::IO::_DefaultImpl::F->new; push @readers, Reader( $fh, $f ); $f->on_cancel( sub { my $f = shift; my $idx = 0; $idx++ while $idx < @readers and $readers[$idx]->f != $f; splice @readers, $idx, 1, (); }); return $f; } sub ready_for_write { my $class = shift; my ( $fh ) = @_; croak "This implementation can only cope with a single pending filehandle in ->syswrite" if @writers and $writers[-1]->fh != $fh; my $f = Future::IO::_DefaultImpl::F->new; push @writers, Writer( $fh, $f ); $f->on_cancel( sub { my $f = shift; my $idx = 0; $idx++ while $idx < @writers and $writers[$idx]->f != $f; splice @writers, $idx, 1, (); }); return $f; } sub waitpid { croak "This implementation cannot handle waitpid"; } sub _done_at { shift; my ( $time ) = @_; my $f = Future::IO::_DefaultImpl::F->new; # TODO: Binary search my $idx = 0; $idx++ while $idx < @alarms and $alarms[$idx]->time < $time; splice @alarms, $idx, 0, Alarm( $time, $f ); $f->on_cancel( sub { my $self = shift; my $idx = 0; $idx++ while $idx < @alarms and $alarms[$idx]->f != $f; splice @alarms, $idx, 1, (); } ); return $f; } package # hide Future::IO::_DefaultImpl::F; use base qw( Future ); use Time::HiRes qw( time ); sub _await_once { die "Cowardly refusing to sit idle and do nothing" unless @alarms || @readers || @writers; # If we always select() then problematic platforms like MSWin32 would # always break. Instead, we'll only select() if we're waiting on more than # one of alarm, reader, writer. If not we'll just presume the one operation # we're waiting for is definitely ready right now. my $do_select = @alarms || ( @readers && @writers ); my $rready; my $wready; redo_select: if( $do_select ) { my $rvec = ''; vec( $rvec, $readers[0]->fh->fileno, 1 ) = 1 if @readers; my $wvec = ''; vec( $wvec, $writers[0]->fh->fileno, 1 ) = 1 if @writers; my $maxwait; $maxwait = $alarms[0]->time - time() if @alarms; my $ret = select( $rvec, $wvec, undef, $maxwait ); $rready = $ret && @readers && vec( $rvec, $readers[0]->fh->fileno, 1 ); $wready = $ret && @writers && vec( $wvec, $writers[0]->fh->fileno, 1 ); } else { $rready = !!@readers; $wready = !!@writers; } if( $rready ) { ( shift @readers )->f->done; } if( $wready ) { ( shift @writers )->f->done; } my $now = time(); while( @alarms and $alarms[0]->time <= $now ) { ( shift @alarms )->f->done; } } =head2 await $f = $f->await I Blocks until this future is ready (either by success or failure). Does not throw an exception if failed. =cut sub await { my $self = shift; _await_once until $self->is_ready; return $self; } =head1 THE C<$IMPL> VARIABLE I As an alternative to setting an implementation by using L, a package variable is also available that allows modules such as event systems to opportunistically provide an implementation without needing to depend on the module, or loading it C. Simply directly set that package variable to the name of an implementing package or an object instance. Additionally, implementors may use a name within the C namespace, suffixed by the name of their event system. For example, something like the following code arrangement is recommended. package Future::IO::Impl::BananaLoop; { no warnings 'once'; ( $Future::IO::IMPL //= __PACKAGE__ ) eq __PACKAGE__ or warn "Unable to set Future::IO implementation to " . __PACKAGE__ . " as it is already $Future::IO::IMPL\n"; } sub sleep { ... } sub sysread { ... } sub syswrite { ... } sub waitpid { ... } Optionally, you can also implement L and L: sub sysread_exactly { ... } sub syswrite_exactly { ... } If not, they will be emulated by C itself, making multiple calls to the non-C<_exactly> versions. =head1 AUTHOR Paul Evans =cut 0x55AA; Future-IO-0.11/lib/Future/IO000755001750001750 014165715533 14274 5ustar00leoleo000000000000Future-IO-0.11/lib/Future/IO/ImplBase.pm000444001750001750 1011214165715533 16476 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2019-2021 -- leonerd@leonerd.org.uk package Future::IO::ImplBase; use strict; use warnings; our $VERSION = '0.11'; use Errno qw( EAGAIN EWOULDBLOCK EINPROGRESS ); use Socket qw( SOL_SOCKET SO_ERROR ); # connect() yields EWOULDBLOCK on MSWin32 use constant CONNECT_EWOULDBLOCK => ( $^O eq "MSWin32" ); use constant HAVE_MULTIPLE_FILEHANDLES => 1; =head1 NAME C - base class for C implementations =head1 DESCRIPTION This package provides a few utility methods that may help writing actual L implementation classes. It is entirely optional; implementations are not required to use it. =cut =head1 CLASS METHODS =cut =head2 APPLY __PACKAGE__->APPLY Attempts to set the value of the C<$Future::IO::IMPL> variable to the name of the calling package. =cut sub APPLY { my $pkg = shift; no warnings 'once'; ( $Future::IO::IMPL //= $pkg ) eq $pkg or warn "Unable to set Future::IO implementation to $pkg". " as it is already $Future::IO::IMPL\n"; } =head1 DEFAULT METHODS These methods are provided based on lower-level functionallity that the implementing class should provide. =cut =head2 accept Implemented by wrapping C, as L uses. =cut sub accept { my $self = shift; my ( $fh ) = @_; return $self->ready_for_read( $fh )->then( sub { my $accepted = $fh->accept; if( $accepted ) { return Future->done( $accepted ); } else { return Future->fail( "accept: $!\n", accept => $fh, $! ); } } ); } =head2 alarm Implemented by wrapping C. =cut sub alarm { my $self = shift; my ( $time ) = @_; return $self->sleep( $time - Time::HiRes::time() ); } =head2 connect Implemented by wrapping C, as L uses. =cut sub connect { my $self = shift; my ( $fh, $name ) = @_; # We can't use IO::Socket->connect here because # https://github.com/Perl/perl5/issues/19326 my $ret = CORE::connect( $fh, $name ); my $errno = $!; if( $ret ) { return Future->done; } elsif( $errno != EINPROGRESS and !CONNECT_EWOULDBLOCK || $errno != EWOULDBLOCK ) { return Future->fail( "connect: $errno\n", connect => $fh, $errno ); } # not synchronous result return $self->ready_for_write( $fh )->then( sub { $errno = $fh->getsockopt( SOL_SOCKET, SO_ERROR ); if( $errno ) { $! = $errno; return Future->fail( "connect: $!\n", connect => $fh, $! ); } return Future->done; } ); } =head2 sysread Requires a lower-level method $f = $class->ready_for_read( $fh ) which should return a Future that completes when the given filehandle may be ready for reading. =cut sub sysread { my $self = shift; my ( $fh, $length ) = @_; $self->ready_for_read( $fh )->then( sub { my $ret = $fh->sysread( my $buf, $length ); if( $ret ) { return Future->done( $buf ); } elsif( defined $ret ) { # EOF return Future->done(); } elsif( $! == EAGAIN or $! == EWOULDBLOCK ) { # Try again return $self->sysread( $fh, $length ); } else { return Future->fail( "sysread: $!\n", sysread => $fh, $! ); } }); } =head2 syswrite Requires a lower-level method $f = $class->ready_for_write( $fh ) which should return a Future that completes when the given filehandle may be ready for writing. =cut sub syswrite { my $self = shift; my ( $fh, $data ) = @_; return $self->ready_for_write( $fh )->then( sub { my $len = $fh->syswrite( $data ); if( defined $len ) { return Future->done( $len ); } elsif( $! == EAGAIN or $! == EWOULDBLOCK ) { # Try again return $self->syswrite( $fh, $data ); } else { return Future->fail( "syswrite: $!\n", syswrite => $fh, $! ); } }); } =head1 AUTHOR Paul Evans =cut 0x55AA; Future-IO-0.11/lib/Test000755001750001750 014165715533 13432 5ustar00leoleo000000000000Future-IO-0.11/lib/Test/Future000755001750001750 014165715533 14704 5ustar00leoleo000000000000Future-IO-0.11/lib/Test/Future/IO000755001750001750 014165715533 15213 5ustar00leoleo000000000000Future-IO-0.11/lib/Test/Future/IO/Impl.pm000444001750001750 2042314165715533 16630 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2021 -- leonerd@leonerd.org.uk package Test::Future::IO::Impl; use strict; use warnings; our $VERSION = '0.11'; use Test::More; use Test::Builder; use Errno qw( EINVAL EPIPE ); use IO::Handle; use Time::HiRes qw( time ); use Exporter 'import'; our @EXPORT = qw( run_tests ); =head1 NAME C - acceptance tests for C implementations =head1 SYNOPSIS use Test::More; use Test::Future::IO::Impl; use Future::IO; use Future::IO::Impl::MyNewImpl; run_tests 'sleep'; done_testing; =head1 DESCRIPTION This module contains a collection of acceptance tests for implementations of L. =cut =head1 FUNCTIONS =cut my $errstr_EPIPE = do { # On MSWin32 we don't get EPIPE, but EINVAL local $! = $^O eq "MSWin32" ? EINVAL : EPIPE; "$!"; }; my $errstr_ECONNREFUSED = do { local $! = Errno::ECONNREFUSED; "$!"; }; sub time_about(&@) { my ( $code, $want_time, $name ) = @_; my $test = Test::Builder->new; my $t0 = time(); $code->(); my $t1 = time(); my $got_time = $t1 - $t0; $test->ok( $got_time >= $want_time * 0.9 && $got_time <= $want_time * 1.5, $name ) or $test->diag( sprintf "Test took %.3f seconds", $got_time ); } =head2 run_tests run_tests @suitenames Runs a collection of tests against C. It is expected that the caller has already loaded the specific implementation module to be tested against before this function is called. =cut sub run_tests { foreach my $test ( @_ ) { my $code = __PACKAGE__->can( "run_${test}_test" ) or die "Unrecognised test suite name $test"; __PACKAGE__->$code(); } } =head1 TEST SUITES The following test suite names may be passed to the L function: =cut =head2 accept Tests the C<< Future::IO->accept >> method. =cut sub run_accept_test { require IO::Socket::INET; my $serversock = IO::Socket::INET->new( Type => Socket::SOCK_STREAM(), LocalPort => 0, Listen => 1, ) or die "Cannot socket()/listen() - $@"; $serversock->blocking( 0 ); my $f = Future::IO->accept( $serversock ); my $sockname = $serversock->sockname; my $clientsock = IO::Socket::INET->new( Type => Socket::SOCK_STREAM(), ) or die "Cannot socket() - $@"; $clientsock->connect( $sockname ) or die "Cannot connect() - $@"; my $acceptedsock = $f->get; ok( $clientsock->peername eq $acceptedsock->sockname, 'Accepted socket address matches' ); } =head2 connect Tests the C<< Future::IO->connect >> method. =cut sub run_connect_test { require IO::Socket::INET; my $serversock = IO::Socket::INET->new( Type => Socket::SOCK_STREAM(), LocalPort => 0, Listen => 1, ) or die "Cannot socket()/listen() - $@"; my $sockname = $serversock->sockname; # ->connect success { my $clientsock = IO::Socket::INET->new( Type => Socket::SOCK_STREAM(), ) or die "Cannot socket() - $@"; $clientsock->blocking( 0 ); my $f = Future::IO->connect( $clientsock, $sockname ); $f->get; my $acceptedsock = $serversock->accept; ok( $clientsock->peername eq $acceptedsock->sockname, 'Accepted socket address matches' ); } $serversock->close; undef $serversock; # ->connect fails { my $clientsock = IO::Socket::INET->new( Type => Socket::SOCK_STREAM(), ) or die "Cannot socket() - $@"; $clientsock->blocking( 0 ); my $f = Future::IO->connect( $clientsock, $sockname ); ok( !eval { $f->get; 1 }, 'Future::IO->connect fails on closed server' ); is_deeply( [ $f->failure ], [ "connect: $errstr_ECONNREFUSED\n", connect => $clientsock, $errstr_ECONNREFUSED ], 'Future::IO->connect failure' ); } } =head2 sleep Tests the C<< Future::IO->sleep >> method. =cut sub run_sleep_test { my $test = Test::Builder->new; time_about sub { Future::IO->sleep( 0.2 )->get; }, 0.2, 'Future::IO->sleep( 0.2 ) sleeps 0.2 seconds'; time_about sub { my $f1 = Future::IO->sleep( 0.1 ); my $f2 = Future::IO->sleep( 0.3 ); $f1->cancel; $f2->get; }, 0.3, 'Future::IO->sleep can be cancelled'; { my $f1 = Future::IO->sleep( 0.1 ); my $f2 = Future::IO->sleep( 0.3 ); is( $f2->await, $f2, '->await returns Future' ); ok( $f2->is_ready, '$f2 is ready after ->await' ); ok( $f1->is_ready, '$f1 is also ready after ->await' ); } time_about sub { Future::IO->alarm( time() + 0.2 )->get; }, 0.2, 'Future::IO->alarm( now + 0.2 ) sleeps 0.2 seconds'; } =head2 sysread Tests the C<< Future::IO->sysread >> method. =cut sub run_sysread_test { # ->sysread yielding bytes { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; $wr->autoflush(); $wr->print( "BYTES" ); my $f = Future::IO->sysread( $rd, 5 ); is( scalar $f->get, "BYTES", 'Future::IO->sysread yields bytes from pipe' ); } # ->sysread yielding EOF { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; $wr->close; undef $wr; my $f = Future::IO->sysread( $rd, 1 ); is_deeply( [ $f->get ], [], 'Future::IO->sysread yields nothing on EOF' ); } # TODO: is there a nice portable way we can test for an IO error? # ->sysread can be cancelled { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; $wr->autoflush(); $wr->print( "BYTES" ); my $f1 = Future::IO->sysread( $rd, 3 ); my $f2 = Future::IO->sysread( $rd, 3 ); $f1->cancel; is( scalar $f2->get, "BYT", 'Future::IO->sysread can be cancelled' ); } } =head2 syswrite Tests the C<< Future::IO->syswrite >> method. =cut sub run_syswrite_test { # ->syswrite success { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; my $f = Future::IO->syswrite( $wr, "BYTES" ); is( scalar $f->get, 5, 'Future::IO->syswrite yields written count' ); $rd->read( my $buf, 5 ); is( $buf, "BYTES", 'Future::IO->syswrite wrote bytes' ); } # ->syswrite yielding EAGAIN SKIP: { $^O eq "MSWin32" and skip "MSWin32 doesn't do EAGAIN properly", 2; pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; $wr->blocking( 0 ); # Attempt to fill the pipe $wr->syswrite( "X" x 4096 ) for 1..256; my $f = Future::IO->syswrite( $wr, "more" ); ok( !$f->is_ready, '$f is still pending' ); # Now make some space $rd->read( my $buf, 4096 ); is( scalar $f->get, 4, 'Future::IO->syswrite yields written count' ); } # ->syswrite yielding EPIPE { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; $rd->close; undef $rd; local $SIG{PIPE} = 'IGNORE'; my $f = Future::IO->syswrite( $wr, "BYTES" ); ok( !eval { $f->get }, 'Future::IO->syswrite fails on EPIPE' ); is_deeply( [ $f->failure ], [ "syswrite: $errstr_EPIPE\n", syswrite => $wr, $errstr_EPIPE ], 'Future::IO->syswrite failure for EPIPE' ); } # ->syswrite can be cancelled { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; my $f1 = Future::IO->syswrite( $wr, "BY" ); my $f2 = Future::IO->syswrite( $wr, "TES" ); $f1->cancel; is( scalar $f2->get, 3, 'Future::IO->syswrite after cancelled one still works' ); $rd->read( my $buf, 3 ); is( $buf, "TES", 'Cancelled Future::IO->syswrite did not write bytes' ); } } =head2 waitpid Tests the C<< Future::IO->waitpid >> method. =cut sub run_waitpid_test { # pre-exit { defined( my $pid = fork() ) or die "Unable to fork() - $!"; if( $pid == 0 ) { # child exit 3; } Time::HiRes::sleep 0.1; my $f = Future::IO->waitpid( $pid ); is( scalar $f->get, ( 3 << 8 ), 'Future::IO->waitpid yields child wait status for pre-exit' ); } # post-exit { defined( my $pid = fork() ) or die "Unable to fork() - $!"; if( $pid == 0 ) { # child Time::HiRes::sleep 0.1; exit 4; } my $f = Future::IO->waitpid( $pid ); is( scalar $f->get, ( 4 << 8 ), 'Future::IO->waitpid yields child wait status for post-exit' ); } } =head1 AUTHOR Paul Evans =cut 0x55AA; Future-IO-0.11/t000755001750001750 014165715533 12210 5ustar00leoleo000000000000Future-IO-0.11/t/00use.t000444001750001750 25314165715533 13446 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use_ok( "Future::IO" ); use_ok( "Future::IO::ImplBase" ); use_ok( "Test::Future::IO::Impl" ); done_testing; Future-IO-0.11/t/01sleep.t000444001750001750 21414165715533 13760 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use Test::Future::IO::Impl; use Future::IO; run_tests 'sleep'; done_testing; Future-IO-0.11/t/02sysread.t000444001750001750 21614165715533 14325 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use Test::Future::IO::Impl; use Future::IO; run_tests 'sysread'; done_testing; Future-IO-0.11/t/03sysread_exactly.t000444001750001750 172014165715533 16100 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use IO::Handle; use Future::IO; # ->sysread_exactly yielding bytes { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; $wr->autoflush(); $wr->print( "BYTES" ); my $f = Future::IO->sysread_exactly( $rd, 5 ); my @read; no warnings 'redefine'; local *IO::Handle::sysread = sub { my ( $fh, undef, $len ) = @_; push @read, $len; return CORE::sysread( $fh, $_[1], 1 ); }; is( scalar $f->get, "BYTES", 'Future::IO->sysread_exactly eventually yields all the bytes' ); is_deeply( \@read, [ 5, 4, 3, 2, 1 ], 'IO::Handle->sysread override worked' ); } # ->sysread_exactly yielding EOF { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; $wr->autoflush(); my $f = Future::IO->sysread_exactly( $rd, 5 ); $wr->print( "BY" ); $wr->close; undef $wr; is_deeply( [ $f->get ], [], 'Future::IO->sysread_exactly yields nothing on EOF' ); } done_testing; Future-IO-0.11/t/04syswrite.t000444001750001750 21714165715533 14547 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use Test::Future::IO::Impl; use Future::IO; run_tests 'syswrite'; done_testing; Future-IO-0.11/t/05syswrite_exactly.t000444001750001750 134214165715533 16321 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use IO::Handle; use Future::IO; # ->syswrite_exactly writing bytes { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; my $f = Future::IO->syswrite_exactly( $wr, "ABC" ); my @written; no warnings 'redefine'; local *IO::Handle::syswrite = sub { my ( $fh, $bytes ) = @_; push @written, $bytes; return CORE::syswrite( $fh, substr $bytes, 0, 1 ); }; is( scalar $f->get, 3, 'Future::IO->syswrite_exactly eventually writes all bytes' ); is_deeply( \@written, [ "ABC", "BC", "C" ], 'IO::Handle->syswrite override worked' ); $rd->read( my $buf, 3 ); is( $buf, "ABC", 'Future::IO->syswrite_exactly wrote bytes' ); } done_testing; Future-IO-0.11/t/06connect.t000444001750001750 21614165715533 14310 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use Test::Future::IO::Impl; use Future::IO; run_tests 'connect'; done_testing; Future-IO-0.11/t/07accept.t000444001750001750 21514165715533 14116 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use Test::Future::IO::Impl; use Future::IO; run_tests 'accept'; done_testing; Future-IO-0.11/t/10sleep+sysread.t000444001750001750 147314165715533 15456 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use IO::Handle; use Future::IO; plan skip_all => "Cannot select() on pipes on Windows" if $^O eq "MSWin32"; # sleep + sysread IO ready { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; $wr->autoflush(); $wr->print( "BYTES" ); my $f = Future->needs_any( Future::IO->sysread( $rd, 5 ), Future::IO->sleep( 2 ), ); is( scalar $f->get, "BYTES", 'Future::IO ->sleep ->sysread concurrently yields bytes' ); } # sleep + sysread timeout { pipe my ( $rd, $wr ) or die "Cannot pipe() - $!"; my $f = Future->needs_any( Future::IO->sysread( $rd, 5 ), Future::IO->sleep( 0.2 )->then_done( "timeout" ), ); is( scalar $f->get, "timeout", 'Future::IO ->sleep ->sysread concurrently yields timeout' ); } done_testing; Future-IO-0.11/t/20impl-override.t000444001750001750 143114165715533 15451 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use Test::Identity; use Future::IO; my @impl_args; my $impl_f; { package TestImplementation; sub sleep { shift; @impl_args = @_; return $impl_f; } sub sysread { shift; @impl_args = @_; return $impl_f; } } Future::IO->override_impl( "TestImplementation" ); # sleep { $impl_f = Future->new; my $f = Future::IO->sleep( 5 ); is_deeply( \@impl_args, [ 5 ], '->sleep args' ); identical( $f, $impl_f, '->sleep return' ); $f->cancel; } # sysread { $impl_f = Future->new; my $f = Future::IO->sysread( "FH", 1024 ); is_deeply( \@impl_args, [ "FH", 1024 ], '->sysread args' ); identical( $f, $impl_f, '->sysread return' ); $f->cancel; } done_testing; Future-IO-0.11/t/21impl-before-load.t000444001750001750 51714165715533 15776 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; use Test::Identity; no warnings 'once'; $Future::IO::IMPL = "TestImplementation"; require Future::IO; { package TestImplementation; sub sleep { return "TestFuture" } } is( Future::IO->sleep(123), "TestFuture", 'override before require still works' ); done_testing; Future-IO-0.11/t/99pod.t000444001750001750 25714165715533 13462 0ustar00leoleo000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok();