From c812b45445935df402b36d40f8ed46f4ec650837 Mon Sep 17 00:00:00 2001 From: Jan Olszak Date: Mon, 13 Jul 2015 10:31:03 +0200 Subject: [PATCH] SignalFD tests [Feature] N/A [Cause] N/A [Solution] N/A [Verification] Build, install, run tests Change-Id: I9dbcd567c958c66e826785f77912f60af8f521d1 --- tests/unit_tests/utils/ut-signalfd.cpp | 105 +++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 tests/unit_tests/utils/ut-signalfd.cpp diff --git a/tests/unit_tests/utils/ut-signalfd.cpp b/tests/unit_tests/utils/ut-signalfd.cpp new file mode 100644 index 0000000..57cf226 --- /dev/null +++ b/tests/unit_tests/utils/ut-signalfd.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Jan Olszak + * + * 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 +#include +#include +#include + + +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() -- 2.7.4