File: 01_fake_mysql.t

package info (click to toggle)
libanyevent-dbi-perl 3.04-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 172 kB
  • sloc: perl: 989; makefile: 2
file content (342 lines) | stat: -rw-r--r-- 8,900 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/perl

BEGIN {
   unless ($ENV{PERL_ANYEVENT_DBI_TESTS}) {
      print "1..0 # SKIP env var PERL_ANYEVENT_DBI_TESTS not set\n"; exit;
   }
   eval {
      require DBIx::MyServer;
      require DBIx::MyServer::DBI;
   };
   if ($@) {
      print "1..0 # SKIP these tests require DBIx::MyServer\n"; exit;
   }
}

no warnings;
use List::Util     qw(sum);
use Cwd            qw(abs_path);
use File::Basename qw(dirname);

use AnyEvent::DBI;

my $topdir = dirname abs_path $0;

# fork off a child to be a mysql server
my $server_pid = fork;
unless ($server_pid) {
   exec "$^X $topdir/fake-mysql --config $topdir/myserver.conf";
   die 'exec failed';
}

# the parent is the test script
eval {
   require Test::More;
   import Test::More tests => 33;
};
if ($@) {
   print 'ok 1 # skip this test requires Test::More'."\n";
   exit 0;
}

# wait for server
sleep 1;
my $cv  = AnyEvent->condvar;
my $dbh = new AnyEvent::DBI(
   "dbi:mysql:database=database;host=127.0.0.1;port=23306",'','',
   PrintError => 0,
   timeout    => 2,
   on_error   => sub { },
   on_connect => sub {
      if (!$_[1]) {
         $cv->send($@);
      } else {
         $cv->send();
      }
   },
);
my $connect_error = $cv->recv();
is($connect_error,undef,'on_connect() called without error, fake mysql server is connected');

# issue a query
$cv = AnyEvent->condvar;
$dbh->exec (
   "select a,b,c from rows14 where num=?", 10, sub {
      my ($dbh,$rows, $metadata) = @_;
      if (! $dbh) {
         $cv->send($@);
      }
      else {
         $cv->send(undef,$rows);
      }
   }
);
my ($error, $rows) = $cv->recv();
#print "@$_\n" for @$rows;
is($error,undef,'query returns no errors');
is(scalar @$rows,14,'query found 14 rows');
is(scalar @{$$rows[0]},3,'first row has 3 data');

