ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-DBI/t/01_fake_mysql.t
Revision: 1.1
Committed: Tue Jun 2 16:16:03 2009 UTC (15 years ago) by root
Content type: application/x-troff
Branch: MAIN
Log Message:
big patch by adam

File Contents

# User Rev Content
1 root 1.1 #!/usr/bin/perl
2     BEGIN {
3     my $server_pid;
4     eval {
5     require DBIx::MyServer;
6     require DBIx::MyServer::DBI;
7     };
8     if ($@) {
9     require Test::More;
10     import Test::More skip_all => 'these tests require DBIx::MyServer';
11     exit 0;
12     }
13     }
14    
15     use List::Util qw( sum );
16     use Cwd qw( abs_path );
17     use File::Basename qw( dirname );
18     use AnyEvent::DBI;
19     my $topdir = dirname(abs_path($0));
20    
21     # fork off a child to be a mysql server
22     my $server_pid = fork;
23     if (! $server_pid ) {
24     exec "$^X $topdir/fake-mysql --config $topdir/myserver.conf";
25     die 'exec failed';
26     }
27    
28     # the parent is the test script
29     eval {
30     require Test::More;
31     import Test::More tests => 34;
32     };
33     if ($@) {
34     print 'ok 1 # skip this test requires Test::More'."\n";
35     exit 0;
36     }
37    
38     # wait for server
39     sleep 1;
40     my $cv = AnyEvent->condvar;
41     my $dbh = new AnyEvent::DBI(
42     "dbi:mysql:database=database;host=127.0.0.1;port=23306",'','',
43     PrintError => 0,
44     timeout => 2,
45     on_error => sub { },
46     on_connect => sub {
47     if (! $_[0]) {
48     $cv->send($@);
49     }
50     else {
51     $cv->send();
52     }
53     },
54     );
55     my $connect_error = $cv->recv();
56     is($connect_error,undef,'on_connect() called without error, fake mysql server is connected');
57    
58     # issue a query
59     $cv = AnyEvent->condvar;
60     $dbh->exec (
61     "select a,b,c from rows14 where num=?", 10, sub {
62     my ($dbh,$rows, $metadata) = @_;
63     if (! $dbh) {
64     $cv->send($@);
65     }
66     else {
67     $cv->send(undef,$rows);
68     }
69     }
70     );
71     my ($error, $rows) = $cv->recv();
72     #print "@$_\n" for @$rows;
73     is($error,undef,'query returns no errors');
74     is(scalar @$rows,14,'query found 14 rows');
75     is(scalar @{$$rows[0]},3,'first row has 3 data');
76    
77     # issue a query that returns an error
78     $cv = AnyEvent->condvar;
79     $dbh->exec (
80     "select a,b,c from nosuchtable", sub {
81     my ($dbh,$rows, $metadata) = @_;
82     if (! $dbh) {
83     $cv->send($@);
84     }
85     else {
86     $cv->send(undef,$rows);
87     }
88     }
89     );
90     ($error, $rows) = $cv->recv();
91     is($error,qq{Table 'database.nosuchtable' doesn't exist},'SELECT on non-existant table returns NONFATAL error');
92    
93     # good query after bad
94     $cv = AnyEvent->condvar;
95     $dbh->exec (
96     "select a,b,c from rows14 where num=?", 10, sub {
97     my ($dbh,$rows, $metadata) = @_;
98     if (! $dbh) {
99     $cv->send($@);
100     }
101     else {
102     $cv->send(undef,$rows);
103     }
104     }
105     );
106     ($error, $rows) = $cv->recv();
107     #print "@$_\n" for @$rows;
108     is($error,undef,'good query after bad returns no errors');
109     is(scalar @$rows,14,'query found 14 rows');
110     is(scalar @{$$rows[0]},3,'first row has 3 data');
111    
112     ############################################################################
113     # enque a series of alternating good/bad queries
114     $cv = AnyEvent->condvar;
115     my @results = ();
116     my $num_qry = 0;
117     my $qrydone = sub {
118     my ($dbh,$rows,$metadata) = @_;
119     my $err = undef;
120     if (! $dbh) {
121     $err = $@;
122     }
123     push @results , [$err,$rows];
124     if (scalar @results == $num_qry) {
125     $cv->send();
126     }
127     };
128    
129     $dbh->exec ("select a,b,c from nosuchtable1", $qrydone); $num_qry++;
130     $dbh->exec ("select a,b,c from rows1" , $qrydone); $num_qry++;
131     $dbh->exec ("select a,b,c from nosuchtable2", $qrydone); $num_qry++;
132     $dbh->exec ("select a,b,c from rows2" , $qrydone); $num_qry++;
133     $dbh->exec ("select a,b,c from nosuchtable3", $qrydone); $num_qry++;
134     $dbh->exec ("select a,b,c from rows3" , $qrydone); $num_qry++;
135     $dbh->exec ("select a,b,c from nosuchtable4", $qrydone); $num_qry++;
136     $dbh->exec ("select a,b,c from rows4" , $qrydone); $num_qry++;
137    
138     $cv->recv();
139     for my $r (0..$num_qry-1) {
140     my $offset = int($r / 2 )+1;
141     if ($r % 2) {
142     ok(! defined $results[$r]->[0],'Multi Query Queue: No error on good queries');
143     is(scalar @{$results[$r]->[1]},$offset,'Multi Query Queue: Good query got right number of rows');
144     }
145     else {
146     is(
147     $results[$r]->[0],
148     qq{Table 'database.nosuchtable$offset' doesn't exist},'Multi Query Queue: Bad query gets correct error'
149     );
150     }
151     }
152    
153     ############################################################################
154    
155     # try to connect to a closed port
156     # NOTE tcp port 9 is 'discard', hopefully not running
157     $cv = AnyEvent->condvar;
158     my $dbh2 = new AnyEvent::DBI(
159     "dbi:mysql:database=test;host=127.0.0.1;port=9",'','',
160     PrintError => 0,
161     timeout => 3,
162     on_error => sub { },
163     on_connect => sub {
164     if (! $_[0]) {
165     $cv->send($@);
166     }
167     else {
168     $cv->send();
169     }
170     },
171     );
172     $connect_error = $cv->recv();
173     like($connect_error,qr{can't connect}i,'mysql connect to localhost:9 refused');
174    
175     # try to connect to a firewalled port
176     $cv = AnyEvent->condvar;
177     $dbh2 = new AnyEvent::DBI(
178     "dbi:mysql:database=test;host=www.google.com;port=23306",'','',
179     timeout => 3,
180     on_error => sub { },
181     on_connect => sub {
182     if (! $_[0]) {
183     $cv->send($@);
184     }
185     else {
186     $cv->send();
187     }
188     },
189     );
190     $connect_error = $cv->recv();
191     is($connect_error,'TIMEOUT','mysql connect to google port 23306 times out');
192     undef $dbh2;
193    
194     # issue a query which times out
195     $cv = AnyEvent->condvar;
196     $dbh->exec (
197     "select a,b,c from delay10 where num=?", 10, sub {
198     my ($dbh,$rows, $metadata) = @_;
199     if (! $dbh) {
200     $cv->send($@);
201     }
202     else {
203     $cv->send(undef,$rows);
204     }
205     }
206     );
207     ($error,$rows) = $cv->recv();
208     is($error,'TIMEOUT','timeout fires during long-running query');
209    
210     # issue a query after a fatal timeout error
211     $cv = AnyEvent->condvar;
212     my $start = AnyEvent->now;
213     my $run = 10;
214     my $ran = 0;
215     my $fin = 0;
216     my $errs = [];
217     while ($ran++ < $run) {
218     $dbh->exec (
219     "select d,e,f,g from rows5 where num=?", 10, sub {
220     my ($dbh,$rows, $metadata) = @_;
221     if (! $dbh) {
222     push @$errs, $@;
223     }
224     if (++$fin == $run) {
225     $cv->send();
226     }
227     }
228     );
229     }
230     $cv->recv();
231     ok(AnyEvent->now -$start < 0.0001,'invalid db handle returns from multiple queries immediately');
232     is (scalar @$errs, 10, 'invalid db handle returned error for all enqueued queries');
233     is($errs->[0],'NO DATABASE CONNECTION','invalid db handle returns correct error');
234     undef $dbh;
235    
236     # check for server process leakage
237     eval {
238     require Proc::Exists;
239     import Proc::Exists qw(pexists);
240     };
241     my $has_pe = ! $@;
242     SKIP: {
243     skip ( 'This test requires Proc::Exists',4) unless $has_pe;
244     # connect three handles
245     $cv = AnyEvent->condvar;
246     my @handles;
247     my @handle_errors;
248     my $connected =0;
249     for (0..2) {
250     my $dbh3 = new AnyEvent::DBI(
251     "dbi:mysql:database=database;host=127.0.0.1;port=23306",'','',
252     PrintError => 0,
253     timeout => 2,
254     on_error => sub { },
255     on_connect => sub {
256     if (! $_[0]) {
257     push @handle_errors, $@;
258     }
259     if (++$connected == 3) {
260     $cv->send();
261     }
262     },
263     );
264     push @handles, $dbh3;
265     }
266     $cv->recv();
267     is(scalar @handles,3,'created three handles');
268     is(scalar @handle_errors,0,'no errors during handle creation');
269     my @pids = map {$_->_server_pid} @handles;
270     ok( defined pexists(@pids, {all=>1}),'Found three start_serve processes');
271     undef @handles;
272    
273     $cv = AnyEvent->condvar;
274     my $cleanup = AnyEvent->timer(after=>0.5,cb=>sub {$cv->send()});
275     $cv->recv();
276     ok(!defined pexists(@pids, {any=>1}),'All start_serve processes exited');
277     }
278    
279     # connect to the server again
280     $cv = AnyEvent->condvar;
281     $dbh = new AnyEvent::DBI(
282     "dbi:mysql:database=database;host=127.0.0.1;port=23306",'','',
283     PrintError => 0,
284     timeout => 2,
285     on_error => sub { },
286     on_connect => sub {
287     if (! $_[0]) {
288     $cv->send($@);
289     }
290     else {
291     $cv->send();
292     }
293     },
294     );
295     $connect_error = $cv->recv();
296     is($connect_error,undef,'on_connect() called without error, fake mysql server is re-connected');
297    
298     # End the server and reap it
299     $cv = AnyEvent->condvar;
300     my $server_process_watcher = AnyEvent->child(
301     pid => $server_pid ,
302     cb => sub {
303     $cv->send(@_);
304     }
305     );
306     kill 2, $server_pid;
307     my ($dead_pid,$dead_status)=$cv->recv();
308     is ($dead_pid,$server_pid,'MySQL Server processess exited');
309     is ($dead_status,2,'Server exited on our signal');
310     sleep 2; # give all mysql server children a chance to exit
311    
312     # try to make another query with a down MYSQL server
313     # issue a query
314     $cv = AnyEvent->condvar;
315     $dbh->exec (
316     "select x from rows1 where num=?", 10, sub {
317     my ($dbh,$rows, $metadata) = @_;
318     if (! $dbh) {
319     $cv->send($@);
320     }
321     else {
322     $cv->send(undef,$rows);
323     }
324     }
325     );
326    
327     ($error, $rows) = $cv->recv();
328     is($error,'TIMEOUT','mysql query to dead server times out');
329     undef $dbh;
330    
331     END {
332     if ($server_pid) {
333     # shut down the fake_mysql server
334     delete $SIG{CLD};
335     kill 15, $server_pid;
336     waitpid $server_pid,0;
337     }
338     exit 0;
339     }