2
* Copyright 2010 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.
17
// Author: jmarantz@google.com (Joshua Marantz)
19
#ifndef PAGESPEED_KERNEL_BASE_MOCK_TIMER_H_
20
#define PAGESPEED_KERNEL_BASE_MOCK_TIMER_H_
22
#include <vector> // for vector
24
#include "pagespeed/kernel/base/basictypes.h"
25
#include "pagespeed/kernel/base/scoped_ptr.h"
26
#include "pagespeed/kernel/base/timer.h"
28
namespace net_instaweb {
33
class MockTimer : public Timer {
35
typedef void (*Callback)(void* user_data);
37
// A useful recent time-constant for testing.
38
static const int64 kApr_5_2010_ms;
40
// Takes ownership of mutex.
41
MockTimer(AbstractMutex* mutex, int64 time_ms);
44
// Sets the time as in microseconds, calling any outstanding alarms
45
// with wakeup times up to and including time_us.
46
void SetTimeUs(int64 new_time_us);
47
void SetTimeMs(int64 new_time_ms) { SetTimeUs(1000 * new_time_ms); }
49
// Advance forward time by the specified number of microseconds.
50
void AdvanceUs(int64 delta_us) { SetTimeUs(time_us_ + delta_us); }
52
// Advance time, in milliseconds.
53
void AdvanceMs(int64 delta_ms) { AdvanceUs(1000 * delta_ms); }
55
// Set time advances in microseconds for the next calls to NowUs/NowMs.
56
void SetTimeDeltaUs(int64 delta_us) {
57
SetTimeDeltaUsWithCallback(delta_us, NULL);
60
// Set time advances in microseconds for the next calls to
61
// NowUs/NowMs, with the corresponding callback to execute right
62
// before that time is returned.
63
void SetTimeDeltaUsWithCallback(int64 delta_us,
66
// Set time advances in milliseconds for the next calls to NowUs/NowMs.
67
void SetTimeDeltaMs(int64 delta_ms) { SetTimeDeltaUs(1000 * delta_ms); }
69
// Returns number of microseconds since 1970.
70
virtual int64 NowUs() const;
71
virtual void SleepUs(int64 us) { AdvanceUs(us); }
72
virtual void SleepMs(int64 ms) { AdvanceUs(1000 * ms); }
79
mutable int64 time_us_;
80
scoped_ptr<AbstractMutex> mutex_;
81
std::vector<TimeAndCallback> deltas_us_;
82
mutable unsigned int next_delta_;
84
DISALLOW_COPY_AND_ASSIGN(MockTimer);
87
} // namespace net_instaweb
89
#endif // PAGESPEED_KERNEL_BASE_MOCK_TIMER_H_