t/issue_67: default UPD port is 0 (off)
#!/usr/bin/perl
use strict;
-use Test::More tests => 22;
+use Test::More tests => 24;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;
use Carp qw(croak);
+use Socket qw(sockaddr_in INADDR_ANY PF_INET SOCK_STREAM);
use Cwd;
my $builddir = getcwd;
@@ -29,12 +30,25 @@ sub validate_port {
if ($expected == -1) {
ok(!defined($got), "$name expected no port, got $got");
} elsif ($expected == 0) {
- ok($got != 11211, "$name expected random port (got $got)");
+ ok(defined($got) && $got != 11211, "$name expected random port (got $got)");
} else {
is($got, $expected, "$name");
}
}
+sub skip_if_default_addr_in_use(&) {
+ my ($block) = @_;
+
+ socket(my $socket, PF_INET, SOCK_STREAM, 0) or die $!;
+ my $addr_in_use = !bind($socket, sockaddr_in(11211, INADDR_ANY));
+ close($socket);
+
+ SKIP: {
+ skip 'Default address is in use. Do you have a running instance?', 2 if $addr_in_use;
+ return $block->();
+ }
+}
+
sub run_server {
my ($args) = @_;
@@ -75,14 +89,13 @@ sub when {
validate_port($name, $ports{'UDP INET'}, $expected_udp);
}
-# Disabling the defaults since it conflicts with a running instance.
-# when('no arguments', '', 11211, 11211);
+skip_if_default_addr_in_use { when('no arguments', '', 11211, -1) };
when('specifying tcp port', '-p 11212', 11212, -1);
when('specifying udp port', '-U 11222', 11222, 11222);
-when('specifying tcp ephemeral port', '-p -1', 0, 0);
+when('specifying tcp ephemeral port', '-p -1', 0, -1);
when('specifying udp ephemeral port', '-U -1', 0, 0);
when('tcp port disabled', '-p 0', -1, -1);
-when('udp port disabled', '-U 0', 11211, -1);
+skip_if_default_addr_in_use { when('udp port disabled', '-U 0', 11211, -1) };
when('specifying tcp and udp ports', '-p 11232 -U 11233', 11232, 11233);
when('specifying tcp and disabling udp', '-p 11242 -U 0', 11242, -1);
when('specifying udp and disabling tcp', '-p -1 -U 11252', 0, 11252);
