AnyEven...CPServer(3)

πŸ“¦ version 1.172150, in alpine 3.24

πŸ’… 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 is 2 seconds.
  • 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>

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