Split out //aos/common:queue_testutils and rename stuff

It had three independent pieces of functionality, one of which had a
pretty bad name.

Change-Id: I4a6937692fa36e74f814b3be4d4c5bc751947088
diff --git a/aos/common/BUILD b/aos/common/BUILD
index 8e911de..9bcf811 100644
--- a/aos/common/BUILD
+++ b/aos/common/BUILD
@@ -17,24 +17,6 @@
 )
 
 cc_library(
-  name = 'queue_testutils',
-  srcs = [
-    'queue_testutils.cc',
-  ],
-  hdrs = [
-    'queue_testutils.h',
-  ],
-  deps = [
-    ':once',
-    ':queues',
-    '//aos/testing:googletest',
-    '//aos/common/logging:implementations',
-    '//aos/linux_code/ipc_lib:shared_mem',
-    ':mutex',
-  ],
-)
-
-cc_library(
   name = 'macros',
   hdrs = [
     'macros.h',
@@ -141,7 +123,7 @@
     '//aos/testing:googletest',
     ':test_queue',
     '//aos/common/logging',
-    ':queue_testutils',
+    '//aos/testing:test_logging',
   ],
 )
 
@@ -193,7 +175,7 @@
   ],
   deps = [
     '//aos/testing:googletest',
-    ':queue_testutils',
+    '//aos/testing:test_shm',
     ':test_queue',
     '//aos/common/util:thread',
     ':die',
@@ -271,18 +253,6 @@
 )
 
 cc_test(
-  name = 'queue_testutils_test',
-  srcs = [
-    'queue_testutils_test.cc',
-  ],
-  deps = [
-    '//aos/testing:googletest',
-    ':queue_testutils',
-    '//aos/common/logging',
-  ],
-)
-
-cc_test(
   name = 'mutex_test',
   srcs = [
     'mutex_test.cc',
@@ -295,7 +265,7 @@
     '//aos/common/util:death_test_log_implementation',
     '//aos/common/util:thread',
     '//aos/common:time',
-    ':queue_testutils',
+    '//aos/testing:test_logging',
   ],
 )
 
@@ -307,7 +277,7 @@
   deps = [
     '//aos/testing:googletest',
     ':event',
-    ':queue_testutils',
+    '//aos/testing:test_logging',
     ':time',
   ],
 )
