1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_ 6#define BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_ 7 8#include <stddef.h> 9 10#include <cstddef> 11#include <string> 12 13#include "base/callback.h" 14#include "base/compiler_specific.h" 15#include "base/macros.h" 16#include "base/memory/ref_counted.h" 17#include "base/run_loop.h" 18#include "base/synchronization/lock.h" 19#include "base/threading/sequenced_worker_pool.h" 20 21namespace base { 22 23class MessageLoop; 24 25// Wrapper around SequencedWorkerPool for testing that blocks destruction 26// until the pool is actually destroyed. This is so that a 27// SequencedWorkerPool from one test doesn't outlive its test and cause 28// strange races with other tests that touch global stuff (like histograms and 29// logging). However, this requires that nothing else on this thread holds a 30// ref to the pool when the SequencedWorkerPoolOwner is destroyed. 31// 32// This class calls Shutdown on the owned SequencedWorkerPool in the destructor. 33// Tests may themselves call Shutdown earlier to test shutdown behavior. 34class SequencedWorkerPoolOwner : public SequencedWorkerPool::TestingObserver { 35 public: 36 SequencedWorkerPoolOwner(size_t max_threads, 37 const std::string& thread_name_prefix); 38 39 ~SequencedWorkerPoolOwner() override; 40 41 // Don't change the returned pool's testing observer. 42 const scoped_refptr<SequencedWorkerPool>& pool(); 43 44 // The given callback will be called on WillWaitForShutdown(). 45 void SetWillWaitForShutdownCallback(const Closure& callback); 46 47 int has_work_call_count() const; 48 49 private: 50 // SequencedWorkerPool::TestingObserver implementation. 51 void OnHasWork() override; 52 void WillWaitForShutdown() override; 53 void OnDestruct() override; 54 55 // Used to run the current thread's message loop until the 56 // SequencedWorkerPool's destruction has been verified. 57 base::RunLoop exit_loop_; 58 MessageLoop* const constructor_message_loop_; 59 60 scoped_refptr<SequencedWorkerPool> pool_; 61 Closure will_wait_for_shutdown_callback_; 62 63 mutable Lock has_work_lock_; 64 int has_work_call_count_; 65 66 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolOwner); 67}; 68 69} // namespace base 70 71#endif // BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_ 72