2
* Copyright 2011 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
// Author: morlovich@google.com (Maksim Orlovich)
18
// This contains things that are common between unit tests for Worker and its
19
// subclasses, such as runtime creation and various closures.
21
#include "base/logging.h"
22
#include "pagespeed/kernel/base/basictypes.h"
23
#include "pagespeed/kernel/base/condvar.h"
24
#include "pagespeed/kernel/base/function.h"
25
#include "pagespeed/kernel/base/gtest.h"
26
#include "pagespeed/kernel/base/scoped_ptr.h"
27
#include "pagespeed/kernel/base/thread_system.h"
29
#ifndef PAGESPEED_KERNEL_THREAD_WORKER_TEST_BASE_H_
30
#define PAGESPEED_KERNEL_THREAD_WORKER_TEST_BASE_H_
32
namespace net_instaweb {
34
class WorkerTestBase : public ::testing::Test {
38
class NotifyRunFunction;
39
class WaitRunFunction;
40
class FailureFunction;
46
scoped_ptr<ThreadSystem> thread_runtime_;
49
DISALLOW_COPY_AND_ASSIGN(WorkerTestBase);
52
// A closure that increments a variable on running.
53
class WorkerTestBase::CountFunction : public Function {
55
explicit CountFunction(int* variable) : variable_(variable) {}
61
virtual void Cancel() {
67
DISALLOW_COPY_AND_ASSIGN(CountFunction);
70
// A way for one thread to wait for another.
71
class WorkerTestBase::SyncPoint {
73
explicit SyncPoint(ThreadSystem* thread_system);
80
scoped_ptr<ThreadSystem::CondvarCapableMutex> mutex_;
81
scoped_ptr<ThreadSystem::Condvar> notify_;
82
DISALLOW_COPY_AND_ASSIGN(SyncPoint);
85
// Notifies of itself having run on a given SyncPoint.
86
class WorkerTestBase::NotifyRunFunction : public Function {
88
explicit NotifyRunFunction(SyncPoint* sync);
93
DISALLOW_COPY_AND_ASSIGN(NotifyRunFunction);
96
// Waits on a given SyncPoint before completing Run()
97
class WorkerTestBase::WaitRunFunction : public Function {
99
explicit WaitRunFunction(SyncPoint* sync);
104
DISALLOW_COPY_AND_ASSIGN(WaitRunFunction);
107
// Function that signals on destruction and check fails when run.
108
class DeleteNotifyFunction : public Function {
110
explicit DeleteNotifyFunction(WorkerTestBase::SyncPoint* sync)
112
virtual ~DeleteNotifyFunction() {
117
LOG(FATAL) << "DeleteNotifyFunction ran.";
121
WorkerTestBase::SyncPoint* sync_;
122
DISALLOW_COPY_AND_ASSIGN(DeleteNotifyFunction);
125
} // namespace net_instaweb
127
#endif // PAGESPEED_KERNEL_THREAD_WORKER_TEST_BASE_H_