@@ -334,12 +304,13 @@
   ],
   deps = [
     '//aos/testing:googletest',
+    '//aos/testing:prevent_exit',
     ':condition',
     '//aos/common/util:thread',
     ':time',
     ':mutex',
     '//aos/common/logging',
-    ':queue_testutils',
+    '//aos/testing:test_shm',
     '//aos/linux_code/ipc_lib:core_lib',
     '//aos/linux_code/ipc_lib:aos_sync',
     ':die',
@@ -392,7 +363,7 @@
   deps = [
     ':stl_mutex',
     '//aos/testing:googletest',
-    ':queue_testutils',
+    '//aos/testing:test_logging',
     '//aos/common/util:thread',
     ':die',
   ],
diff --git a/aos/common/actions/BUILD b/aos/common/actions/BUILD
index de13464..9a86232 100644
--- a/aos/common/actions/BUILD
+++ b/aos/common/actions/BUILD
@@ -48,7 +48,7 @@
     '//aos/testing:googletest',
     ':action_lib',
     ':test_action_queue',
-    '//aos/common:queue_testutils',
+    '//aos/testing:test_shm',
     '//aos/common/logging',
     '//aos/common/logging:queue_logging',
     '//aos/common:queues',
diff --git a/aos/common/actions/action_test.cc b/aos/common/actions/action_test.cc
index 1e94b32..21da2c7 100644
--- a/aos/common/actions/action_test.cc
+++ b/aos/common/actions/action_test.cc
@@ -4,13 +4,14 @@
 #include <thread>
 
 #include "gtest/gtest.h"
+
 #include "aos/common/queue.h"
-#include "aos/common/queue_testutils.h"
 #include "aos/common/actions/actor.h"
 #include "aos/common/actions/actions.h"
 #include "aos/common/actions/actions.q.h"
 #include "aos/common/actions/test_action.q.h"
 #include "aos/common/event.h"
+#include "aos/testing/test_shm.h"
 
 using ::aos::time::Time;
 
@@ -98,8 +99,6 @@
 class ActionTest : public ::testing::Test {
  protected:
   ActionTest() {
-    // Flush the robot state queue so we can use clean shared memory for this.
-    // test.
     actions::test_action.goal.Clear();
     actions::test_action.status.Clear();
     actions::test_action2.goal.Clear();
@@ -114,7 +113,7 @@
   }
 
   // Bring up and down Core.
-  ::aos::common::testing::GlobalCoreInstance my_core;
+  ::aos::testing::TestSharedMemory my_shm_;
   ::aos::common::actions::ActionQueue action_queue_;
 };
 
diff --git a/aos/common/condition_test.cc b/aos/common/condition_test.cc
index 2310273..dbaa546 100644
--- a/aos/common/condition_test.cc
+++ b/aos/common/condition_test.cc
@@ -10,7 +10,7 @@
 
 #include "aos/common/time.h"
 #include "aos/common/mutex.h"
-#include "aos/common/queue_testutils.h"
+#include "aos/testing/test_shm.h"
 #include "aos/common/type_traits.h"
 #include "aos/linux_code/ipc_lib/core_lib.h"
 #include "aos/common/logging/logging.h"
@@ -18,9 +18,9 @@
 #include "aos/linux_code/ipc_lib/aos_sync.h"
 #include "aos/common/die.h"
 #include "aos/common/util/thread.h"
+#include "aos/testing/prevent_exit.h"
 
 using ::aos::time::Time;
-using ::aos::common::testing::GlobalCoreInstance;
 
 namespace aos {
 namespace testing {
@@ -37,7 +37,7 @@
   DISALLOW_COPY_AND_ASSIGN(ConditionTestCommon);
 };
 
-// Some simple tests that don't rely on a GlobalCoreInstance to help with
+// Some simple tests that don't rely on a TestSharedMemory to help with
 // debugging problems that cause tests using that to just completely lock up.
 class SimpleConditionTest : public ConditionTestCommon {
  public:
@@ -93,7 +93,7 @@
     shared_->~Shared();
   }
 
-  GlobalCoreInstance my_core;
+  ::aos::testing::TestSharedMemory my_shm_;
 
   Shared *const shared_;
 
@@ -136,7 +136,7 @@
 
     child_ = fork();
     if (child_ == 0) {  // in child
-      ::aos::common::testing::PreventExit();
+      ::aos::testing::PreventExit();
       Run();
       exit(EXIT_SUCCESS);
     } else {  // in parent
diff --git a/aos/common/controls/BUILD b/aos/common/controls/BUILD
index 08cbd36..5bb31fb 100644
--- a/aos/common/controls/BUILD
+++ b/aos/common/controls/BUILD
@@ -28,7 +28,7 @@
     '//aos/common:time',
     '//aos/common/messages:robot_state',
     '//aos/testing:googletest',
-    '//aos/common:queue_testutils',
+    '//aos/testing:test_shm',
   ],
 )
 
diff --git a/aos/common/controls/control_loop_test.h b/aos/common/controls/control_loop_test.h
index 4fd13a3..30115b8 100644
--- a/aos/common/controls/control_loop_test.h
+++ b/aos/common/controls/control_loop_test.h
@@ -3,7 +3,7 @@
 
 #include "gtest/gtest.h"
 
-#include "aos/common/queue_testutils.h"
+#include "aos/testing/test_shm.h"
 #include "aos/common/time.h"
 
 namespace aos {
@@ -52,7 +52,7 @@
   ::aos::time::Time last_ds_time_ = ::aos::time::Time::InSeconds(0);
   ::aos::time::Time current_time_ = ::aos::time::Time::InSeconds(0);
 
-  ::aos::common::testing::GlobalCoreInstance my_core;
+  ::aos::testing::TestSharedMemory my_shm_;
 };
 
 }  // namespace testing
