Page Speed Optimization Libraries
1.4.26.1
|
00001 /* 00002 * Copyright 2011 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00017 00020 00021 #include "base/logging.h" 00022 #include "net/instaweb/util/public/basictypes.h" 00023 #include "net/instaweb/util/public/condvar.h" 00024 #include "net/instaweb/util/public/function.h" 00025 #include "net/instaweb/util/public/gtest.h" 00026 #include "net/instaweb/util/public/scoped_ptr.h" 00027 #include "net/instaweb/util/public/thread_system.h" 00028 00029 #ifndef NET_INSTAWEB_UTIL_WORKER_TEST_BASE_H_ 00030 #define NET_INSTAWEB_UTIL_WORKER_TEST_BASE_H_ 00031 00032 namespace net_instaweb { 00033 00034 class WorkerTestBase : public ::testing::Test { 00035 public: 00036 class CountFunction; 00037 class SyncPoint; 00038 class NotifyRunFunction; 00039 class WaitRunFunction; 00040 class FailureFunction; 00041 00042 WorkerTestBase(); 00043 ~WorkerTestBase(); 00044 00045 protected: 00046 scoped_ptr<ThreadSystem> thread_runtime_; 00047 00048 private: 00049 DISALLOW_COPY_AND_ASSIGN(WorkerTestBase); 00050 }; 00051 00053 class WorkerTestBase::CountFunction : public Function { 00054 public: 00055 explicit CountFunction(int* variable) : variable_(variable) {} 00056 00057 virtual void Run() { 00058 ++*variable_; 00059 } 00060 00061 virtual void Cancel() { 00062 *variable_ -= 100; 00063 } 00064 00065 private: 00066 int* variable_; 00067 DISALLOW_COPY_AND_ASSIGN(CountFunction); 00068 }; 00069 00071 class WorkerTestBase::SyncPoint { 00072 public: 00073 explicit SyncPoint(ThreadSystem* thread_system); 00074 00075 void Wait(); 00076 void Notify(); 00077 00078 private: 00079 bool done_; 00080 scoped_ptr<ThreadSystem::CondvarCapableMutex> mutex_; 00081 scoped_ptr<ThreadSystem::Condvar> notify_; 00082 DISALLOW_COPY_AND_ASSIGN(SyncPoint); 00083 }; 00084 00086 class WorkerTestBase::NotifyRunFunction : public Function { 00087 public: 00088 explicit NotifyRunFunction(SyncPoint* sync); 00089 virtual void Run(); 00090 00091 private: 00092 SyncPoint* sync_; 00093 DISALLOW_COPY_AND_ASSIGN(NotifyRunFunction); 00094 }; 00095 00097 class WorkerTestBase::WaitRunFunction : public Function { 00098 public: 00099 explicit WaitRunFunction(SyncPoint* sync); 00100 virtual void Run(); 00101 00102 private: 00103 SyncPoint* sync_; 00104 DISALLOW_COPY_AND_ASSIGN(WaitRunFunction); 00105 }; 00106 00108 class DeleteNotifyFunction : public Function { 00109 public: 00110 explicit DeleteNotifyFunction(WorkerTestBase::SyncPoint* sync) 00111 : sync_(sync) {} 00112 virtual ~DeleteNotifyFunction() { 00113 sync_->Notify(); 00114 } 00115 00116 virtual void Run() { 00117 LOG(FATAL) << "DeleteNotifyFunction ran."; 00118 } 00119 00120 private: 00121 WorkerTestBase::SyncPoint* sync_; 00122 DISALLOW_COPY_AND_ASSIGN(DeleteNotifyFunction); 00123 }; 00124 00125 } 00126 00127 #endif ///< NET_INSTAWEB_UTIL_WORKER_TEST_BASE_H_