Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Unified Diff: base/threading/thread_checker_unittest.cc

Issue 2869893003: New Sequence/Thread checking API. (Closed)
Patch Set: fix tests in non-dcheck builds, need fixture can't use lambdas Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/threading/thread_checker.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/threading/thread_checker_unittest.cc
diff --git a/base/threading/thread_checker_unittest.cc b/base/threading/thread_checker_unittest.cc
index 96455e66c704efee659099ff6db2fd484b4b550e..cc409742729ec845c9f5c968daf0ff86f66d0fdb 100644
--- a/base/threading/thread_checker_unittest.cc
+++ b/base/threading/thread_checker_unittest.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/threading/thread_checker.h"
+
#include <memory>
#include "base/bind.h"
@@ -9,9 +11,9 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/sequence_token.h"
+#include "base/test/gtest_util.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/simple_thread.h"
-#include "base/threading/thread_checker_impl.h"
#include "base/threading/thread_task_runner_handle.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -192,4 +194,50 @@ TEST(ThreadCheckerTest, DetachFromThreadWithSequenceToken) {
EXPECT_FALSE(thread_checker.CalledOnValidThread());
}
+namespace {
+
+// This fixture is a helper for unit testing the thread checker macros as it is
+// not possible to inline ExpectDeathOnOtherThread() and
+// ExpectNoDeathOnOtherThreadAfterDetach() as lambdas since binding
+// |Unretained(&my_sequence_checker)| wouldn't compile on non-dcheck builds
+// where it won't be defined.
+class ThreadCheckerMacroTest : public testing::Test {
+ public:
+ ThreadCheckerMacroTest() = default;
+
+ void ExpectDeathOnOtherThread() {
+#if DCHECK_IS_ON()
+ EXPECT_DCHECK_DEATH({ DCHECK_CALLED_ON_VALID_THREAD(my_thread_checker_); });
+#else
+ // Happily no-ops on non-dcheck builds.
+ DCHECK_CALLED_ON_VALID_THREAD(my_thread_checker_);
+#endif
+ }
+
+ void ExpectNoDeathOnOtherThreadAfterDetach() {
+ DCHECK_CALLED_ON_VALID_THREAD(my_thread_checker_);
+ }
+
+ protected:
+ THREAD_CHECKER(my_thread_checker_);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ThreadCheckerMacroTest);
+};
+
+} // namespace
+
+TEST_F(ThreadCheckerMacroTest, Macros) {
+ THREAD_CHECKER(my_thread_checker);
+
+ RunCallbackOnNewThreadSynchronously(Bind(
+ &ThreadCheckerMacroTest::ExpectDeathOnOtherThread, Unretained(this)));
+
+ DETACH_FROM_THREAD(my_thread_checker_);
+
+ RunCallbackOnNewThreadSynchronously(
+ Bind(&ThreadCheckerMacroTest::ExpectNoDeathOnOtherThreadAfterDetach,
+ Unretained(this)));
+}
+
} // namespace base
« no previous file with comments | « base/threading/thread_checker.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698