# issue a query that returns an error
$cv = AnyEvent->condvar;
$dbh->exec (
   "select a,b,c from nosuchtable", sub {
      my ($dbh, $rows, $metadata) = @_;
      if (!$rows) {
         $cv->send($@);
      } else {
         $cv->send(undef,$rows);
      }
   }
);
($error, $rows) = $cv->recv();
is($error,qq{Table 'database.nosuchtable' doesn't exist},"SELECT on non-existant table returns NONFATAL error");

# good query after bad
$cv = AnyEvent->condvar;
$dbh->exec (
   "select a,b,c from rows14 where num=?", 10, sub {
      my ($dbh,$rows, $metadata) = @_;
      if (!$rows) {
         $cv->send($@);
      } else {
         $cv->send(undef,$rows);
      }
   }
);
($error, $rows) = $cv->recv();
#print "@$_\n" for @$rows;
is($error,undef,'good query after bad returns no errors');
is(scalar @$rows,14,'query found 14 rows');
is(scalar @{$$rows[0]},3,'first row has 3 data');

############################################################################
# enque a series of alternating good/bad queries
$cv = AnyEvent->condvar;
my @results = ();
my $num_qry = 0;
my $qrydone = sub {
   my ($dbh,$rows,$metadata) = @_;
   my $err = undef;
   if (!$rows) {
      $err = $@;
   }
   push @results , [$err,$rows];
   if (scalar @results == $num_qry) {
      $cv->send();
   }
};

$dbh->exec ("select a,b,c from nosuchtable1", $qrydone); $num_qry++;
$dbh->exec ("select a,b,c from rows1"       , $qrydone); $num_qry++;
$dbh->exec ("select a,b,c from nosuchtable2", $qrydone); $num_qry++;
$dbh->exec ("select a,b,c from rows2"       , $qrydone); $num_qry++;
$dbh->exec ("select a,b,c from nosuchtable3", $qrydone); $num_qry++;
$dbh->exec ("select a,b,c from rows3"       , $qrydone); $num_qry++;
$dbh->exec ("select a,b,c from nosuchtable4", $qrydone); $num_qry++;
$dbh->exec ("select a,b,c from rows4"       , $qrydone); $num_qry++;

$cv->recv();
for my $r (0..$num_qry-1) {
   my $offset = int($r / 2 )+1;
   if ($r % 2) {
      ok(! defined $results[$r]->[0],'Multi Query Queue: No error on good queries');
      is(scalar @{$results[$r]->[1]},$offset,'Multi Query Queue: Good query got right number of rows');
   } else {
      is(
         $results[$r]->[0],
         qq{Table 'database.nosuchtable$offset' doesn't exist},'Multi Query Queue: Bad query gets correct error'
      );
   }
}

############################################################################

# try to connect to a closed port
# NOTE tcp port 9 is 'discard', hopefully not running
$cv = AnyEvent->condvar;
my $dbh2 = new AnyEvent::DBI(
   "dbi:mysql:database=test;host=127.0.0.1;port=9",'','',
   PrintError => 0,
   timeout    => 3,
   on_error   => sub { },
   on_connect => sub {
      if (!$_[1]) {
         $cv->send($@);
      }
      else {
         $cv->send();
      }
   },
);
$connect_error = $cv->recv();
like($connect_error,qr{can't connect}i,'mysql connect to localhost:9 refused');

# try to connect to a firewalled port
$cv = AnyEvent->condvar;
$dbh2 = new AnyEvent::DBI(
   "dbi:mysql:database=test;host=www.google.com;port=23306",'','',
   timeout    => 3,
   on_error   => sub { },
   on_connect => sub {
      if (!$_[1]) {
         $cv->send($@);
      }
      else {
         $cv->send();
      }
   },
);
$connect_error = $cv->recv();
is($connect_error,'timeout','mysql connect to google port 23306 times out');
undef $dbh2;

# issue a query which times out
$cv = AnyEvent->condvar;
$dbh->exec (
   "select a,b,c from delay10 where num=?", 10, sub {
      my ($dbh,$rows, $metadata) = @_;
      if (! $rows) {
         $cv->send($@);
      }
      else {
         $cv->send(undef,$rows);
      }
   }
);
($error,$rows) = $cv->recv();
is($error,'timeout','timeout fires during long-running query');

# issue a query after a fatal timeout error
$cv = AnyEvent->condvar;
my $start = AnyEvent->now;
my $run   = 10;
my $ran   = 0;
my $fin   = 0;
my $errs  = [];
while ($ran++ < $run) {
   $dbh->exec (
      "select d,e,f,g from rows5 where num=?", 10, sub {
         my ($dbh,$rows, $metadata) = @_;
         if (!$rows) {
            push @$errs, $@;
         }
         if (++$fin == $run) {
            $cv->send();
         }
      }
   );
}
$cv->recv();
ok(AnyEvent->now -$start < 0.0001,'invalid db handle returns from multiple queries immediately');
is (scalar @$errs, 10, 'invalid db handle returned error for all enqueued queries');
is($errs->[0],'no database connection','invalid db handle returns correct error');
undef $dbh;

# check for server process leakage
eval {
   require Proc::Exists;
   import Proc::Exists qw(pexists);
};
my $has_pe = ! $@;
SKIP: {
   skip ( 'This test requires Proc::Exists',4)  unless $has_pe;
   # connect three handles
   $cv  = AnyEvent->condvar;
   my @handles;
   my @handle_errors;
   my $connected =0;
   for (0..2) {
      my $dbh3 = new AnyEvent::DBI(
         "dbi:mysql:database=database;host=127.0.0.1;port=23306",'','',
         PrintError => 0,
         timeout    => 2,
         on_error   => sub { },
         on_connect => sub {
            if (!$_[1]) {
               push @handle_errors, $@;
            }
            if (++$connected == 3) {
               $cv->send();
            }
         },
      );
      push @handles, $dbh3;
   }
   $cv->recv();
   is(scalar @handles,3,'created three handles');
   is(scalar @handle_errors,0,'no errors during handle creation');
   my @pids = map {$_->_server_pid} @handles;
   ok( defined pexists(@pids, {all=>1}),'Found three slave processes');
   undef @handles;

   $cv = AnyEvent->condvar;
   my $cleanup = AnyEvent->timer(after=>0.5,cb=>sub {$cv->send()});
   $cv->recv();
   ok(!defined pexists(@pids, {any=>1}),'All slave processes exited');
}

# connect to the server again
$cv  = AnyEvent->condvar;
$dbh = new AnyEvent::DBI(
   "dbi:mysql:database=database;host=127.0.0.1;port=23306",'','',
   PrintError => 0,
   timeout    => 2,
   on_error   => sub { },
   on_connect => sub {
      if (!$_[1]) {
         $cv->send($@);
      } else {
         $cv->send();
      }
   },
);
$connect_error = $cv->recv();
is($connect_error,undef,'on_connect() called without error, fake mysql server is re-connected');

# End the server and reap it
$cv  = AnyEvent->condvar;
my $server_process_watcher = AnyEvent->child(
   pid => $server_pid,
   cb  => sub {
      $cv->send(@_);
   }
);
kill 2, $server_pid; # 2 is SIGINT, usually
my ($dead_pid,$dead_status)=$cv->recv();
is ($dead_pid,$server_pid,'MySQL Server processess exited');
is ($dead_status,2,'Server exited on our signal');

if (0) {
   # does not seem tor eliably kill all children
sleep 2;

# try to make another query with a down MYSQL server
# issue a query
$cv = AnyEvent->condvar;
$dbh->exec (
   "select x from rows1 where num=?", 10, sub {
      my ($dbh, $rows, $metadata) = @_;
      if (!$rows) {
         $cv->send($@);
      } else {
         $cv->send(undef,$rows);
      }
   }
);

($error, $rows) = $cv->recv();
is($error,'timeout','mysql query to dead server times out');
undef $dbh;
}

END {
   if ($server_pid) {
      # shut down the fake_mysql server
      delete $SIG{CLD};
      kill 15, $server_pid;
      waitpid $server_pid,0;
   }
   exit 0;
}