File: test_thread_queue.cpp

package info (click to toggle)
paho.mqtt.cpp 1.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,672 kB
  • sloc: cpp: 13,068; ansic: 113; sh: 55; makefile: 22
file content (187 lines) | stat: -rw-r--r-- 4,568 bytes parent folder | download | duplicates (2)
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
// test_thread_queue.cpp
//
// Unit tests for the thread_queue class in the Paho MQTT C++ library.
//

/*******************************************************************************
 * Copyright (c) 2022-2024 Frank Pagliughi <fpagliughi@mindspring.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v20.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Frank Pagliughi - Initial implementation
 *******************************************************************************/

#define UNIT_TESTS

#include <chrono>
#include <future>
#include <thread>
#include <vector>

#include "catch2_version.h"
#include "mqtt/thread_queue.h"
#include "mqtt/types.h"

using namespace mqtt;
using namespace std::chrono;

TEST_CASE("thread_queue put/get", "[thread_queue]")
{
    thread_queue<int> que;

    que.put(1);
    que.put(2);
    REQUIRE(que.get() == 1);

    que.put(3);
    REQUIRE(que.get() == 2);
    REQUIRE(que.get() == 3);
}

TEST_CASE("thread_queue tryget", "[thread_queue]")
{
    thread_queue<int> que;
    int n;

    // try_get's should fail on empty queue
    REQUIRE(!que.try_get(&n));
    REQUIRE(!que.try_get_for(&n, 5ms));

    auto timeout = steady_clock::now() + 15ms;
    REQUIRE(!que.try_get_until(&n, timeout));

    que.put(1);
    que.put(2);
    REQUIRE(que.try_get(&n));
    REQUIRE(n == 1);

    que.put(3);
    REQUIRE(que.try_get(&n));
    REQUIRE(n == 2);
    REQUIRE(que.try_get(&n));
    REQUIRE(n == 3);

    // Empty now. Try should fail and leave 'n' unchanged
    REQUIRE(!que.try_get(&n));
    REQUIRE(n == 3);
}

TEST_CASE("thread_queue tryput", "[thread_queue]")
{
    thread_queue<int> que{2};

    REQUIRE(que.try_put(1));
    REQUIRE(que.try_put(2));

    // Queue full. Put should fail
    REQUIRE(!que.try_put(3));
    REQUIRE(!que.try_put_for(3, 5ms));

    auto timeout = steady_clock::now() + 15ms;
    REQUIRE(!que.try_put_until(3, timeout));
}

TEST_CASE("thread_queue mt put/get", "[thread_queue]")
{
    thread_queue<string> que;
    const size_t N = 100000;
    const size_t N_THR = 2;

    auto producer = [&que, &N]() {
        string s;
        for (size_t i = 0; i < 512; ++i) {
            s.push_back('a' + i % 26);
        }

        for (size_t i = 0; i < N; ++i) {
            que.put(s);
        }
    };

    auto consumer = [&que, &N]() {
        string s;
        bool ok = true;
        for (size_t i = 0; i < N && ok; ++i) {
            ok = que.try_get_for(&s, 250ms);
        }
        return ok;
    };

    std::vector<std::thread> producers;
    std::vector<std::future<bool>> consumers;

    for (size_t i = 0; i < N_THR; ++i) {
        producers.push_back(std::thread(producer));
    }

    for (size_t i = 0; i < N_THR; ++i) {
        consumers.push_back(std::async(consumer));
    }

    for (size_t i = 0; i < N_THR; ++i) {
        producers[i].join();
    }

    for (size_t i = 0; i < N_THR; ++i) {
        REQUIRE(consumers[i].get());
    }
}

TEST_CASE("thread_queue close", "[thread_queue]")
{
    thread_queue<int> que;
    REQUIRE(!que.closed());

    que.put(1);
    que.put(2);
    que.close();

    // Queue is closed. Shouldn't accept any new items.
    REQUIRE(que.closed());
    REQUIRE(que.size() == 2);

    REQUIRE_THROWS_AS(que.put(3), queue_closed);
    REQUIRE(!que.try_put(3));
    REQUIRE(!que.try_put_for(3, 10ms));
    REQUIRE(!que.try_put_until(3, steady_clock::now() + 10ms));

    // But can get any items already in there.
    REQUIRE(que.get() == 1);
    REQUIRE(que.get() == 2);

    // When done (closed and empty), should throw on a get(),
    // or fail on a try_get
    REQUIRE(que.empty());
    REQUIRE(que.done());

    int n;
    REQUIRE_THROWS_AS(que.get(), queue_closed);
    REQUIRE(!que.try_get(&n));
    REQUIRE(!que.try_get_for(&n, 10ms));
    REQUIRE(!que.try_get_until(&n, steady_clock::now() + 10ms));
}

TEST_CASE("thread_queue close_signals", "[thread_queue]")
{
    thread_queue<int> que;
    REQUIRE(!que.closed());

    auto thr = std::thread([&que] {
        std::this_thread::sleep_for(10ms);
        que.close();
    });

    // Should initially block, but then throw when the queue
    // is closed by the other thread.
    REQUIRE_THROWS_AS(que.get(), queue_closed);

    thr.join();
}