ifw-daq 3.1.0
IFW Data Acquisition modules
Loading...
Searching...
No Matches
testRsyncAsyncProcess.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup daq_test
4 * @copyright (c) Copyright ESO 2022
5 * All Rights Reserved
6 * ESO (eso.org) is an Intergovernmental Organisation, and therefore special legal conditions apply.
7 *
8 * @brief daq::RsyncAsyncProcess integration tests
9 *
10 * This starts subprocesses that depend on @c rsync.
11 */
12#include <stdlib.h>
13
14#include <filesystem>
15#include <sstream>
16#include <thread>
17
18#include <gtest/gtest.h>
19
20#include <rad/ioExecutor.hpp>
21
23
24namespace fs = std::filesystem;
25
26class RsyncAsyncProcessTest : public ::testing::Test {
27public:
29 }
30
31 /**
32 * Creates a temporary file source (m_source) and destination (m_dest) that can be used by test
33 * cases.
34 */
35 void SetUp() override {
36 m_source = "rsync-XXXXXX";
37 m_dest = "rsync-XXXXXX";
38 // Create a source file with known size
39 int fd = mkstemp(m_source.data());
40 close(fd);
41 truncate(m_source.c_str(), 4 * 1024);
42 // Dest
43 fd = mkstemp(m_dest.data());
44 close(fd);
45 }
46 /**
47 * Delete temp files.
48 */
49 void TearDown() override {
50 unlink(m_source.c_str());
51 unlink(m_dest.c_str());
52 }
53
54protected:
55 boost::asio::io_context m_ctx;
57 std::string m_source;
58 std::string m_dest;
59};
60
63 opts.bw_limit = 200; // 200 kB/s
64 opts.whole_file = true;
65 auto proc = daq::RsyncAsyncProcess(m_ctx, m_source, m_dest, opts);
66
67 std::stringstream stderr;
68
69 unsigned num_progresses = 0;
70 proc.ConnectProgress([&](pid_t, daq::RsyncProgress const& p) { num_progresses++; });
71 proc.ConnectStderr([&](pid_t, std::string const& line) { stderr << line; });
72
73 bool done = false;
74 proc.Initiate().then(m_exec, [&](boost::future<int> exit_code) {
75 done = true;
76 EXPECT_FALSE(proc.IsRunning());
77 auto code = exit_code.get();
78 EXPECT_EQ(code, 0) << "Unexpected exit code for SIGTERM, rsync: " << stderr.str();
79 });
80
81 EXPECT_TRUE(proc.IsRunning());
82
83 m_ctx.run(); // runs until there's no more work
84
85 EXPECT_TRUE(done)
86 << "Expected continuation to have been executed as io_context::run has exited";
87
88 EXPECT_EQ(4 * 1024, fs::file_size(m_dest));
89 EXPECT_GT(num_progresses, 0) << "There should have been at least one progress report..";
90}
boost::asio::io_context m_ctx
void SetUp() override
Creates a temporary file source (m_source) and destination (m_dest) that can be used by test cases.
void TearDown() override
Delete temp files.
Represents an rsync process as an asynchronous operation.
Adapts boost::asio::io_context into a compatible boost::thread Executor type.
Definition: ioExecutor.hpp:12
TEST_F(RsyncAsyncProcessTest, Copy)
std::optional< bool > whole_file
Transfer with/without delta xfer algorithm.
std::optional< unsigned > bw_limit
Enables rate-limiting in kb/s.
Options controlling rsync invocation.
Describes file transfer progress,.
daq::RsyncAsyncProcess and related class declarations.
EXPECT_EQ(meta.rr_uri, "zpb.rr://meta")