Skip to content

Commit 7ba0bcb

Browse files
author
Rui Rocha
committed
Adding tests
1 parent 7d181b8 commit 7ba0bcb

File tree

7 files changed

+69
-2
lines changed

7 files changed

+69
-2
lines changed

BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ test_suite(
2121
name = "unit_tests",
2222
tests = [
2323
"//examples:unit_tests",
24+
"//mq:test",
2425
],
2526
)

mq/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ cc_library(
1212
cc_test(
1313
name = "test",
1414
srcs = glob(["test/*.cpp"]),
15+
linkopts = [
16+
"-lzmq",
17+
],
1518
deps = [
1619
":mq",
20+
"@cppzmq",
1721
"@googletest//:gtest",
1822
"@googletest//:gtest_main",
1923
],

mq/src/IMQSocket.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class IMQSocket {
2020

2121
public:
2222
virtual ~IMQSocket() = default;
23+
virtual auto connect(const std::string& endpoint) -> bool = 0;
2324
virtual auto bind(const std::string& endpoint) -> bool = 0;
2425
virtual auto send(const std::vector<uint8_t>& data) -> bool = 0;
2526
virtual auto receive() -> std::vector<uint8_t> = 0;

mq/src/ZeroMQSocket.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,24 @@ void ZeroMQSocket::close() {
1010

1111
auto ZeroMQSocket::bind(const std::string& endpoint) -> bool {
1212
std::cout << "Binding to " << endpoint << "\n";
13-
m_socket.bind(endpoint);
14-
return true;
13+
try {
14+
m_socket.bind(endpoint);
15+
return true;
16+
} catch (const zmq::error_t& e) {
17+
std::cout << "Error binding to " << endpoint << "\n";
18+
return false;
19+
}
20+
}
21+
22+
auto ZeroMQSocket::connect(const std::string& endpoint) -> bool {
23+
std::cout << "Connecting to " << endpoint << "\n";
24+
try {
25+
m_socket.connect(endpoint);
26+
return true;
27+
} catch (const zmq::error_t& e) {
28+
std::cout << "Error connecting to " << endpoint << "\n";
29+
return false;
30+
}
1531
}
1632

1733
auto ZeroMQSocket::send(const std::vector<uint8_t>& data) -> bool {

mq/src/ZeroMQSocket.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ZeroMQSocket : public IMQSocket {
1010
ZeroMQSocket(zmq::context_t& context, zmq::socket_type type) : m_socket(context, type) {}
1111
~ZeroMQSocket() override = default;
1212

13+
auto connect(const std::string& endpoint) -> bool override;
1314
auto bind(const std::string& endpoint) -> bool override;
1415
auto send(const std::vector<uint8_t>& data) -> bool override;
1516
auto receive() -> std::vector<uint8_t> override;

mq/test/ZeroMQSocketTest.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <cstdint>
4+
#include <vector>
5+
#include <zmq.hpp>
6+
7+
#include "mq/src/ZeroMQSocket.hpp"
8+
9+
TEST(ZeroMQSocketTest, SendAndReceive) {
10+
zmq::context_t context(1);
11+
MQ::ZeroMQSocket sender(context, zmq::socket_type::push);
12+
MQ::ZeroMQSocket receiver(context, zmq::socket_type::pull);
13+
14+
ASSERT_TRUE(sender.bind("inproc://test_socket"));
15+
ASSERT_TRUE(receiver.connect("inproc://test_socket"));
16+
17+
std::vector<uint8_t> data{1, 2, 3};
18+
ASSERT_TRUE(sender.send(data));
19+
20+
std::vector<uint8_t> received_message = receiver.receive();
21+
ASSERT_EQ(data, received_message);
22+
23+
sender.close();
24+
receiver.close();
25+
}
26+
27+
TEST(ZeroMQSocketTest, ConnectFailure) {
28+
zmq::context_t context(1);
29+
MQ::ZeroMQSocket socket(context, zmq::socket_type::pub);
30+
ASSERT_FALSE(socket.connect("invalid_endpoint")); // Expect connection failure
31+
}
32+
33+
TEST(ZeroMQSocketTest, BindFailure) {
34+
zmq::context_t context(1);
35+
MQ::ZeroMQSocket socket1(context, zmq::socket_type::pub);
36+
MQ::ZeroMQSocket socket2(context, zmq::socket_type::pub); // Bind to the same endpoint
37+
38+
ASSERT_TRUE(socket1.bind("inproc://test_bind"));
39+
ASSERT_FALSE(socket2.bind("inproc://test_bind")); // Should fail because already bound
40+
41+
socket1.close();
42+
}

mq/test/ZeroMQWorkerTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class MockMQSocket : public MQ::IMQSocket { // Create a Mock Class
1111
// NOLINTNEXTLINE(modernize-use-trailing-return-type)
1212
MOCK_METHOD(bool, bind, (const std::string &), (override));
1313
// NOLINTNEXTLINE(modernize-use-trailing-return-type)
14+
MOCK_METHOD(bool, connect, (const std::string &), (override));
15+
// NOLINTNEXTLINE(modernize-use-trailing-return-type)
1416
MOCK_METHOD(bool, send, (const std::vector<uint8_t> &), (override));
1517
// NOLINTNEXTLINE(modernize-use-trailing-return-type)
1618
MOCK_METHOD(std::vector<uint8_t>, receive, (), (override));

0 commit comments

Comments
 (0)