aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/common/dummystoragelink.cpp
blob: 4017fab893d506db34247ba48df92262e72a96e7 (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
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "dummystoragelink.h"
#include <vespa/storageframework/defaultimplementation/clock/realclock.h>
#include <vespa/vespalib/util/exceptions.h>
#include <cassert>

namespace storage {

DummyStorageLink* DummyStorageLink::_last(nullptr);

DummyStorageLink::DummyStorageLink()
    : StorageLink("Dummy storage link"),
      _commands(),
      _replies(),
      _injected(),
      _autoReply(false),
      _useDispatch(false),
      _ignore(false),
      _waitMonitor()
{
    _last = this;
}

DummyStorageLink::~DummyStorageLink()
{
        // Often a chain with dummy link on top is deleted in unit tests.
        // If they haven't been closed already, close them for a cleaner
        // shutdown
    if (getState() == OPENED) {
        close();
        flush();
    }
    closeNextLink();
    reset();
}

bool
DummyStorageLink::handleInjectedReply()
{
    std::lock_guard guard(_lock);
    if (!_injected.empty()) {
        sendUp(*_injected.begin());
        _injected.pop_front();
        return true;
    }
    return false;
}

bool DummyStorageLink::onDown(const api::StorageMessage::SP& cmd)
{
    if (_ignore) {
        return false;
    }
    bool injected = handleInjectedReply();
    if (!injected && _autoReply) {
        if (!cmd->getType().isReply()) {
            std::shared_ptr<api::StorageReply> reply(
                    std::dynamic_pointer_cast<api::StorageCommand>(cmd)
                    ->makeReply().release());
            reply->setResult(api::ReturnCode(
                    api::ReturnCode::OK, "Automatically generated reply"));
            sendUp(reply);
        }
    }
    if (isBottom()) {
        std::lock_guard lock(_waitMonitor);
        {
            std::lock_guard guard(_lock);
            _commands.push_back(cmd);
        }
        _waitCond.notify_all();
        return true;
    }
    return StorageLink::onDown(cmd);
}

bool DummyStorageLink::onUp(const api::StorageMessage::SP& reply) {
    if (isTop()) {
        std::lock_guard lock(_waitMonitor);
        {
            std::lock_guard guard(_lock);
            _replies.push_back(reply);
        }
        _waitCond.notify_all();
        return true;
    }
    return StorageLink::onUp(reply);

}

void DummyStorageLink::injectReply(api::StorageReply* reply)
{
    assert(reply);
    std::lock_guard guard(_lock);
    _injected.push_back(std::shared_ptr<api::StorageReply>(reply));
}

void DummyStorageLink::reset() {
    std::lock_guard lock(_waitMonitor);
    std::lock_guard guard(_lock);
    _commands.clear();
    _replies.clear();
    _injected.clear();
}

void DummyStorageLink::waitForMessages(unsigned int msgCount, int timeout)
{
    framework::defaultimplementation::RealClock clock;
    vespalib::steady_time endTime = clock.getMonotonicTime() + vespalib::from_s(timeout);
    std::unique_lock guard(_waitMonitor);
    while (_commands.size() + _replies.size() < msgCount) {
        if (timeout != 0 && clock.getMonotonicTime() > endTime) {
            std::ostringstream ost;
            ost << "Timed out waiting for " << msgCount << " messages to "
                << "arrive in dummy storage link. Only "
                << (_commands.size() + _replies.size()) << " messages seen "
                << "after timout of " << timeout << " seconds was reached.";
            throw vespalib::IllegalStateException(ost.str(), VESPA_STRLOC);
        }
        if (timeout >= 0) {
            _waitCond.wait_until(guard, endTime);
        } else {
            _waitCond.wait(guard);
        }
    }
}

void DummyStorageLink::waitForMessage(const api::MessageType& type, int timeout)
{
    framework::defaultimplementation::RealClock clock;
    vespalib::steady_time endTime = clock.getMonotonicTime() + vespalib::from_s(timeout);
    std::unique_lock lock(_waitMonitor);
    while (true) {
        for (uint32_t i=0; i<_commands.size(); ++i) {
            if (_commands[i]->getType() == type) return;
        }
        for (uint32_t i=0; i<_replies.size(); ++i) {
            if (_replies[i]->getType() == type) return;
        }
        if (timeout != 0 && clock.getMonotonicTime() > endTime) {
            std::ostringstream ost;
            ost << "Timed out waiting for " << type << " message to "
                << "arrive in dummy storage link. Only "
                << (_commands.size() + _replies.size()) << " messages seen "
                << "after timout of " << timeout << " seconds was reached.";
            if (_commands.size() == 1) {
                ost << " Found command of type " << _commands[0]->getType();
            }
            if (_replies.size() == 1) {
                ost << " Found command of type " << _replies[0]->getType();
            }
            throw vespalib::IllegalStateException(ost.str(), VESPA_STRLOC);
        }
        if (timeout >= 0) {
            _waitCond.wait_until(lock, endTime);
        } else {
            _waitCond.wait(lock);
        }
    }
}

api::StorageMessage::SP
DummyStorageLink::getAndRemoveMessage(const api::MessageType& type)
{
    std::lock_guard lock(_waitMonitor);
    for (std::vector<api::StorageMessage::SP>::iterator it = _commands.begin();
         it != _commands.end(); ++it)
    {
        if ((*it)->getType() == type) {
            api::StorageMessage::SP result(*it);
            _commands.erase(it);
            return result;
        }
    }
    for (std::vector<api::StorageMessage::SP>::iterator it = _replies.begin();
         it != _replies.end(); ++it)
    {
        if ((*it)->getType() == type) {
            api::StorageMessage::SP result(*it);
            _replies.erase(it);
            return result;
        }
    }
    std::ostringstream ost;
    ost << "No message of type " << type << " found.";
    throw vespalib::IllegalStateException(ost.str(), VESPA_STRLOC);
}

} // storage