Serenity Operating System
1/*
2 * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/Atomic.h>
8#include <AK/Time.h>
9#include <LibCore/ElapsedTimer.h>
10#include <LibTest/TestCase.h>
11#include <signal.h>
12#include <unistd.h>
13
14class SuccessContext {
15public:
16 static Atomic<bool> alarm_fired;
17
18 static Core::ElapsedTimer signal_timer;
19
20 static constexpr auto timer_value = Time::from_seconds(1);
21
22 static void test_signal_handler(int signal)
23 {
24 auto actual_duration = SuccessContext::signal_timer.elapsed_time();
25 auto expected_duration = SuccessContext::timer_value;
26
27 // Add a small buffer to allow for latency on the system.
28 constexpr auto buffer_duration = Time::from_milliseconds(50);
29
30 dbgln("Signal Times - Actual: {} Expected: {}", actual_duration.to_milliseconds(), expected_duration.to_milliseconds());
31 EXPECT(actual_duration >= expected_duration);
32 EXPECT(actual_duration < expected_duration + buffer_duration);
33
34 EXPECT_EQ(signal, SIGALRM);
35 SuccessContext::alarm_fired = true;
36 }
37};
38
39Atomic<bool> SuccessContext::alarm_fired { false };
40Core::ElapsedTimer SuccessContext::signal_timer {};
41
42TEST_CASE(success_case)
43{
44 signal(SIGALRM, SuccessContext::test_signal_handler);
45
46 SuccessContext::signal_timer.start();
47 auto previous_time = alarm(SuccessContext::timer_value.to_seconds());
48 EXPECT_EQ(previous_time, 0u);
49
50 auto sleep_time = SuccessContext::timer_value + Time::from_seconds(1);
51 sleep(sleep_time.to_seconds());
52
53 EXPECT(SuccessContext::alarm_fired);
54}
55
56// Regression test for issues #9071
57// See: https://github.com/SerenityOS/serenity/issues/9071
58TEST_CASE(regression_inifinite_loop)
59{
60 constexpr auto hour_long_timer_value = Time::from_seconds(60 * 60);
61
62 // Create an alarm timer significantly far into the future.
63 auto previous_time = alarm(hour_long_timer_value.to_seconds());
64 EXPECT_EQ(previous_time, 0u);
65
66 // Update the alarm with a zero value before the previous timer expires.
67 previous_time = alarm(0);
68 EXPECT_EQ(previous_time, hour_long_timer_value.to_seconds());
69
70 // Update the alarm with a zero value again, this shouldn't get stuck
71 // in an infinite loop trying to cancel the previous timer in the kernel.
72 previous_time = alarm(0);
73 EXPECT_EQ(previous_time, 0u);
74}