File Coverage

blib/lib/Couchbase/Test/Netfail.pm
Criterion Covered Total %
statement 42 95 44.2
branch 0 4 0.0
condition n/a
subroutine 14 19 73.6
pod 0 5 0.0
total 56 123 45.5


line stmt bran cond sub pod time code
1             package Couchbase::Test::Netfail;
2 2     2   1504 use strict;
  2         4  
  2         84  
3 2     2   12 use warnings;
  2         6  
  2         64  
4 2     2   12 use Test::More;
  2         4  
  2         16  
5 2     2   662 use base qw(Couchbase::Test::Common);
  2         4  
  2         190  
6 2     2   10 use Couchbase::Client::Errors;
  2         2  
  2         256  
7 2     2   10 use Couchbase::MockServer;
  2         12  
  2         40  
8 2     2   10 use Time::HiRes qw(sleep);
  2         2  
  2         18  
9 2     2   172 use Data::Dumper;
  2         4  
  2         92  
10              
11             use Class::XSAccessor {
12 2         20 accessors => [qw(cbo vbconf)]
13 2     2   18 };
  2         4  
14              
15             my $have_vbucket = eval {
16             require Couchbase::Config::UA;
17             require Couchbase::VBucket;
18             die('');
19             1;
20             };
21              
22             if($Couchbase::Test::Common::RealServer) {
23             __PACKAGE__->SKIP_CLASS("Can't perform network failure tests on real cluster");
24             }
25              
26             sub startup_tests :Test(startup)
27             {
28 0     0 0 0 my $self = shift;
29 0         0 $self->mock_init();
30 0 0       0 if($have_vbucket) {
31 0         0 my $confua = Couchbase::Config::UA->new(
32             $self->common_options->{server},
33             username => $self->common_options->{username},
34             password => $self->common_options->{password});
35 0         0 my $pool = $confua->list_pools();
36 0         0 $confua->pool_info($pool);
37 0         0 my $buckets = $confua->list_buckets($pool);
38 0         0 my $bucket = (grep {$_->name eq $self->common_options->{bucket}}
  0         0  
39             @$buckets)[0];
40 0         0 $self->vbconf($bucket->vbconf);
41             }
42            
43 2     2   760 }
  2         6  
  2         12  
44              
45             sub setup_test :Test(setup) {
46 0     0 0 0 my $self = shift;
47 0         0 my $options = $self->common_options("couchbase");
48 0         0 my $cbo = Couchbase::Client->new({
49 0         0 %{$self->common_options("couchbase")},
50             no_init_connect => 1
51             });
52 0         0 $self->cbo($cbo);
53 0         0 alarm(30); #things can hang, so don't wait more than a minute for each
54             #function
55 2     2   744 }
  2         4  
  2         8  
56              
57             sub teardown_test :Test(teardown) {
58 0     0 0 0 alarm(0);
59             #$SIG{ALRM} = 'DEFAULT';
60 2     2   336 }
  2         4  
  2         10  
61              
62             sub T40_tmpfail_basic :Test(no_plan) {
63 0     0 0 0 my $self = shift;
64            
65 0         0 my $cbo = $self->cbo;
66 0         0 my $mock = $self->mock;
67 0         0 my $wv;
68            
69 0         0 note "Suspending mock server";
70 0         0 $mock->suspend_process();
71 0         0 $cbo->timeout(0.5);
72 0         0 ok(!$cbo->connect(), "Connect failed");
73 0         0 my $errors = $cbo->get_errors;
74 0         0 ok(scalar @$errors, "Have connection error");
75 0         0 is($errors->[0]->[0], COUCHBASE_CONNECT_ERROR, "CONNECT_ERROR");
76            
77 0         0 note "Resuming mock server";
78 0         0 $mock->resume_process();
79 0         0 $wv = $cbo->connect();
80 0         0 $cbo->timeout(5);
81            
82 0         0 ok($wv, "Connected ok");
83 0         0 ok($cbo->set("Foo", "foo_value")->is_ok, "set ok");
84 2     2   638 }
  2         4  
  2         8  
85              
86             sub T41_degraded :Test(no_plan) {
87 0     0 0   my $self = shift;
88            
89 0           local $TODO = "CouchbaseMock does not have 'server-down' mode";
90 0           return;
91              
92 0           my $cbo = $self->cbo;
93 0           my $mock = $self->mock;
94            
95 0 0         if(!$have_vbucket) {
96 0           $self->builder->skip("Need Couchbase::VBucket");
97 0           return;
98             }
99            
100 0           my $key = "Foo";
101 0           my $value = "foofoo";
102            
103 0           ok($cbo->connect(), "Connected ok (sanity check)");
104 0           ok($cbo->set($key,$value)->is_ok, "Set (initial, sanity check)");
105            
106            
107 0           my ($server,$idx) = $self->vbconf->map($key);
108 0           $mock->failover_node($idx, $self->common_options->{bucket});
109            
110 0           my $ret = $cbo->set($key, $value);
111 0           is($ret->errnum, COUCHBASE_ETMPFAIL, "got expected error");
112            
113 0           $mock->respawn_node($idx, $self->common_options->{bucket});
114 0           $ret = $cbo->set($key, $value);
115 0           ok($ret->is_ok, "Respawned node, set is OK");
116 2     2   728 }
  2         4  
  2         8  
117              
118             1;