diff options
author | Brian Gianforcaro <bgianf@serenityos.org> | 2021-08-02 18:48:34 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-03 18:44:01 +0200 |
commit | 4df16578980ef3d2b11bb364626ba4f9e16cfcc0 (patch) | |
tree | 9a751b2a6e75faf753d27b2cfa1b5d75eee14950 /Tests/Kernel/TestKernelAlarm.cpp | |
parent | ea401fb3c336de90e2446fbd661a8aae22685aa3 (diff) | |
download | serenity-4df16578980ef3d2b11bb364626ba4f9e16cfcc0.zip |
Tests: Add coverage for sys$alarm() success case
Diffstat (limited to 'Tests/Kernel/TestKernelAlarm.cpp')
-rw-r--r-- | Tests/Kernel/TestKernelAlarm.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Tests/Kernel/TestKernelAlarm.cpp b/Tests/Kernel/TestKernelAlarm.cpp index 6bdc7dc251..f4b85e910c 100644 --- a/Tests/Kernel/TestKernelAlarm.cpp +++ b/Tests/Kernel/TestKernelAlarm.cpp @@ -4,10 +4,56 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <AK/Atomic.h> #include <AK/Time.h> +#include <LibCore/ElapsedTimer.h> #include <LibTest/TestCase.h> +#include <signal.h> +#include <signal_numbers.h> #include <unistd.h> +class SuccessContext { +public: + static Atomic<bool> alarm_fired; + + static Core::ElapsedTimer signal_timer; + + static constexpr auto timer_value = Time::from_seconds(1); + + static void test_signal_handler(int signal) + { + auto actual_duration = Time::from_milliseconds(SuccessContext::signal_timer.elapsed()); + auto expected_duration = SuccessContext::timer_value; + + // Add a small buffer to allow for latency on the system. + constexpr auto buffer_duration = Time::from_milliseconds(50); + + dbgln("Signal Times - Actual: {} Expected: {}", actual_duration.to_milliseconds(), expected_duration.to_milliseconds()); + EXPECT(actual_duration >= expected_duration); + EXPECT(actual_duration < expected_duration + buffer_duration); + + EXPECT_EQ(signal, SIGALRM); + SuccessContext::alarm_fired = true; + } +}; + +Atomic<bool> SuccessContext::alarm_fired { false }; +Core::ElapsedTimer SuccessContext::signal_timer {}; + +TEST_CASE(success_case) +{ + signal(SIGALRM, SuccessContext::test_signal_handler); + + SuccessContext::signal_timer.start(); + auto previous_time = alarm(SuccessContext::timer_value.to_seconds()); + EXPECT_EQ(previous_time, 0u); + + auto sleep_time = SuccessContext::timer_value + Time::from_seconds(1); + sleep(sleep_time.to_seconds()); + + EXPECT(SuccessContext::alarm_fired); +} + // Regression test for issues #9071 // See: https://github.com/SerenityOS/serenity/issues/9071 TEST_CASE(regression_inifinite_loop) |