diff --git a/aos/common/event_test.cc b/aos/common/event_test.cc
index 7331309..91af403 100644
--- a/aos/common/event_test.cc
+++ b/aos/common/event_test.cc
@@ -4,7 +4,7 @@
 
 #include "gtest/gtest.h"
 
-#include "aos/common/queue_testutils.h"
+#include "aos/testing/test_logging.h"
 #include "aos/common/time.h"
 
 namespace aos {
@@ -16,7 +16,7 @@
 
  protected:
   void SetUp() override {
-    ::aos::common::testing::EnableTestLogging();
+    ::aos::testing::EnableTestLogging();
   }
 };
 
diff --git a/aos/common/mutex_test.cc b/aos/common/mutex_test.cc
index 6614d5c..af80bd1 100644
--- a/aos/common/mutex_test.cc
+++ b/aos/common/mutex_test.cc
@@ -13,7 +13,7 @@
 #include "aos/common/util/death_test_log_implementation.h"
 #include "aos/common/util/thread.h"
 #include "aos/common/time.h"
-#include "aos/common/queue_testutils.h"
+#include "aos/testing/test_logging.h"
 #include "aos/linux_code/ipc_lib/core_lib.h"
 
 namespace aos {
@@ -25,7 +25,7 @@
 
  protected:
   void SetUp() override {
-    ::aos::common::testing::EnableTestLogging();
+    ::aos::testing::EnableTestLogging();
     SetDieTestMode(true);
   }
 };
diff --git a/aos/common/network/BUILD b/aos/common/network/BUILD
index fd9a831..19335ba 100644
--- a/aos/common/network/BUILD
+++ b/aos/common/network/BUILD
@@ -44,7 +44,6 @@
   deps = [
     ':team_number',
     '//aos/testing:googletest',
-    '//aos/common:queue_testutils',
   ],
 )
 
diff --git a/aos/common/queue_test.cc b/aos/common/queue_test.cc
index 310301d..dc8c62c 100644
--- a/aos/common/queue_test.cc
+++ b/aos/common/queue_test.cc
@@ -3,10 +3,11 @@
 #include <memory>
 
 #include "gtest/gtest.h"
+
 #include "aos/common/test_queue.q.h"
-#include "aos/common/queue_testutils.h"
 #include "aos/common/util/thread.h"
 #include "aos/common/die.h"
+#include "aos/testing/test_shm.h"
 
 using ::aos::time::Time;
 
@@ -20,7 +21,7 @@
     SetDieTestMode(true);
   }
 
-  GlobalCoreInstance my_core;
+  ::aos::testing::TestSharedMemory my_shm_;
   // Create a new instance of the test queue so that it invalidates the queue
   // that it points to.  Otherwise, we will have a pointer to shared memory that
   // is no longer valid.
@@ -210,17 +211,19 @@
 
 class GroupTest : public ::testing::Test {
  protected:
-  GlobalCoreInstance my_core;
-  // Create a new instance of the test group so that it invalidates the queue
-  // that it points to.  Otherwise, we will have a pointer to shared memory that
-  // is no longer valid.
-  TwoQueues my_test_queuegroup;
-
   GroupTest()
       : my_test_queuegroup(".aos.common.testing.test_queuegroup",
                            0x20561114,
                            ".aos.common.testing.test_queuegroup.first",
                            ".aos.common.testing.test_queuegroup.second") {}
+
+  // Create a new instance of the test group so that it invalidates the queue
+  // that it points to.  Otherwise, we will have a pointer to shared memory that
+  // is no longer valid.
+  TwoQueues my_test_queuegroup;
+
+ private:
+  ::aos::testing::TestSharedMemory my_shm_;
 };
 
 // Tests that the hash gets preserved.
