1 // Copyright 2018 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/task/thread_pool/service_thread.h" 6 7 #include <string> 8 9 #include "base/debug/stack_trace.h" 10 #include "base/functional/bind.h" 11 #include "build/build_config.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace base { 15 namespace internal { 16 17 namespace { 18 19 // Verifies that |query| is found on the current stack. Ignores failures if this 20 // configuration doesn't have symbols. VerifyHasStringOnStack(const std::string & query)21void VerifyHasStringOnStack(const std::string& query) { 22 const std::string stack = debug::StackTrace().ToString(); 23 SCOPED_TRACE(stack); 24 const bool found_on_stack = stack.find(query) != std::string::npos; 25 const bool stack_has_symbols = 26 stack.find("WorkerThread") != std::string::npos; 27 EXPECT_TRUE(found_on_stack || !stack_has_symbols) << query; 28 } 29 30 } // namespace 31 32 #if BUILDFLAG(IS_POSIX) 33 // Many POSIX bots flakily crash on |debug::StackTrace().ToString()|, 34 // https://crbug.com/840429. 35 #define MAYBE_StackHasIdentifyingFrame DISABLED_StackHasIdentifyingFrame 36 #else 37 #define MAYBE_StackHasIdentifyingFrame StackHasIdentifyingFrame 38 #endif 39 TEST(ThreadPoolServiceThreadTest,MAYBE_StackHasIdentifyingFrame)40TEST(ThreadPoolServiceThreadTest, MAYBE_StackHasIdentifyingFrame) { 41 ServiceThread service_thread; 42 service_thread.Start(); 43 44 service_thread.task_runner()->PostTask( 45 FROM_HERE, BindOnce(&VerifyHasStringOnStack, "ServiceThread")); 46 47 service_thread.FlushForTesting(); 48 } 49 50 } // namespace internal 51 } // namespace base 52