SignalFD tests 96/43696/7
authorJan Olszak <j.olszak@samsung.com>
Mon, 13 Jul 2015 08:31:03 +0000 (10:31 +0200)
committerJan Olszak <j.olszak@samsung.com>
Tue, 14 Jul 2015 07:55:16 +0000 (09:55 +0200)
[Feature]       N/A
[Cause]         N/A
[Solution]      N/A
[Verification]  Build, install, run tests

Change-Id: I9dbcd567c958c66e826785f77912f60af8f521d1

tests/unit_tests/utils/ut-signalfd.cpp [new file with mode: 0644]

diff --git a/tests/unit_tests/utils/ut-signalfd.cpp b/tests/unit_tests/utils/ut-signalfd.cpp
new file mode 100644 (file)
index 0000000..57cf226
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Jan Olszak <j.olszak@samsung.com>
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+
+/**
+ * @file
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Unit tests of SignalFD
+ */
+
+#include "config.hpp"
+#include "ut.hpp"
+
+#include "utils/signalfd.hpp"
+#include "ipc/epoll/event-poll.hpp"
+#include <atomic>
+#include <chrono>
+#include <thread>
+#include <csignal>
+
+
+using namespace utils;
+
+namespace {
+
+volatile sig_atomic_t gAsyncSignal = 0;
+
+struct Fixture {
+    Fixture()
+    {
+        ::signal(SIGINT, &Fixture::signalHandler);
+    }
+    ~Fixture()
+    {
+        ::signal(SIGINT, SIG_DFL);
+    }
+
+    static void signalHandler(int s)
+    {
+        // Signal should never propagate to this handler
+        gAsyncSignal = s;
+    }
+
+    static bool isSignalHandled() {
+        return gAsyncSignal == 0;
+    }
+};
+
+
+} // namespace
+
+BOOST_FIXTURE_TEST_SUITE(SignalFDSuite, Fixture)
+
+const int TIMEOUT = 100;
+
+BOOST_AUTO_TEST_CASE(ConstructorDesctructor)
+{
+    ipc::epoll::EventPoll poll;
+    SignalFD s(poll);
+}
+
+BOOST_AUTO_TEST_CASE(BlockingSignalHandler)
+{
+    ipc::epoll::EventPoll poll;
+    SignalFD s(poll);
+    s.setHandler(SIGUSR1, [](const int) {});
+    s.setHandler(SIGINT, [](const int) {});
+
+    ::raise(SIGINT);
+    std::this_thread::sleep_for(std::chrono::milliseconds(TIMEOUT));
+    BOOST_REQUIRE(isSignalHandled());
+}
+
+BOOST_AUTO_TEST_CASE(SignalHandler)
+{
+    ipc::epoll::EventPoll poll;
+    SignalFD s(poll);
+
+    bool isSignalCalled = false;
+    s.setHandler(SIGINT, [&](const int) {
+        isSignalCalled = true;
+    });
+
+    ::raise(SIGINT);
+    poll.dispatchIteration(TIMEOUT);
+    BOOST_REQUIRE(isSignalCalled);
+    BOOST_REQUIRE(isSignalHandled());
+}
+
+BOOST_AUTO_TEST_SUITE_END()