1#  Copyright 2018 - present MongoDB, Inc.
2#
3#  Licensed under the Apache License, Version 2.0 (the "License");
4#  you may not use this file except in compliance with the License.
5#  You may obtain a copy of the License at
6#
7#  http://www.apache.org/licenses/LICENSE-2.0
8#
9#  Unless required by applicable law or agreed to in writing, software
10#  distributed under the License is distributed on an "AS IS" BASIS,
11#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12#  See the License for the specific language governing permissions and
13#  limitations under the License.
14
15use strict;
16use warnings;
17use Test::More;
18
19use lib "t/lib";
20use MongoDBTest qw/
21    skip_unless_mongod
22    build_client
23    get_test_db
24    server_version
25    check_min_server_version
26/;
27
28skip_unless_mongod();
29
30my @events;
31my $conn = build_client(monitoring_callback => sub {
32    push @events, shift;
33});
34my $server_version = server_version($conn);
35my $testdb = get_test_db($conn);
36my $coll = $testdb->get_collection('test_collection');
37
38for my $index (0..1000) {
39    $coll->insert_one({
40        type => 'testval',
41        value => $index,
42    });
43}
44
45my $id;
46@events = ();
47do {
48    my $results = $coll->query({ type => 'testval' });
49    ok defined($results->next), 'fetch one document';
50    $id = $results->result->_cursor_id;
51    ok defined($id), 'cursor id';
52    undef $results;
53};
54
55my ($event) = grep {
56    $_->{commandName} eq 'killCursors'
57    &&
58    $_->{type} eq 'command_succeeded'
59} @events;
60
61ok defined($event), 'successful killcursors event';
62
63if (defined $event and defined $id) {
64    is $event->{reply}{ok}, 1,
65        'reply ok';
66    unless ( check_min_server_version($conn, 'v3.2.0') ) {
67        ok( !grep { $_ eq $id } @{$event->{reply}{cursorsAlive}}, "cursor id not in alive list" );
68        is_deeply( $event->{reply}{cursorsKilled}, [$id], 'cursor id in killed list' );
69    }
70}
71
72done_testing;
73