mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 09:34:57 +09:00
Tests: Add InodeWatcher and FileWatcher tests
This patch adds some rudimentary tests for InodeWatcher. It tests the basic functionality, but maybe there are corner cases I haven't caught. Additionally, this is our first LibCore test. :^)
This commit is contained in:
parent
ae9e352104
commit
60eb4adac2
Notes:
sideshowbarker
2024-07-18 18:16:56 +09:00
Author: https://github.com/sin-ack
Commit: 60eb4adac2
Pull-request: https://github.com/SerenityOS/serenity/pull/6993
Reviewed-by: https://github.com/alimpfard
Reviewed-by: https://github.com/awesomekling
Reviewed-by: https://github.com/bgianfo
Reviewed-by: https://github.com/gunnarbeutner
5 changed files with 240 additions and 0 deletions
8
Tests/LibCore/CMakeLists.txt
Normal file
8
Tests/LibCore/CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
set(
|
||||
TEST_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCoreFileWatcher.cpp
|
||||
)
|
||||
|
||||
foreach(source ${TEST_SOURCES})
|
||||
serenity_test(${source} LibCore)
|
||||
endforeach()
|
60
Tests/LibCore/TestLibCoreFileWatcher.cpp
Normal file
60
Tests/LibCore/TestLibCoreFileWatcher.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2021, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <Kernel/API/InodeWatcherEvent.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/FileWatcher.h>
|
||||
#include <LibCore/Timer.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
TEST_CASE(file_watcher_child_events)
|
||||
{
|
||||
auto event_loop = Core::EventLoop();
|
||||
auto maybe_file_watcher = Core::FileWatcher::create();
|
||||
EXPECT_NE(maybe_file_watcher.is_error(), true);
|
||||
|
||||
auto file_watcher = maybe_file_watcher.release_value();
|
||||
auto watch_result = file_watcher->add_watch("/tmp/",
|
||||
Core::FileWatcherEvent::Type::ChildCreated
|
||||
| Core::FileWatcherEvent::Type::ChildDeleted);
|
||||
EXPECT_NE(watch_result.is_error(), true);
|
||||
|
||||
int event_count = 0;
|
||||
file_watcher->on_change = [&](Core::FileWatcherEvent const& event) {
|
||||
if (event_count == 0) {
|
||||
EXPECT_EQ(event.event_path, "/tmp/testfile");
|
||||
EXPECT_EQ(event.type, Core::FileWatcherEvent::Type::ChildCreated);
|
||||
} else if (event_count == 1) {
|
||||
EXPECT_EQ(event.event_path, "/tmp/testfile");
|
||||
EXPECT_EQ(event.type, Core::FileWatcherEvent::Type::ChildDeleted);
|
||||
|
||||
event_loop.quit(0);
|
||||
}
|
||||
|
||||
event_count++;
|
||||
};
|
||||
|
||||
auto timer1 = Core::Timer::create_single_shot(500, [&] {
|
||||
int rc = creat("/tmp/testfile", 0777);
|
||||
EXPECT_NE(rc, -1);
|
||||
});
|
||||
timer1->start();
|
||||
|
||||
auto timer2 = Core::Timer::create_single_shot(1000, [&] {
|
||||
int rc = unlink("/tmp/testfile");
|
||||
EXPECT_NE(rc, -1);
|
||||
});
|
||||
timer2->start();
|
||||
|
||||
auto catchall_timer = Core::Timer::create_single_shot(2000, [&] {
|
||||
VERIFY_NOT_REACHED();
|
||||
});
|
||||
catchall_timer->start();
|
||||
|
||||
event_loop.exec();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue