File: causal_consistency.t

package info (click to toggle)
libmongodb-perl 2.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,292 kB
  • sloc: perl: 14,421; python: 299; makefile: 20; sh: 11
file content (388 lines) | stat: -rw-r--r-- 11,291 bytes parent folder | download | duplicates (4)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#  Copyright 2018 - present MongoDB, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

use strict;
use warnings;
use Test::More 0.96;
use Test::Fatal;
use Test::Deep qw/!blessed/;

use utf8;
use Tie::IxHash;

use MongoDB;
use MongoDB::Error;

use lib "t/lib";

use MongoDBTest qw/
    build_client
    get_test_db
    server_version
    server_type
    clear_testdbs
    get_unique_collection
    skip_unless_mongod
    skip_unless_sessions
/;

skip_unless_mongod();
skip_unless_sessions();

my @events;

sub clear_events { @events = () }
sub event_count { scalar @events }
sub event_cb { push @events, $_[0] }

my $conn           = build_client(
  monitoring_callback => \&event_cb,
);
my $testdb         = get_test_db($conn);
my $server_version = server_version($conn);
my $server_type    = server_type($conn);
my $coll           = $testdb->get_collection('test_collection');

# spec test 1
subtest 'session operation_time undef on init' => sub {
    my $session = $conn->start_session;
    is $session->operation_time, undef, 'is undef';
};

clear_events();

# spec test 2
subtest 'first read' => sub {
    my $session = $conn->start_session({ causalConsistency => 1 });

    my $response = $coll->find_one({ _id => 1 }, {}, { session => $session });

    my $event = $events[-2];

    ok ! exists $event->{ command }->{ 'afterClusterTime' }, 'afterClusterTime not sent on first read';
};

clear_events();

# spec test 3
subtest 'update operation_time' => sub {
    my $session = $conn->start_session({ causalConsistency => 1 });

    is $session->operation_time, undef, 'Empty operation time';

    my $response = $coll->insert_one({ _id => 1 }, { session => $session });

    my $event = $events[-1];

    # for some reason 'is' wont do the comparison correctly...
    ok $session->operation_time == $event->{reply}->{operationTime}, 'response has operation time and is updated in session';

    $session->end_session;

    $session = $conn->start_session({ causalConsistency => 1 });

    is $session->operation_time, undef, 'Empty operation time';

    # Try inserting the same thing again (_id must be unique in a collection afaik)
    my $err = exception { $coll->insert_one({ _id => 1 }, { session => $session }) };

    isa_ok( $err, 'MongoDB::DatabaseError', "duplicate insert error" );

    my $err_event = $events[-1];

    ok $session->operation_time == $err_event->{reply}->{operationTime}, 'response has operation time and is updated in session';
};

clear_events();

# spec test 4
subtest 'find_one then read includes operationtime' => sub {
    # run for all read ops:
    # * find
    # * find_one
    # * find_id
    # * aggregate
    # * count
    # * distinct

    my $tests = {
        find            => [ {_id => 1 } ],
        find_one        => [ { _id => 1 }, {} ],
        find_id         => [ 1, {} ],
        aggregate       => [ [ { '$match' => { count => { '$gt' => 0 } } } ] ],
        count_documents => [ { _id => 1 } ],
        distinct        => [ "id_", { _id => 1 } ],
    };

    for my $key ( qw/
      find
      find_one
      find_id
      aggregate
      count_documents
      distinct / ) {
        clear_events();
        subtest $key => sub {
            my $session = find_one_and_get_session();
            my $op_time = $session->operation_time;

            my $ret = $coll->$key(@{ $tests->{$key} }, { session => $session });
            if ( $key eq 'find' ) { $ret->result }

            my $event = $events[-2];

            is $op_time, $event->{command}->{'readConcern'}->{afterClusterTime}, 'has correct afterClusterTime';
        };
    }
};

sub find_one_and_get_session {
    my $session = $conn->start_session({ causalConsistency => 1 });

    my $find = $coll->find_one({ _id => 1 }, {}, { session => $session });

    return $session;
}

clear_events();

