summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/document_source_check_resume_token_test.cpp
blob: f81f1880616c07acd5348ffa35fb82645ee45eaf (plain)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include <boost/intrusive_ptr.hpp>
#include <memory>

#include "mongo/bson/bsonelement.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/db/catalog/collection_mock.h"
#include "mongo/db/exec/collection_scan.h"
#include "mongo/db/exec/document_value/document_value_test_util.h"
#include "mongo/db/exec/plan_stats.h"
#include "mongo/db/pipeline/aggregation_context_fixture.h"
#include "mongo/db/pipeline/document_source_change_stream_check_resumability.h"
#include "mongo/db/pipeline/document_source_change_stream_ensure_resume_token_present.h"
#include "mongo/db/pipeline/document_source_mock.h"
#include "mongo/db/pipeline/expression_context.h"
#include "mongo/db/pipeline/process_interface/stub_mongo_process_interface.h"
#include "mongo/db/pipeline/resume_token.h"
#include "mongo/db/query/collation/collator_interface_mock.h"
#include "mongo/db/service_context.h"
#include "mongo/db/storage/devnull/devnull_kv_engine.h"
#include "mongo/unittest/death_test.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/uuid.h"

using boost::intrusive_ptr;

namespace mongo {
namespace {
static constexpr StringData kOtherNs = "test.other.ns"_sd;
static constexpr StringData kTestNs = "test.ns"_sd;

class ChangeStreamOplogCursorMock : public SeekableRecordCursor {
public:
    ChangeStreamOplogCursorMock(std::deque<Record>* records) : _records(records) {}

    virtual ~ChangeStreamOplogCursorMock() {}

    boost::optional<Record> next() override {
        if (_records->empty()) {
            return boost::none;
        }
        auto& next = _records->front();
        _records->pop_front();
        return next;
    }

    boost::optional<Record> seekExact(const RecordId& id) override {
        return Record{};
    }
    boost::optional<Record> seekNear(const RecordId& id) override {
        return boost::none;
    }
    void save() override {}
    bool restore() override {
        return true;
    }
    void detachFromOperationContext() override {}
    void reattachToOperationContext(OperationContext* opCtx) override {}

private:
    std::deque<Record>* _records;
};

class ChangeStreamOplogCollectionMock : public CollectionMock {
public:
    ChangeStreamOplogCollectionMock() : CollectionMock(NamespaceString::kRsOplogNamespace) {
        _recordStore =
            _devNullEngine.getRecordStore(nullptr, NamespaceString::kRsOplogNamespace.ns(), "", {});
    }

    void push_back(Document doc) {
        // Every entry we push into the oplog should have both 'ts' and 'ns' fields.
        invariant(doc["ts"].getType() == BSONType::bsonTimestamp);
        invariant(doc["ns"].getType() == BSONType::String);
        // Events should always be added in ascending ts order.
        auto lastTs =
            _records.empty() ? Timestamp(0, 0) : _records.back().data.toBson()["ts"].timestamp();
        invariant(ValueComparator().compare(Value(lastTs), doc["ts"]) <= 0);
        // Fill out remaining required fields in the oplog entry.
        MutableDocument mutableDoc{doc};
        mutableDoc.setField("op", Value("n"_sd));
        mutableDoc.setField("o", Value(Document{}));
        mutableDoc.setField("wall",
                            Value(Date_t::fromMillisSinceEpoch(doc["ts"].getTimestamp().asLL())));
        // Must remove _id since the oplog expects either no _id or an OID.
        mutableDoc.remove("_id");
        // Convert to owned BSON and create corresponding Records.
        _data.push_back(mutableDoc.freeze().toBson());
        Record record;
        record.data = {_data.back().objdata(), _data.back().objsize()};
        record.id = RecordId{doc["ts"].getTimestamp().asLL()};
        _records.push_back(std::move(record));
    }

    std::unique_ptr<SeekableRecordCursor> getCursor(OperationContext* opCtx,
                                                    bool forward) const override {
        return std::make_unique<ChangeStreamOplogCursorMock>(&_records);
    }

    RecordStore* getRecordStore() const override {
        return _recordStore.get();
    }

private:
    // We retain the owned record queue here because cursors may be destroyed and recreated.
    mutable std::deque<Record> _records;
    std::deque<BSONObj> _data;

    // These are no-op structures which are required by the CollectionScan.
    std::unique_ptr<RecordStore> _recordStore;
    DevNullKVEngine _devNullEngine;
};

/**
 * Acts as an initial source for the change stream pipeline, taking the place of DSOplogMatch. This
 * class maintains its own queue of documents added by each test, but also pushes each doc into an
 * underlying ChangeStreamOplogCollectionMock. When doGetNext() is called, it retrieves the next
 * document by pulling it from the mocked oplog collection via a CollectionScan, in order to test
 * the latter's changestream-specific functionality. The reason this class keeps its own queue in
 * addition to the ChangeStreamOplogCollectionMock is twofold:
 *
 *   - The _id must be stripped from each document before it can be added to the mocked oplog, since
 *     the _id field of the test document is a resume token but oplog entries are only permitted to
 *     have OID _ids. We therefore have to restore the _id field of the document pulled from the
 *     CollectionScan before passing it into the pipeline.
 *
 *   - The concept of GetNextResult::ReturnStatus::kPauseExecution does not exist in CollectionScan;
 *     NEED_TIME is somewhat analogous but cannot be artificially induced. For tests which exercise
 *     kPauseExecution, these events are stored only in the DocumentSourceChangeStreamMock queue
 *     with no corresponding entry in the ChangeStreamOplogCollectionMock queue.
 */
class DocumentSourceChangeStreamMock : public DocumentSourceMock {
public:
    DocumentSourceChangeStreamMock(const boost::intrusive_ptr<ExpressionContextForTest>& expCtx)
        : DocumentSourceMock({}, expCtx), _collectionPtr(&_collection) {
        _filterExpr = BSON("ns" << kTestNs);
        _filter = MatchExpressionParser::parseAndNormalize(_filterExpr, pExpCtx);
        _params.assertTsHasNotFallenOffOplog = Timestamp(0);
        _params.shouldTrackLatestOplogTimestamp = true;
        _params.minRecord = RecordId(0);
        _params.tailable = true;
    }

