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 19
/
Copy pathcursor.t
334 lines (309 loc) · 10.3 KB
/
cursor.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
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
use Mojo::Base -strict;
use Test::More;
plan skip_all => 'set TEST_ONLINE to enable this test'
unless $ENV{TEST_ONLINE};
use Mango;
use Mojo::IOLoop;
# Clean up before start
my $mango = Mango->new($ENV{TEST_ONLINE});
my $collection = $mango->db->collection('cursor_test');
$collection->drop if $collection->options;
# Add some documents to fetch
my $oids = $collection->insert([{test => 3}, {test => 1}, {test => 2}]);
is scalar @$oids, 3, 'three documents inserted';
# Fetch documents blocking
my $cursor = $collection->find->batch_size(2);
my @docs;
ok !$cursor->id, 'no cursor id';
push @docs, $cursor->next;
ok $cursor->id, 'cursor has id';
push @docs, $cursor->next;
push @docs, $cursor->next;
ok !$cursor->next, 'no more documents';
@docs = sort { $a->{test} <=> $b->{test} } @docs;
is $docs[0]{test}, 1, 'right document';
is $docs[1]{test}, 2, 'right document';
is $docs[2]{test}, 3, 'right document';
# Fetch all documents blocking
my $docs = $collection->find->batch_size(2)->all;
@$docs = sort { $a->{test} <=> $b->{test} } @$docs;
is $docs->[0]{test}, 1, 'right document';
is $docs->[1]{test}, 2, 'right document';
is $docs->[2]{test}, 3, 'right document';
# Fetch two documents blocking
$docs = $collection->find->limit(-2)->sort({test => 1})->all;
is scalar @$docs, 2, 'two documents';
is $docs->[0]{test}, 1, 'right document';
is $docs->[1]{test}, 2, 'right document';
# Build query
$cursor = $collection->find({test => 1});
is_deeply $cursor->build_query, {test => 1}, 'right query';
is_deeply $cursor->build_query(1), {'$query' => {test => 1}, '$explain' => 1},
'right query';
$cursor->sort({test => -1});
is_deeply $cursor->build_query,
{'$query' => {test => 1}, '$orderby' => {test => -1}}, 'right query';
$cursor->sort(undef)->hint({test => 1})->snapshot(1);
is_deeply $cursor->build_query,
{'$query' => {test => 1}, '$hint' => {test => 1}, '$snapshot' => 1},
'right query';
$cursor->hint(undef)->snapshot(undef)->max_scan(500);
is_deeply $cursor->build_query, {'$query' => {test => 1}, '$maxScan' => 500},
'right query';
$cursor = $collection->find({'$query' => {foo => 'bar'}, '$foo' => 'bar'});
is_deeply $cursor->build_query, {'$query' => {foo => 'bar'}, '$foo' => 'bar'},
'right query';
$cursor = $collection->find({'$query' => {foo => 'bar'}, '$foo' => 'bar'});
is_deeply $cursor->build_query(1),
{'$query' => {foo => 'bar'}, '$foo' => 'bar', '$explain' => 1},
'right query';
is_deeply $cursor->query, {'$query' => {foo => 'bar'}, '$foo' => 'bar'},
'query has not changed';
$cursor = $collection->find({})->comment('Test!')->max_time_ms(500);
is_deeply $cursor->build_query,
{'$query' => {}, '$comment' => 'Test!', '$maxTimeMS' => 500}, 'right query';
$cursor = $collection->find({})->read_preference({mode => 'SECONDARY'});
is_deeply $cursor->build_query,
{'$query' => {}, '$readPreference' => {mode => 'SECONDARY'}}, 'right query';
# Clone cursor
$cursor
= $collection->find({test => {'$exists' => 1}})->batch_size(2)
->comment('Test')->limit(3)->skip(1)->sort({test => 1})->fields({test => 1})
->max_scan(100);
my $doc = $cursor->next;
ok defined $cursor->id, 'has a cursor id';
ok $doc->{test}, 'right document';
my $clone
= $cursor->snapshot(1)->hint({test => 1})->max_time_ms(500)->tailable(1)
->await_data(1)->read_preference({mode => 'SECONDARY'})->clone;
isnt $cursor, $clone, 'different objects';
ok !defined $clone->id, 'has no cursor id';
is $clone->batch_size, 2, 'right batch size';
is $clone->comment, 'Test', 'right comment';
is_deeply $clone->fields, {test => 1}, 'right fields';
is_deeply $clone->hint, {test => 1}, 'right hint value';
is $clone->limit, 3, 'right limit';
is_deeply $clone->query, {test => {'$exists' => 1}}, 'right query';
is $clone->skip, 1, 'right skip value';
is $clone->snapshot, 1, 'right snapshot value';
is $clone->max_scan, 100, 'right max_scan value';
is $clone->max_time_ms, 500, 'right max_time_ms value';
is_deeply $clone->read_preference, {mode => 'SECONDARY'}, 'right fields';
is $clone->tailable, 1, 'is tailable';
is $clone->await_data, 1, 'is awaiting data';
is_deeply $clone->sort, {test => 1}, 'right sort value';
$cursor = $collection->find({foo => 'bar'}, {foo => 1});
is_deeply $cursor->clone->query, {foo => 'bar'}, 'right query';
is_deeply $cursor->clone->fields, {foo => 1}, 'right fields';
# Number of results to return
is $collection->find->num_to_return, 0, 'right number of results';
$cursor = $collection->find;
is $cursor->batch_size(5)->num_to_return, 5, 'right number of results';
$cursor = $collection->find;
is $cursor->limit(5)->num_to_return, 5, 'right number of results';
$cursor = $collection->find;
is $cursor->limit(-5)->num_to_return, -5, 'right number of results';
$cursor = $collection->find;
is $cursor->limit(4)->batch_size(2)->num_to_return, 2,
'right number of results';
is $cursor->limit(2)->batch_size(4)->num_to_return, 2,
'right number of results';
is $cursor->limit(-4)->batch_size(2)->num_to_return, -4,
'right number of results';
is $cursor->limit(-2)->batch_size(4)->num_to_return, -2,
'right number of results';
# Explain blocking
$cursor = $collection->find({test => 2});
$doc = $cursor->explain;
is $doc->{n}, 1, 'one document';
$doc = $cursor->next;
is $doc->{test}, 2, 'right document';
# Explain non-blocking
$cursor = $collection->find({test => 2});
my ($fail, $result);
$cursor->explain(
sub {
my ($cursor, $err, $doc) = @_;
$fail = $err;
$result = $doc->{n};
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is $result, 1, 'one document';
is $cursor->next->{test}, 2, 'right document';
# Get distinct values blocking
is_deeply [
sort @{$collection->find({test => {'$gt' => 1}})->distinct('test')}
], [2, 3], 'right values';
# Get distinct values non-blocking
($fail, $result) = ();
$collection->find({test => {'$gt' => 1}})->distinct(
test => sub {
my ($cursor, $err, $values) = @_;
$fail = $err;
$result = $values;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is_deeply [sort @$result], [2, 3], 'right values';
# Count documents blocking
is $collection->find({foo => 'bar'})->count, 0, 'no documents';
is $collection->find->skip(1)->limit(1)->count, 1, 'one document';
is $collection->find->count, 3, 'three documents';
# Count documents non-blocking
$fail = undef;
my @results;
my $delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
$collection->find->count($delay->begin);
},
sub {
my ($delay, $err, $count) = @_;
return $delay->pass($err) if $err;
push @results, $count;
$collection->find({foo => 'bar'})->count($delay->begin);
},
sub {
my ($delay, $err, $count) = @_;
$fail = $err;
push @results, $count;
}
);
$delay->wait;
ok !$fail, 'no error';
is_deeply \@results, [3, 0], 'right number of documents';
# Fetch documents non-blocking
$cursor = $collection->find->batch_size(2);
@docs = ();
$fail = undef;
$delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
$cursor->next($delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
return $delay->pass($err) if $err;
push @docs, $doc;
$cursor->next($delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
return $delay->pass($err) if $err;
push @docs, $doc;
$cursor->next($delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
$fail = $err;
push @docs, $doc;
}
);
$delay->wait;
ok !$fail, 'no error';
@docs = sort { $a->{test} <=> $b->{test} } @docs;
is $docs[0]{test}, 1, 'right document';
is $docs[1]{test}, 2, 'right document';
is $docs[2]{test}, 3, 'right document';
# Fetch all documents non-blocking
@docs = ();
$collection->find->batch_size(2)->all(
sub {
my ($collection, $err, $docs) = @_;
@docs = @$docs;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
@docs = sort { $a->{test} <=> $b->{test} } @docs;
is $docs[0]{test}, 1, 'right document';
is $docs[1]{test}, 2, 'right document';
is $docs[2]{test}, 3, 'right document';
# Fetch subset of documents sorted
$docs = $collection->find->fields({_id => 0})->sort({test => 1})->all;
is_deeply $docs, [{test => 1}, {test => 2}, {test => 3}], 'right subset';
# Rewind cursor blocking
$cursor = $collection->find;
ok !$cursor->id, 'no cursor id';
$cursor->rewind;
$doc = $cursor->next;
ok $doc, 'found a document';
$cursor->rewind;
is_deeply $cursor->next, $doc, 'found same document again';
# Rewind cursor non-blocking
$fail = undef;
@docs = ();
$cursor = $collection->find;
$delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
$cursor->next($delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
return $delay->pass($err) if $err;
push @docs, $doc;
$cursor->rewind($delay->begin);
},
sub {
my ($delay, $err) = @_;
return $delay->pass($err) if $err;
$cursor->next($delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
$fail = $err;
push @docs, $doc;
}
);
$delay->wait;
ok !$fail, 'no error';
is_deeply $docs[0], $docs[1], 'found same document again';
is $collection->remove->{n}, 3, 'three documents removed';
# Try to restart aggregate cursor
$collection->insert({stuff => $_}) for 1 .. 30;
$cursor = $collection->aggregate([{'$match' => {stuff => {'$gt' => 0}}}],
{cursor => {batchSize => 5}});
is $cursor->next->{stuff}, 1, 'right result';
ok $cursor->id, 'cursor has id';
$cursor->rewind;
ok !$cursor->id, 'no cursor id';
eval { $cursor->next };
like $@, qr/Cursor cannot be restarted/, 'right error';
is $collection->remove->{n}, 30, 'thirty documents removed';
# Tailable cursor
$collection->drop;
$collection->create({capped => \1, max => 2, size => 100000});
my $collection2 = $mango->db->collection('cursor_test');
$collection2->insert([{test => 1}, {test => 2}]);
$cursor = $collection->find->tailable(1)->await_data(1);
is $cursor->next->{test}, 1, 'right document';
is $cursor->next->{test}, 2, 'right document';
($fail, $result) = ();
my $tail;
$delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
my $end = $delay->begin;
$cursor->next($delay->begin);
Mojo::IOLoop->timer(
0.5 => sub { $collection2->insert({test => 3} => $end) });
},
sub {
my ($delay, $err1, $oid, $err2, $doc) = @_;
$fail = $err1 || $err2;
$result = $oid;
$tail = $doc;
}
);
$delay->wait;
ok !$fail, 'no error';
is $tail->{test}, 3, 'right document';
is $tail->{_id}, $result, 'same document';
$collection->drop;
done_testing();