~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/thread/worker_test_base.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2011 Google Inc.
3
 
 *
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
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
// Author: morlovich@google.com (Maksim Orlovich)
17
 
 
18
 
// This contains things that are common between unit tests for Worker and its
19
 
// subclasses, such as runtime creation and various closures.
20
 
 
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"
28
 
 
29
 
#ifndef PAGESPEED_KERNEL_THREAD_WORKER_TEST_BASE_H_
30
 
#define PAGESPEED_KERNEL_THREAD_WORKER_TEST_BASE_H_
31
 
 
32
 
namespace net_instaweb {
33
 
 
34
 
class WorkerTestBase : public ::testing::Test {
35
 
 public:
36
 
  class CountFunction;
37
 
  class SyncPoint;
38
 
  class NotifyRunFunction;
39
 
  class WaitRunFunction;
40
 
  class FailureFunction;
41
 
 
42
 
  WorkerTestBase();
43
 
  ~WorkerTestBase();
44
 
 
45
 
 protected:
46
 
  scoped_ptr<ThreadSystem> thread_runtime_;
47
 
 
48
 
 private:
49
 
  DISALLOW_COPY_AND_ASSIGN(WorkerTestBase);
50
 
};
51
 
 
52
 
// A closure that increments a variable on running.
53
 
class WorkerTestBase::CountFunction : public Function {
54
 
 public:
55
 
  explicit CountFunction(int* variable) : variable_(variable) {}
56
 
 
57
 
  virtual void Run() {
58
 
    ++*variable_;
59
 
  }
60
 
 
61
 
  virtual void Cancel() {
62
 
    *variable_ -= 100;
63
 
  }
64
 
 
65
 
 private:
66
 
  int* variable_;
67
 
  DISALLOW_COPY_AND_ASSIGN(CountFunction);
68
 
};
69
 
 
70
 
// A way for one thread to wait for another.
71
 
class WorkerTestBase::SyncPoint {
72
 
 public:
73
 
  explicit SyncPoint(ThreadSystem* thread_system);
74
 
 
75
 
  void Wait();
76
 
  void Notify();
77
 
 
78
 
 private:
79
 
  bool done_;
80
 
  scoped_ptr<ThreadSystem::CondvarCapableMutex> mutex_;
81
 
  scoped_ptr<ThreadSystem::Condvar> notify_;
82
 
  DISALLOW_COPY_AND_ASSIGN(SyncPoint);
83
 
};
84
 
 
85
 
// Notifies of itself having run on a given SyncPoint.
86
 
class WorkerTestBase::NotifyRunFunction : public Function {
87
 
 public:
88
 
  explicit NotifyRunFunction(SyncPoint* sync);
89
 
  virtual void Run();
90
 
 
91
 
 private:
92
 
  SyncPoint* sync_;
93
 
  DISALLOW_COPY_AND_ASSIGN(NotifyRunFunction);
94
 
};
95
 
 
96
 
// Waits on a given SyncPoint before completing Run()
97
 
class WorkerTestBase::WaitRunFunction : public Function {
98
 
 public:
99
 
  explicit WaitRunFunction(SyncPoint* sync);
100
 
  virtual void Run();
101
 
 
102
 
 private:
103
 
  SyncPoint* sync_;
104
 
  DISALLOW_COPY_AND_ASSIGN(WaitRunFunction);
105
 
};
106
 
 
107
 
// Function that signals on destruction and check fails when run.
108
 
class DeleteNotifyFunction : public Function {
109
 
 public:
110
 
  explicit DeleteNotifyFunction(WorkerTestBase::SyncPoint* sync)
111
 
      : sync_(sync) {}
112
 
  virtual ~DeleteNotifyFunction() {
113
 
    sync_->Notify();
114
 
  }
115
 
 
116
 
  virtual void Run() {
117
 
    LOG(FATAL) << "DeleteNotifyFunction ran.";
118
 
  }
119
 
 
120
 
 private:
121
 
  WorkerTestBase::SyncPoint* sync_;
122
 
  DISALLOW_COPY_AND_ASSIGN(DeleteNotifyFunction);
123
 
};
124
 
 
125
 
}  // namespace net_instaweb
126
 
 
127
 
#endif  // PAGESPEED_KERNEL_THREAD_WORKER_TEST_BASE_H_