diff --git a/aos/common/queue_testutils.cc b/aos/common/queue_testutils.cc
deleted file mode 100644
index 5843e67..0000000
--- a/aos/common/queue_testutils.cc
+++ /dev/null
@@ -1,177 +0,0 @@
-#include "aos/common/queue_testutils.h"
-
-#include <string.h>
-#include <sys/mman.h>
-#include <unistd.h>
-#include <stdlib.h>
-
-#include "gtest/gtest.h"
-
-#include "aos/common/queue.h"
-#include "aos/common/logging/implementations.h"
-#include "aos/common/once.h"
-#include "aos/common/mutex.h"
-
-using ::aos::logging::LogMessage;
-
-namespace aos {
-namespace common {
-namespace testing {
-namespace {
-
-class TestLogImplementation : public logging::HandleMessageLogImplementation {
- public:
-  const ::std::vector<LogMessage> &messages() { return messages_; }
-
-  // This class has to be a singleton so that everybody can get access to the
-  // same instance to read out the messages etc.
-  static TestLogImplementation *GetInstance() {
-    static Once<TestLogImplementation> once(CreateInstance);
-    return once.Get();
-  }
-
-  // Clears out all of the messages already recorded.
-  void ClearMessages() {
-    ::aos::MutexLocker locker(&messages_mutex_);
-    messages_.clear();
-  }
-
-  // Prints out all of the messages (like when a test fails).
-  void PrintAllMessages() {
-    ::aos::MutexLocker locker(&messages_mutex_);
-    for (auto it = messages_.begin(); it != messages_.end(); ++it) {
-      logging::internal::PrintMessage(stdout, *it);
-    }
-  }
-
-  void SetOutputFile(const char *filename) {
-    if (strcmp("-", filename) != 0) {
-      FILE *newfile = fopen(filename, "w");
-
-      if (newfile) {
-        output_file_ = newfile;
-      }
-    }
-  }
-
-  void PrintMessagesAsTheyComeIn() { print_as_messages_come_in_ = true; }
-
- private:
-  TestLogImplementation() {}
-  ~TestLogImplementation() {
-    if (output_file_ != stdout) {
-      fclose(output_file_);
-    }
-  }
-
-  static TestLogImplementation *CreateInstance() {
-    return new TestLogImplementation();
-  }
-
-  virtual void HandleMessage(const LogMessage &message) override {
-    ::aos::MutexLocker locker(&messages_mutex_);
-    if (message.level == FATAL || print_as_messages_come_in_) {
-      logging::internal::PrintMessage(output_file_, message);
-    }
-
-    messages_.push_back(message);
-  }
-
-  ::std::vector<LogMessage> messages_;
-  bool print_as_messages_come_in_ = false;
-  FILE *output_file_ = stdout;
-  ::aos::Mutex messages_mutex_;
-};
-
-class MyTestEventListener : public ::testing::EmptyTestEventListener {
-  virtual void OnTestStart(const ::testing::TestInfo &/*test_info*/) {
-    TestLogImplementation::GetInstance()->ClearMessages();
-  }
-  virtual void OnTestEnd(const ::testing::TestInfo &test_info) {
-    if (test_info.result()->Failed()) {
-      printf("Test %s failed. Use '--print-logs' to see all log messages.\n",
-             test_info.name());
-    }
-  }
-
-  virtual void OnTestPartResult( const ::testing::TestPartResult &result) {
-    if (result.failed()) {
-      const char *failure_type = "unknown";
-      switch (result.type()) {
-        case ::testing::TestPartResult::Type::kNonFatalFailure:
-          failure_type = "EXPECT";
-          break;
-        case ::testing::TestPartResult::Type::kFatalFailure:
-          failure_type = "ASSERT";
-          break;
-        case ::testing::TestPartResult::Type::kSuccess:
-          break;
-      }
-      log_do(ERROR, "%s: %d: gtest %s failure\n%s\n",
-             result.file_name(),
-             result.line_number(),
-             failure_type,
-             result.message());
-    }
-  }
-};
-
-void *DoEnableTestLogging() {
-  logging::Init();
-  logging::AddImplementation(TestLogImplementation::GetInstance());
-
-  ::testing::UnitTest::GetInstance()->listeners().Append(
-      new MyTestEventListener());
-
-  return NULL;
-}
-
-Once<void> enable_test_logging_once(DoEnableTestLogging);
-
-const size_t kCoreSize = 0x100000;
-
-void TerminateExitHandler() {
-  _exit(EXIT_SUCCESS);
-}
-
-}  // namespace
-
-GlobalCoreInstance::GlobalCoreInstance() {
-  global_core = &global_core_data_;
-  global_core->owner = true;
-  // Use mmap(2) manually instead of through malloc(3) so that we can pass
-  // MAP_SHARED which allows forked processes to communicate using the
-  // "shared" memory.
-  void *memory = mmap(NULL, kCoreSize, PROT_READ | PROT_WRITE,
-                      MAP_SHARED | MAP_ANONYMOUS, -1, 0);
-  CHECK_NE(memory, MAP_FAILED);
-
-  aos_core_use_address_as_shared_mem(memory, kCoreSize);
-
-  EnableTestLogging();
-}
-
-GlobalCoreInstance::~GlobalCoreInstance() {
-  PCHECK(munmap(global_core->mem_struct, kCoreSize));
-  global_core = NULL;
-}
-
-void EnableTestLogging() {
-  enable_test_logging_once.Get();
-}
-
-void PreventExit() {
-  CHECK_EQ(atexit(TerminateExitHandler), 0);
-}
-
-void SetLogFileName(const char* filename) {
-  TestLogImplementation::GetInstance()->SetOutputFile(filename);
-}
-
-void ForcePrintLogsDuringTests() {
-  TestLogImplementation::GetInstance()->PrintMessagesAsTheyComeIn();
-}
-
-}  // namespace testing
-}  // namespace common
-}  // namespace aos
diff --git a/aos/common/queue_testutils.h b/aos/common/queue_testutils.h
deleted file mode 100644
index fb0ffcd..0000000
--- a/aos/common/queue_testutils.h
+++ /dev/null
@@ -1,52 +0,0 @@
-#ifndef AOS_COMMON_QUEUE_TESTUTILS_H_
-#define AOS_COMMON_QUEUE_TESTUTILS_H_
-
-#include "aos/linux_code/ipc_lib/shared_mem.h"
-
-// This file has some general helper functions for dealing with testing things
-// that use shared memory etc.
-
-namespace aos {
-namespace common {
-namespace testing {
-
-// Manages creating and cleaning up "shared memory" which works within this
-// process and any that it fork(2)s.
-class GlobalCoreInstance {
- public:
-  // Calls EnableTestLogging().
-  GlobalCoreInstance();
-  ~GlobalCoreInstance();
-
- private:
-  struct aos_core global_core_data_;
-};
-
-// Enables the logging framework for use during a gtest test.
-// It will print out all WARNING and above messages all of the time. It will
-// also print out all log messages when a test fails.
-// This function only needs to be called once in each process (after gtest is
-// initialized), however it can be called more than that.
-void EnableTestLogging();
-
-// Registers an exit handler (using atexit(3)) which will call _exit(2).
-// Intended to be called in a freshly fork(2)ed process where it will run before
-// any other exit handlers that were already registered and prevent them from
-// being run.
-void PreventExit();
-
-// Redirect the messages enabled by EnableTestLogging() function to a file.
-// By default the messages are printed to standard output.
-void SetLogFileName(const char* filename);
-
-// Force the messages to be printed as they are handled by the logging
-// framework. This can be useful for tests that hang where no messages would
-// otherwise be printed. This is also useful for tests that do pass, but where
-// we want to use graphing tools to verify what's happening.
-void ForcePrintLogsDuringTests();
-
-}  // namespace testing
-}  // namespace common
-}  // namespace aos
-
-#endif  // AOS_COMMON_QUEUE_TESTUTILS_H_
diff --git a/aos/common/queue_testutils_test.cc b/aos/common/queue_testutils_test.cc
deleted file mode 100644
index 2defe74..0000000
--- a/aos/common/queue_testutils_test.cc
+++ /dev/null
@@ -1,31 +0,0 @@
-#include "aos/common/queue_testutils.h"
-
-#include <thread>
-
-#include "gtest/gtest.h"
-
-#include "aos/common/logging/logging.h"
-
-namespace aos {
-namespace common {
-namespace testing {
-
-// Tests logging from multiple threads.
-// tsan used to complain about this.
-TEST(QueueTestutilsTest, MultithreadedLog) {
-  EnableTestLogging();
-
-  ::std::thread thread([]() {
-    for (int i = 0; i < 1000; ++i) {
-      LOG(INFO, "test from thread\n");
-    }
-  });
-  for (int i = 0; i < 1000; ++i) {
-    LOG(INFO, "not from thread\n");
-  }
-  thread.join();
-}
-
-}  // namespace testing
-}  // namespace common
-}  // namespace aos
diff --git a/aos/common/queue_types_test.cc b/aos/common/queue_types_test.cc
index b8b5e47..bbadf34 100644
--- a/aos/common/queue_types_test.cc
+++ b/aos/common/queue_types_test.cc
@@ -10,7 +10,7 @@
 #include "aos/common/byteorder.h"
 #include "aos/common/queue_primitives.h"
 #include "aos/common/logging/logging.h"
