This repository has been archived by the owner on Dec 11, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
database.t
56 lines (49 loc) · 1.49 KB
/
database.t
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
use Mojo::Base -strict;
use Test::More;
use Mango;
use Mojo::IOLoop;
plan skip_all => 'set TEST_ONLINE to enable this test'
unless $ENV{TEST_ONLINE};
# Run command blocking
my $mango = Mango->new($ENV{TEST_ONLINE});
ok $mango->db->command('getnonce')->{nonce}, 'command was successful';
ok !$mango->is_active, 'no operations in progress';
# Run command non-blocking
my ($fail, $nonce);
$mango->db->command(
'getnonce' => sub {
my ($db, $err, $doc) = @_;
$fail = $err;
$nonce = $doc->{nonce};
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$mango->is_active, 'no operations in progress';
ok !$fail, 'no error';
ok $nonce, 'command was successful';
# Interrupted blocking command
my $port = Mojo::IOLoop->generate_port;
$mango = Mango->new("mongodb://localhost:$port");
my $id = $mango->ioloop->server((port => $port) => sub { $_[1]->close });
eval { $mango->db->command('getnonce') };
like $@, qr/Premature connection close/, 'right error';
$mango->ioloop->remove($id);
# Interrupted non-blocking command
Mojo::IOLoop->generate_port;
$mango = Mango->new("mongodb://localhost:$port");
$id = Mojo::IOLoop->server((port => $port) => sub { $_[1]->close });
$fail = $nonce = undef;
$mango->db->command(
'getnonce' => sub {
my ($db, $err, $doc) = @_;
$fail = $err;
$nonce = $doc;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
Mojo::IOLoop->remove($id);
like $fail, qr/Premature connection close/, 'right error';
ok !$nonce, 'command was not successful';
done_testing();