AU: Add unit tests for the Terminator class.
Also, improve the implementation so that the Terminator can be initialized
multiple times.
BUG=6243
TEST=unit tests
Change-Id: I8d7f1a2007ac28f8c79506f46d628654413b6607
Review URL: http://codereview.chromium.org/5104007
diff --git a/SConstruct b/SConstruct
index 97cc6e3..d036745 100644
--- a/SConstruct
+++ b/SConstruct
@@ -308,6 +308,7 @@
split_file_writer_unittest.cc
subprocess_unittest.cc
tarjan_unittest.cc
+ terminator_unittest.cc
test_utils.cc
topological_sort_unittest.cc
update_attempter_unittest.cc
diff --git a/terminator.cc b/terminator.cc
index 1d33e32..9165afb 100644
--- a/terminator.cc
+++ b/terminator.cc
@@ -12,6 +12,8 @@
volatile sig_atomic_t Terminator::exit_requested_ = 0;
void Terminator::Init() {
+ exit_blocked_ = 0;
+ exit_requested_ = 0;
signal(SIGTERM, HandleSignal);
}
diff --git a/terminator.h b/terminator.h
index 1c014ea..b3fdba6 100644
--- a/terminator.h
+++ b/terminator.h
@@ -7,6 +7,8 @@
#include <signal.h>
+#include <gtest/gtest_prod.h> // for FRIEND_TEST
+
namespace chromeos_update_engine {
// A class allowing graceful delayed exit.
@@ -21,6 +23,7 @@
// Set to true if the terminator should block termination requests in an
// attempt to block exiting.
static void set_exit_blocked(bool block) { exit_blocked_ = block ? 1 : 0; }
+ static bool exit_blocked() { return exit_blocked_ != 0; }
// Returns true if the system is trying to terminate the process, false
// otherwise. Returns true only if exit was blocked when the termination
@@ -28,6 +31,9 @@
static bool exit_requested() { return exit_requested_ != 0; }
private:
+ FRIEND_TEST(TerminatorTest, HandleSignalTest);
+ FRIEND_TEST(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest);
+
// The signal handler.
static void HandleSignal(int signum);
diff --git a/terminator_unittest.cc b/terminator_unittest.cc
new file mode 100644
index 0000000..e1fb573
--- /dev/null
+++ b/terminator_unittest.cc
@@ -0,0 +1,69 @@
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <gtest/gtest.h>
+#include <gtest/gtest-spi.h>
+
+#include "update_engine/terminator.h"
+
+using std::string;
+using testing::ExitedWithCode;
+
+namespace chromeos_update_engine {
+
+class TerminatorTest : public ::testing::Test {
+ protected:
+ virtual void SetUp() {
+ Terminator::Init();
+ ASSERT_FALSE(Terminator::exit_blocked());
+ ASSERT_FALSE(Terminator::exit_requested());
+ }
+};
+
+typedef TerminatorTest TerminatorDeathTest;
+
+namespace {
+void UnblockExitThroughUnblocker() {
+ ScopedTerminatorExitUnblocker unblocker = ScopedTerminatorExitUnblocker();
+}
+
+void RaiseSIGTERM() {
+ ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(0), "");
+}
+} // namespace {}
+
+TEST_F(TerminatorTest, HandleSignalTest) {
+ Terminator::set_exit_blocked(true);
+ Terminator::HandleSignal(SIGTERM);
+ ASSERT_TRUE(Terminator::exit_requested());
+}
+
+TEST_F(TerminatorTest, ScopedTerminatorExitUnblockerTest) {
+ Terminator::set_exit_blocked(true);
+ ASSERT_TRUE(Terminator::exit_blocked());
+ ASSERT_FALSE(Terminator::exit_requested());
+ UnblockExitThroughUnblocker();
+ ASSERT_FALSE(Terminator::exit_blocked());
+ ASSERT_FALSE(Terminator::exit_requested());
+}
+
+TEST_F(TerminatorDeathTest, ExitTest) {
+ ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(0), "");
+ Terminator::set_exit_blocked(true);
+ ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(0), "");
+}
+
+TEST_F(TerminatorDeathTest, RaiseSignalTest) {
+ RaiseSIGTERM();
+ Terminator::set_exit_blocked(true);
+ EXPECT_FATAL_FAILURE(RaiseSIGTERM(), "");
+}
+
+TEST_F(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest) {
+ Terminator::set_exit_blocked(true);
+ Terminator::exit_requested_ = 1;
+ ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(0), "");
+}
+
+} // namespace chromeos_update_engine