-#include "aos/common/queue_testutils.h"
+#include "aos/testing/test_logging.h"
 
 using ::aos::common::testing::Structure;
 using ::aos::common::testing::MessageWithStructure;
@@ -30,7 +30,7 @@
 class QueueTypesTest : public ::testing::Test {
  public:
   QueueTypesTest() {
-    ::aos::common::testing::EnableTestLogging();
+    ::aos::testing::EnableTestLogging();
   }
 
   ::testing::AssertionResult Equal(const MessageType &l, const MessageType &r) {
diff --git a/aos/common/stl_mutex_test.cc b/aos/common/stl_mutex_test.cc
index 7d84416..236f506 100644
--- a/aos/common/stl_mutex_test.cc
+++ b/aos/common/stl_mutex_test.cc
@@ -2,7 +2,7 @@
 
 #include "gtest/gtest.h"
 
-#include "aos/common/queue_testutils.h"
+#include "aos/testing/test_logging.h"
 #include "aos/common/util/thread.h"
 #include "aos/common/die.h"
 
@@ -12,7 +12,7 @@
 class StlMutexDeathTest : public ::testing::Test {
  protected:
   void SetUp() override {
-    ::aos::common::testing::EnableTestLogging();
+    ::aos::testing::EnableTestLogging();
     SetDieTestMode(true);
   }
 };
diff --git a/aos/common/util/BUILD b/aos/common/util/BUILD
index 35d8e87..cbaf8ba 100644
--- a/aos/common/util/BUILD
+++ b/aos/common/util/BUILD
@@ -61,7 +61,6 @@
   deps = [
     '//aos/common/logging',
     '//aos/common:time',
-    '//aos/common:queue_testutils',
   ],
 )
 
@@ -211,5 +210,6 @@
   deps = [
     ':phased_loop',
     '//aos/testing:googletest',
+    '//aos/testing:test_logging',
   ],
 )
diff --git a/aos/common/util/phased_loop_test.cc b/aos/common/util/phased_loop_test.cc
index 5846f25..1641a92 100644
--- a/aos/common/util/phased_loop_test.cc
+++ b/aos/common/util/phased_loop_test.cc
@@ -2,7 +2,7 @@
 
 #include "gtest/gtest.h"
 
-#include "aos/common/queue_testutils.h"
+#include "aos/testing/test_logging.h"
 
 namespace aos {
 namespace time {
@@ -11,7 +11,7 @@
 class PhasedLoopTest : public ::testing::Test {
  protected:
   PhasedLoopTest() {
-    ::aos::common::testing::EnableTestLogging();
+    ::aos::testing::EnableTestLogging();
   }
 };