CheckSocket

NAME

CheckSocket - test if a socket is listening

SYNOPSIS


     use Test;
     use CheckSocket;

     if not check-socket(80, "localhost") {
        skip-all "no web server";
        exit;
     }

     ...

DESCRIPTION

This exports a single function that returns a Bool to indicate whether something is listening on the specified TCP port or UNIX domain socket:

check-socket

multi sub check-socket(Int $port, Str $host = "localhost" --> Bool )
    multi sub check-socket(Str $socket-path --> Bool )

This attempts to connect to the socket specified by $port and $host ( or $socket-path,) and if succesfull will return True otherwise it will catch any exception caused by the attempt and return False. It makes no attempt to report any reason for the failure so means it is probably not useful for network diagnosis, it's primary intent is for tests to be able to determine whether a particular network service is present to test against.

wait-socket

multi sub wait-socket( Int $port, Str $host = "localhost", Int $wait = 1, Int $tries = 3 --> Bool )
    multi sub wait-socket( Str $socket-path, Int $wait = 1, Int $tries = 3 --> Bool )

This attempts to connects to the socket specified by $port and $host (or $socket-path for UNIX domain sockets,) retrying a maximum of $tries times every $wait second and then returning a Bool to indicate whether the server is available as check-socket. This may be useful when you want to start a server asynchronously in some test code and wait for it to be ready to use.

CheckSocket v0.0.9

very simple Raku function to test if a TCP or UNIX domain socket is listening on a given address

Authors

  • Jonathan Stowe

License

Artistic-2.0

Dependencies

Test Dependencies

Provides

  • CheckSocket

Documentation

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.