Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

tokuhirom (7396)

tokuhirom
  (email not shown publicly)
http://d.hatena.ne.jp/tokuhirom/

Journal of tokuhirom (7396)

Sunday August 17, 2008
01:30 AM

I wrote Test::TCP

[ #37210 ]

I've wrote Test::TCP today.

If you write tests with Test::TCP, easy to write the test, that using TCP socket.

follow is example code.

        use warnings;
        use strict;

        package MyEchoServer;
        use IO::Socket::INET;

        sub new {
                my ($class, $port) = @_;

                my $sock = IO::Socket::INET->new(
                        LocalPort => $port,
                        LocalAddr => '127.0.0.1',
                        Proto => 'tcp',
                        Listen => 5,
                        Type => SOCK_STREAM,
                ) or die "Cannot open server socket: $!";
                bless { sock => $sock }, $class;
        }

        sub run {
                my $self = shift;

                while (my $remote = $self->{sock}->accept) {
                        while (my $line = ) {
                                print {$remote} $line;
                        }
                }
        }

        package main;
        use strict;
        use warnings;
        use Test::More tests => 1;
        use Test::TCP;
        use IO::Socket::INET;

        test_tcp(
                client => sub {
                        my $port = shift;
                        my $sock = IO::Socket::INET->new(
                                PeerPort => $port,
                                PeerAddr => '127.0.0.1',
                                Proto => 'tcp'
                        ) or die "Cannot open client socket: $!";
                        print {$sock} "foo\n";
                        my $res = ;
                        is $res, "foo\n";
                },
                server => sub {
                        my $port = shift;
                        MyEchoServer->new($port)->run;
                },
        );

and repository is here:

http://svn.coderepos.org/share/lang/perl/Test-TCP/trunk/lib/Test/TCP.pm

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.