| OLD | NEW |
| (Empty) |
| 1 // Copyright 2004-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include "base/basictypes.h" | |
| 17 #include "base/scoped_ptr.h" | |
| 18 #include "omaha/base/thread_pool.h" | |
| 19 #include "omaha/base/timer.h" | |
| 20 #include "omaha/testing/unit_test.h" | |
| 21 | |
| 22 namespace omaha { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 volatile LONG g_completed_count = 0; | |
| 27 | |
| 28 // Increments the global count by 1. | |
| 29 class MyJob1 : public UserWorkItem { | |
| 30 public: | |
| 31 MyJob1() {} | |
| 32 | |
| 33 private: | |
| 34 virtual void DoProcess() { ::InterlockedExchangeAdd(&g_completed_count, 1); } | |
| 35 | |
| 36 DISALLOW_EVIL_CONSTRUCTORS(MyJob1); | |
| 37 }; | |
| 38 | |
| 39 // Increments the global count by 2. | |
| 40 class MyJob2 : public UserWorkItem { | |
| 41 public: | |
| 42 MyJob2() {} | |
| 43 | |
| 44 private: | |
| 45 virtual void DoProcess() { ::InterlockedExchangeAdd(&g_completed_count, 2); } | |
| 46 | |
| 47 DISALLOW_EVIL_CONSTRUCTORS(MyJob2); | |
| 48 }; | |
| 49 | |
| 50 // Increments the global count by 3. | |
| 51 class MyJob3 : public UserWorkItem { | |
| 52 public: | |
| 53 MyJob3() {} | |
| 54 | |
| 55 private: | |
| 56 virtual void DoProcess() { ::InterlockedExchangeAdd(&g_completed_count, 3); } | |
| 57 | |
| 58 DISALLOW_EVIL_CONSTRUCTORS(MyJob3); | |
| 59 }; | |
| 60 | |
| 61 HRESULT QueueMyJob1(ThreadPool* thread_pool) { | |
| 62 scoped_ptr<MyJob1> job(new MyJob1); | |
| 63 HRESULT hr = thread_pool->QueueUserWorkItem(job.get(), WT_EXECUTEDEFAULT); | |
| 64 if (FAILED(hr)) { | |
| 65 return hr; | |
| 66 } | |
| 67 job.release(); | |
| 68 return S_OK; | |
| 69 } | |
| 70 | |
| 71 HRESULT QueueMyJob2(ThreadPool* thread_pool) { | |
| 72 scoped_ptr<MyJob2> job(new MyJob2); | |
| 73 HRESULT hr = thread_pool->QueueUserWorkItem(job.get(), WT_EXECUTEDEFAULT); | |
| 74 if (FAILED(hr)) { | |
| 75 return hr; | |
| 76 } | |
| 77 job.release(); | |
| 78 return S_OK; | |
| 79 } | |
| 80 | |
| 81 HRESULT QueueMyJob3(ThreadPool* thread_pool) { | |
| 82 scoped_ptr<MyJob3> job(new MyJob3); | |
| 83 HRESULT hr = thread_pool->QueueUserWorkItem(job.get(), WT_EXECUTEDEFAULT); | |
| 84 if (FAILED(hr)) { | |
| 85 return hr; | |
| 86 } | |
| 87 job.release(); | |
| 88 return S_OK; | |
| 89 } | |
| 90 | |
| 91 } // namespace | |
| 92 | |
| 93 // Creates several jobs to increment a global counter by different values and | |
| 94 // then it checks the value is correct. | |
| 95 TEST(ThreadPoolTest, ThreadPool) { | |
| 96 const int kShutdownDelayMs = 0; | |
| 97 const int kNumJobsEachType = 100; | |
| 98 | |
| 99 ThreadPool thread_pool; | |
| 100 ASSERT_HRESULT_SUCCEEDED(thread_pool.Initialize(kShutdownDelayMs)); | |
| 101 | |
| 102 for (int i = 0; i != kNumJobsEachType; ++i) { | |
| 103 EXPECT_HRESULT_SUCCEEDED(QueueMyJob1(&thread_pool)); | |
| 104 EXPECT_HRESULT_SUCCEEDED(QueueMyJob2(&thread_pool)); | |
| 105 EXPECT_HRESULT_SUCCEEDED(QueueMyJob3(&thread_pool)); | |
| 106 } | |
| 107 | |
| 108 const int kMaxWaitForJobsMs = 2000; | |
| 109 LowResTimer t(true); | |
| 110 while (thread_pool.HasWorkItems() && | |
| 111 t.GetMilliseconds() < kMaxWaitForJobsMs) { | |
| 112 ::Sleep(100); | |
| 113 } | |
| 114 EXPECT_EQ(g_completed_count, 6 * kNumJobsEachType); | |
| 115 } | |
| 116 | |
| 117 } // namespace omaha | |
| 118 | |
| OLD | NEW |