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
// Copyright (C) 2021-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <asiolink/asio_wrapper.h>
#include <asiolink/interval_timer.h>
#include <asiolink/io_service.h>
#include <asiolink/io_service_thread_pool.h>
#include <exceptions/exceptions.h>
#include <testutils/gtest_utils.h>

#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc;
using namespace isc::asiolink;

namespace {

/// @brief Test timeout (ms).
const long TEST_TIMEOUT = 10000;

/// @brief Simple test fixture for testing IoServiceThreadPool.
class IoServiceThreadPoolTest : public ::testing::Test {
public:
    /// @brief Constructor.
    IoServiceThreadPoolTest()
        : io_service_(new IOService()) {
    }

    /// @brief Destructor.
    virtual ~IoServiceThreadPoolTest() {
        io_service_->stop();
    }

    /// @brief IOService instance used by thread pools.
    IOServicePtr io_service_;
};

// URL contains scheme and hostname.
TEST_F(IoServiceThreadPoolTest, invalidConstruction) {<--- syntax error
    IoServiceThreadPoolPtr pool;

    // Constructing with pool size of 0 should fail.
    ASSERT_THROW_MSG(pool.reset(new IoServiceThreadPool(io_service_, 0)),
                     BadValue, "pool_size must be non 0");
}

// Verifies that a pool can be created without starting it.
TEST_F(IoServiceThreadPoolTest, deferredStartConstruction) {
    IoServiceThreadPoolPtr pool;

    ASSERT_NO_THROW_LOG(pool.reset(new IoServiceThreadPool(io_service_, 3, true)));

    // State should be stopped.
    // Pool size should be 3.
    // IOService should be there.
    // IOService is new, so it should not be stopped.
    // No threads in the pool.
    ASSERT_TRUE(pool->isStopped());
    EXPECT_EQ(pool->getPoolSize(), 3);
    ASSERT_TRUE(pool->getIOService());
    EXPECT_FALSE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 0);

    // Destructor should not throw.
    ASSERT_NO_THROW_LOG(pool.reset());
}

// Verifies that a pool can be started within the constructor.
TEST_F(IoServiceThreadPoolTest, startDuringConstruction) {
    IoServiceThreadPoolPtr pool;

    ASSERT_NO_THROW_LOG(pool.reset(new IoServiceThreadPool(io_service_, 3)));

    // State should be running.
    // Pool size should be 3.
    // IOService should be there.
    // IOService is new, so it should not be stopped.
    // Should have 3 threads in the pool.
    ASSERT_TRUE(pool->isRunning());
    EXPECT_EQ(pool->getPoolSize(), 3);
    ASSERT_TRUE(pool->getIOService());
    EXPECT_FALSE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 3);

    // Destructor should not throw.
    ASSERT_NO_THROW_LOG(pool.reset());
}

// Verifies that pool can move from STOPPED to RUNNING.
TEST_F(IoServiceThreadPoolTest, stoppedToRunning) {
    IoServiceThreadPoolPtr pool;

    // Create a stopped pool.
    ASSERT_NO_THROW_LOG(pool.reset(new IoServiceThreadPool(io_service_, 3, true)));
    ASSERT_TRUE(pool->isStopped());

    // Call run from STOPPED.
    ASSERT_NO_THROW_LOG(pool->run());

    // State should be RUNNING, IOService should not be stopped, we should
    // have 3 threads in the pool.
    ASSERT_TRUE(pool->isRunning());
    EXPECT_FALSE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 3);

    // Calling run again should be harmless.
    ASSERT_NO_THROW_LOG(pool->run());

    // State should be RUNNING, IOService should not be stopped, we should
    // have 3 threads in the pool.
    ASSERT_TRUE(pool->isRunning());
    EXPECT_FALSE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 3);

    // Destroying the pool should be fine.
    ASSERT_NO_THROW_LOG(pool.reset());
}

// Verifies that pool can move from RUNNING to STOPPED.
TEST_F(IoServiceThreadPoolTest, runningToStopped) {
    IoServiceThreadPoolPtr pool;

    // Create a running pool.
    ASSERT_NO_THROW_LOG(pool.reset(new IoServiceThreadPool(io_service_, 3, false)));
    ASSERT_TRUE(pool->isRunning());

    // Call stop.
    ASSERT_NO_THROW_LOG(pool->stop());

    // State should be STOPPED, IOService should be stopped, we should
    // have 0 threads in the pool.
    ASSERT_TRUE(pool->isStopped());
    EXPECT_TRUE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 0);

    // Calling stop again should be harmless.
    ASSERT_NO_THROW_LOG(pool->stop());

    // State should be STOPPED, IOService should be stopped, we should
    // have 0 threads in the pool.
    ASSERT_TRUE(pool->isStopped());
    EXPECT_TRUE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 0);

    // Destroying the pool should be fine.
    ASSERT_NO_THROW_LOG(pool.reset());
}

