blob: bf5f2f766e151e29ff7b2a78780e01c5c5715fc4 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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
Nick Bray0bcbd3b2015-03-12 16:29:36 -07005#ifndef BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_
6#define BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_
James Robinson646469d2014-10-03 15:33:28 -07007
8#include <cstddef>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
13#include "base/compiler_specific.h"
14#include "base/memory/ref_counted.h"
15#include "base/synchronization/lock.h"
16#include "base/threading/sequenced_worker_pool.h"
17
18namespace base {
19
20class MessageLoop;
21
22// Wrapper around SequencedWorkerPool for testing that blocks destruction
23// until the pool is actually destroyed. This is so that a
24// SequencedWorkerPool from one test doesn't outlive its test and cause
25// strange races with other tests that touch global stuff (like histograms and
26// logging). However, this requires that nothing else on this thread holds a
27// ref to the pool when the SequencedWorkerPoolOwner is destroyed.
28class SequencedWorkerPoolOwner : public SequencedWorkerPool::TestingObserver {
29 public:
30 SequencedWorkerPoolOwner(size_t max_threads,
31 const std::string& thread_name_prefix);
32
James Robinsone1b30cf2014-10-21 12:25:40 -070033 ~SequencedWorkerPoolOwner() override;
James Robinson646469d2014-10-03 15:33:28 -070034
35 // Don't change the returned pool's testing observer.
36 const scoped_refptr<SequencedWorkerPool>& pool();
37
38 // The given callback will be called on WillWaitForShutdown().
39 void SetWillWaitForShutdownCallback(const Closure& callback);
40
41 int has_work_call_count() const;
42
43 private:
44 // SequencedWorkerPool::TestingObserver implementation.
James Robinsone1b30cf2014-10-21 12:25:40 -070045 void OnHasWork() override;
46 void WillWaitForShutdown() override;
47 void OnDestruct() override;
James Robinson646469d2014-10-03 15:33:28 -070048
49 MessageLoop* const constructor_message_loop_;
50 scoped_refptr<SequencedWorkerPool> pool_;
51 Closure will_wait_for_shutdown_callback_;
52
53 mutable Lock has_work_lock_;
54 int has_work_call_count_;
55
56 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolOwner);
57};
58
59} // namespace base
60
Nick Bray0bcbd3b2015-03-12 16:29:36 -070061#endif // BASE_TEST_SEQUENCED_WORKER_POOL_OWNER_H_