2018-05-13 12:26:27 +08:00
|
|
|
// eventpp library
|
|
|
|
// Copyright (C) 2018 Wang Qi (wqking)
|
|
|
|
// Github: https://github.com/wqking/eventpp
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "test.h"
|
2018-05-19 11:16:12 +08:00
|
|
|
#include "eventpp/eventqueue.h"
|
2018-05-13 12:26:27 +08:00
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <numeric>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <random>
|
|
|
|
#include <vector>
|
2018-05-19 09:52:10 +08:00
|
|
|
#include <atomic>
|
2018-05-13 12:26:27 +08:00
|
|
|
|
|
|
|
TEST_CASE("queue, std::string, void (const std::string &)")
|
|
|
|
{
|
2018-05-19 12:43:51 +08:00
|
|
|
eventpp::EventQueue<std::string, void (const std::string &)> queue;
|
2018-05-13 12:26:27 +08:00
|
|
|
|
|
|
|
int a = 1;
|
|
|
|
int b = 5;
|
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.appendListener("event1", [&a](const std::string &) {
|
2018-05-13 12:26:27 +08:00
|
|
|
a = 2;
|
|
|
|
});
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.appendListener("event1", eraseArgs1([&b]() {
|
2018-05-13 12:26:27 +08:00
|
|
|
b = 8;
|
|
|
|
}));
|
|
|
|
|
|
|
|
REQUIRE(a != 2);
|
|
|
|
REQUIRE(b != 8);
|
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue("event1");
|
|
|
|
queue.process();
|
2018-05-13 12:26:27 +08:00
|
|
|
REQUIRE(a == 2);
|
|
|
|
REQUIRE(b == 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("queue, int, void ()")
|
|
|
|
{
|
2018-05-19 12:43:51 +08:00
|
|
|
eventpp::EventQueue<int, void ()> queue;
|
2018-05-13 12:26:27 +08:00
|
|
|
|
|
|
|
int a = 1;
|
|
|
|
int b = 5;
|
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.appendListener(3, [&a]() {
|
2018-05-13 12:26:27 +08:00
|
|
|
a += 1;
|
|
|
|
});
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.appendListener(3, [&b]() {
|
2018-05-13 12:26:27 +08:00
|
|
|
b += 3;
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(a != 2);
|
|
|
|
REQUIRE(b != 8);
|
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(3);
|
|
|
|
queue.process();
|
2018-05-13 12:26:27 +08:00
|
|
|
|
|
|
|
REQUIRE(a == 2);
|
|
|
|
REQUIRE(b == 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("queue, int, void (const std::string &, int)")
|
|
|
|
{
|
2018-05-19 12:43:51 +08:00
|
|
|
eventpp::EventQueue<int, void (const std::string &, int)> queue;
|
2018-05-13 12:26:27 +08:00
|
|
|
|
|
|
|
const int event = 3;
|
|
|
|
|
|
|
|
std::vector<std::string> sList(2);
|
|
|
|
std::vector<int> iList(sList.size());
|
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.appendListener(event, [&sList, &iList](const std::string & s, const int i) {
|
2018-05-13 12:26:27 +08:00
|
|
|
sList[0] = s;
|
|
|
|
iList[0] = i;
|
|
|
|
});
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.appendListener(event, [&sList, &iList](const std::string & s, const int i) {
|
2018-05-13 12:26:27 +08:00
|
|
|
sList[1] = s + "2";
|
|
|
|
iList[1] = i + 5;
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(sList[0] != "first");
|
|
|
|
REQUIRE(sList[1] != "first2");
|
|
|
|
REQUIRE(iList[0] != 3);
|
|
|
|
REQUIRE(iList[1] != 8);
|
|
|
|
|
|
|
|
SECTION("Parameters") {
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(event, "first", 3);
|
|
|
|
queue.process();
|
2018-05-13 12:26:27 +08:00
|
|
|
|
|
|
|
REQUIRE(sList[0] == "first");
|
|
|
|
REQUIRE(sList[1] == "first2");
|
|
|
|
REQUIRE(iList[0] == 3);
|
|
|
|
REQUIRE(iList[1] == 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Reference parameters should not be modified") {
|
|
|
|
std::string s = "first";
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(event, s, 3);
|
2018-05-13 12:26:27 +08:00
|
|
|
s = "";
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.process();
|
2018-05-13 12:26:27 +08:00
|
|
|
|
|
|
|
REQUIRE(sList[0] == "first");
|
|
|
|
REQUIRE(sList[1] == "first2");
|
|
|
|
REQUIRE(iList[0] == 3);
|
|
|
|
REQUIRE(iList[1] == 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-19 20:30:38 +08:00
|
|
|
TEST_CASE("queue, customized event")
|
|
|
|
{
|
|
|
|
struct MyEvent {
|
|
|
|
int type;
|
|
|
|
std::string message;
|
|
|
|
int param;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MyEventTypeGetter : public eventpp::EventGetterBase
|
|
|
|
{
|
|
|
|
using Event = int;
|
|
|
|
|
|
|
|
static Event getEvent(const MyEvent & e, std::string) {
|
|
|
|
return e.type;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
eventpp::EventQueue<MyEventTypeGetter, void (const MyEvent &, std::string)> queue;
|
|
|
|
|
|
|
|
std::string a = "Hello ";
|
|
|
|
std::string b = "World ";
|
|
|
|
|
|
|
|
queue.appendListener(3, [&a](const MyEvent & e, const std::string & s) {
|
|
|
|
a += e.message + s + std::to_string(e.param);
|
|
|
|
});
|
|
|
|
queue.appendListener(3, [&b](const MyEvent & e, const std::string & s) {
|
|
|
|
b += e.message + s + std::to_string(e.param);
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(a == "Hello ");
|
|
|
|
REQUIRE(b == "World ");
|
|
|
|
|
|
|
|
queue.enqueue({ 3, "very ", 38 }, "good");
|
|
|
|
queue.process();
|
|
|
|
|
|
|
|
REQUIRE(a == "Hello very good38");
|
|
|
|
REQUIRE(b == "World very good38");
|
|
|
|
}
|
|
|
|
|
2018-05-19 09:52:10 +08:00
|
|
|
TEST_CASE("queue multi threading, int, void (int)")
|
2018-05-13 12:26:27 +08:00
|
|
|
{
|
2018-05-19 12:43:51 +08:00
|
|
|
using EQ = eventpp::EventQueue<int, void (int)>;
|
|
|
|
EQ queue;
|
2018-05-13 12:26:27 +08:00
|
|
|
|
|
|
|
constexpr int threadCount = 256;
|
|
|
|
constexpr int dataCountPerThread = 1024 * 4;
|
|
|
|
constexpr int itemCount = threadCount * dataCountPerThread;
|
|
|
|
|
|
|
|
std::vector<int> eventList(itemCount);
|
|
|
|
std::iota(eventList.begin(), eventList.end(), 0);
|
|
|
|
std::shuffle(eventList.begin(), eventList.end(), std::mt19937(std::random_device()()));
|
|
|
|
|
|
|
|
std::vector<int> dataList(itemCount);
|
|
|
|
|
|
|
|
for(int i = 0; i < itemCount; ++i) {
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.appendListener(eventList[i], [&queue, i, &dataList](const int d) {
|
2018-05-13 12:26:27 +08:00
|
|
|
dataList[i] += d;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::thread> threadList;
|
|
|
|
for(int i = 0; i < threadCount; ++i) {
|
2018-05-19 12:43:51 +08:00
|
|
|
threadList.emplace_back([i, dataCountPerThread, &queue, itemCount]() {
|
2018-05-13 12:26:27 +08:00
|
|
|
for(int k = i * dataCountPerThread; k < (i + 1) * dataCountPerThread; ++k) {
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(k, 3);
|
2018-05-13 12:26:27 +08:00
|
|
|
}
|
|
|
|
for(int k = 0; k < 10; ++k) {
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.process();
|
2018-05-13 12:26:27 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for(int i = 0; i < threadCount; ++i) {
|
|
|
|
threadList[i].join();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> compareList(itemCount);
|
|
|
|
std::fill(compareList.begin(), compareList.end(), 3);
|
|
|
|
REQUIRE(dataList == compareList);
|
|
|
|
}
|
|
|
|
|
2018-05-19 14:24:34 +08:00
|
|
|
TEST_CASE("queue multi threading, one thread waits")
|
2018-05-19 09:52:10 +08:00
|
|
|
{
|
2018-05-19 12:43:51 +08:00
|
|
|
using EQ = eventpp::EventQueue<int, void (int)>;
|
|
|
|
EQ queue;
|
2018-05-19 09:52:10 +08:00
|
|
|
|
|
|
|
// note, all events will be process from the other thread instead of main thread
|
|
|
|
constexpr int stopEvent = 1;
|
|
|
|
constexpr int otherEvent = 2;
|
|
|
|
|
|
|
|
constexpr int itemCount = 5;
|
|
|
|
|
|
|
|
std::vector<int> dataList(itemCount);
|
|
|
|
|
|
|
|
std::atomic<int> threadProcessCount(0);
|
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
std::thread thread([stopEvent, otherEvent, &dataList, &queue, &threadProcessCount]() {
|
2018-05-19 09:52:10 +08:00
|
|
|
volatile bool shouldStop = false;
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.appendListener(stopEvent, [&shouldStop](int) {
|
2018-05-19 09:52:10 +08:00
|
|
|
shouldStop = true;
|
|
|
|
});
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.appendListener(otherEvent, [&dataList](const int index) {
|
2018-05-19 09:52:10 +08:00
|
|
|
dataList[index] += index + 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
while(! shouldStop) {
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.wait();
|
2018-05-19 09:52:10 +08:00
|
|
|
|
|
|
|
++threadProcessCount;
|
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.process();
|
2018-05-19 09:52:10 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(threadProcessCount.load() == 0);
|
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
auto waitUntilQueueEmpty = [&queue]() {
|
|
|
|
while(queue.waitFor(std::chrono::nanoseconds(0))) ;
|
2018-05-19 09:52:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
SECTION("Enqueue one by one") {
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(otherEvent, 1);
|
2018-05-19 09:52:10 +08:00
|
|
|
waitUntilQueueEmpty();
|
|
|
|
REQUIRE(threadProcessCount.load() == 1);
|
2018-05-19 12:43:51 +08:00
|
|
|
REQUIRE(queue.empty());
|
2018-05-19 09:52:10 +08:00
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 2, 0, 0, 0 });
|
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(otherEvent, 3);
|
2018-05-19 09:52:10 +08:00
|
|
|
waitUntilQueueEmpty();
|
|
|
|
REQUIRE(threadProcessCount.load() == 2);
|
2018-05-19 12:43:51 +08:00
|
|
|
REQUIRE(queue.empty());
|
2018-05-19 09:52:10 +08:00
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 2, 0, 4, 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Enqueue two") {
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(otherEvent, 1);
|
2018-05-19 09:52:10 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
REQUIRE(threadProcessCount.load() == 1);
|
2018-05-19 12:43:51 +08:00
|
|
|
REQUIRE(queue.empty());
|
2018-05-19 09:52:10 +08:00
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(otherEvent, 3);
|
2018-05-19 09:52:10 +08:00
|
|
|
waitUntilQueueEmpty();
|
|
|
|
|
|
|
|
REQUIRE(threadProcessCount.load() == 2);
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 2, 0, 4, 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Batching enqueue") {
|
|
|
|
{
|
2018-05-19 12:43:51 +08:00
|
|
|
EQ::DisableQueueNotify disableNotify(&queue);
|
2018-05-19 09:52:10 +08:00
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(otherEvent, 2);
|
2018-05-19 09:52:10 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
REQUIRE(threadProcessCount.load() == 0);
|
2018-05-19 12:43:51 +08:00
|
|
|
REQUIRE(! queue.empty());
|
2018-05-19 09:52:10 +08:00
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(otherEvent, 4);
|
2018-05-19 09:52:10 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
REQUIRE(threadProcessCount.load() == 0);
|
2018-05-19 12:43:51 +08:00
|
|
|
REQUIRE(! queue.empty());
|
2018-05-19 09:52:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
waitUntilQueueEmpty();
|
|
|
|
REQUIRE(threadProcessCount.load() == 1);
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 0, 3, 0, 5 });
|
|
|
|
}
|
|
|
|
|
2018-05-19 12:43:51 +08:00
|
|
|
queue.enqueue(stopEvent, 1);
|
2018-05-19 09:52:10 +08:00
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
2018-05-19 14:24:34 +08:00
|
|
|
TEST_CASE("queue multi threading, many threads wait")
|
|
|
|
{
|
|
|
|
using EQ = eventpp::EventQueue<int, void (int)>;
|
|
|
|
EQ queue;
|
|
|
|
|
|
|
|
// note, all events will be process from the other thread instead of main thread
|
|
|
|
constexpr int stopEvent = 1;
|
|
|
|
constexpr int otherEvent = 2;
|
|
|
|
|
|
|
|
constexpr int unit = 3;
|
|
|
|
constexpr int itemCount = 30 * unit;
|
|
|
|
|
|
|
|
std::vector<int> dataList(itemCount);
|
|
|
|
|
|
|
|
std::vector<std::thread> threadList;
|
|
|
|
|
|
|
|
std::atomic<bool> shouldStop(false);
|
|
|
|
|
|
|
|
queue.appendListener(stopEvent, [&shouldStop](int) {
|
|
|
|
shouldStop = true;
|
|
|
|
});
|
|
|
|
queue.appendListener(otherEvent, [&dataList](const int index) {
|
|
|
|
++dataList[index];
|
|
|
|
});
|
|
|
|
|
|
|
|
for(int i = 0; i < itemCount; ++i) {
|
|
|
|
threadList.emplace_back([stopEvent, otherEvent, i, &dataList, &queue, &shouldStop]() {
|
|
|
|
for(;;) {
|
2018-05-19 17:42:41 +08:00
|
|
|
// can't use queue.wait() because the thread can't be waken up by shouldStop = true
|
|
|
|
while(! queue.waitFor(std::chrono::milliseconds(10)) && ! shouldStop.load()) ;
|
2018-05-19 14:24:34 +08:00
|
|
|
|
|
|
|
if(shouldStop.load()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.process();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < itemCount; ++i) {
|
|
|
|
queue.enqueue(otherEvent, i);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < itemCount; i += unit) {
|
|
|
|
EQ::DisableQueueNotify disableNotify(&queue);
|
|
|
|
for(int k = 0; k < unit; ++k) {
|
|
|
|
queue.enqueue(otherEvent, i);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.enqueue(stopEvent);
|
|
|
|
|
|
|
|
for(auto & thread : threadList) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
REQUIRE(std::accumulate(dataList.begin(), dataList.end(), 0) == itemCount * 2);
|
|
|
|
}
|
|
|
|
|