Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mock_timer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 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 
18 
19 #ifndef PAGESPEED_KERNEL_BASE_MOCK_TIMER_H_
20 #define PAGESPEED_KERNEL_BASE_MOCK_TIMER_H_
21 
22 #include <vector>
23 
27 
28 namespace net_instaweb {
29 
30 class AbstractMutex;
31 class Function;
32 
33 class MockTimer : public Timer {
34  public:
35  typedef void (*Callback)(void* user_data);
36 
38  static const int64 kApr_5_2010_ms;
39 
41  MockTimer(AbstractMutex* mutex, int64 time_ms);
42  virtual ~MockTimer();
43 
46  void SetTimeUs(int64 new_time_us);
47  void SetTimeMs(int64 new_time_ms) { SetTimeUs(1000 * new_time_ms); }
48 
50  void AdvanceUs(int64 delta_us) { SetTimeUs(time_us_ + delta_us); }
51 
53  void AdvanceMs(int64 delta_ms) { AdvanceUs(1000 * delta_ms); }
54 
56  void SetTimeDeltaUs(int64 delta_us) {
57  SetTimeDeltaUsWithCallback(delta_us, NULL);
58  }
59 
63  void SetTimeDeltaUsWithCallback(int64 delta_us,
64  Function* callback);
65 
67  void SetTimeDeltaMs(int64 delta_ms) { SetTimeDeltaUs(1000 * delta_ms); }
68 
70  virtual int64 NowUs() const;
71  virtual void SleepUs(int64 us) { AdvanceUs(us); }
72  virtual void SleepMs(int64 ms) { AdvanceUs(1000 * ms); }
73 
74  private:
75  typedef struct {
76  int64 time;
77  Function* callback;
78  } TimeAndCallback;
79  mutable int64 time_us_;
80  scoped_ptr<AbstractMutex> mutex_;
81  std::vector<TimeAndCallback> deltas_us_;
82  mutable unsigned int next_delta_;
83 
84 
85 };
86 
87 }
88 
89 #endif
Definition: mock_timer.h:33
virtual int64 NowUs() const
Returns number of microseconds since 1970.
void AdvanceMs(int64 delta_ms)
Advance time, in milliseconds.
Definition: mock_timer.h:53
static const int64 kApr_5_2010_ms
A useful recent time-constant for testing.
Definition: mock_timer.h:38
void SetTimeUs(int64 new_time_us)
Abstract interface for implementing a mutex.
Definition: abstract_mutex.h:28
void SetTimeDeltaUs(int64 delta_us)
Set time advances in microseconds for the next calls to NowUs/NowMs.
Definition: mock_timer.h:56
void SetTimeDeltaUsWithCallback(int64 delta_us, Function *callback)
Definition: function.h:47
MockTimer(AbstractMutex *mutex, int64 time_ms)
Takes ownership of mutex.
virtual void SleepMs(int64 ms)
Sleep for given number of milliseconds.
Definition: mock_timer.h:72
void SetTimeDeltaMs(int64 delta_ms)
Set time advances in milliseconds for the next calls to NowUs/NowMs.
Definition: mock_timer.h:67
void AdvanceUs(int64 delta_us)
Advance forward time by the specified number of microseconds.
Definition: mock_timer.h:50
virtual void SleepUs(int64 us)
Sleep for given number of microseconds.
Definition: mock_timer.h:71
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27