# spec test 5
subtest 'write then find_one includes operationTime' => sub {
    # repeat for all write ops:
    # * insert_one
    # * insert_many
    # * delete_one
    # * delete_many
    # * replace_one
    # * update_one
    # * update_many
    # * find_one_and_delete
    # * find_one_and_replace
    # * find_one_and_update
    # * ordered_bulk
    # * unordered_bulk

    # Undef exceptions are only due to not knowing how to cause one
    my $tests = {
        insert_one => {
            success => [ { _id => 100 } ],
            exception => [ { _id => 100 } ],
        },
        insert_many => {
            success => [ [ map { { _id => $_ } } 101..111 ] ],
            exception => [ [ map { { _id => $_ } } 101..111 ] ],
        },
        delete_one => {
            success => [ { _id => 100 } ],
            exception => undef,
        },
        delete_many => {
            success => [ { _id => { '$in' => [101,102] } } ],
            exception => undef,
        },
        replace_one => {
            success => [ { _id => 103 }, { _id => 103, foo => 'qux' } ],
            exception => undef,
        },
        update_one => {
            success => [ { _id => 104 }, { '$set' => { bar => 'baz' } } ],
            exception => undef,
        },
        update_many => {
            success => [ { _id => { '$in' => [105,106] } }, { '$set' => { bar => 'baz' } } ],
            exception => undef,
        },
        find_one_and_delete => {
            success => [ { _id => 107 } ],
            exception => undef,
        },
        find_one_and_replace => {
            success => [ { _id => 108 }, { _id => 108, bar => 'baz' } ],
            exception => undef,
        },
        find_one_and_update => {
            success => [ { _id => 109 }, { '$set' => { foo => 'qux' } } ],
            exception => undef,
        },
    };

    # Order of these actually matters - the insert_one and insert_many must go first
    for my $key ( qw/
      insert_one
      insert_many
      delete_one
      delete_many
      replace_one
      update_one
      update_many
      find_one_and_delete
      find_one_and_replace
      find_one_and_update / ) {
        clear_events();
        subtest $key => sub {
            my $session = $conn->start_session({ causalConsistency => 1 });

            $coll->$key( @{ $tests->{ $key }->{ success } }, { session => $session });

            find_one_and_assert( $session );

            return unless defined $tests->{ $key }->{ exception };

            $session->end_session;

            $session = $conn->start_session({ causalConsistency => 1 });

            my $err = exception {
                $coll->$key( @{ $tests->{ $key }->{ exception } }, { session => $session })
            };

            isa_ok( $err, 'MongoDB::DatabaseError' );

            find_one_and_assert( $session );
        };
    }

    subtest 'ordered_bulk' => sub {
        my $session = $conn->start_session({ causalConsistency => 1 });

        my $bulk = $coll->ordered_bulk;
        $bulk->insert_one({ _id => 120 });
        $bulk->insert_one({ _id => 121 });
        $bulk->execute(undef, { session => $session });

        find_one_and_assert( $session );

        $session->end_session;

        $session = $conn->start_session({ causalConsistency => 1 });

        my $err = exception {
            my $bulk2 = $coll->ordered_bulk;
            $bulk2->insert_one({ _id => 120 });
            $bulk2->insert_one({ _id => 121 });
            $bulk2->execute(undef, { session => $session });
        };
        isa_ok( $err, 'MongoDB::DatabaseError' );

        find_one_and_assert( $session );
    };

    subtest 'unordered_bulk' => sub {
        my $session = $conn->start_session({ causalConsistency => 1 });

        my $bulk = $coll->unordered_bulk;
        $bulk->insert_one({ _id => 123 });
        $bulk->insert_one({ _id => 124 });
        $bulk->execute(undef, { session => $session });

        find_one_and_assert( $session );

        $session->end_session;

        $session = $conn->start_session({ causalConsistency => 1 });

        my $err = exception {
            my $bulk2 = $coll->unordered_bulk;
            $bulk2->insert_one({ _id => 123 });
            $bulk2->insert_one({ _id => 124 });
            $bulk2->execute(undef, { session => $session });
        };
        isa_ok( $err, 'MongoDB::DatabaseError' );

        find_one_and_assert( $session );
    };
};

sub find_one_and_assert {
    my $session = shift;
    my $op_time = $session->operation_time;

    ok defined $op_time, 'got operationTime in session';

    $coll->find_one({ _id => 1 }, {}, { session => $session });

    my $event = $events[-2];

    is $op_time, $event->{command}->{'readConcern'}->{afterClusterTime}, 'has correct afterClusterTime';
}

clear_events();

# spec test 6
subtest 'turn off causalConsistency' => sub {
    my $session = $conn->start_session({ causalConsistency => 0 });

    $coll->find_one({ _id => 1 }, {}, { session => $session });

    $coll->find_one({ _id => 1 }, {}, { session => $session });

    my $event = $events[-2];

    ok ! exists $event->{command}->{'readConcern'}, 'no readconcern, so no afterClusterTime';
};

clear_events();

# spec test 8
subtest 'using default readConcern' => sub {
    my $session = $conn->start_session({ causalConsistency => 1 });

    # collection uses server ReadConcern by default
    $coll->find_one({ _id => 1 }, {}, { session => $session });

    my $op_time = $session->operation_time;

    $coll->find_one({ _id => 1 }, {}, { session => $session });

    my $event = $events[-2];

    ok ! defined $event->{command}->{'readConcern'}->{level}, 'no read concern level with default value';
};

clear_events();

# spec test 9
subtest 'using custom readConcern' => sub {
    my $session = $conn->start_session({ causalConsistency => 1 });

    my $custom_coll = get_unique_collection( $testdb, 'custom_readconcern', { read_concern => { level => 'local' } } );
    # collection uses server ReadConcern by default
    $custom_coll->find_one({ _id => 1 }, {}, { session => $session });

    my $op_time = $session->operation_time;

    $custom_coll->find_one({ _id => 1 }, {}, { session => $session });

    my $event = $events[-2];

    my $read_concern = $event->{command}->{'readConcern'};

    is $read_concern->{level}, 'local', 'read concern level with custom value';
    is $read_concern->{afterClusterTime}, $op_time, 'read concern afterClusterTime present';
};

#spec test 10
subtest 'unacknowledged writes' => sub {
    my $session = $conn->start_session({ causalConsistency => 1 });

    my $custom_coll = get_unique_collection( $testdb, 'unac_write', { write_concern => { w => 0 } } );

    $custom_coll->update_one({ _id => 1 }, { '$set' => { 'manamana' => 'doo dooo doo doodoo' } }, { session => $session });

    ok ! defined $session->operation_time, 'no operation time set from unac write';
};

clear_testdbs;

done_testing;