Breakup generate tests.
[m6w6/libmemcached] / memcached / t / issue_67.t
1 #!/usr/bin/perl
2
3 use strict;
4 use Test::More tests => 22;
5 use FindBin qw($Bin);
6 use lib "$Bin/lib";
7 use MemcachedTest;
8 use Carp qw(croak);
9
10 use Cwd;
11 my $builddir = getcwd;
12
13 $ENV{'MEMCACHED_PORT_FILENAME'} = "/tmp/ports.$$";
14
15 sub read_ports {
16 my %rv = ();
17 open(my $f, "/tmp/ports.$$") || die("Can't open ports file.");
18 while(<$f>) {
19 my ($type, $port) = split(/:\s+/);
20 $rv{$type} = $port + 0;
21 }
22 unlink "/tmp/ports.$$";
23 return %rv;
24 }
25
26 sub validate_port {
27 my ($name, $got, $expected) = @_;
28 # diag "Wanted $expected, got $got";
29 if ($expected == -1) {
30 ok(!defined($got), "$name expected no port, got $got");
31 } elsif ($expected == 0) {
32 ok($got != 11211, "$name expected random port (got $got)");
33 } else {
34 is($got, $expected, "$name");
35 }
36 }
37
38 sub run_server {
39 my ($args) = @_;
40
41 my $exe = "$builddir/memcached-debug";
42 croak("memcached binary doesn't exist. Haven't run 'make' ?\n") unless -e $exe;
43
44 my $childpid = fork();
45
46 my $root = '';
47 $root = "-u root" if ($< == 0);
48 my $cmd = "$builddir/timedrun 10 $exe $root $args";
49
50 unless($childpid) {
51 exec $cmd;
52 exit; # NOTREACHED
53 }
54
55 for (1..20) {
56 if (-f "/tmp/ports.$$") {
57 return Memcached::Handle->new(pid => $childpid);
58 }
59 select undef, undef, undef, 0.10;
60 }
61 croak "Failed to start server.";
62 }
63
64 sub when {
65 my ($name, $params, $expected_tcp, $expected_udp) = @_;
66
67 my $server = run_server($params);
68 my %ports = read_ports();
69
70 validate_port($name, $ports{'TCP INET'}, $expected_tcp);
71 validate_port($name, $ports{'UDP INET'}, $expected_udp);
72 }
73
74 # Disabling the defaults since it conflicts with a running instance.
75 # when('no arguments', '', 11211, 11211);
76 when('specifying tcp port', '-p 11212', 11212, 11212);
77 when('specifying udp port', '-U 11222', 11222, 11222);
78 when('specifying tcp ephemeral port', '-p -1', 0, 0);
79 when('specifying udp ephemeral port', '-U -1', 0, 0);
80 when('tcp port disabled', '-p 0', -1, -1);
81 when('udp port disabled', '-U 0', -1, -1);
82 when('specifying tcp and udp ports', '-p 11232 -U 11233', 11232, 11233);
83 when('specifying tcp and disabling udp', '-p 11242 -U 0', 11242, -1);
84 when('specifying udp and disabling tcp', '-p -1 -U 11252', 0, 11252);
85 when('specifying tcp and ephemeral udp', '-p 11262 -U -1', 11262, 0);
86 when('specifying udp and ephemeral tcp', '-p -1 -U 11272', 0, 11272);