Skip to content

Commit 9c2de90

Browse files
authored
More unit tests (#26)
- "Own" -> "Owned" - I bit less code duplication at IPC clients (added `ClientContext`) - Added unit tests for SDK Publisher.
1 parent f0237c6 commit 9c2de90

39 files changed

+627
-291
lines changed

include/ocvsmd/sdk/defines.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ enum class CyphalPriority : std::uint8_t
124124

125125
/// Defines helper which owns mutable raw data buffer.
126126
///
127-
struct OwnMutablePayload
127+
struct OwnedMutablePayload
128128
{
129129
/// Holds the size of the raw data buffer. It could be less than it was allocated.
130130
std::size_t size;

include/ocvsmd/sdk/node_pub_sub.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Publisher
5252
/// @param timeout The maximum time to keep the published raw message as valid in the Cyphal network.
5353
/// @return An execution sender which emits the async result of the operation.
5454
///
55-
virtual SenderOf<OptError>::Ptr rawPublish(OwnMutablePayload&& raw_payload,
55+
virtual SenderOf<OptError>::Ptr rawPublish(OwnedMutablePayload&& raw_payload,
5656
const std::chrono::microseconds timeout) = 0;
5757

5858
/// Sets priority for messages to be issued by this publisher.
@@ -98,7 +98,7 @@ class Publisher
9898
//
9999
constexpr std::size_t BufferSize = Message::_traits_::SerializationBufferSizeBytes;
100100
// NOLINTNEXTLINE(*-avoid-c-arrays)
101-
OwnMutablePayload payload{BufferSize, std::make_unique<cetl::byte[]>(BufferSize)};
101+
OwnedMutablePayload payload{BufferSize, std::make_unique<cetl::byte[]>(BufferSize)};
102102
//
103103
// No lint b/c of integration with Nunavut.
104104
// NOLINTNEXTLINE(*-pro-type-reinterpret-cast)
@@ -150,7 +150,7 @@ class Subscriber
150150
{
151151
struct Success
152152
{
153-
OwnMutablePayload payload;
153+
OwnedMutablePayload payload;
154154
CyphalPriority priority;
155155
cetl::optional<CyphalNodeId> publisher_node_id;
156156
};

src/cli/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ void tryPublisherScenario(Executor& executor, cetl::pmr::memory_resource& memory
479479

480480
if (const auto opt_error = publisher->setPriority(ocvsmd::sdk::CyphalPriority::Low))
481481
{
482-
spdlog::warn("Failed to set 'low' priority publisher (err={}).", *opt_error);
482+
spdlog::warn("Failed to set 'low' priority publishing (err={}).", *opt_error);
483483
}
484484

485485
uavcan::time::Synchronization_1_0 sync_msg{&memory};
@@ -495,7 +495,7 @@ void tryPublisherScenario(Executor& executor, cetl::pmr::memory_resource& memory
495495
{
496496
if (const auto opt_error = publisher->setPriority(ocvsmd::sdk::CyphalPriority::High))
497497
{
498-
spdlog::warn("Failed to set 'high' priority publisher (err={}).", *opt_error);
498+
spdlog::warn("Failed to set 'high' priority publishing (err={}).", *opt_error);
499499
}
500500
}
501501

src/common/io/io.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace common
1414
namespace io
1515
{
1616

17-
void OwnFd::reset() noexcept
17+
void OwnedFd::reset() noexcept
1818
{
1919
if (fd_ >= 0)
2020
{
@@ -24,7 +24,7 @@ void OwnFd::reset() noexcept
2424
}
2525
}
2626

27-
OwnFd::~OwnFd()
27+
OwnedFd::~OwnedFd()
2828
{
2929
reset();
3030
}

src/common/io/io.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,40 @@ namespace io
1717

1818
/// RAII wrapper for a file descriptor.
1919
///
20-
class OwnFd final
20+
class OwnedFd final
2121
{
2222
public:
23-
OwnFd()
23+
OwnedFd()
2424
: fd_{-1}
2525
{
2626
}
2727

28-
explicit OwnFd(const int fd)
28+
explicit OwnedFd(const int fd)
2929
: fd_{fd}
3030
{
3131
}
3232

33-
OwnFd(OwnFd&& other) noexcept
33+
OwnedFd(OwnedFd&& other) noexcept
3434
: fd_{std::exchange(other.fd_, -1)}
3535
{
3636
}
3737

38-
OwnFd& operator=(OwnFd&& other) noexcept
38+
OwnedFd& operator=(OwnedFd&& other) noexcept
3939
{
40-
const OwnFd old{std::move(*this)};
40+
const OwnedFd old{std::move(*this)};
4141
fd_ = std::exchange(other.fd_, -1);
4242
return *this;
4343
}
4444

45-
OwnFd& operator=(std::nullptr_t)
45+
OwnedFd& operator=(std::nullptr_t)
4646
{
47-
const OwnFd old{std::move(*this)};
47+
const OwnedFd old{std::move(*this)};
4848
return *this;
4949
}
5050

5151
// Disallow copy.
52-
OwnFd(const OwnFd&) = delete;
53-
OwnFd& operator=(const OwnFd&) = delete;
52+
OwnedFd(const OwnedFd&) = delete;
53+
OwnedFd& operator=(const OwnedFd&) = delete;
5454

5555
int get() const
5656
{
@@ -59,12 +59,12 @@ class OwnFd final
5959

6060
void reset() noexcept;
6161

62-
~OwnFd();
62+
~OwnedFd();
6363

6464
private:
6565
int fd_;
6666

67-
}; // OwnFd
67+
}; // OwnedFd
6868

6969
} // namespace io
7070
} // namespace common

src/common/io/socket_address.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ std::string SocketAddress::toString() const
121121

122122
SocketAddress::SocketResult::Var SocketAddress::socket(const int socket_type) const
123123
{
124-
OwnFd out_fd;
124+
OwnedFd out_fd;
125125

126126
const auto& addr_generic = asGenericAddr();
127127
if (const int err = platform::posixSyscallError([this, socket_type, &addr_generic, &out_fd] {
128128
//
129129
const int fd = ::socket(addr_generic.sa_family, socket_type, 0);
130130
if (fd != -1)
131131
{
132-
out_fd = OwnFd{fd};
132+
out_fd = OwnedFd{fd};
133133
}
134134
return fd;
135135
}))
@@ -158,7 +158,7 @@ SocketAddress::SocketResult::Var SocketAddress::socket(const int socket_type) co
158158
return out_fd;
159159
}
160160

161-
sdk::OptError SocketAddress::bind(const OwnFd& socket_fd) const
161+
sdk::OptError SocketAddress::bind(const OwnedFd& socket_fd) const
162162
{
163163
const int raw_fd = socket_fd.get();
164164
CETL_DEBUG_ASSERT(raw_fd != -1, "");
@@ -190,7 +190,7 @@ sdk::OptError SocketAddress::bind(const OwnFd& socket_fd) const
190190
return sdk::OptError{};
191191
}
192192

193-
sdk::OptError SocketAddress::connect(const OwnFd& socket_fd) const
193+
sdk::OptError SocketAddress::connect(const OwnedFd& socket_fd) const
194194
{
195195
const int raw_fd = socket_fd.get();
196196
CETL_DEBUG_ASSERT(raw_fd != -1, "");
@@ -212,14 +212,14 @@ sdk::OptError SocketAddress::connect(const OwnFd& socket_fd) const
212212
}
213213
}
214214

215-
cetl::optional<OwnFd> SocketAddress::accept(const OwnFd& server_fd)
215+
cetl::optional<OwnedFd> SocketAddress::accept(const OwnedFd& server_fd)
216216
{
217217
CETL_DEBUG_ASSERT(server_fd.get() != -1, "");
218218

219219
while (true)
220220
{
221221
addr_len_ = sizeof(addr_storage_);
222-
OwnFd client_fd{::accept(server_fd.get(), &asGenericAddr(), &addr_len_)};
222+
OwnedFd client_fd{::accept(server_fd.get(), &asGenericAddr(), &addr_len_)};
223223
if (client_fd.get() >= 0)
224224
{
225225
if (const int err = platform::posixSyscallError([&client_fd] {
@@ -289,7 +289,7 @@ cetl::optional<OwnFd> SocketAddress::accept(const OwnFd& server_fd)
289289

290290
/// Disables Nagle's algorithm for TCP sockets, so that our small IPC packets are sent immediately.
291291
///
292-
void SocketAddress::configureNoDelay(const OwnFd& fd)
292+
void SocketAddress::configureNoDelay(const OwnedFd& fd)
293293
{
294294
if (const int err = platform::posixSyscallError([&fd] {
295295
//

src/common/io/socket_address.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ class SocketAddress final
5757
struct SocketResult
5858
{
5959
using Failure = sdk::Error;
60-
using Success = OwnFd;
60+
using Success = OwnedFd;
6161
using Var = cetl::variant<Success, Failure>;
6262
};
6363
SocketResult::Var socket(const int socket_type) const;
6464

65-
sdk::OptError bind(const OwnFd& socket_fd) const;
66-
sdk::OptError connect(const OwnFd& socket_fd) const;
67-
cetl::optional<OwnFd> accept(const OwnFd& server_fd);
65+
sdk::OptError bind(const OwnedFd& socket_fd) const;
66+
sdk::OptError connect(const OwnedFd& socket_fd) const;
67+
cetl::optional<OwnedFd> accept(const OwnedFd& server_fd);
6868

6969
private:
70-
static void configureNoDelay(const OwnFd& fd);
70+
static void configureNoDelay(const OwnedFd& fd);
7171
static cetl::optional<ParseResult::Var> tryParseAsUnixDomain(const std::string& conn_str);
7272
static cetl::optional<ParseResult::Var> tryParseAsAbstractUnixDomain(const std::string& conn_str);
7373
static cetl::optional<ParseResult::Var> tryParseAsTcpAddress(const std::string& conn_str,

src/common/ipc/pipe/client_context.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ClientContext final
3232
public:
3333
using Ptr = std::unique_ptr<ClientContext>;
3434

35-
ClientContext(const ServerPipe::ClientId id, io::OwnFd&& fd, Logger& logger)
35+
ClientContext(const ServerPipe::ClientId id, io::OwnedFd&& fd, Logger& logger)
3636
: id_{id}
3737
, logger_{logger}
3838
{

src/common/ipc/pipe/socket_base.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SocketBase
4545
};
4646
using MsgPart = cetl::variant<MsgHeader, MsgPayload>;
4747

48-
io::OwnFd fd;
48+
io::OwnedFd fd;
4949
std::size_t rx_partial_size{0};
5050
MsgPart rx_msg_part{MsgHeader{}};
5151
std::function<sdk::OptError(io::Payload)> on_rx_msg_payload;

src/common/ipc/pipe/socket_server.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class SocketServer final : public SocketBase, public ServerPipe
5252
CETL_NODISCARD sdk::OptError start(EventHandler event_handler) override;
5353
CETL_NODISCARD sdk::OptError send(const ClientId client_id, io::SocketBuffer& sock_buff) override;
5454

55-
io::OwnFd server_fd_;
55+
io::OwnedFd server_fd_;
5656
io::SocketAddress socket_address_;
5757
platform::IPosixExecutorExtension* const posix_executor_ext_;
5858
ClientId unique_client_id_counter_;

0 commit comments

Comments
 (0)