    void setResumeToken(ResumeTokenData resumeToken) {
        invariant(!_collScan);
        _filterExpr = BSON("ns" << kTestNs << "ts" << BSON("$gte" << resumeToken.clusterTime));
        _filter = MatchExpressionParser::parseAndNormalize(_filterExpr, pExpCtx);
        _params.minRecord = RecordId(resumeToken.clusterTime.asLL());
        _params.assertTsHasNotFallenOffOplog = resumeToken.clusterTime;
    }

    void push_back(GetNextResult&& result) {
        // We should never push an explicit EOF onto the queue.
        invariant(!result.isEOF());
        // If there is a document supplied, add it to the mock collection.
        if (result.isAdvanced()) {
            _collection.push_back(result.getDocument());
        }
        // Both documents and pauses are stored in the DSMock queue.
        DocumentSourceMock::push_back(std::move(result));
    }

    void push_back(const GetNextResult& result) {
        MONGO_UNREACHABLE;
    }

    bool isPermanentlyEOF() const {
        return _collScan->getCommonStats()->isEOF;
    }

protected:
    GetNextResult doGetNext() override {
        // If this is the first call to doGetNext, we must create the COLLSCAN.
        if (!_collScan) {
            _collScan = std::make_unique<CollectionScan>(
                pExpCtx.get(), _collectionPtr, _params, &_ws, _filter.get());
            // The first call to doWork will create the cursor and return NEED_TIME. But it won't
            // actually scan any of the documents that are present in the mock cursor queue.
            ASSERT_EQ(_collScan->doWork(nullptr), PlanStage::NEED_TIME);
            ASSERT_EQ(_getNumDocsTested(), 0);
        }
        while (true) {
            // If the next result is a pause, return it and don't collscan.
            auto nextResult = DocumentSourceMock::doGetNext();
            if (nextResult.isPaused()) {
                return nextResult;
            }
            // Otherwise, retrieve the document via the CollectionScan stage.
            auto id = WorkingSet::INVALID_ID;
            switch (_collScan->doWork(&id)) {
                case PlanStage::IS_EOF:
                    invariant(nextResult.isEOF());
                    return nextResult;
                case PlanStage::ADVANCED: {
                    // We need to restore the _id field which was removed when we added this
                    // entry into the oplog. This is like a stripped-down DSCSTransform stage.
                    MutableDocument mutableDoc{_ws.get(id)->doc.value()};
                    mutableDoc["_id"] = nextResult.getDocument()["_id"];
                    return mutableDoc.freeze();
                }
                case PlanStage::NEED_TIME:
                    continue;
                case PlanStage::NEED_YIELD:
                    MONGO_UNREACHABLE;
            }
        }
        MONGO_UNREACHABLE;
    }

private:
    size_t _getNumDocsTested() {
        return static_cast<const CollectionScanStats*>(_collScan->getSpecificStats())->docsTested;
    }

    ChangeStreamOplogCollectionMock _collection;
    CollectionPtr _collectionPtr;
    std::unique_ptr<CollectionScan> _collScan;
    CollectionScanParams _params;

    std::unique_ptr<MatchExpression> _filter;
    BSONObj _filterExpr;

    WorkingSet _ws;
};

class CheckResumeTokenTest : public AggregationContextFixture {
public:
    CheckResumeTokenTest() : _mock(make_intrusive<DocumentSourceChangeStreamMock>(getExpCtx())) {}

protected:
    /**
     * Pushes a document with a resume token corresponding to the given ResumeTokenData into the
     * mock queue. This document will have an ns field that matches the test namespace, and will
     * appear in the change stream pipeline if its timestamp is at or after the resume timestamp.
     */
    void addOplogEntryOnTestNS(ResumeTokenData tokenData) {
        _mock->push_back(Document{{"ns", kTestNs},
                                  {"ts", tokenData.clusterTime},
                                  {"_id", ResumeToken(std::move(tokenData)).toDocument()}});
    }

    /**
     * Pushes a document with a resume token corresponding to the given timestamp, version,
     * txnOpIndex, docKey, and namespace into the mock queue.
     */
    void addOplogEntryOnTestNS(
        Timestamp ts, int version, std::size_t txnOpIndex, Document docKey, UUID uuid) {
        return addOplogEntryOnTestNS({ts, version, txnOpIndex, uuid, Value(docKey)});
    }

    /**
     * Pushes a document with a resume token corresponding to the given timestamp, version,
     * txnOpIndex, docKey, and namespace into the mock queue.
     */
    void addOplogEntryOnTestNS(Timestamp ts, Document docKey, UUID uuid = testUuid()) {
        addOplogEntryOnTestNS(ts, 0, 0, docKey, uuid);
    }
    /**
     * Pushes a document with a resume token corresponding to the given timestamp, _id string, and
     * namespace into the mock queue.
     */
    void addOplogEntryOnTestNS(Timestamp ts, std::string id, UUID uuid = testUuid()) {
        addOplogEntryOnTestNS(ts, 0, 0, Document{{"_id", id}}, uuid);
    }

    /**
     * Pushes a document that does not match the test namespace into the mock oplog. This will be
     * examined by the oplog CollectionScan but will not produce an event in the pipeline.
     */
    void addOplogEntryOnOtherNS(Timestamp ts) {
        _mock->push_back(Document{{"ns", kOtherNs}, {"ts", ts}});
    }

    /**
     * Pushes a pause in execution into the pipeline queue.
     */
    void addPause() {
        _mock->push_back(DocumentSource::GetNextResult::makePauseExecution());
    }

    /**
     * Convenience method to create the class under test with a given ResumeTokenData.
     */
    intrusive_ptr<DocumentSourceChangeStreamEnsureResumeTokenPresent>
    createDSEnsureResumeTokenPresent(ResumeTokenData tokenData) {
        auto checkResumeToken =
            DocumentSourceChangeStreamEnsureResumeTokenPresent::create(getExpCtx(), tokenData);
        _mock->setResumeToken(std::move(tokenData));
        checkResumeToken->setSource(_mock.get());
        return checkResumeToken;
    }

    /**
     * Convenience method to create the class under test with a given timestamp, docKey, and
     * namespace.
     */
    intrusive_ptr<DocumentSourceChangeStreamEnsureResumeTokenPresent>
    createDSEnsureResumeTokenPresent(Timestamp ts,
                                     int version,
                                     std::size_t txnOpIndex,
                                     boost::optional<Document> docKey,
                                     UUID uuid) {
        return createDSEnsureResumeTokenPresent(
            {ts, version, txnOpIndex, uuid, docKey ? Value(*docKey) : Value()});
    }

    /**
     * Convenience method to create the class under test with a given timestamp, docKey, and
     * namespace.
     */
    intrusive_ptr<DocumentSourceChangeStreamEnsureResumeTokenPresent>
    createDSEnsureResumeTokenPresent(Timestamp ts,
                                     boost::optional<Document> docKey,
                                     UUID uuid = testUuid()) {
        return createDSEnsureResumeTokenPresent(ts, 0, 0, docKey, uuid);
    }

    /**
     * Convenience method to create the class under test with a given timestamp, _id string, and
     * namespace.
     */
    intrusive_ptr<DocumentSourceChangeStreamEnsureResumeTokenPresent>
    createDSEnsureResumeTokenPresent(Timestamp ts, StringData id, UUID uuid = testUuid()) {
        return createDSEnsureResumeTokenPresent(ts, 0, 0, Document{{"_id", id}}, uuid);
    }

    /**
     * This method is required to avoid a static initialization fiasco resulting from calling
     * UUID::gen() in file or class static scope.
     */
    static const UUID& testUuid() {
        static const UUID* uuid_gen = new UUID(UUID::gen());
        return *uuid_gen;
    }

    intrusive_ptr<DocumentSourceChangeStreamMock> _mock;
};

class CheckResumabilityTest : public CheckResumeTokenTest {
protected:
    intrusive_ptr<DocumentSourceChangeStreamCheckResumability> createDSCheckResumability(
        ResumeTokenData tokenData) {
        auto dsCheckResumability =
            DocumentSourceChangeStreamCheckResumability::create(getExpCtx(), tokenData);
        _mock->setResumeToken(std::move(tokenData));
        dsCheckResumability->setSource(_mock.get());
        return dsCheckResumability;
    }
    intrusive_ptr<DocumentSourceChangeStreamCheckResumability> createDSCheckResumability(
        Timestamp ts) {
        return createDSCheckResumability(ResumeToken::makeHighWaterMarkToken(ts).getData());
    }
};

TEST_F(CheckResumeTokenTest, ShouldSucceedWithOnlyResumeToken) {
    Timestamp resumeTimestamp(100, 1);

    auto checkResumeToken = createDSEnsureResumeTokenPresent(resumeTimestamp, "1");
    addOplogEntryOnTestNS(resumeTimestamp, "1");
    // We should not see the resume token.
    ASSERT_TRUE(checkResumeToken->getNext().isEOF());
}

TEST_F(CheckResumeTokenTest, ShouldSucceedWithPausesBeforeResumeToken) {
    Timestamp resumeTimestamp(100, 1);

    auto checkResumeToken = createDSEnsureResumeTokenPresent(resumeTimestamp, "1");
    addPause();
    addOplogEntryOnTestNS(resumeTimestamp, "1");

    // We see the pause we inserted, but not the resume token.
    ASSERT_TRUE(checkResumeToken->getNext().isPaused());
    ASSERT_TRUE(checkResumeToken->getNext().isEOF());
}

TEST_F(CheckResumeTokenTest, ShouldSucceedWithPausesAfterResumeToken) {
    Timestamp resumeTimestamp(100, 1);
    Timestamp doc1Timestamp(100, 2);

    auto checkResumeToken = createDSEnsureResumeTokenPresent(resumeTimestamp, "1");
    addOplogEntryOnTestNS(resumeTimestamp, "1");
    addPause();
    addOplogEntryOnTestNS(doc1Timestamp, "2");

    // Pause added explicitly.
    ASSERT_TRUE(checkResumeToken->getNext().isPaused());
    // The document after the resume token should be the first.
    auto result1 = checkResumeToken->getNext();
    ASSERT_TRUE(result1.isAdvanced());
    auto& doc1 = result1.getDocument();
    ASSERT_EQ(doc1Timestamp, ResumeToken::parse(doc1["_id"].getDocument()).getData().clusterTime);
    ASSERT_TRUE(checkResumeToken->getNext().isEOF());
}

TEST_F(CheckResumeTokenTest, ShouldSucceedWithMultipleDocumentsAfterResumeToken) {
    Timestamp resumeTimestamp(100, 1);

    auto checkResumeToken = createDSEnsureResumeTokenPresent(resumeTimestamp, "0");
    addOplogEntryOnTestNS(resumeTimestamp, "0");

    Timestamp doc1Timestamp(100, 2);
    Timestamp doc2Timestamp(101, 1);
    addOplogEntryOnTestNS(doc1Timestamp, "1");
    addOplogEntryOnTestNS(doc2Timestamp, "2");

    auto result1 = checkResumeToken->getNext();
    ASSERT_TRUE(result1.isAdvanced());
    auto& doc1 = result1.getDocument();
    ASSERT_EQ(doc1Timestamp, ResumeToken::parse(doc1["_id"].getDocument()).getData().clusterTime);
    auto result2 = checkResumeToken->getNext();
    ASSERT_TRUE(result2.isAdvanced());
    auto& doc2 = result2.getDocument();
    ASSERT_EQ(doc2Timestamp, ResumeToken::parse(doc2["_id"].getDocument()).getData().clusterTime);
    ASSERT_TRUE(checkResumeToken->getNext().isEOF());
}

TEST_F(CheckResumeTokenTest, ShouldFailIfFirstDocHasWrongResumeToken) {
    // The first timestamp in the oplog precedes the resume token, and the second matches it...
    Timestamp doc1Timestamp(100, 1);
    Timestamp resumeTimestamp(100, 2);
    Timestamp doc2Timestamp = resumeTimestamp;

    auto checkResumeToken = createDSEnsureResumeTokenPresent(resumeTimestamp, "1");

    // ... but there's no entry in the oplog that matches the full token.
    addOplogEntryOnTestNS(doc1Timestamp, "1");
    addOplogEntryOnTestNS(doc2Timestamp, "2");
    ASSERT_THROWS_CODE(
        checkResumeToken->getNext(), AssertionException, ErrorCodes::ChangeStreamFatalError);
}

TEST_F(CheckResumeTokenTest, ShouldIgnoreChangeWithEarlierResumeToken) {
    Timestamp resumeTimestamp(100, 1);

    auto checkResumeToken = createDSEnsureResumeTokenPresent(resumeTimestamp, "1");

    // Add an entry into the oplog with the same timestamp but a lower documentKey. We swallow it
    // but don't throw - we haven't surpassed the token yet and still may see it in the next doc.
    addOplogEntryOnTestNS(resumeTimestamp, "0");
    ASSERT_TRUE(checkResumeToken->getNext().isEOF());
}

TEST_F(CheckResumeTokenTest, ShouldFailIfTokenHasWrongNamespace) {
    Timestamp resumeTimestamp(100, 1);

    auto resumeTokenUUID = UUID::gen();
    auto checkResumeToken = createDSEnsureResumeTokenPresent(resumeTimestamp, "1", resumeTokenUUID);
    auto otherUUID = UUID::gen();
    addOplogEntryOnTestNS(resumeTimestamp, "1", otherUUID);
    ASSERT_THROWS_CODE(
        checkResumeToken->getNext(), AssertionException, ErrorCodes::ChangeStreamFatalError);
}

TEST_F(CheckResumeTokenTest, ShouldSucceedWithBinaryCollation) {
    CollatorInterfaceMock collatorCompareLower(CollatorInterfaceMock::MockType::kToLowerString);
    getExpCtx()->setCollator(collatorCompareLower.clone());

    Timestamp resumeTimestamp(100, 1);

    auto checkResumeToken = createDSEnsureResumeTokenPresent(resumeTimestamp, "abc");
    // We must not see the following document.
    addOplogEntryOnTestNS(resumeTimestamp, "ABC");
    ASSERT_TRUE(checkResumeToken->getNext().isEOF());
}

TEST_F(CheckResumeTokenTest, UnshardedTokenSucceedsForShardedResumeOnMongosIfIdMatchesFirstDoc) {
    // Verify that a resume token whose documentKey only contains _id can be used to resume a stream
    // on a sharded collection as long as its _id matches the first document. We set 'inMongos'
    // since this behaviour is only applicable when
    // DocumentSourceChangeStreamEnsureResumeTokenPresent is running on mongoS.
    Timestamp resumeTimestamp(100, 1);
    getExpCtx()->inMongos = true;

    auto checkResumeToken =
        createDSEnsureResumeTokenPresent(resumeTimestamp, Document{{"_id"_sd, 1}});

    Timestamp doc1Timestamp(100, 1);
    addOplogEntryOnTestNS(doc1Timestamp, {{"x"_sd, 0}, {"_id"_sd, 1}});
    Timestamp doc2Timestamp(100, 2);
    Document doc2DocKey{{"x"_sd, 0}, {"_id"_sd, 2}};
    addOplogEntryOnTestNS(doc2Timestamp, doc2DocKey);

    // We should skip doc1 since it satisfies the resume token, and retrieve doc2.
    const auto firstDocAfterResume = checkResumeToken->getNext();
    const auto tokenFromFirstDocAfterResume =
        ResumeToken::parse(firstDocAfterResume.getDocument()["_id"].getDocument()).getData();

    ASSERT_EQ(tokenFromFirstDocAfterResume.clusterTime, doc2Timestamp);
    ASSERT_DOCUMENT_EQ(tokenFromFirstDocAfterResume.documentKey.getDocument(), doc2DocKey);
}

TEST_F(CheckResumeTokenTest, UnshardedTokenFailsForShardedResumeOnMongosIfIdDoesNotMatchFirstDoc) {
    Timestamp resumeTimestamp(100, 1);
    getExpCtx()->inMongos = true;

    auto checkResumeToken =
        createDSEnsureResumeTokenPresent(resumeTimestamp, Document{{"_id"_sd, 1}});

    addOplogEntryOnTestNS(Timestamp(100, 1), {{"x"_sd, 0}, {"_id"_sd, 0}});
    addOplogEntryOnTestNS(Timestamp(100, 2), {{"x"_sd, 0}, {"_id"_sd, 2}});

    ASSERT_THROWS_CODE(
        checkResumeToken->getNext(), AssertionException, ErrorCodes::ChangeStreamFatalError);
}

TEST_F(CheckResumeTokenTest, ShardedResumeFailsOnMongosIfTokenHasSubsetOfDocumentKeyFields) {
    // Verify that the relaxed _id check only applies if _id is the sole field present in the
    // client's resume token, even if all the fields that are present match the first doc. We set
    // 'inMongos' since this is only applicable when
    // DocumentSourceChangeStreamEnsureResumeTokenPresent is running on mongoS.
    Timestamp resumeTimestamp(100, 1);
    getExpCtx()->inMongos = true;

    auto checkResumeToken =
        createDSEnsureResumeTokenPresent(resumeTimestamp, Document{{"x"_sd, 0}, {"_id"_sd, 1}});

    addOplogEntryOnTestNS(Timestamp(100, 1), {{"x"_sd, 0}, {"y"_sd, -1}, {"_id"_sd, 1}});
    addOplogEntryOnTestNS(Timestamp(100, 2), {{"x"_sd, 0}, {"y"_sd, -1}, {"_id"_sd, 2}});

    ASSERT_THROWS_CODE(
        checkResumeToken->getNext(), AssertionException, ErrorCodes::ChangeStreamFatalError);
}

TEST_F(CheckResumeTokenTest, ShardedResumeFailsOnMongosIfDocumentKeyIsNonObject) {
    // Verify that a resume token whose documentKey is not a valid object will neither succeed nor
    // cause an invariant when we perform the relaxed documentKey._id check when running in a
    // sharded context.
    Timestamp resumeTimestamp(100, 1);
    getExpCtx()->inMongos = true;

    auto checkResumeToken = createDSEnsureResumeTokenPresent(resumeTimestamp, boost::none);

    addOplogEntryOnTestNS(Timestamp(100, 1), {{"x"_sd, 0}, {"_id"_sd, 1}});
    addOplogEntryOnTestNS(Timestamp(100, 2), {{"x"_sd, 0}, {"_id"_sd, 2}});

    ASSERT_THROWS_CODE(
        checkResumeToken->getNext(), AssertionException, ErrorCodes::ChangeStreamFatalError);
}

TEST_F(CheckResumeTokenTest, ShardedResumeFailsOnMongosIfDocumentKeyOmitsId) {
    // Verify that a resume token whose documentKey omits the _id field will neither succeed nor
    // cause an invariant when we perform the relaxed documentKey._id, even when compared against an
    // artificial stream token whose _id is also missing.
    Timestamp resumeTimestamp(100, 1);
    getExpCtx()->inMongos = true;

    auto checkResumeToken =
        createDSEnsureResumeTokenPresent(resumeTimestamp, Document{{"x"_sd, 0}});

    addOplogEntryOnTestNS(Timestamp(100, 1), {{"x"_sd, 0}, {"y"_sd, -1}, {"_id", 1}});
    addOplogEntryOnTestNS(Timestamp(100, 1), {{"x"_sd, 0}, {"y"_sd, -1}});
    addOplogEntryOnTestNS(Timestamp(100, 2), {{"x"_sd, 0}, {"y"_sd, -1}});

    ASSERT_THROWS_CODE(
        checkResumeToken->getNext(), AssertionException, ErrorCodes::ChangeStreamFatalError);
}

TEST_F(CheckResumeTokenTest,
       ShardedResumeSucceedsOnMongosWithSameClusterTimeIfUUIDsSortBeforeResumeToken) {
    // On a sharded cluster, the documents observed by the pipeline during a resume attempt may have
    // the same clusterTime if they come from different shards. If this is a whole-db or
    // cluster-wide changeStream, then their UUIDs may legitimately differ. As long as the UUID of
    // the current document sorts before the client's resume token, we should continue to examine
    // the next document in the stream.
    Timestamp resumeTimestamp(100, 1);
    getExpCtx()->inMongos = true;

    // Create an ordered array of 2 UUIDs.
    UUID uuids[2] = {UUID::gen(), UUID::gen()};

    if (uuids[0] > uuids[1]) {
        std::swap(uuids[0], uuids[1]);
    }

    // Create the resume token using the higher-sorting UUID.
    auto checkResumeToken =
        createDSEnsureResumeTokenPresent(resumeTimestamp, Document{{"_id"_sd, 1}}, uuids[1]);

    // Add two documents which have the same clusterTime but a lower UUID. One of the documents has
    // a lower docKey than the resume token, the other has a higher docKey; this demonstrates that
    // the UUID is the discriminating factor.
    addOplogEntryOnTestNS(resumeTimestamp, {{"_id"_sd, 0}}, uuids[0]);
    addOplogEntryOnTestNS(resumeTimestamp, {{"_id"_sd, 2}}, uuids[0]);

    // Add a third document that matches the resume token.
    addOplogEntryOnTestNS(resumeTimestamp, {{"_id"_sd, 1}}, uuids[1]);

    // Add a fourth document with the same timestamp and UUID whose docKey sorts after the token.
    auto expectedDocKey = Document{{"_id"_sd, 3}};
    addOplogEntryOnTestNS(resumeTimestamp, expectedDocKey, uuids[1]);

    // We should skip the first two docs, swallow the resume token, and return the fourth doc.
    const auto firstDocAfterResume = checkResumeToken->getNext();
    const auto tokenFromFirstDocAfterResume =
        ResumeToken::parse(firstDocAfterResume.getDocument()["_id"].getDocument()).getData();

    ASSERT_EQ(tokenFromFirstDocAfterResume.clusterTime, resumeTimestamp);
    ASSERT_DOCUMENT_EQ(tokenFromFirstDocAfterResume.documentKey.getDocument(), expectedDocKey);
}

TEST_F(CheckResumeTokenTest,
       ShardedResumeFailsOnMongosWithSameClusterTimeIfUUIDsSortAfterResumeToken) {
    Timestamp resumeTimestamp(100, 1);
    getExpCtx()->inMongos = true;

    // Create an ordered array of 2 UUIDs.
    UUID uuids[2] = {UUID::gen(), UUID::gen()};

    if (uuids[0] > uuids[1]) {
        std::swap(uuids[0], uuids[1]);
    }

    // Create the resume token using the lower-sorting UUID.
    auto checkResumeToken =
        createDSEnsureResumeTokenPresent(resumeTimestamp, Document{{"_id"_sd, 1}}, uuids[0]);

    // Add a document which has the same clusterTime and a lower docKey but a higher UUID, followed
    // by a document which matches the resume token. This is not possible in practice, but it serves
    // to demonstrate that the resume attempt fails even when the resume token is present.
    addOplogEntryOnTestNS(resumeTimestamp, {{"_id"_sd, 0}}, uuids[1]);
    addOplogEntryOnTestNS(resumeTimestamp, {{"_id"_sd, 1}}, uuids[0]);

    ASSERT_THROWS_CODE(
        checkResumeToken->getNext(), AssertionException, ErrorCodes::ChangeStreamFatalError);
}

TEST_F(CheckResumeTokenTest, ShouldSwallowInvalidateFromEachShardForStartAfterInvalidate) {
    Timestamp resumeTimestamp(100, 1);
    Timestamp firstEventAfter(100, 2);

    // Create an array of 2 UUIDs. The first represents the UUID of the namespace before it was
    // dropped. The second is the UUID of the collection after it is recreated.
    UUID uuids[2] = {UUID::gen(), UUID::gen()};

    // This behaviour is only relevant when DSEnsureResumeTokenPresent is running on mongoS.
    getExpCtx()->inMongos = true;

    // Create a resume token representing an 'invalidate' event, and use it to seed the stage. A
    // resume token with {fromInvalidate:true} can only be used with startAfter, to start a new
    // stream after the old stream is invalidated.
    ResumeTokenData invalidateToken;
    invalidateToken.clusterTime = resumeTimestamp;
    invalidateToken.uuid = uuids[0];
    invalidateToken.fromInvalidate = ResumeTokenData::kFromInvalidate;
    auto checkResumeToken = createDSEnsureResumeTokenPresent(invalidateToken);

    // Add three documents which each have the invalidate resume token. We expect to see this in the
    // event that we are starting after an invalidate and the invalidating event occurred on several
    // shards at the same clusterTime.
    addOplogEntryOnTestNS(invalidateToken);
    addOplogEntryOnTestNS(invalidateToken);
    addOplogEntryOnTestNS(invalidateToken);

    // Add a document representing an insert which recreated the collection after it was dropped.
    auto expectedDocKey = Document{{"_id"_sd, 1}};
    addOplogEntryOnTestNS(Timestamp{100, 2}, expectedDocKey, uuids[1]);

    // DSEnsureResumeTokenPresent should confirm that the invalidate event is present, swallow it
    // and the next two invalidates, and return the insert event after the collection drop.
    const auto firstDocAfterResume = checkResumeToken->getNext();
    const auto tokenFromFirstDocAfterResume =
        ResumeToken::parse(firstDocAfterResume.getDocument()["_id"].getDocument()).getData();

    ASSERT_EQ(tokenFromFirstDocAfterResume.clusterTime, firstEventAfter);
    ASSERT_DOCUMENT_EQ(tokenFromFirstDocAfterResume.documentKey.getDocument(), expectedDocKey);
}

TEST_F(CheckResumeTokenTest, ShouldNotSwallowUnrelatedInvalidateForStartAfterInvalidate) {
    Timestamp resumeTimestamp(100, 1);

    // This behaviour is only relevant when DSEnsureResumeTokenPresent is running on mongoS.
    getExpCtx()->inMongos = true;

    // Create an ordered array of of 2 UUIDs.
    std::vector<UUID> uuids = {UUID::gen(), UUID::gen()};
    std::sort(uuids.begin(), uuids.end());

    // Create a resume token representing an 'invalidate' event, and use it to seed the stage. A
    // resume token with {fromInvalidate:true} can only be used with startAfter, to start a new
    // stream after the old stream is invalidated.
    ResumeTokenData invalidateToken;
    invalidateToken.clusterTime = resumeTimestamp;
    invalidateToken.uuid = uuids[0];
    invalidateToken.fromInvalidate = ResumeTokenData::kFromInvalidate;
    auto checkResumeToken = createDSEnsureResumeTokenPresent(invalidateToken);

    // Create a second invalidate token with the same clusterTime but a different UUID.
    auto unrelatedInvalidateToken = invalidateToken;
    unrelatedInvalidateToken.uuid = uuids[1];

    // Add three documents which each have the invalidate resume token. We expect to see this in the
    // event that we are starting after an invalidate and the invalidating event occurred on several
    // shards at the same clusterTime.
    addOplogEntryOnTestNS(invalidateToken);
    addOplogEntryOnTestNS(invalidateToken);
    addOplogEntryOnTestNS(invalidateToken);

    // Add a fourth document which has the unrelated invalidate at the same clusterTime.
    addOplogEntryOnTestNS(unrelatedInvalidateToken);

    // DSEnsureResumeTokenPresent should confirm that the invalidate event is present, swallow it
    // and the next two invalidates, but decline to swallow the unrelated invalidate.
    const auto firstDocAfterResume = checkResumeToken->getNext();
    const auto tokenFromFirstDocAfterResume =
        ResumeToken::parse(firstDocAfterResume.getDocument()["_id"].getDocument()).getData();

    ASSERT_EQ(tokenFromFirstDocAfterResume, unrelatedInvalidateToken);
}

TEST_F(CheckResumeTokenTest, ShouldSkipResumeTokensWithEarlierTxnOpIndex) {
    Timestamp resumeTimestamp(100, 1);

    // Create an ordered array of 3 UUIDs.
    std::vector<UUID> uuids = {UUID::gen(), UUID::gen(), UUID::gen()};

    std::sort(uuids.begin(), uuids.end());

    auto checkResumeToken =
        createDSEnsureResumeTokenPresent(resumeTimestamp, 0, 2, Document{{"_id"_sd, 1}}, uuids[1]);

    // Add two documents which have the same clusterTime and version but a lower applyOps index. One
    // of the documents has a lower uuid than the resume token, the other has a higher uuid; this
    // demonstrates that the applyOps index is the discriminating factor.
    addOplogEntryOnTestNS(resumeTimestamp, 0, 0, {{"_id"_sd, 0}}, uuids[0]);
    addOplogEntryOnTestNS(resumeTimestamp, 0, 1, {{"_id"_sd, 2}}, uuids[2]);

    // Add a third document that matches the resume token.
    addOplogEntryOnTestNS(resumeTimestamp, 0, 2, {{"_id"_sd, 1}}, uuids[1]);

    // Add a fourth document with the same timestamp and version whose applyOps sorts after the
    // resume token.
    auto expectedDocKey = Document{{"_id"_sd, 3}};
    addOplogEntryOnTestNS(resumeTimestamp, 0, 3, expectedDocKey, uuids[1]);

    // We should skip the first two docs, swallow the resume token, and return the fourth doc.
    const auto firstDocAfterResume = checkResumeToken->getNext();
    const auto tokenFromFirstDocAfterResume =
        ResumeToken::parse(firstDocAfterResume.getDocument()["_id"].getDocument()).getData();

    ASSERT_EQ(tokenFromFirstDocAfterResume.clusterTime, resumeTimestamp);
    ASSERT_DOCUMENT_EQ(tokenFromFirstDocAfterResume.documentKey.getDocument(), expectedDocKey);
}

/**
 * We should _error_ on the no-document case, because that means the resume token was not found.
 */
TEST_F(CheckResumeTokenTest, ShouldSucceedWithNoDocuments) {
    Timestamp resumeTimestamp(100, 1);

    auto checkResumeToken = createDSEnsureResumeTokenPresent(resumeTimestamp, "0");
    ASSERT_TRUE(checkResumeToken->getNext().isEOF());
}

TEST_F(CheckResumabilityTest, ShouldSucceedIfResumeTokenIsPresentAndEarliestOplogEntryBeforeToken) {
    Timestamp oplogTimestamp(100, 1);
    Timestamp resumeTimestamp(100, 2);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    addOplogEntryOnTestNS(resumeTimestamp, "ID");
    // We should see the resume token.
    auto result = dsCheckResumability->getNext();
    ASSERT_TRUE(result.isAdvanced());
    auto& doc = result.getDocument();
    ASSERT_EQ(resumeTimestamp, ResumeToken::parse(doc["_id"].getDocument()).getData().clusterTime);
}

TEST_F(CheckResumabilityTest,
       ShouldSucceedIfResumeTokenIsPresentAndEarliestOplogEntryEqualToToken) {
    Timestamp resumeTimestamp(100, 1);
    Timestamp oplogTimestamp(100, 1);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    addOplogEntryOnTestNS(resumeTimestamp, "ID");
    // We should see the resume token.
    auto result = dsCheckResumability->getNext();
    ASSERT_TRUE(result.isAdvanced());
    auto& doc = result.getDocument();
    ASSERT_EQ(resumeTimestamp, ResumeToken::parse(doc["_id"].getDocument()).getData().clusterTime);
}

TEST_F(CheckResumabilityTest, ShouldPermanentlyEofIfOplogIsEmpty) {
    Timestamp resumeTimestamp(100, 1);

    // As with other tailable cursors, starting a change stream on an empty capped collection will
    // cause the cursor to immediately and permanently EOF. This should never happen in practice,
    // since a replset member can only accept requests while in PRIMARY, SECONDARY or RECOVERING
    // states, and there must be at least one entry in the oplog in order to reach those states.
    auto shardCheckResumability = createDSCheckResumability(resumeTimestamp);
    auto result = shardCheckResumability->getNext();
    ASSERT_TRUE(result.isEOF());
    ASSERT_TRUE(_mock->isPermanentlyEOF());
}

TEST_F(CheckResumabilityTest,
       ShouldSucceedWithNoDocumentsInPipelineAndEarliestOplogEntryBeforeToken) {
    Timestamp oplogTimestamp(100, 1);
    Timestamp resumeTimestamp(100, 2);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    auto result = dsCheckResumability->getNext();
    ASSERT_TRUE(result.isEOF());
}

TEST_F(CheckResumabilityTest,
       ShouldSucceedWithNoDocumentsInPipelineAndEarliestOplogEntryEqualToToken) {
    Timestamp oplogTimestamp(100, 1);
    Timestamp resumeTimestamp(100, 1);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    auto result = dsCheckResumability->getNext();
    ASSERT_TRUE(result.isEOF());
}

TEST_F(CheckResumabilityTest, ShouldFailWithNoDocumentsInPipelineAndEarliestOplogEntryAfterToken) {
    Timestamp resumeTimestamp(100, 1);
    Timestamp oplogTimestamp(100, 2);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    ASSERT_THROWS_CODE(
        dsCheckResumability->getNext(), AssertionException, ErrorCodes::ChangeStreamHistoryLost);
}

TEST_F(CheckResumabilityTest, ShouldSucceedWithNoDocumentsInPipelineAndOplogIsEmpty) {
    Timestamp resumeTimestamp(100, 2);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    auto result = dsCheckResumability->getNext();
    ASSERT_TRUE(result.isEOF());
}

TEST_F(CheckResumabilityTest,
       ShouldSucceedWithLaterDocumentsInPipelineAndEarliestOplogEntryBeforeToken) {
    Timestamp oplogTimestamp(100, 1);
    Timestamp resumeTimestamp(100, 2);
    Timestamp docTimestamp(100, 3);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    addOplogEntryOnTestNS(docTimestamp, "ID");
    auto result = dsCheckResumability->getNext();
    ASSERT_TRUE(result.isAdvanced());
    auto& doc = result.getDocument();
    ASSERT_EQ(docTimestamp, ResumeToken::parse(doc["_id"].getDocument()).getData().clusterTime);
}

TEST_F(CheckResumabilityTest,
       ShouldSucceedWithLaterDocumentsInPipelineAndEarliestOplogEntryEqualToToken) {
    Timestamp oplogTimestamp(100, 1);
    Timestamp resumeTimestamp(100, 1);
    Timestamp docTimestamp(100, 3);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    addOplogEntryOnTestNS(docTimestamp, "ID");
    auto result = dsCheckResumability->getNext();
    ASSERT_TRUE(result.isAdvanced());
    auto& doc = result.getDocument();
    ASSERT_EQ(docTimestamp, ResumeToken::parse(doc["_id"].getDocument()).getData().clusterTime);
}

TEST_F(CheckResumabilityTest,
       ShouldFailWithLaterDocumentsInPipelineAndEarliestOplogEntryAfterToken) {
    Timestamp resumeTimestamp(100, 1);
    Timestamp oplogTimestamp(100, 2);
    Timestamp docTimestamp(100, 3);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    addOplogEntryOnTestNS(docTimestamp, "ID");
    ASSERT_THROWS_CODE(
        dsCheckResumability->getNext(), AssertionException, ErrorCodes::ChangeStreamHistoryLost);
}

TEST_F(CheckResumabilityTest,
       ShouldFailWithoutReadingLaterDocumentsInPipelineIfEarliestOplogEntryAfterToken) {
    Timestamp resumeTimestamp(100, 1);
    Timestamp oplogTimestamp(100, 2);
    Timestamp docTimestamp(100, 3);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    addOplogEntryOnTestNS(docTimestamp, "ID");
    // Confirm that there are two documents queued in the mock oplog.
    ASSERT_EQ(_mock->size(), 2);
    ASSERT_THROWS_CODE(
        dsCheckResumability->getNext(), AssertionException, ErrorCodes::ChangeStreamHistoryLost);
    // Confirm that only the first document was read before the assertion was thrown.
    ASSERT_EQ(_mock->size(), 1);
}

TEST_F(CheckResumabilityTest, ShouldIgnoreOplogAfterFirstDoc) {
    Timestamp oplogTimestamp(100, 1);
    Timestamp resumeTimestamp(100, 2);
    Timestamp oplogFutureTimestamp(100, 3);
    Timestamp docTimestamp(100, 4);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    addOplogEntryOnTestNS(docTimestamp, "ID");
    auto result1 = dsCheckResumability->getNext();
    ASSERT_TRUE(result1.isAdvanced());
    auto& doc1 = result1.getDocument();
    ASSERT_EQ(docTimestamp, ResumeToken::parse(doc1["_id"].getDocument()).getData().clusterTime);

    addOplogEntryOnOtherNS(oplogFutureTimestamp);
    auto result2 = dsCheckResumability->getNext();
    ASSERT_TRUE(result2.isEOF());
}

TEST_F(CheckResumabilityTest, ShouldSucceedWhenOplogEntriesExistBeforeAndAfterResumeToken) {
    Timestamp oplogTimestamp(100, 1);
    Timestamp resumeTimestamp(100, 2);
    Timestamp oplogFutureTimestamp(100, 3);
    Timestamp docTimestamp(100, 4);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    addOplogEntryOnOtherNS(oplogFutureTimestamp);
    addOplogEntryOnTestNS(docTimestamp, "ID");

    auto result1 = dsCheckResumability->getNext();
    ASSERT_TRUE(result1.isAdvanced());
    auto& doc1 = result1.getDocument();
    ASSERT_EQ(docTimestamp, ResumeToken::parse(doc1["_id"].getDocument()).getData().clusterTime);
    auto result2 = dsCheckResumability->getNext();
    ASSERT_TRUE(result2.isEOF());
}

TEST_F(CheckResumabilityTest, ShouldIgnoreOplogAfterFirstEOF) {
    Timestamp oplogTimestamp(100, 1);
    Timestamp resumeTimestamp(100, 2);
    Timestamp oplogFutureTimestamp(100, 3);

    auto dsCheckResumability = createDSCheckResumability(resumeTimestamp);
    addOplogEntryOnOtherNS(oplogTimestamp);
    auto result1 = dsCheckResumability->getNext();
    ASSERT_TRUE(result1.isEOF());

    addOplogEntryOnOtherNS(oplogFutureTimestamp);
    auto result2 = dsCheckResumability->getNext();
    ASSERT_TRUE(result2.isEOF());
}

TEST_F(CheckResumabilityTest, ShouldSwallowAllEventsAtSameClusterTimeUpToResumeToken) {
    Timestamp resumeTimestamp(100, 2);

    // Set up the DocumentSourceChangeStreamCheckResumability to check for an exact event
    // ResumeToken.
    ResumeTokenData token(resumeTimestamp, 0, 0, testUuid(), Value(Document{{"_id"_sd, "3"_sd}}));
    auto dsCheckResumability = createDSCheckResumability(token);

    // Add 2 events at the same clusterTime as the resume token but whose docKey sort before it.
    addOplogEntryOnTestNS(resumeTimestamp, "1");
    addOplogEntryOnTestNS(resumeTimestamp, "2");
    // Add the resume token, plus one further event whose docKey sorts after the token.
    addOplogEntryOnTestNS(resumeTimestamp, "3");
    addOplogEntryOnTestNS(resumeTimestamp, "4");

    // The first event we see should be the resume token...
    auto result = dsCheckResumability->getNext();
    ASSERT_TRUE(result.isAdvanced());
    auto doc = result.getDocument();
    ASSERT_EQ(token, ResumeToken::parse(doc["_id"].getDocument()).getData());
    // ... then the post-token event, and then finally EOF.
    result = dsCheckResumability->getNext();
    ASSERT_TRUE(result.isAdvanced());
    auto postResumeTokenDoc =
        ResumeToken({resumeTimestamp, 0, 0, testUuid(), Value(Document{{"_id"_sd, "4"_sd}})})
            .toDocument();
    ASSERT_DOCUMENT_EQ(result.getDocument()["_id"].getDocument(), postResumeTokenDoc);
    ASSERT_TRUE(dsCheckResumability->getNext().isEOF());
}

TEST_F(CheckResumabilityTest, ShouldSwallowAllEventsAtSameClusterTimePriorToResumeToken) {
    Timestamp resumeTimestamp(100, 2);

    // Set up the DocumentSourceChangeStreamCheckResumability to check for an exact event
    // ResumeToken.
    ResumeTokenData token(resumeTimestamp, 0, 0, testUuid(), Value(Document{{"_id"_sd, "3"_sd}}));
    auto dsCheckResumability = createDSCheckResumability(token);

    // Add 2 events at the same clusterTime as the resume token but whose docKey sort before it.
    addOplogEntryOnTestNS(resumeTimestamp, "1");
    addOplogEntryOnTestNS(resumeTimestamp, "2");
    // Add one further event whose docKey sorts after the token.
    addOplogEntryOnTestNS(resumeTimestamp, "4");

    // The first event we see should be the post-token event, followed by EOF.
    auto result = dsCheckResumability->getNext();
    ASSERT_TRUE(result.isAdvanced());
    auto postResumeTokenDoc =
        ResumeToken({resumeTimestamp, 0, 0, testUuid(), Value(Document{{"_id"_sd, "4"_sd}})})
            .toDocument();
    ASSERT_DOCUMENT_EQ(result.getDocument()["_id"].getDocument(), postResumeTokenDoc);
    ASSERT_TRUE(dsCheckResumability->getNext().isEOF());
}

}  // namespace
}  // namespace mongo