Files
splash/tests/unit_tests/network/channel_sh4lt.cpp
2025-11-18 09:42:24 -05:00

84 lines
2.5 KiB
C++

/*
* This file is part of Splash.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Splash is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Splash. If not, see <http://www.gnu.org/licenses/>.
*/
#include "./network/channel_sh4lt.h"
#include <atomic>
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include <doctest.h>
#include "./core/root_object.h"
#include "./core/serialized_object.h"
using namespace Splash;
/*************/
TEST_CASE("Test sending a message and a buffer through a sh4lt channel")
{
auto root = RootObject();
bool isMsgReceived = false;
bool isBufferReceived = false;
std::mutex receivedMutex;
std::vector<uint8_t> receivedMsg;
SerializedObject receivedObj;
auto channelOutput = ChannelOutput_Sh4lt(&root, "output");
auto channelInput = ChannelInput_Sh4lt(
&root,
"input",
[&](const std::vector<uint8_t> msg) {
std::unique_lock<std::mutex> lock(receivedMutex);
isMsgReceived = true;
receivedMsg = msg;
},
[&](SerializedObject&& obj) {
std::unique_lock<std::mutex> lock(receivedMutex);
isBufferReceived = true;
receivedObj = std::move(obj);
});
CHECK(channelInput.connectTo("output"));
std::vector<uint8_t> msg = {1, 2, 3, 4};
CHECK(channelOutput.sendMessage(msg));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
{
std::unique_lock<std::mutex> lock(receivedMutex);
CHECK(isMsgReceived);
CHECK_EQ(msg, receivedMsg);
}
auto array = ResizableArray<uint8_t>({1, 2, 3});
auto object = SerializedObject(std::move(array));
CHECK(channelOutput.sendBuffer(std::move(object)));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
{
std::unique_lock<std::mutex> lock(receivedMutex);
CHECK(isBufferReceived);
CHECK_EQ(receivedObj.data()[0], 1);
CHECK_EQ(receivedObj.data()[1], 2);
CHECK_EQ(receivedObj.data()[2], 3);
}
}