2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2015-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
#include <chrono>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2022-08-10 13:34:51 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define LIBTRANSMISSION_WATCHDIR_MODULE
|
2022-01-13 02:13:58 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-01-13 02:13:58 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/file.h>
|
|
|
|
#include <libtransmission/net.h>
|
|
|
|
#include <libtransmission/watchdir.h>
|
|
|
|
#include <libtransmission/watchdir-base.h>
|
|
|
|
#include <libtransmission/timer-ev.h>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
static auto constexpr GenericRescanInterval = 100ms;
|
|
|
|
static auto constexpr RetryDuration = 100ms;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
// should be at least 2x the watchdir-generic size to ensure that
|
|
|
|
// we have time to pump all events at least once in processEvents()
|
|
|
|
static auto constexpr ProcessEventsTimeout = 300ms;
|
|
|
|
static_assert(ProcessEventsTimeout > GenericRescanInterval);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
namespace libtransmission::test
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
enum class WatchMode
|
|
|
|
{
|
|
|
|
NATIVE,
|
|
|
|
GENERIC
|
|
|
|
};
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
class WatchDirTest
|
|
|
|
: public SandboxedTest
|
|
|
|
, public ::testing::WithParamInterface<WatchMode>
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::shared_ptr<struct event_base> ev_base_;
|
2022-08-10 13:34:51 +00:00
|
|
|
std::unique_ptr<libtransmission::TimerMaker> timer_maker_;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void SetUp() override
|
|
|
|
{
|
|
|
|
SandboxedTest::SetUp();
|
|
|
|
ev_base_.reset(event_base_new(), event_base_free);
|
2022-08-10 13:34:51 +00:00
|
|
|
timer_maker_ = std::make_unique<libtransmission::EvTimerMaker>(ev_base_.get());
|
2023-05-06 04:11:05 +00:00
|
|
|
Watchdir::set_generic_rescan_interval(GenericRescanInterval);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override
|
|
|
|
{
|
|
|
|
ev_base_.reset();
|
|
|
|
|
|
|
|
SandboxedTest::TearDown();
|
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
auto createWatchDir(std::string_view path, Watchdir::Callback callback)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
auto const force_generic = GetParam() == WatchMode::GENERIC;
|
2022-08-10 13:34:51 +00:00
|
|
|
auto watchdir = force_generic ?
|
2023-05-06 04:11:05 +00:00
|
|
|
Watchdir::create_generic(path, std::move(callback), *timer_maker_, GenericRescanInterval) :
|
2022-08-10 13:34:51 +00:00
|
|
|
Watchdir::create(path, std::move(callback), *timer_maker_, ev_base_.get());
|
2022-08-13 17:11:07 +00:00
|
|
|
|
|
|
|
if (auto* const base_watchdir = dynamic_cast<impl::BaseWatchdir*>(watchdir.get()); base_watchdir != nullptr)
|
|
|
|
{
|
|
|
|
base_watchdir->setRetryDuration(RetryDuration);
|
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
return watchdir;
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
void createFile(std::string_view dirname, std::string_view basename, std::string_view contents = ""sv)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
createFileWithContents(tr_pathbuf{ dirname, '/', basename }, contents);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
static std::string createDir(std::string_view dirname, std::string_view basename)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
auto path = std::string{ dirname };
|
2020-08-11 18:11:55 +00:00
|
|
|
path += TR_PATH_DELIMITER;
|
2022-08-10 13:34:51 +00:00
|
|
|
path += basename;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-05-22 01:17:00 +00:00
|
|
|
tr_sys_dir_create(path, 0, 0700);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
void processEvents(std::chrono::milliseconds wait_interval = ProcessEventsTimeout)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
auto tv = timeval{};
|
|
|
|
auto const seconds = std::chrono::duration_cast<std::chrono::seconds>(wait_interval);
|
|
|
|
tv.tv_sec = static_cast<decltype(tv.tv_sec)>(seconds.count());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
wait_interval -= seconds;
|
|
|
|
auto const usec = std::chrono::duration_cast<std::chrono::microseconds>(wait_interval);
|
|
|
|
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec.count());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
event_base_loopexit(ev_base_.get(), &tv);
|
|
|
|
event_base_dispatch(ev_base_.get());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_P(WatchDirTest, construct)
|
|
|
|
{
|
|
|
|
auto const path = sandboxDir();
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
auto callback = [](std::string_view /*dirname*/, std::string_view /*basename*/)
|
|
|
|
{
|
|
|
|
return Watchdir::Action::Done;
|
|
|
|
};
|
|
|
|
auto watchdir = createWatchDir(path, callback);
|
|
|
|
EXPECT_TRUE(watchdir);
|
|
|
|
EXPECT_EQ(path, watchdir->dirname());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(WatchDirTest, initialScan)
|
|
|
|
{
|
|
|
|
auto const path = sandboxDir();
|
|
|
|
|
|
|
|
// setup: start with an empty directory.
|
|
|
|
// this block confirms that it's empty
|
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
auto called = bool{ false };
|
|
|
|
auto callback = [&called](std::string_view /*dirname*/, std::string_view /*basename*/)
|
|
|
|
{
|
|
|
|
called = true;
|
|
|
|
return Watchdir::Action::Done;
|
|
|
|
};
|
|
|
|
auto watchdir = createWatchDir(path, callback);
|
|
|
|
EXPECT_TRUE(watchdir);
|
2020-08-11 18:11:55 +00:00
|
|
|
processEvents();
|
2022-08-10 13:34:51 +00:00
|
|
|
EXPECT_FALSE(called);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add a file
|
2022-08-10 13:34:51 +00:00
|
|
|
auto const base_name = "test.txt"sv;
|
2020-08-11 18:11:55 +00:00
|
|
|
createFile(path, base_name);
|
|
|
|
|
|
|
|
// confirm that a wd will pick up the file that
|
|
|
|
// was created before the wd was instantiated
|
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
auto names = std::set<std::string>{};
|
|
|
|
auto callback = [&names](std::string_view /*dirname*/, std::string_view basename)
|
|
|
|
{
|
|
|
|
names.insert(std::string{ basename });
|
|
|
|
return Watchdir::Action::Done;
|
|
|
|
};
|
|
|
|
auto watchdir = createWatchDir(path, callback);
|
|
|
|
EXPECT_TRUE(watchdir);
|
2020-08-11 18:11:55 +00:00
|
|
|
processEvents();
|
2022-08-10 13:34:51 +00:00
|
|
|
EXPECT_EQ(1U, std::size(names));
|
|
|
|
EXPECT_EQ(base_name, *names.begin());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(WatchDirTest, watch)
|
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
auto const dirname = sandboxDir();
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// create a new watchdir and confirm it's empty
|
2022-08-10 13:34:51 +00:00
|
|
|
auto names = std::vector<std::string>{};
|
|
|
|
auto callback = [&names](std::string_view /*dirname*/, std::string_view basename)
|
|
|
|
{
|
2022-11-12 15:53:09 +00:00
|
|
|
names.emplace_back(basename);
|
2022-08-10 13:34:51 +00:00
|
|
|
return Watchdir::Action::Done;
|
|
|
|
};
|
|
|
|
auto watchdir = createWatchDir(dirname, callback);
|
2020-08-11 18:11:55 +00:00
|
|
|
processEvents();
|
2022-08-10 13:34:51 +00:00
|
|
|
EXPECT_TRUE(watchdir);
|
|
|
|
EXPECT_TRUE(std::empty(names));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// test that a new file in an empty directory shows up
|
2022-08-10 13:34:51 +00:00
|
|
|
auto const file1 = "test1"sv;
|
|
|
|
createFile(dirname, file1);
|
2020-08-11 18:11:55 +00:00
|
|
|
processEvents();
|
2022-08-10 13:34:51 +00:00
|
|
|
EXPECT_EQ(1U, std::size(names));
|
|
|
|
if (!std::empty(names))
|
|
|
|
{
|
|
|
|
EXPECT_EQ(file1, names.front());
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// test that a new file in a nonempty directory shows up
|
2022-08-10 13:34:51 +00:00
|
|
|
names.clear();
|
|
|
|
auto const file2 = "test2"sv;
|
|
|
|
createFile(dirname, file2);
|
2020-08-11 18:11:55 +00:00
|
|
|
processEvents();
|
|
|
|
processEvents();
|
2022-08-10 13:34:51 +00:00
|
|
|
EXPECT_EQ(1U, std::size(names));
|
|
|
|
if (!std::empty(names))
|
|
|
|
{
|
|
|
|
EXPECT_EQ(file2, names.front());
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
// test that folders don't trigger the callback
|
|
|
|
names.clear();
|
|
|
|
createDir(dirname, "test3"sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
processEvents();
|
2022-08-10 13:34:51 +00:00
|
|
|
EXPECT_TRUE(std::empty(names));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 23:35:22 +00:00
|
|
|
TEST_P(WatchDirTest, DISABLED_retry)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
auto const path = sandboxDir();
|
|
|
|
|
|
|
|
// test setup:
|
|
|
|
// Start watching the test directory.
|
2022-08-10 13:34:51 +00:00
|
|
|
// Create a file and return 'retry' back to the watchdir code from our callback.
|
|
|
|
// This should cause the wd to wait a bit and try again.
|
|
|
|
auto names = std::vector<std::string>{};
|
|
|
|
auto callback = [&names](std::string_view /*dirname*/, std::string_view basename)
|
|
|
|
{
|
2022-11-12 15:53:09 +00:00
|
|
|
names.emplace_back(basename);
|
2022-08-10 13:34:51 +00:00
|
|
|
return Watchdir::Action::Retry;
|
|
|
|
};
|
|
|
|
auto watchdir = createWatchDir(path, callback);
|
|
|
|
auto constexpr FastRetryWaitTime = 20ms;
|
2022-09-22 20:42:06 +00:00
|
|
|
auto constexpr SlowRetryWaitTime = 200ms;
|
2022-08-13 17:11:07 +00:00
|
|
|
auto* const base_watchdir = dynamic_cast<impl::BaseWatchdir*>(watchdir.get());
|
|
|
|
ASSERT_TRUE(base_watchdir != nullptr);
|
|
|
|
base_watchdir->setRetryDuration(FastRetryWaitTime);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-09-22 20:42:06 +00:00
|
|
|
processEvents(SlowRetryWaitTime);
|
2022-08-10 13:34:51 +00:00
|
|
|
EXPECT_EQ(0U, std::size(names));
|
2021-09-29 17:25:52 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
auto const test_file = "test.txt"sv;
|
|
|
|
createFile(path, test_file);
|
2022-09-22 20:42:06 +00:00
|
|
|
processEvents(SlowRetryWaitTime);
|
2022-08-11 19:30:00 +00:00
|
|
|
EXPECT_LE(2U, std::size(names));
|
2022-08-10 13:34:51 +00:00
|
|
|
for (auto const& name : names)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(test_file, name);
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P( //
|
2020-08-11 18:11:55 +00:00
|
|
|
WatchDir,
|
|
|
|
WatchDirTest,
|
2021-08-15 09:41:48 +00:00
|
|
|
::testing::Values( //
|
|
|
|
WatchMode::NATIVE,
|
|
|
|
WatchMode::GENERIC));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
} // namespace libtransmission::test
|