summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/operation_sequencer_test.cpp
blob: af9112aaeecbbbb52198b0514f827e7ab4a18a39 (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vdstestlib/cppunit/macros.h>
#include <vespa/storage/distributor/operation_sequencer.h>
#include <vespa/document/base/documentid.h>

namespace storage::distributor {

using document::DocumentId;

class OperationSequencerTest : public CppUnit::TestFixture {
    CPPUNIT_TEST_SUITE(OperationSequencerTest);
    CPPUNIT_TEST(can_get_sequencing_handle_for_id_without_existing_handle);
    CPPUNIT_TEST(can_get_sequencing_handle_for_different_ids);
    CPPUNIT_TEST(cannot_get_sequencing_handle_for_id_with_existing_handle);
    CPPUNIT_TEST(releasing_handle_allows_for_getting_new_handles_for_id);
    CPPUNIT_TEST_SUITE_END();

    void can_get_sequencing_handle_for_id_without_existing_handle();
    void can_get_sequencing_handle_for_different_ids();
    void cannot_get_sequencing_handle_for_id_with_existing_handle();
    void releasing_handle_allows_for_getting_new_handles_for_id();
};

CPPUNIT_TEST_SUITE_REGISTRATION(OperationSequencerTest);

void OperationSequencerTest::can_get_sequencing_handle_for_id_without_existing_handle() {
    OperationSequencer sequencer;
    auto handle = sequencer.try_acquire(DocumentId("id:foo:test::abcd"));
    CPPUNIT_ASSERT(handle.valid());
}

void OperationSequencerTest::cannot_get_sequencing_handle_for_id_with_existing_handle() {
    OperationSequencer sequencer;
    auto first_handle = sequencer.try_acquire(DocumentId("id:foo:test::abcd"));
    auto second_handle = sequencer.try_acquire(DocumentId("id:foo:test::abcd"));
    CPPUNIT_ASSERT(! second_handle.valid());
}

void OperationSequencerTest::can_get_sequencing_handle_for_different_ids() {
    OperationSequencer sequencer;
    auto first_handle = sequencer.try_acquire(DocumentId("id:foo:test::abcd"));
    auto second_handle = sequencer.try_acquire(DocumentId("id:foo:test::efgh"));
    CPPUNIT_ASSERT(first_handle.valid());
    CPPUNIT_ASSERT(second_handle.valid());
}

void OperationSequencerTest::releasing_handle_allows_for_getting_new_handles_for_id() {
    OperationSequencer sequencer;
    auto first_handle = sequencer.try_acquire(DocumentId("id:foo:test::abcd"));
    // Explicit release
    first_handle.release();
    {
        auto second_handle = sequencer.try_acquire(DocumentId("id:foo:test::abcd"));
        CPPUNIT_ASSERT(second_handle.valid());
        // Implicit release by scope exit
    }
    auto third_handle = sequencer.try_acquire(DocumentId("id:foo:test::abcd"));
    CPPUNIT_ASSERT(third_handle.valid());
}

} // storage::distributor