// Verifies that pool can move from RUNNING to PAUSED.
TEST_F(IoServiceThreadPoolTest, runningToPaused) {
    IoServiceThreadPoolPtr pool;

    // Create a running pool.
    ASSERT_NO_THROW_LOG(pool.reset(new IoServiceThreadPool(io_service_, 3, false)));
    ASSERT_TRUE(pool->isRunning());

    // Call pause from RUNNING.
    ASSERT_NO_THROW_LOG(pool->pause());

    // State should be PAUSED, IOService should be stopped, we should
    // have 3 threads in the pool.
    ASSERT_TRUE(pool->isPaused());
    EXPECT_TRUE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 3);

    // Calling pause again should be harmless.
    ASSERT_NO_THROW_LOG(pool->pause());

    // State should be PAUSED, IOService should be stopped, we should
    // have 3 threads in the pool.
    ASSERT_TRUE(pool->isPaused());
    EXPECT_TRUE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 3);

    // Destroying the pool should be fine.
    ASSERT_NO_THROW_LOG(pool.reset());
}

// Verifies that pool can move from PAUSED to RUNNING.
TEST_F(IoServiceThreadPoolTest, pausedToRunning) {
    IoServiceThreadPoolPtr pool;

    // Create a running pool.
    ASSERT_NO_THROW_LOG(pool.reset(new IoServiceThreadPool(io_service_, 3, false)));
    ASSERT_TRUE(pool->isRunning());

    // Call pause from RUNNING.
    ASSERT_NO_THROW_LOG(pool->pause());
    ASSERT_TRUE(pool->isPaused());

    // Call run.
    ASSERT_NO_THROW_LOG(pool->run());

    // State should be RUNNING, IOService should not be stopped, we should
    // have 3 threads in the pool.
    ASSERT_TRUE(pool->isRunning());
    EXPECT_FALSE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 3);

    // Destroying the pool should be fine.
    ASSERT_NO_THROW_LOG(pool.reset());
}

// Verifies that pool can move from PAUSED to STOPPED.
TEST_F(IoServiceThreadPoolTest, pausedToStopped) {
    IoServiceThreadPoolPtr pool;

    // Create a running pool.
    ASSERT_NO_THROW_LOG(pool.reset(new IoServiceThreadPool(io_service_, 3, false)));
    ASSERT_TRUE(pool->isRunning());

    // Call pause from RUNNING.
    ASSERT_NO_THROW_LOG(pool->pause());
    ASSERT_TRUE(pool->isPaused());

    // Call stop.
    ASSERT_NO_THROW_LOG(pool->stop());

    // State should be STOPPED, IOService should be stopped, we should
    // have 0 threads in the pool.
    ASSERT_TRUE(pool->isStopped());
    EXPECT_TRUE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 0);

    // Destroying the pool should be fine.
    ASSERT_NO_THROW_LOG(pool.reset());
}

// Verifies that attempting to pause a STOPPED pool has no effect.
TEST_F(IoServiceThreadPoolTest, stoppedToPaused) {
    IoServiceThreadPoolPtr pool;

    // Create a stopped pool.
    ASSERT_NO_THROW_LOG(pool.reset(new IoServiceThreadPool(io_service_, 3, true)));
    ASSERT_TRUE(pool->isStopped());

    // State should be STOPPED, IOService won't be stopped because it was
    // never started. We should have 0 threads in the pool.
    ASSERT_TRUE(pool->isStopped());
    EXPECT_FALSE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 0);

    // Call pause from STOPPED.
    ASSERT_NO_THROW_LOG(pool->pause());

    // Should have no effect.
    ASSERT_TRUE(pool->isStopped());

    // State should be STOPPED, IOService won't be stopped because it was
    // never started. We should have 0 threads in the pool.
    ASSERT_TRUE(pool->isStopped());
    EXPECT_FALSE(pool->getIOService()->stopped());
    EXPECT_EQ(pool->getThreadCount(), 0);

    // Destroying the pool should be fine.
    ASSERT_NO_THROW_LOG(pool.reset());
}

}