summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/throttlingoperationstartertest.cpp
blob: 8e7d98005c1a7e4e32f03a283377bea4b6279ccc (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
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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/vdstestlib/cppunit/macros.h>
#include <string>
#include <sstream>
#include <memory>
#include <vespa/storage/distributor/throttlingoperationstarter.h>
#include <tests/distributor/maintenancemocks.h>

namespace storage {

namespace distributor {

using document::BucketId;

class ThrottlingOperationStarterTest : public CppUnit::TestFixture {
    CPPUNIT_TEST_SUITE(ThrottlingOperationStarterTest);
    CPPUNIT_TEST(testOperationNotThrottledWhenSlotAvailable);
    CPPUNIT_TEST(testOperationStartingIsForwardedToImplementation);
    CPPUNIT_TEST(testOperationThrottledWhenNoAvailableSlots);
    CPPUNIT_TEST(testThrottlingWithMaxPendingRange);
    CPPUNIT_TEST(testStartingOperationsFillsUpPendingWindow);
    CPPUNIT_TEST(testFinishingOperationsAllowsMoreToStart);
    CPPUNIT_TEST_SUITE_END();

    std::shared_ptr<Operation> createMockOperation() {
        return std::shared_ptr<Operation>(new MockOperation(BucketId(16, 1)));
    }

    std::unique_ptr<MockOperationStarter> _starterImpl;
    std::unique_ptr<ThrottlingOperationStarter> _operationStarter;

public:
    void testOperationNotThrottledWhenSlotAvailable();
    void testOperationStartingIsForwardedToImplementation();
    void testOperationThrottledWhenNoAvailableSlots();
    void testThrottlingWithMaxPendingRange();
    void testStartingOperationsFillsUpPendingWindow();
    void testFinishingOperationsAllowsMoreToStart();

    void setUp() override;
    void tearDown() override;
};

CPPUNIT_TEST_SUITE_REGISTRATION(ThrottlingOperationStarterTest);

void
ThrottlingOperationStarterTest::setUp()
{
    _starterImpl.reset(new MockOperationStarter());
    _operationStarter.reset(new ThrottlingOperationStarter(*_starterImpl));
}

void
ThrottlingOperationStarterTest::tearDown()
{
    // Must clear before _operationStarter goes out of scope, or operation
    // destructors will try to call method on destroyed object.
    _starterImpl->getOperations().clear();
}

void
ThrottlingOperationStarterTest::testOperationNotThrottledWhenSlotAvailable()
{
    CPPUNIT_ASSERT(_operationStarter->start(createMockOperation(),
                                            OperationStarter::Priority(0)));
}

void
ThrottlingOperationStarterTest::testOperationStartingIsForwardedToImplementation()
{
    CPPUNIT_ASSERT(_operationStarter->start(createMockOperation(),
                                            OperationStarter::Priority(0)));
    CPPUNIT_ASSERT_EQUAL(std::string("BucketId(0x4000000000000001), pri 0\n"),
                         _starterImpl->toString());
}

void
ThrottlingOperationStarterTest::testOperationThrottledWhenNoAvailableSlots()
{
    _operationStarter->setMaxPendingRange(0, 0);
    CPPUNIT_ASSERT(!_operationStarter->start(createMockOperation(),
                                             OperationStarter::Priority(0)));
}

void
ThrottlingOperationStarterTest::testThrottlingWithMaxPendingRange()
{
    _operationStarter->setMaxPendingRange(0, 1);
    CPPUNIT_ASSERT(!_operationStarter->canStart(0, OperationStarter::Priority(255)));
    CPPUNIT_ASSERT(_operationStarter->canStart(0, OperationStarter::Priority(0)));

    _operationStarter->setMaxPendingRange(1, 1);
    CPPUNIT_ASSERT(_operationStarter->canStart(0, OperationStarter::Priority(255)));
    CPPUNIT_ASSERT(_operationStarter->canStart(0, OperationStarter::Priority(0)));

    _operationStarter->setMaxPendingRange(1, 3);
    CPPUNIT_ASSERT(!_operationStarter->canStart(1, OperationStarter::Priority(255)));
    CPPUNIT_ASSERT(_operationStarter->canStart(1, OperationStarter::Priority(100)));
    CPPUNIT_ASSERT(_operationStarter->canStart(1, OperationStarter::Priority(0)));
    CPPUNIT_ASSERT(_operationStarter->canStart(2, OperationStarter::Priority(0)));
    CPPUNIT_ASSERT(!_operationStarter->canStart(3, OperationStarter::Priority(0)));
    CPPUNIT_ASSERT(!_operationStarter->canStart(4, OperationStarter::Priority(0)));
}

void
ThrottlingOperationStarterTest::testStartingOperationsFillsUpPendingWindow()
{
    _operationStarter->setMaxPendingRange(1, 3);
    CPPUNIT_ASSERT(_operationStarter->start(createMockOperation(),
                                            OperationStarter::Priority(255)));
    CPPUNIT_ASSERT(!_operationStarter->start(createMockOperation(),
                                             OperationStarter::Priority(255)));
    CPPUNIT_ASSERT(_operationStarter->start(createMockOperation(),
                                            OperationStarter::Priority(100)));
    CPPUNIT_ASSERT(!_operationStarter->start(createMockOperation(),
                                             OperationStarter::Priority(100)));
    CPPUNIT_ASSERT(_operationStarter->start(createMockOperation(),
                                            OperationStarter::Priority(0)));
    CPPUNIT_ASSERT(!_operationStarter->start(createMockOperation(),
                                             OperationStarter::Priority(0)));
}

void
ThrottlingOperationStarterTest::testFinishingOperationsAllowsMoreToStart()
{
    _operationStarter->setMaxPendingRange(1, 1);
    CPPUNIT_ASSERT(_operationStarter->start(createMockOperation(),
                                            OperationStarter::Priority(255)));
    CPPUNIT_ASSERT(!_operationStarter->start(createMockOperation(),
                                             OperationStarter::Priority(255)));
    CPPUNIT_ASSERT(!_starterImpl->getOperations().empty());

    _starterImpl->getOperations().pop_back();

    CPPUNIT_ASSERT(_operationStarter->start(createMockOperation(),
                                            OperationStarter::Priority(255)));
    CPPUNIT_ASSERT(!_starterImpl->getOperations().empty());
}

}
}