AnyEven...CPServer(3)
π AnyEvent::MockTCPServer β like, a whole fake TCP server, bestie
Ok so picture this: you're in AnyEvent::MockTCPServer (that's the module, from the User Contributed Perl Documentation crew), and you need a TCP server that just... does exactly what you tell it to. That's the whole vibe. β¨
πΈ VERSION
version 1.172150 β yes she's got a version number, respect her.
π SYNOPSIS
use AnyEvent::MockTCPServer qw/:all/;
my $cv = AnyEvent->condvar;
my $server =
AnyEvent::MockTCPServer->new(connections =>
[
[ # first connection
[ recv => 'HELLO', 'wait for "HELLO"' ],
[ sleep => 0.1, 'wait 0.1s' ],
[ code => sub { $cv->send('done') },
'send "done" with condvar' ],
[ send => 'BYE', 'send "BYE"' ],
# ...
],
[ # second connection
# ...
]],
# ...
);
Literally just read it top to bottom, it's giving "scripted conversation but make it network". π¦
π§ DESCRIPTION
This module hands you a TCP server with a whole set of defined behaviours, and the whole point is testing TCP clients. Specifically: it's meant to be used when you're testing AnyEvent TCP client interfaces. No cap, that's the entire job. π
π METHODS
β¨ new(%parameters)
Builds you a brand-new AnyEvent::MockTCPServer object. The parameter hash can hold values for these keys:
connectionsβ a list reference with one element per expected connection. Each of those elements is another list reference holding action elements. And each action element is a list with an action method name plus whatever arguments that action method takes. By convention, the final argument to the action method should be a description. (Peep the action method descriptions for the other arguments. iykyk.)hostβ the host IP the server should listen on. Default is the IPv4 loopback address,127.0.0.1.portβ the port the server should listen on. Default is to pick a free port.timeoutβ the timeout for IO operations, in seconds. Default is2seconds.on_timeoutβ the callback to call when a timeout occurs. Default is to die with message"server timeout\n".
π listening()
Condvar that gets notified when the mock server is ready. The value you receive is an array reference containing the address and port the server is listening on. π«Ά
β connect_address()
An array reference containing the address and port the server is listening on. This method blocks on the listening() condvar until the server is actually listening.
π connect_host()
The address the server is listening on. Also blocks on the listening() condvar until the server is listening.
πΈ connect_port()
The port the server is listening on. Also blocks on the listening() condvar until the server is listening.
π¦ connect_string()
A string with the address and port the server is listening on, separated by a colon, ':'. Also blocks on the listening() condvar until the server is listening.
π finished_cv()
Condvar that gets notified when the mock server has finished processing all the expected connections. So like, the "we're done here" moment. π
π next_action($handle, $actions)
Internal method, called by the action methods when the server should proceed with the next action. Must be called by any action methods written in subclasses of this class. It's the rule, don't fight it.
π₯ ACTION METHOD ARGUMENTS
These methods (plus any methods added by derived classes) can be used in action lists passed via the constructor's connections parameter. The $handle and $actions arguments should be omitted from the action lists, because the framework supplies them for you. Free labour, honestly. π
π ACTION METHODS
send($handle, $actions, $send, $desc)
Sends the payload $send to the client.
packsend($handle, $actions, $send, $desc)
Sends the payload $send to the client, but first removes whitespace and packs it with 'H*'. This method is equivalent to the send($handle, $actions, $send, $desc) method when you pass it the packed string β except debug messages contain the unpacked strings, which are way easier to read.
recv($handle, $actions, $expect, $desc)
Waits for the data $expect from the client.
recvline($handle, $actions, $expect, $desc)
Waits for a line of data $expect from the client. Go check AnyEvent::Handle for the definition of 'line'. π
packrecv($handle, $actions, $expect, $desc)
Removes whitespace and packs the string $expect with 'H*', then waits for the resulting data from the client. This method is equivalent to the recv($handle, $actions, $expect, $desc) method when you pass it the packed string β except debug messages contain the unpacked strings, which are easier to read.
sleep($handle, $actions, $interval, $desc)
Makes the server sleep for $interval seconds. Nap time, but professional. π΄
code($handle, $actions, $code, $desc)
Makes the server execute the code reference, with the client handle as the first argument.
Heads up bestie: these action methods are the scripted moves your mock server makes. Get the order right in your connections list and it all slays. π₯
π«Ά AUTHOR
Mark Hindess <soft-cpan@temporalanomaly.com>
π COPYRIGHT AND LICENSE
This software is copyright (c) 2014, 2017 by Mark Hindess.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Ok that's the whole page, you ate that. π Generated with perl v5.42.2 on 2026-02-15 β mwah, go write some tests π
β¨
π¦ where this page comes from
- alpine 3.24: perl-anyevent-mocktcpserver-doc 1.172150-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms