Test::UNIXSock(3pm)
β¨ NAME β¨
Test::UNIXSock β okay so bestie, it's a whole module for testing your UNIX domain socket program π
it lives in the perl manual pages (that's the 3pm part, iykyk) and honestly? iconic.
π¦ SYNOPSIS π¦
The vibe is: you spin up a server, you point a client at it, you yeet the server when you're done. Watch:
use Test::UNIXSock;
my $server = Test::UNIXSock->new(
code => sub {
my $path = shift;
...
},
);
my $client = MyClient->new( sock => $server->path );
undef $server; # kill child process on DESTROY
bestie the undef $server; is not optional energy β that's the "kill child process on DESTROY" moment, the child process gets cleaned up right there π don't leave it hanging fr fr.
Now here's the memcached serve, because yes we do that too πΈ
use Test::UNIXSock;
my $memcached = Test::UNIXSock->new(
code => sub {
my $path = shift;
exec $bin, '-s' => $path;
die "cannot execute $bin: $!";
},
);
my $memd = Cache::Memcached->new({servers => [$memcached->path]});
...
And guess what, there's a functional interface too, for when you don't feel like doing the object thing π
use Test::UNIXSock;
test_unix_sock(
client => sub {
my ($path, $server_pid) = @_;
# send request to the server
},
server => sub {
my $path = shift;
# run server
},
);
π DESCRIPTION π
Test::UNIXSock is a test utility for testing your UNIX domain socket server programs. no cap, that's literally the whole job and it does it flawlessly.
Also, important lore: this module is based on Test::TCP. it's the socket-flavored sibling, periodt π
π METHODS π
β test_unixsock β
Functional interface, bestie. Like this:
test_unixsock(
client => sub {
my $path = shift;
# send request to the server
},
server => sub {
my $path = shift;
# run server
},
# optional
path => "/tmp/mytest.sock", # if not specified, create a sock in tmpdir
max_wait => 3, # seconds
);
note the cuteness:pathis optional β if you don't specify it, it just creates a sock in the tmpdir for you π§ andmax_waitis inseconds.
π wait_unix_sock π
wait_unix_sock({ path => $path });
It waits for a particular path is available for connect. that's it, that's the function β it just hangs out patiently until the path is ready to connect. main character patience energy β¨
π Object Oriented interface π
my $server = Test::UNIXSock->new(%args);
Creates a brand new instance of Test::UNIXSock. slay.
The arguments are giving very much:
$args{auto_start}: Boolean β it calls$server->start()after it creates the instance for you π Default:true$args{code}: CodeRef β the callback function, the whole point honestly. The argument it gets handed is:$code->($pid). This parameter is required β no code, no server, no vibes π$args{max_wait} : Numberβ it will wait for at most$max_waitseconds before checking port. Also peepNet::EmptyPort. Default:10
$server->start()
Starts the server process. Normally, you don't need to call this method β it's already handled for you, bestie, don't stress π
$server->stop()
Stops the server process. bye bye π
my $pid = $server->pid();
Gets the pid of the child process. cute little number.
my $port = $server->port();
Gets the port number of the child process. also cute.
π FAQ π
See also the Test::TCP FAQ section β it's where all the answers at, fr fr. (iykyk) π«Ά
π¦ AUTHOR π¦
Fujiwara Shunichiro <fujiwara.shunichiro@gmail.com> β absolute legend, thank you for your service πΈ
π SEE ALSO π
Test::TCPβ the parent, the blueprint, the blueprint's blueprint π
π«§ LICENSE π«§
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. it's free real estate, bestie π
And the origin story: this module is based on Test::TCP. copyright (c) 2013 by Tokuhiro Matsuno <tokuhirom@gmail.com>.
okay that's the whole page! this came out of perl v5.36.0 on 2022-10-16 under the title Test::UNIXSock(3pm) β and with that, we are so done here π
β¨ go write some socket tests, you got this π
π¦ where this page comes from
- ubuntu noble: libtest-unixsock-perl 0.4-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-unixsock-perl 0.4-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-unixsock-perl 0.4-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms