• Stars
    star
    33
  • Rank 761,623 (Top 16 %)
  • Language
    Perl
  • License
    Other
  • Created about 15 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Test::TCP for perl

NAME

Test::TCP - testing TCP program

SYNOPSIS

use Test::TCP;

my $server = Test::TCP->new(
    listen => 1,
    code => sub {
        my $socket = shift;
        ...
    },
);
my $client = MyClient->new(host => '127.0.0.1', port => $server->port);
undef $server; # kill child process on DESTROY

If using a server that can only accept a port number, e.g. memcached:

use Test::TCP;

my $memcached = Test::TCP->new(
    code => sub {
        my $port = shift;

        exec $bin, '-p' => $port;
        die "cannot execute $bin: $!";
    },
);
my $memd = Cache::Memcached->new({servers => ['127.0.0.1:' . $memcached->port]});
...

N.B.: This is vulnerable to race conditions, if another process binds to the same port after Net::EmptyPort found it available.

And functional interface is available:

use Test::TCP;
test_tcp(
    listen => 1,
    client => sub {
        my ($port, $server_pid) = @_;
        # send request to the server
    },
    server => sub {
        my $socket = shift;
        # run server, calling $socket->accept
    },
);

test_tcp(
    client => sub {
        my ($port, $server_pid) = @_;
        # send request to the server
    },
    server => sub {
        my $port = shift;
        # run server, binding to $port
    },
);

DESCRIPTION

Test::TCP is a test utility to test TCP/IP-based server programs.

METHODS

  • test_tcp

    Functional interface.

      test_tcp(
          listen => 1,
          client => sub {
              my $port = shift;
              # send request to the server
          },
          server => sub {
              my $socket = shift;
              # run server
          },
          # optional
          host => '127.0.0.1', # specify '::1' to test using IPv6
          port => 8080,
          max_wait => 3, # seconds
      );
    

    If listen is false, server is instead passed a port number that was free before it was called.

  • wait_port

      wait_port(8080);
    

    Waits for a particular port is available for connect.

Object Oriented interface

  • my $server = Test::TCP->new(%args);

    Create new instance of Test::TCP.

    Arguments are following:

    • $args{auto_start}: Boolean

      Call $server->start() after create instance.

      Default: true

    • $args{code}: CodeRef

      The callback function. Argument for callback function is: $code->($socket) or $code->($port), depending on the value of listen.

      This parameter is required.

    • $args{max_wait} : Number

      Will wait for at most $max_wait seconds before checking port.

      See also Net::EmptyPort.

      Default: 10

    • $args{listen} : Boolean

      If true, open a listening socket and pass this to the callback. Otherwise find a free port and pass the number of it to the callback.

  • $server->start()

    Start the server process. Normally, you don't need to call this method.

  • $server->stop()

    Stop the server process.

  • my $pid = $server->pid();

    Get the pid of child process.

  • my $port = $server->port();

    Get the port number of child process.

FAQ

  • How to invoke two servers?

    You can call test_tcp() twice!

      test_tcp(
          client => sub {
              my $port1 = shift;
              test_tcp(
                  client => sub {
                      my $port2 = shift;
                      # some client code here
                  },
                  server => sub {
                      my $port2 = shift;
                      # some server2 code here
                  },
              );
          },
          server => sub {
              my $port1 = shift;
              # some server1 code here
          },
      );
    

    Or use the OO interface instead.

      my $server1 = Test::TCP->new(code => sub {
          my $port1 = shift;
          ...
      });
      my $server2 = Test::TCP->new(code => sub {
          my $port2 = shift;
          ...
      });
    
      # your client code here.
      ...
    
  • How do you test server program written in other languages like memcached?

    You can use exec() in child process.

      use strict;
      use warnings;
      use utf8;
      use Test::More;
      use Test::TCP 1.08;
      use File::Which;
    
      my $bin = scalar which 'memcached';
      plan skip_all => 'memcached binary is not found' unless defined $bin;
    
      my $memcached = Test::TCP->new(
          code => sub {
              my $port = shift;
    
              exec $bin, '-p' => $port;
              die "cannot execute $bin: $!";
          },
      );
    
      use Cache::Memcached;
      my $memd = Cache::Memcached->new({servers => ['127.0.0.1:' . $memcached->port]});
      $memd->set(foo => 'bar');
      is $memd->get('foo'), 'bar';
    
      done_testing;
    
  • How do I use address other than "127.0.0.1" for testing?

    You can use the host parameter to specify the bind address.

      # let the server bind to "0.0.0.0" for testing
      test_tcp(
          client => sub {
              ...
          },
          server => sub {
              ...
          },
          host => '0.0.0.0',
      );
    
  • How should I write IPv6 tests?

    You should use the "can_bind" in Net::EmptyPort function to check if the program can bind to the loopback address of IPv6, as well as the host parameter of the "test_tcp" function to specify the same address as the bind address.

      use Net::EmptyPort qw(can_bind);
    
      plan skip_all => "IPv6 not available"
          unless can_bind('::1');
    
      test_tcp(
          client => sub {
              ...
          },
          server => sub {
              ...
          },
          host => '::1',
      );
    

AUTHOR

Tokuhiro Matsuno [email protected]

THANKS TO

kazuhooku

dragon3

charsbar

Tatsuhiko Miyagawa

lestrrat

SEE ALSO

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

More Repositories

1

plenv

Perl binary manager
Shell
500
star
2

Amon

yet another web application framework
Perl
160
star
3

java-handbook

137
star
4

git-xlsx-textconv

Go
132
star
5

akaza

Yet another Japanese IME for IBus/Linux
Rust
127
star
6

Furl

pretty fast http client library for perl5
Perl
103
star
7

Minilla

Authorizing tool for CPAN modules
Perl
97
star
8

Perl-Build

Perl
77
star
9

spring-vue-sample

Java
71
star
10

p6-Crust

PSGI library stack for Perl6
Raku
66
star
11

node-perl

Node perl wrapper
C++
63
star
12

toydi

Java
63
star
13

cpan-outdated

detect outdated CPAN modules
Perl
52
star
14

tora

Tora! Tora! Tora!
Perl
51
star
15

avans

Tiny thin web application framework for Java 8
Java
50
star
16

js-xlsx-demo

JavaScript
50
star
17

SQL-Maker

Perl
46
star
18

jawiki-kana-kanji-dict

Generate SKK/MeCab dictionary from Wikipedia(Japanese edition)
Python
40
star
19

node-mruby

C++
40
star
20

Daiku

Yet another build tool on Perl5
Perl
38
star
21

teng-handbook

Perl
38
star
22

mobirc

IRC Gateway for MobilePhone/iPhone/PC
JavaScript
34
star
23

jsref

JavaScript
33
star
24

re2c-lemon-tutorial

C++
32
star
25

p5-router-simple

simple http router
Perl
32
star
26

tinyorm

Tiny O/R mapper for Java
Java
29
star
27

p5-psgiref

(DEPRECATED)just a prototype!
Perl
28
star
28

Web-Query

Perl
27
star
29

OrePAN

Perl
26
star
30

obsidian-2hop-links-plugin

TypeScript
26
star
31

optimize-perl-doc

how to optimize your perl code?
Perl
26
star
32

strftime-js

JavaScript
25
star
33

frepan

freshness mirror of cpan viewer
JavaScript
25
star
34

menta

General extlib/ for CGI applications.
Perl
25
star
35

PJP

Perl
24
star
36

micro_dispatcher.js

JavaScript
22
star
37

data-model-tutorial

the tutorial documents for Data::Model
Perl
22
star
38

OrePAN2

Perl
22
star
39

hoshipad

JavaScript
22
star
40

FormValidator-Lite

very lite and fast validation library for perl
Perl
21
star
41

go-examples

my Golang examples repo.
Go
21
star
42

THWebViewController

Minimalistic WebViewController
Objective-C
20
star
43

go-hsperfdata

Quite fast jps/jstat
Go
20
star
44

Test-Pretty

Perl
20
star
45

p5-http-server-fast

(DEPRECATED)
C++
18
star
46

p6-HTTP-Server-Tiny

Web application server for Perl6
Perl 6
18
star
47

gearman-stat.psgi

Display gearman stats.
18
star
48

visualwidth-js

JavaScript
18
star
49

Router-Boom

Perl
18
star
50

obsidian-pomodoro-plugin

TypeScript
17
star
51

Archer

Perl
17
star
52

nanobench

Tiny benchmarking framework for Java 7+
Java
17
star
53

nanowww

C++ lightweight, fast, portable HTTP client library
Perl
17
star
54

mouse

Moose minus the antlers
Perl
16
star
55

http-session

http session management library for perl
Perl
16
star
56

www-mobilecarrierjp

WWW::MobileCarrierJP
Perl
16
star
57

Tiffany

Template-For-All, Generic interface for perl template engines.
Perl
16
star
58

HTML-TreeBuilder-LibXML

drop-in-replacement for HTML::TreeBuilder::XPath
Perl
16
star
59

DBIx-Inspector

Perl
16
star
60

w3m

my private repo of w3m
C
15
star
61

File-Zglob

Perl
15
star
62

madeye

simple infrastructure monitoring tool
Perl
15
star
63

Smart-Args

the new args.pm!
Perl
15
star
64

Test-Kantan

Perl
14
star
65

Caroline

Yet another line editing library for Perl5
Perl
14
star
66

p5-net-drizzle

libdrizzle bindings for perl5
C
14
star
67

perl-echo-servers

C
14
star
68

gearman-starter.pl

bootstrap script for gearman worker
Perl
13
star
69

MySQL-BinLog

Perl
13
star
70

obsidian-stopwatch-plugin

TypeScript
13
star
71

MetaCPANExplorer

CSS
13
star
72

node-tcc

C++
13
star
73

HTML-Pictogram-MobileJp

[emoji:1] ใฟใŸใ„ใชใฎใ‚’ๅ‡ฆ็†ใงใใ‚‹ไบบ
Perl
13
star
74

App-watcher

Perl
13
star
75

tinyvalidator

Tiny validation library for Java 8
Java
13
star
76

Text-Markdown-Hoedown

Perl
13
star
77

java-samples

Java
12
star
78

nanotap

yet another tap header library
HTML
12
star
79

App-scan_prereqs_cpanfile

Scan prerequisite modules and generate CPANfile
Perl
12
star
80

MojaMoja

(PoC)yet another sinatra-ish framework built on CPAN modules
Perl
12
star
81

Test-SharedFork

Test::SharedFork
Perl
12
star
82

p5-cgi-emulate-psgi

CGI::Emulate::PSGI
Perl
11
star
83

p5-anyevent-mprpc

MessagePack RPC component for AnyEvent!
Perl
11
star
84

mRuby.pm

Perl
11
star
85

devel-bindpp

Devel::BindPP makes writing perl extension easily
C++
11
star
86

Test-Power

Perl
11
star
87

regexp-trie

Regexp::Trie for Java7
Java
11
star
88

Sub-Retry

Perl
11
star
89

blosxom.psgi

cho45 style blosxom thing for psgi. see http://coderepos.org/share/wiki/BlosxomClones
10
star
90

cgi-extlib-perl

General extlib/ for Perl CGI applications.
Perl
10
star
91

p5-module-install-forc

M::I extension for standalone C program/library
Perl
10
star
92

DBIx-Kohada

Perl
10
star
93

Amon2-Lite

Perl
10
star
94

autolink.js

JavaScript
10
star
95

Module-Spy

Perl
10
star
96

p6-WebSocket

Perl 6
10
star
97

Harriet

Perl
10
star
98

Cache-KyotoTycoon

KyotoTycoon client library for Perl5
Perl
10
star
99

mindcheese

Editable mindmap library written in TypeScript
TypeScript
10
star
100

Perl-Lexer

C
10
star