?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. // Copyright 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. //     * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. //     * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. //     * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  
  30.  
  31. // Tests that SCOPED_TRACE() and various Google Test assertions can be
  32. // used in a large number of threads concurrently.
  33.  
  34. #include "gtest/gtest.h"
  35.  
  36. #include <vector>
  37.  
  38. #include "src/gtest-internal-inl.h"
  39.  
  40. #if GTEST_IS_THREADSAFE
  41.  
  42. namespace testing {
  43. namespace {
  44.  
  45. using internal::Notification;
  46. using internal::TestPropertyKeyIs;
  47. using internal::ThreadWithParam;
  48. using internal::scoped_ptr;
  49.  
  50. // In order to run tests in this file, for platforms where Google Test is
  51. // thread safe, implement ThreadWithParam. See the description of its API
  52. // in gtest-port.h, where it is defined for already supported platforms.
  53.  
  54. // How many threads to create?
  55. const int kThreadCount = 50;
  56.  
  57. std::string IdToKey(int id, const char* suffix) {
  58.   Message key;
  59.   key << "key_" << id << "_" << suffix;
  60.   return key.GetString();
  61. }
  62.  
  63. std::string IdToString(int id) {
  64.   Message id_message;
  65.   id_message << id;
  66.   return id_message.GetString();
  67. }
  68.  
  69. void ExpectKeyAndValueWereRecordedForId(
  70.     const std::vector<TestProperty>& properties,
  71.     int id, const char* suffix) {
  72.   TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str());
  73.   const std::vector<TestProperty>::const_iterator property =
  74.       std::find_if(properties.begin(), properties.end(), matches_key);
  75.   ASSERT_TRUE(property != properties.end())
  76.       << "expecting " << suffix << " value for id " << id;
  77.   EXPECT_STREQ(IdToString(id).c_str(), property->value());
  78. }
  79.  
  80. // Calls a large number of Google Test assertions, where exactly one of them
  81. // will fail.
  82. void ManyAsserts(int id) {
  83.   GTEST_LOG_(INFO) << "Thread #" << id << " running...";
  84.  
  85.   SCOPED_TRACE(Message() << "Thread #" << id);
  86.  
  87.   for (int i = 0; i < kThreadCount; i++) {
  88.     SCOPED_TRACE(Message() << "Iteration #" << i);
  89.  
  90.     // A bunch of assertions that should succeed.
  91.     EXPECT_TRUE(true);
  92.     ASSERT_FALSE(false) << "This shouldn't fail.";
  93.     EXPECT_STREQ("a", "a");
  94.     ASSERT_LE(5, 6);
  95.     EXPECT_EQ(i, i) << "This shouldn't fail.";
  96.  
  97.     // RecordProperty() should interact safely with other threads as well.
  98.     // The shared_key forces property updates.
  99.     Test::RecordProperty(IdToKey(id, "string").c_str(), IdToString(id).c_str());
  100.     Test::RecordProperty(IdToKey(id, "int").c_str(), id);
  101.     Test::RecordProperty("shared_key", IdToString(id).c_str());
  102.  
  103.     // This assertion should fail kThreadCount times per thread.  It
  104.     // is for testing whether Google Test can handle failed assertions in a
  105.     // multi-threaded context.
  106.     EXPECT_LT(i, 0) << "This should always fail.";
  107.   }
  108. }
  109.  
  110. void CheckTestFailureCount(int expected_failures) {
  111.   const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
  112.   const TestResult* const result = info->result();
  113.   GTEST_CHECK_(expected_failures == result->total_part_count())
  114.       << "Logged " << result->total_part_count() << " failures "
  115.       << " vs. " << expected_failures << " expected";
  116. }
  117.  
  118. // Tests using SCOPED_TRACE() and Google Test assertions in many threads
  119. // concurrently.
  120. TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
  121.   {
  122.     scoped_ptr<ThreadWithParam<int> > threads[kThreadCount];
  123.     Notification threads_can_start;
  124.     for (int i = 0; i != kThreadCount; i++)
  125.       threads[i].reset(new ThreadWithParam<int>(&ManyAsserts,
  126.                                                 i,
  127.                                                 &threads_can_start));
  128.  
  129.     threads_can_start.Notify();
  130.  
  131.     // Blocks until all the threads are done.
  132.     for (int i = 0; i != kThreadCount; i++)
  133.       threads[i]->Join();
  134.   }
  135.  
  136.   // Ensures that kThreadCount*kThreadCount failures have been reported.
  137.   const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
  138.   const TestResult* const result = info->result();
  139.  
  140.   std::vector<TestProperty> properties;
  141.   // We have no access to the TestResult's list of properties but we can
  142.   // copy them one by one.
  143.   for (int i = 0; i < result->test_property_count(); ++i)
  144.     properties.push_back(result->GetTestProperty(i));
  145.  
  146.   EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count())
  147.       << "String and int values recorded on each thread, "
  148.       << "as well as one shared_key";
  149.   for (int i = 0; i < kThreadCount; ++i) {
  150.     ExpectKeyAndValueWereRecordedForId(properties, i, "string");
  151.     ExpectKeyAndValueWereRecordedForId(properties, i, "int");
  152.   }
  153.   CheckTestFailureCount(kThreadCount*kThreadCount);
  154. }
  155.  
  156. void FailingThread(bool is_fatal) {
  157.   if (is_fatal)
  158.     FAIL() << "Fatal failure in some other thread. "
  159.            << "(This failure is expected.)";
  160.   else
  161.     ADD_FAILURE() << "Non-fatal failure in some other thread. "
  162.                   << "(This failure is expected.)";
  163. }
  164.  
  165. void GenerateFatalFailureInAnotherThread(bool is_fatal) {
  166.   ThreadWithParam<bool> thread(&FailingThread, is_fatal, NULL);
  167.   thread.Join();
  168. }
  169.  
  170. TEST(NoFatalFailureTest, ExpectNoFatalFailureIgnoresFailuresInOtherThreads) {
  171.   EXPECT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true));
  172.   // We should only have one failure (the one from
  173.   // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE
  174.   // should succeed.
  175.   CheckTestFailureCount(1);
  176. }
  177.  
  178. void AssertNoFatalFailureIgnoresFailuresInOtherThreads() {
  179.   ASSERT_NO_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true));
  180. }
  181. TEST(NoFatalFailureTest, AssertNoFatalFailureIgnoresFailuresInOtherThreads) {
  182.   // Using a subroutine, to make sure, that the test continues.
  183.   AssertNoFatalFailureIgnoresFailuresInOtherThreads();
  184.   // We should only have one failure (the one from
  185.   // GenerateFatalFailureInAnotherThread()), since the EXPECT_NO_FATAL_FAILURE
  186.   // should succeed.
  187.   CheckTestFailureCount(1);
  188. }
  189.  
  190. TEST(FatalFailureTest, ExpectFatalFailureIgnoresFailuresInOtherThreads) {
  191.   // This statement should fail, since the current thread doesn't generate a
  192.   // fatal failure, only another one does.
  193.   EXPECT_FATAL_FAILURE(GenerateFatalFailureInAnotherThread(true), "expected");
  194.   CheckTestFailureCount(2);
  195. }
  196.  
  197. TEST(FatalFailureOnAllThreadsTest, ExpectFatalFailureOnAllThreads) {
  198.   // This statement should succeed, because failures in all threads are
  199.   // considered.
  200.   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(
  201.       GenerateFatalFailureInAnotherThread(true), "expected");
  202.   CheckTestFailureCount(0);
  203.   // We need to add a failure, because main() checks that there are failures.
  204.   // But when only this test is run, we shouldn't have any failures.
  205.   ADD_FAILURE() << "This is an expected non-fatal failure.";
  206. }
  207.  
  208. TEST(NonFatalFailureTest, ExpectNonFatalFailureIgnoresFailuresInOtherThreads) {
  209.   // This statement should fail, since the current thread doesn't generate a
  210.   // fatal failure, only another one does.
  211.   EXPECT_NONFATAL_FAILURE(GenerateFatalFailureInAnotherThread(false),
  212.                           "expected");
  213.   CheckTestFailureCount(2);
  214. }
  215.  
  216. TEST(NonFatalFailureOnAllThreadsTest, ExpectNonFatalFailureOnAllThreads) {
  217.   // This statement should succeed, because failures in all threads are
  218.   // considered.
  219.   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(
  220.       GenerateFatalFailureInAnotherThread(false), "expected");
  221.   CheckTestFailureCount(0);
  222.   // We need to add a failure, because main() checks that there are failures,
  223.   // But when only this test is run, we shouldn't have any failures.
  224.   ADD_FAILURE() << "This is an expected non-fatal failure.";
  225. }
  226.  
  227. }  // namespace
  228. }  // namespace testing
  229.  
  230. int main(int argc, char **argv) {
  231.   testing::InitGoogleTest(&argc, argv);
  232.  
  233.   const int result = RUN_ALL_TESTS();  // Expected to fail.
  234.   GTEST_CHECK_(result == 1) << "RUN_ALL_TESTS() did not fail as expected";
  235.  
  236.   printf("\nPASS\n");
  237.   return 0;
  238. }
  239.  
  240. #else
  241. TEST(StressTest,
  242.      DISABLED_ThreadSafetyTestsAreSkippedWhenGoogleTestIsNotThreadSafe) {
  243. }
  244.  
  245. int main(int argc, char **argv) {
  246.   testing::InitGoogleTest(&argc, argv);
  247.   return RUN_ALL_TESTS();
  248. }
  249. #endif  // GTEST_IS_THREADSAFE
  250.