Kylin/UnitTest/Nng.cpp

41 lines
1.4 KiB
C++
Raw Normal View History

2024-10-29 21:51:37 +08:00
#include "BoostLog.h"
#include "Nng/Socket.h"
#include "Nng/SocketAisoWrapper.h"
#include <boost/asio/io_context.hpp>
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(HelloWorld) {
using namespace Nng;
Socket reply(Reply);
reply.listen("tcp://localhost:8000");
Socket request(Request);
request.dial("tcp://localhost:8000");
request.send((void *)"hello", strlen("hello") + 1);
auto buffer = reply.recv();
BOOST_CHECK_EQUAL(buffer.data<char>(), std::string_view("hello"));
reply.send((void *)"world", strlen("world") + 1);
buffer = request.recv();
BOOST_CHECK_EQUAL(buffer.data<char>(), std::string_view("world"));
}
BOOST_AUTO_TEST_CASE(AisoHelloWorld) {
using namespace Nng;
boost::asio::io_context ioContext;
Asio::Socket reply(ioContext, Reply);
reply.asyncReceive([&reply](const boost::system::error_code &error, const Buffer &buffer) {
BOOST_CHECK_EQUAL(buffer.data<char>(), std::string_view("hello"));
reply.send((void *)"world", strlen("world") + 1);
});
reply.listen("tcp://localhost:8000");
Asio::Socket request(ioContext, Request);
request.asyncReceive([&request](const boost::system::error_code &error, const Buffer &buffer) {
BOOST_CHECK_EQUAL(buffer.data<char>(), std::string_view("world"));
});
request.dial("tcp://localhost:8000");
request.send((void *)"hello", strlen("hello") + 1);
ioContext.run();
}