Move clock related functions to Clock in clock.cc.
This CL factors out clock module from base:util to sort out
dependencies.
BUG=
TEST=unittest
REF_BUG=19010851
REF_CL=84935258
Noriyuki Takahashi authored 8 years ago
Yohei Yukawa committed 8 years ago
99 | 99 | 'sources': [ |
100 | 100 | '<(gen_out_dir)/character_set.h', |
101 | 101 | '<(gen_out_dir)/version_def.h', |
102 | 'clock.cc', | |
102 | 103 | 'file_stream.cc', |
103 | 104 | 'file_util.cc', |
104 | 105 | 'flags.cc', |
198 | 198 | }, |
199 | 199 | }, |
200 | 200 | { |
201 | 'target_name': 'clock_test', | |
202 | 'type': 'executable', | |
203 | 'sources': [ | |
204 | 'clock_test.cc', | |
205 | ], | |
206 | 'dependencies': [ | |
207 | '../testing/testing.gyp:gtest_main', | |
208 | 'base.gyp:base', | |
209 | ], | |
210 | 'variables': { | |
211 | 'test_size': 'small', | |
212 | }, | |
213 | }, | |
214 | { | |
201 | 215 | 'target_name': 'encoding_util_test', |
202 | 216 | 'type': 'executable', |
203 | 217 | 'sources': [ |
375 | 389 | 'base_core_test', |
376 | 390 | 'base_init_test', |
377 | 391 | 'base_test', |
392 | 'clock_test', | |
378 | 393 | 'config_file_stream_test', |
379 | 394 | 'encoding_util_test', |
380 | 395 | 'encryptor_test', |
0 | // Copyright 2010-2015, Google Inc. | |
1 | // All rights reserved. | |
2 | // | |
3 | // Redistribution and use in source and binary forms, with or without | |
4 | // modification, are permitted provided that the following conditions are | |
5 | // met: | |
6 | // | |
7 | // * Redistributions of source code must retain the above copyright | |
8 | // notice, this list of conditions and the following disclaimer. | |
9 | // * Redistributions in binary form must reproduce the above | |
10 | // copyright notice, this list of conditions and the following disclaimer | |
11 | // in the documentation and/or other materials provided with the | |
12 | // distribution. | |
13 | // * Neither the name of Google Inc. nor the names of its | |
14 | // contributors may be used to endorse or promote products derived from | |
15 | // this software without specific prior written permission. | |
16 | // | |
17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | ||
29 | #include "base/clock.h" | |
30 | ||
31 | #ifdef OS_WIN | |
32 | #include <Windows.h> | |
33 | #include <time.h> | |
34 | #else // OS_WIN | |
35 | #ifdef OS_MACOSX | |
36 | #include <mach/mach_time.h> | |
37 | #elif defined(__native_client__) // OS_MACOSX | |
38 | #include <irt.h> | |
39 | #endif // OS_MACOSX or __native_client__ | |
40 | #include <sys/time.h> | |
41 | #endif // OS_WIN | |
42 | ||
43 | #include "base/singleton.h" | |
44 | ||
45 | namespace mozc { | |
46 | namespace { | |
47 | ||
48 | class ClockImpl : public ClockInterface { | |
49 | public: | |
50 | #ifndef __native_client__ | |
51 | ClockImpl() {} | |
52 | #else // __native_client__ | |
53 | ClockImpl() : timezone_offset_sec_(0) {} | |
54 | #endif // __native_client__ | |
55 | ||
56 | virtual ~ClockImpl() {} | |
57 | ||
58 | virtual void GetTimeOfDay(uint64 *sec, uint32 *usec) { | |
59 | #ifdef OS_WIN | |
60 | FILETIME file_time; | |
61 | GetSystemTimeAsFileTime(&file_time); | |
62 | ULARGE_INTEGER time_value; | |
63 | time_value.HighPart = file_time.dwHighDateTime; | |
64 | time_value.LowPart = file_time.dwLowDateTime; | |
65 | // Convert into microseconds | |
66 | time_value.QuadPart /= 10; | |
67 | // kDeltaEpochInMicroSecs is difference between January 1, 1970 and | |
68 | // January 1, 1601 in microsecond. | |
69 | // This number is calculated as follows. | |
70 | // ((1970 - 1601) * 365 + 89) * 24 * 60 * 60 * 1000000 | |
71 | // 89 is the number of leap years between 1970 and 1601. | |
72 | const uint64 kDeltaEpochInMicroSecs = 11644473600000000ULL; | |
73 | // Convert file time to unix epoch | |
74 | time_value.QuadPart -= kDeltaEpochInMicroSecs; | |
75 | *sec = static_cast<uint64>(time_value.QuadPart / 1000000UL); | |
76 | *usec = static_cast<uint32>(time_value.QuadPart % 1000000UL); | |
77 | #else // OS_WIN | |
78 | struct timeval tv; | |
79 | gettimeofday(&tv, nullptr); | |
80 | *sec = tv.tv_sec; | |
81 | *usec = tv.tv_usec; | |
82 | #endif // OS_WIN | |
83 | } | |
84 | ||
85 | virtual uint64 GetTime() { | |
86 | #ifdef OS_WIN | |
87 | return static_cast<uint64>(_time64(nullptr)); | |
88 | #else | |
89 | return static_cast<uint64>(time(nullptr)); | |
90 | #endif // OS_WIN | |
91 | } | |
92 | ||
93 | virtual bool GetTmWithOffsetSecond(time_t offset_sec, tm *output) { | |
94 | const time_t current_sec = static_cast<time_t>(this->GetTime()); | |
95 | const time_t modified_sec = current_sec + offset_sec; | |
96 | ||
97 | #ifdef OS_WIN | |
98 | if (_localtime64_s(output, &modified_sec) != 0) { | |
99 | return false; | |
100 | } | |
101 | #elif defined(__native_client__) | |
102 | const time_t localtime_sec = modified_sec + timezone_offset_sec_; | |
103 | if (gmtime_r(&localtime_sec, output) == nullptr) { | |
104 | return false; | |
105 | } | |
106 | #else // !OS_WIN && !__native_client__ | |
107 | if (localtime_r(&modified_sec, output) == nullptr) { | |
108 | return false; | |
109 | } | |
110 | #endif // OS_WIN | |
111 | return true; | |
112 | } | |
113 | ||
114 | virtual uint64 GetFrequency() { | |
115 | #if defined(OS_WIN) | |
116 | LARGE_INTEGER timestamp; | |
117 | // TODO(yukawa): Consider the case where QueryPerformanceCounter is not | |
118 | // available. | |
119 | const BOOL result = ::QueryPerformanceFrequency(×tamp); | |
120 | return static_cast<uint64>(timestamp.QuadPart); | |
121 | #elif defined(OS_MACOSX) | |
122 | static mach_timebase_info_data_t timebase_info; | |
123 | mach_timebase_info(&timebase_info); | |
124 | return static_cast<uint64>( | |
125 | 1.0e9 * timebase_info.denom / timebase_info.numer); | |
126 | #elif defined(OS_LINUX) | |
127 | return 1000000uLL; | |
128 | #else // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...) | |
129 | #error "Not supported platform" | |
130 | #endif // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...) | |
131 | } | |
132 | ||
133 | virtual uint64 GetTicks() { | |
134 | // TODO(team): Use functions in <chrono> once the use of it is approved. | |
135 | #if defined(OS_WIN) | |
136 | LARGE_INTEGER timestamp; | |
137 | // TODO(yukawa): Consider the case where QueryPerformanceCounter is not | |
138 | // available. | |
139 | const BOOL result = ::QueryPerformanceCounter(×tamp); | |
140 | return static_cast<uint64>(timestamp.QuadPart); | |
141 | #elif defined(OS_MACOSX) | |
142 | return static_cast<uint64>(mach_absolute_time()); | |
143 | #elif defined(OS_LINUX) | |
144 | uint64 sec; | |
145 | uint32 usec; | |
146 | GetTimeOfDay(&sec, &usec); | |
147 | return sec * 1000000 + usec; | |
148 | #else // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...) | |
149 | #error "Not supported platform" | |
150 | #endif // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...) | |
151 | } | |
152 | ||
153 | #ifdef __native_client__ | |
154 | virtual void SetTimezoneOffset(int32 timezone_offset_sec) { | |
155 | timezone_offset_sec_ = timezone_offset_sec; | |
156 | } | |
157 | ||
158 | private: | |
159 | int32 timezone_offset_sec_; | |
160 | #endif // __native_client__ | |
161 | }; | |
162 | ||
163 | ClockInterface *g_clock = nullptr; | |
164 | ||
165 | inline ClockInterface *GetClock() { | |
166 | return g_clock != nullptr ? g_clock : Singleton<ClockImpl>::get(); | |
167 | } | |
168 | ||
169 | } // namespace | |
170 | ||
171 | void Clock::GetTimeOfDay(uint64 *sec, uint32 *usec) { | |
172 | GetClock()->GetTimeOfDay(sec, usec); | |
173 | } | |
174 | ||
175 | uint64 Clock::GetTime() { | |
176 | return GetClock()->GetTime(); | |
177 | } | |
178 | ||
179 | bool Clock::GetTmWithOffsetSecond(tm *time_with_offset, int offset_sec) { | |
180 | return GetClock()->GetTmWithOffsetSecond(offset_sec, time_with_offset); | |
181 | } | |
182 | ||
183 | uint64 Clock::GetFrequency() { | |
184 | return GetClock()->GetFrequency(); | |
185 | } | |
186 | ||
187 | uint64 Clock::GetTicks() { | |
188 | return GetClock()->GetTicks(); | |
189 | } | |
190 | ||
191 | #ifdef __native_client__ | |
192 | void Clock::SetTimezoneOffset(int32 timezone_offset_sec) { | |
193 | return GetClock()->SetTimezoneOffset(timezone_offset_sec); | |
194 | } | |
195 | #endif // __native_client__ | |
196 | ||
197 | void Clock::SetClockForUnitTest(ClockInterface *clock) { | |
198 | g_clock = clock; | |
199 | } | |
200 | ||
201 | } // namespace mozc |
0 | // Copyright 2010-2015, Google Inc. | |
1 | // All rights reserved. | |
2 | // | |
3 | // Redistribution and use in source and binary forms, with or without | |
4 | // modification, are permitted provided that the following conditions are | |
5 | // met: | |
6 | // | |
7 | // * Redistributions of source code must retain the above copyright | |
8 | // notice, this list of conditions and the following disclaimer. | |
9 | // * Redistributions in binary form must reproduce the above | |
10 | // copyright notice, this list of conditions and the following disclaimer | |
11 | // in the documentation and/or other materials provided with the | |
12 | // distribution. | |
13 | // * Neither the name of Google Inc. nor the names of its | |
14 | // contributors may be used to endorse or promote products derived from | |
15 | // this software without specific prior written permission. | |
16 | // | |
17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | ||
29 | #ifndef MOZC_BASE_CLOCK_H_ | |
30 | #define MOZC_BASE_CLOCK_H_ | |
31 | ||
32 | #include <ctime> | |
33 | ||
34 | #include "base/port.h" | |
35 | ||
36 | namespace mozc { | |
37 | ||
38 | class ClockInterface { | |
39 | public: | |
40 | virtual ~ClockInterface() {} | |
41 | ||
42 | virtual void GetTimeOfDay(uint64 *sec, uint32 *usec) = 0; | |
43 | virtual uint64 GetTime() = 0; | |
44 | virtual bool GetTmWithOffsetSecond(time_t offset_sec, tm *output) = 0; | |
45 | ||
46 | // High accuracy clock. | |
47 | virtual uint64 GetFrequency() = 0; | |
48 | virtual uint64 GetTicks() = 0; | |
49 | ||
50 | #ifdef __native_client__ | |
51 | virtual void SetTimezoneOffset(int32 timezone_offset_sec) = 0; | |
52 | #endif // __native_client__ | |
53 | ||
54 | protected: | |
55 | ClockInterface() {} | |
56 | }; | |
57 | ||
58 | class Clock { | |
59 | public: | |
60 | // Gets the current time using gettimeofday-like functions. | |
61 | // sec: number of seconds from epoch | |
62 | // usec: micro-second passed: [0,1000000) | |
63 | static void GetTimeOfDay(uint64 *sec, uint32 *usec); | |
64 | ||
65 | // Gets the current time using time-like function | |
66 | // For Windows, _time64() is used. | |
67 | // For Linux/Mac, time() is used. | |
68 | static uint64 GetTime(); | |
69 | ||
70 | // Gets local time, which is offset_sec seconds after now. Returns true if | |
71 | // succeeded. | |
72 | static bool GetTmWithOffsetSecond(tm *time_with_offset, int offset_sec); | |
73 | ||
74 | // Gets the current local time to current_time. Returns true if succeeded. | |
75 | static bool GetCurrentTm(tm *current_time) { | |
76 | return GetTmWithOffsetSecond(current_time, 0); | |
77 | } | |
78 | ||
79 | // Gets the system frequency to calculate the time from ticks. | |
80 | static uint64 GetFrequency(); | |
81 | ||
82 | // Gets the current ticks. It may return incorrect value on Virtual Machines. | |
83 | // If you'd like to get a value in secs, it is necessary to divide a result by | |
84 | // GetFrequency(). | |
85 | static uint64 GetTicks(); | |
86 | ||
87 | #ifdef __native_client__ | |
88 | // Sets the time difference between local time and UTC time in seconds. | |
89 | // We use this function in NaCl Mozc because we can't know the local timezone | |
90 | // in NaCl environment. | |
91 | static void SetTimezoneOffset(int32 timezone_offset_sec); | |
92 | #endif // __native_client__ | |
93 | ||
94 | // TESTONLY: The behavior of global system clock can be overridden by using | |
95 | // this method. Set to nullptr to restore the default clock. This method | |
96 | // doesn't take the ownership of |clock|. | |
97 | static void SetClockForUnitTest(ClockInterface *clock); | |
98 | ||
99 | private: | |
100 | DISALLOW_IMPLICIT_CONSTRUCTORS(Clock); | |
101 | }; | |
102 | ||
103 | } // namespace mozc | |
104 | ||
105 | #endif // MOZC_BASE_CLOCK_H_ |
29 | 29 | #ifndef MOZC_BASE_CLOCK_MOCK_H_ |
30 | 30 | #define MOZC_BASE_CLOCK_MOCK_H_ |
31 | 31 | |
32 | #include <ctime> | |
33 | ||
32 | #include "base/clock.h" | |
34 | 33 | #include "base/port.h" |
35 | #include "base/util.h" | |
36 | 34 | |
37 | 35 | namespace mozc { |
38 | 36 | |
39 | 37 | // Standard mock clock implementation. |
40 | 38 | // This mock behaves in UTC |
41 | class ClockMock : public Util::ClockInterface { | |
39 | class ClockMock : public ClockInterface { | |
42 | 40 | public: |
43 | 41 | ClockMock(uint64 sec, uint32 usec); |
44 | 42 | virtual ~ClockMock(); |
0 | // Copyright 2010-2015, Google Inc. | |
1 | // All rights reserved. | |
2 | // | |
3 | // Redistribution and use in source and binary forms, with or without | |
4 | // modification, are permitted provided that the following conditions are | |
5 | // met: | |
6 | // | |
7 | // * Redistributions of source code must retain the above copyright | |
8 | // notice, this list of conditions and the following disclaimer. | |
9 | // * Redistributions in binary form must reproduce the above | |
10 | // copyright notice, this list of conditions and the following disclaimer | |
11 | // in the documentation and/or other materials provided with the | |
12 | // distribution. | |
13 | // * Neither the name of Google Inc. nor the names of its | |
14 | // contributors may be used to endorse or promote products derived from | |
15 | // this software without specific prior written permission. | |
16 | // | |
17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | ||
29 | #include "base/clock.h" | |
30 | ||
31 | #include "base/clock_mock.h" | |
32 | #include "testing/base/public/gunit.h" | |
33 | ||
34 | namespace mozc { | |
35 | namespace { | |
36 | ||
37 | // 2020-12-23 13:24:35 (Wed) UTC | |
38 | // 123456 [usec] | |
39 | const uint64 kTestSeconds = 1608729875uLL; | |
40 | const uint32 kTestMicroSeconds = 123456u; | |
41 | ||
42 | TEST(ClockTest, TimeTestWithMock) { | |
43 | ClockMock clock_mock(kTestSeconds, kTestMicroSeconds); | |
44 | Clock::SetClockForUnitTest(&clock_mock); | |
45 | ||
46 | // GetTime | |
47 | { | |
48 | EXPECT_EQ(kTestSeconds, Clock::GetTime()); | |
49 | } | |
50 | ||
51 | // GetTimeOfDay | |
52 | { | |
53 | uint64 current_sec; | |
54 | uint32 current_usec; | |
55 | Clock::GetTimeOfDay(¤t_sec, ¤t_usec); | |
56 | EXPECT_EQ(kTestSeconds, current_sec); | |
57 | EXPECT_EQ(kTestMicroSeconds, current_usec); | |
58 | } | |
59 | ||
60 | // GetCurrentTm | |
61 | // 2020-12-23 13:24:35 (Wed) | |
62 | { | |
63 | tm current_tm; | |
64 | Clock::GetCurrentTm(¤t_tm); | |
65 | EXPECT_EQ(120, current_tm.tm_year); | |
66 | EXPECT_EQ(11, current_tm.tm_mon); | |
67 | EXPECT_EQ(23, current_tm.tm_mday); | |
68 | EXPECT_EQ(13, current_tm.tm_hour); | |
69 | EXPECT_EQ(24, current_tm.tm_min); | |
70 | EXPECT_EQ(35, current_tm.tm_sec); | |
71 | EXPECT_EQ(3, current_tm.tm_wday); | |
72 | } | |
73 | ||
74 | // GetTmWithoutOffsetSecond | |
75 | // 2024/02/23 23:11:15 (Fri) | |
76 | { | |
77 | const int offset_seconds = 100000000; | |
78 | tm offset_tm; | |
79 | Clock::GetTmWithOffsetSecond(&offset_tm, offset_seconds); | |
80 | EXPECT_EQ(124, offset_tm.tm_year); | |
81 | EXPECT_EQ(1, offset_tm.tm_mon); | |
82 | EXPECT_EQ(23, offset_tm.tm_mday); | |
83 | EXPECT_EQ(23, offset_tm.tm_hour); | |
84 | EXPECT_EQ(11, offset_tm.tm_min); | |
85 | EXPECT_EQ(15, offset_tm.tm_sec); | |
86 | EXPECT_EQ(5, offset_tm.tm_wday); | |
87 | } | |
88 | ||
89 | // GetFrequency / GetTicks | |
90 | { | |
91 | const uint64 kFrequency = 12345; | |
92 | const uint64 kTicks = 54321; | |
93 | clock_mock.SetFrequency(kFrequency); | |
94 | EXPECT_EQ(kFrequency, Clock::GetFrequency()); | |
95 | clock_mock.SetTicks(kTicks); | |
96 | EXPECT_EQ(kTicks, Clock::GetTicks()); | |
97 | } | |
98 | ||
99 | // Restore the default clock | |
100 | Clock::SetClockForUnitTest(nullptr); | |
101 | ||
102 | // GetFrequency / GetTicks without ClockMock | |
103 | { | |
104 | EXPECT_NE(0, Clock::GetFrequency()); | |
105 | EXPECT_NE(0, Clock::GetTicks()); | |
106 | } | |
107 | } | |
108 | ||
109 | TEST(ClockTest, TimeTestWithoutMock) { | |
110 | uint64 get_time_of_day_sec, get_time_sec; | |
111 | uint32 get_time_of_day_usec; | |
112 | ||
113 | Clock::GetTimeOfDay(&get_time_of_day_sec, &get_time_of_day_usec); | |
114 | get_time_sec = Clock::GetTime(); | |
115 | ||
116 | // hmm, unstable test. | |
117 | const int margin = 1; | |
118 | EXPECT_NEAR(get_time_of_day_sec, get_time_sec, margin) | |
119 | << ": This test have possibilities to fail " | |
120 | << "when system is busy and slow."; | |
121 | } | |
122 | ||
123 | } // namespace | |
124 | } // namespace mozc |
52 | 52 | #ifdef OS_ANDROID |
53 | 53 | #include "base/const.h" |
54 | 54 | #endif // OS_ANDROID |
55 | #include "base/clock.h" | |
55 | 56 | #include "base/file_stream.h" |
56 | 57 | #include "base/file_util.h" |
57 | 58 | #include "base/flags.h" |
58 | 59 | #include "base/mutex.h" |
59 | 60 | #include "base/singleton.h" |
60 | 61 | #include "base/system_util.h" |
61 | #include "base/util.h" | |
62 | 62 | |
63 | 63 | DEFINE_bool(colored_log, true, "Enables colored log messages on tty devices"); |
64 | 64 | DEFINE_bool(logtostderr, |
102 | 102 | string Logging::GetLogMessageHeader() { |
103 | 103 | #ifndef OS_ANDROID |
104 | 104 | tm tm_time; |
105 | Util::GetCurrentTm(&tm_time); | |
105 | Clock::GetCurrentTm(&tm_time); | |
106 | 106 | |
107 | 107 | char buf[512]; |
108 | 108 | snprintf(buf, sizeof(buf), |
26 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | #include <string.h> | |
29 | #include "base/mutex.h" | |
30 | ||
31 | #include "base/clock.h" | |
30 | 32 | #include "base/logging.h" |
31 | #include "base/mutex.h" | |
32 | 33 | #include "base/thread.h" |
34 | #include "base/thread_annotations.h" | |
33 | 35 | #include "base/util.h" |
34 | 36 | #include "testing/base/public/gunit.h" |
35 | 37 | |
130 | 132 | |
131 | 133 | uint64 start_sec; |
132 | 134 | uint32 start_usec; |
133 | Util::GetTimeOfDay(&start_sec, &start_usec); | |
135 | Clock::GetTimeOfDay(&start_sec, &start_usec); | |
134 | 136 | t.Start(); |
135 | 137 | |
136 | 138 | Util::Sleep(100); |
141 | 143 | |
142 | 144 | uint64 end_sec; |
143 | 145 | uint32 end_usec; |
144 | Util::GetTimeOfDay(&end_sec, &end_usec); | |
146 | Clock::GetTimeOfDay(&end_sec, &end_usec); | |
145 | 147 | const uint64 elapsed_usec = |
146 | 148 | (end_sec - start_sec) * 1000000 + end_usec - start_usec; |
147 | 149 | EXPECT_GE(1000000, elapsed_usec); |
32 | 32 | #include <map> |
33 | 33 | #include <utility> |
34 | 34 | |
35 | #include "base/clock.h" | |
35 | 36 | #include "base/logging.h" |
36 | 37 | #include "base/mutex.h" |
37 | 38 | #include "base/port.h" |
142 | 143 | class SchedulerImpl : public Scheduler::SchedulerInterface { |
143 | 144 | public: |
144 | 145 | SchedulerImpl() { |
145 | Util::SetRandomSeed(static_cast<uint32>(Util::GetTime())); | |
146 | Util::SetRandomSeed(static_cast<uint32>(Clock::GetTime())); | |
146 | 147 | } |
147 | 148 | |
148 | 149 | virtual ~SchedulerImpl() { |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | 29 | #include "base/stopwatch.h" |
30 | #include "base/util.h" | |
30 | ||
31 | #include "base/clock.h" | |
31 | 32 | |
32 | 33 | namespace mozc { |
34 | ||
33 | 35 | Stopwatch Stopwatch::StartNew() { |
34 | 36 | Stopwatch stopwatch = Stopwatch(); |
35 | 37 | stopwatch.Start(); |
41 | 43 | frequency_(1000), |
42 | 44 | start_timestamp_(0), |
43 | 45 | elapsed_timestamp_(0) { |
44 | frequency_ = Util::GetFrequency(); | |
46 | frequency_ = Clock::GetFrequency(); | |
45 | 47 | |
46 | 48 | Reset(); |
47 | 49 | } |
53 | 55 | |
54 | 56 | void Stopwatch::Start() { |
55 | 57 | if (state_ == STOPWATCH_STOPPED) { |
56 | start_timestamp_ = Util::GetTicks(); | |
58 | start_timestamp_ = Clock::GetTicks(); | |
57 | 59 | state_ = STOPWATCH_RUNNING; |
58 | 60 | } |
59 | 61 | } |
60 | 62 | |
61 | 63 | void Stopwatch::Stop() { |
62 | 64 | if (state_ == STOPWATCH_RUNNING) { |
63 | const int64 stop_timestamp = Util::GetTicks(); | |
65 | const int64 stop_timestamp = Clock::GetTicks(); | |
64 | 66 | elapsed_timestamp_ += (stop_timestamp - start_timestamp_); |
65 | 67 | start_timestamp_ = 0; |
66 | 68 | state_ = STOPWATCH_STOPPED; |
84 | 86 | return elapsed_timestamp_; |
85 | 87 | } |
86 | 88 | |
87 | const int64 current_timestamp = Util::GetTicks(); | |
89 | const int64 current_timestamp = Clock::GetTicks(); | |
88 | 90 | elapsed_timestamp_ += (current_timestamp - start_timestamp_); |
89 | 91 | start_timestamp_ = current_timestamp; |
90 | 92 | |
94 | 96 | bool Stopwatch::IsRunning() const { |
95 | 97 | return state_ == STOPWATCH_RUNNING; |
96 | 98 | } |
99 | ||
97 | 100 | } // namespace mozc |
26 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | #include "base/stopwatch.h" | |
30 | ||
29 | 31 | #include <numeric> |
30 | 32 | |
33 | #include "base/clock.h" | |
31 | 34 | #include "base/clock_mock.h" |
32 | 35 | #include "base/scoped_ptr.h" |
33 | #include "base/stopwatch.h" | |
34 | #include "base/util.h" | |
35 | #include "testing/base/public/googletest.h" | |
36 | 36 | #include "testing/base/public/gunit.h" |
37 | 37 | |
38 | 38 | namespace mozc { |
43 | 43 | clock_mock_.reset(new ClockMock(0, 0)); |
44 | 44 | // 1GHz (Accuracy = 1ns) |
45 | 45 | clock_mock_->SetFrequency(1000000000uLL); |
46 | Util::SetClockHandler(clock_mock_.get()); | |
46 | Clock::SetClockForUnitTest(clock_mock_.get()); | |
47 | 47 | } |
48 | 48 | |
49 | 49 | void TearDown() { |
50 | Util::SetClockHandler(NULL); | |
50 | Clock::SetClockForUnitTest(nullptr); | |
51 | 51 | } |
52 | 52 | |
53 | 53 | void PutForwardNanoseconds(uint64 nano_sec) { |
1036 | 1036 | ::srand(seed); |
1037 | 1037 | } |
1038 | 1038 | |
1039 | namespace { | |
1040 | class ClockImpl : public Util::ClockInterface { | |
1041 | public: | |
1042 | #ifndef __native_client__ | |
1043 | ClockImpl() {} | |
1044 | #else // __native_client__ | |
1045 | ClockImpl() : timezone_offset_sec_(0) {} | |
1046 | #endif // __native_client__ | |
1047 | virtual ~ClockImpl() {} | |
1048 | ||
1049 | virtual void GetTimeOfDay(uint64 *sec, uint32 *usec) { | |
1050 | #ifdef OS_WIN | |
1051 | FILETIME file_time; | |
1052 | GetSystemTimeAsFileTime(&file_time); | |
1053 | ULARGE_INTEGER time_value; | |
1054 | time_value.HighPart = file_time.dwHighDateTime; | |
1055 | time_value.LowPart = file_time.dwLowDateTime; | |
1056 | // Convert into microseconds | |
1057 | time_value.QuadPart /= 10; | |
1058 | // kDeltaEpochInMicroSecs is difference between January 1, 1970 and | |
1059 | // January 1, 1601 in microsecond. | |
1060 | // This number is calculated as follows. | |
1061 | // ((1970 - 1601) * 365 + 89) * 24 * 60 * 60 * 1000000 | |
1062 | // 89 is the number of leap years between 1970 and 1601. | |
1063 | const uint64 kDeltaEpochInMicroSecs = 11644473600000000ULL; | |
1064 | // Convert file time to unix epoch | |
1065 | time_value.QuadPart -= kDeltaEpochInMicroSecs; | |
1066 | *sec = static_cast<uint64>(time_value.QuadPart / 1000000UL); | |
1067 | *usec = static_cast<uint32>(time_value.QuadPart % 1000000UL); | |
1068 | #else // OS_WIN | |
1069 | struct timeval tv; | |
1070 | gettimeofday(&tv, NULL); | |
1071 | *sec = tv.tv_sec; | |
1072 | *usec = tv.tv_usec; | |
1073 | #endif // OS_WIN | |
1074 | } | |
1075 | ||
1076 | virtual uint64 GetTime() { | |
1077 | #ifdef OS_WIN | |
1078 | return static_cast<uint64>(_time64(NULL)); | |
1079 | #else | |
1080 | return static_cast<uint64>(time(NULL)); | |
1081 | #endif // OS_WIN | |
1082 | } | |
1083 | ||
1084 | virtual bool GetTmWithOffsetSecond(time_t offset_sec, tm *output) { | |
1085 | const time_t current_sec = static_cast<time_t>(this->GetTime()); | |
1086 | const time_t modified_sec = current_sec + offset_sec; | |
1087 | ||
1088 | #ifdef OS_WIN | |
1089 | if (_localtime64_s(output, &modified_sec) != 0) { | |
1090 | return false; | |
1091 | } | |
1092 | #elif defined(__native_client__) | |
1093 | const time_t localtime_sec = modified_sec + timezone_offset_sec_; | |
1094 | if (gmtime_r(&localtime_sec, output) == NULL) { | |
1095 | return false; | |
1096 | } | |
1097 | #else // !OS_WIN && !__native_client__ | |
1098 | if (localtime_r(&modified_sec, output) == NULL) { | |
1099 | return false; | |
1100 | } | |
1101 | #endif // OS_WIN | |
1102 | return true; | |
1103 | } | |
1104 | ||
1105 | virtual uint64 GetFrequency() { | |
1106 | #if defined(OS_WIN) | |
1107 | LARGE_INTEGER timestamp; | |
1108 | // TODO(yukawa): Consider the case where QueryPerformanceCounter is not | |
1109 | // available. | |
1110 | const BOOL result = ::QueryPerformanceFrequency(×tamp); | |
1111 | return static_cast<uint64>(timestamp.QuadPart); | |
1112 | #elif defined(OS_MACOSX) | |
1113 | static mach_timebase_info_data_t timebase_info; | |
1114 | mach_timebase_info(&timebase_info); | |
1115 | return static_cast<uint64>( | |
1116 | 1.0e9 * timebase_info.denom / timebase_info.numer); | |
1117 | #elif defined(OS_LINUX) | |
1118 | return 1000000uLL; | |
1119 | #else // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...) | |
1120 | #error "Not supported platform" | |
1121 | #endif // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...) | |
1122 | } | |
1123 | ||
1124 | virtual uint64 GetTicks() { | |
1125 | // TODO(team): Use functions in <chrono> once the use of it is approved. | |
1126 | #if defined(OS_WIN) | |
1127 | LARGE_INTEGER timestamp; | |
1128 | // TODO(yukawa): Consider the case where QueryPerformanceCounter is not | |
1129 | // available. | |
1130 | const BOOL result = ::QueryPerformanceCounter(×tamp); | |
1131 | return static_cast<uint64>(timestamp.QuadPart); | |
1132 | #elif defined(OS_MACOSX) | |
1133 | return static_cast<uint64>(mach_absolute_time()); | |
1134 | #elif defined(OS_LINUX) | |
1135 | uint64 sec; | |
1136 | uint32 usec; | |
1137 | GetTimeOfDay(&sec, &usec); | |
1138 | return sec * 1000000 + usec; | |
1139 | #else // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...) | |
1140 | #error "Not supported platform" | |
1141 | #endif // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...) | |
1142 | } | |
1143 | ||
1144 | #ifdef __native_client__ | |
1145 | virtual void SetTimezoneOffset(int32 timezone_offset_sec) { | |
1146 | timezone_offset_sec_ = timezone_offset_sec; | |
1147 | } | |
1148 | ||
1149 | private: | |
1150 | int32 timezone_offset_sec_; | |
1151 | #endif // __native_client__ | |
1152 | }; | |
1153 | ||
1154 | Util::ClockInterface *g_clock_handler = NULL; | |
1155 | ||
1156 | Util::ClockInterface *GetClockHandler() { | |
1157 | if (g_clock_handler != NULL) { | |
1158 | return g_clock_handler; | |
1159 | } else { | |
1160 | return Singleton<ClockImpl>::get(); | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | } // namespace | |
1165 | ||
1166 | void Util::SetClockHandler(Util::ClockInterface *handler) { | |
1167 | g_clock_handler = handler; | |
1168 | } | |
1169 | ||
1170 | void Util::GetTimeOfDay(uint64 *sec, uint32 *usec) { | |
1171 | GetClockHandler()->GetTimeOfDay(sec, usec); | |
1172 | } | |
1173 | ||
1174 | uint64 Util::GetTime() { | |
1175 | return GetClockHandler()->GetTime(); | |
1176 | } | |
1177 | ||
1178 | bool Util::GetCurrentTm(tm *current_time) { | |
1179 | return GetTmWithOffsetSecond(current_time, 0); | |
1180 | } | |
1181 | ||
1182 | bool Util::GetTmWithOffsetSecond(tm *time_with_offset, int offset_sec) { | |
1183 | return GetClockHandler()->GetTmWithOffsetSecond(offset_sec, time_with_offset); | |
1184 | } | |
1185 | ||
1186 | uint64 Util::GetFrequency() { | |
1187 | return GetClockHandler()->GetFrequency(); | |
1188 | } | |
1189 | ||
1190 | uint64 Util::GetTicks() { | |
1191 | return GetClockHandler()->GetTicks(); | |
1192 | } | |
1193 | ||
1194 | 1039 | void Util::Sleep(uint32 msec) { |
1195 | 1040 | #ifdef OS_WIN |
1196 | 1041 | ::Sleep(msec); |
1198 | 1043 | usleep(msec * 1000); |
1199 | 1044 | #endif // OS_WIN |
1200 | 1045 | } |
1201 | ||
1202 | #ifdef __native_client__ | |
1203 | void Util::SetTimezoneOffset(int32 timezone_offset_sec) { | |
1204 | return GetClockHandler()->SetTimezoneOffset(timezone_offset_sec); | |
1205 | } | |
1206 | #endif // __native_client__ | |
1207 | 1046 | |
1208 | 1047 | namespace { |
1209 | 1048 |
280 | 280 | |
281 | 281 | // Set the seed of Util::Random(). |
282 | 282 | static void SetRandomSeed(uint32 seed); |
283 | ||
284 | // Get the current time info using gettimeofday-like functions. | |
285 | // sec: number of seconds from epoch | |
286 | // usec: micro-second passed: [0,1000000) | |
287 | static void GetTimeOfDay(uint64 *sec, uint32 *usec); | |
288 | ||
289 | // Get the current time info using time-like function | |
290 | // For Windows, _time64() is used. | |
291 | // For Linux/Mac, time() is used. | |
292 | static uint64 GetTime(); | |
293 | ||
294 | // Get the current local time to current_time. Returns true if succeeded. | |
295 | static bool GetCurrentTm(tm *current_time); | |
296 | // Get local time, which is offset_sec seconds after now. Returns true if | |
297 | // succeeded. | |
298 | static bool GetTmWithOffsetSecond(tm *time_with_offset, int offset_sec); | |
299 | ||
300 | // Get the system frequency to calculate the time from ticks. | |
301 | static uint64 GetFrequency(); | |
302 | ||
303 | // Get the current ticks. It may return incorrect value on Virtual Machines. | |
304 | // If you'd like to get a value in secs, it is necessary to divide a result by | |
305 | // GetFrequency(). | |
306 | static uint64 GetTicks(); | |
307 | ||
308 | #ifdef __native_client__ | |
309 | // Sets the time difference between local time and UTC time in seconds. | |
310 | // We use this function in NaCl Mozc because we can't know the local timezone | |
311 | // in NaCl environment. | |
312 | static void SetTimezoneOffset(int32 timezone_offset_sec); | |
313 | #endif // __native_client__ | |
314 | ||
315 | // Interface of the helper class. | |
316 | // Default implementation is defined in the .cc file. | |
317 | class ClockInterface { | |
318 | public: | |
319 | virtual ~ClockInterface() {} | |
320 | virtual void GetTimeOfDay(uint64 *sec, uint32 *usec) = 0; | |
321 | virtual uint64 GetTime() = 0; | |
322 | virtual bool GetTmWithOffsetSecond(time_t offset_sec, tm *output) = 0; | |
323 | ||
324 | // High accuracy clock. | |
325 | virtual uint64 GetFrequency() = 0; | |
326 | virtual uint64 GetTicks() = 0; | |
327 | #ifdef __native_client__ | |
328 | virtual void SetTimezoneOffset(int32 timezone_offset_sec) = 0; | |
329 | #endif // __native_client__ | |
330 | }; | |
331 | ||
332 | // This function is provided for test. | |
333 | // The behavior of system clock can be customized by replacing this handler. | |
334 | static void SetClockHandler(Util::ClockInterface *handler); | |
335 | 283 | |
336 | 284 | // Suspends the execution of the current thread until |
337 | 285 | // the time-out interval elapses. |
31 | 31 | #include <climits> |
32 | 32 | #include <cstdlib> |
33 | 33 | #include <cstring> |
34 | #include <ctime> | |
35 | 34 | #include <map> |
36 | 35 | #include <sstream> |
37 | 36 | #include <string> |
38 | 37 | |
39 | #include "base/clock_mock.h" | |
40 | 38 | #include "base/compiler_specific.h" |
41 | 39 | #include "base/file_stream.h" |
42 | 40 | #include "base/file_util.h" |
1492 | 1490 | line = "line\n\n\n"; |
1493 | 1491 | EXPECT_TRUE(Util::ChopReturns(&line)); |
1494 | 1492 | EXPECT_EQ("line", line); |
1495 | } | |
1496 | ||
1497 | // 2020-12-23 13:24:35 (Wed) UTC | |
1498 | // 123456 [usec] | |
1499 | const uint64 kTestSeconds = 1608729875uLL; | |
1500 | const uint32 kTestMicroSeconds = 123456u; | |
1501 | ||
1502 | // time utility test with mock clock | |
1503 | TEST(UtilTest, TimeTestWithMock) { | |
1504 | scoped_ptr<ClockMock> mock_clock( | |
1505 | new ClockMock(kTestSeconds, kTestMicroSeconds)); | |
1506 | Util::SetClockHandler(mock_clock.get()); | |
1507 | ||
1508 | // GetTime, | |
1509 | { | |
1510 | EXPECT_EQ(kTestSeconds, Util::GetTime()); | |
1511 | } | |
1512 | ||
1513 | // GetTimeOfDay | |
1514 | { | |
1515 | uint64 current_sec; | |
1516 | uint32 current_usec; | |
1517 | Util::GetTimeOfDay(¤t_sec, ¤t_usec); | |
1518 | EXPECT_EQ(kTestSeconds, current_sec); | |
1519 | EXPECT_EQ(kTestMicroSeconds, current_usec); | |
1520 | } | |
1521 | ||
1522 | // GetCurrentTm | |
1523 | // 2020-12-23 13:24:35 (Wed) | |
1524 | { | |
1525 | tm current_tm; | |
1526 | Util::GetCurrentTm(¤t_tm); | |
1527 | EXPECT_EQ(120, current_tm.tm_year); | |
1528 | EXPECT_EQ(11, current_tm.tm_mon); | |
1529 | EXPECT_EQ(23, current_tm.tm_mday); | |
1530 | EXPECT_EQ(13, current_tm.tm_hour); | |
1531 | EXPECT_EQ(24, current_tm.tm_min); | |
1532 | EXPECT_EQ(35, current_tm.tm_sec); | |
1533 | EXPECT_EQ(3, current_tm.tm_wday); | |
1534 | } | |
1535 | ||
1536 | // GetTmWithoutOffsetSecond | |
1537 | // 2024/02/23 23:11:15 (Fri) | |
1538 | { | |
1539 | const int offset_seconds = 100000000; | |
1540 | tm offset_tm; | |
1541 | Util::GetTmWithOffsetSecond(&offset_tm, offset_seconds); | |
1542 | EXPECT_EQ(124, offset_tm.tm_year); | |
1543 | EXPECT_EQ(1, offset_tm.tm_mon); | |
1544 | EXPECT_EQ(23, offset_tm.tm_mday); | |
1545 | EXPECT_EQ(23, offset_tm.tm_hour); | |
1546 | EXPECT_EQ(11, offset_tm.tm_min); | |
1547 | EXPECT_EQ(15, offset_tm.tm_sec); | |
1548 | EXPECT_EQ(5, offset_tm.tm_wday); | |
1549 | } | |
1550 | ||
1551 | // GetFrequency / GetTicks | |
1552 | { | |
1553 | const uint64 kFrequency = 12345; | |
1554 | const uint64 kTicks = 54321; | |
1555 | mock_clock->SetFrequency(kFrequency); | |
1556 | EXPECT_EQ(kFrequency, Util::GetFrequency()); | |
1557 | mock_clock->SetTicks(kTicks); | |
1558 | EXPECT_EQ(kTicks, Util::GetTicks()); | |
1559 | } | |
1560 | ||
1561 | // unset clock handler | |
1562 | Util::SetClockHandler(NULL); | |
1563 | ||
1564 | // GetFrequency / GetTicks without ClockMock | |
1565 | { | |
1566 | EXPECT_NE(0, Util::GetFrequency()); | |
1567 | EXPECT_NE(0, Util::GetTicks()); | |
1568 | } | |
1569 | } | |
1570 | ||
1571 | // time utility test without mock clock | |
1572 | TEST(UtilTest, TimeTestWithoutMock) { | |
1573 | uint64 get_time_of_day_sec, get_time_sec; | |
1574 | uint32 get_time_of_day_usec; | |
1575 | ||
1576 | Util::GetTimeOfDay(&get_time_of_day_sec, &get_time_of_day_usec); | |
1577 | get_time_sec = Util::GetTime(); | |
1578 | ||
1579 | // hmm, unstable test. | |
1580 | const int margin = 1; | |
1581 | EXPECT_NEAR(get_time_of_day_sec, get_time_sec, margin) | |
1582 | << ": This test have possibilities to fail " | |
1583 | << "when system is busy and slow."; | |
1584 | 1493 | } |
1585 | 1494 | |
1586 | 1495 | TEST(UtilTest, EncodeURI) { |
36 | 36 | #include <queue> |
37 | 37 | #include <string> |
38 | 38 | |
39 | #include "base/clock.h" | |
39 | 40 | #include "base/flags.h" |
40 | 41 | #include "base/logging.h" |
41 | 42 | #include "base/mutex.h" |
169 | 170 | } |
170 | 171 | |
171 | 172 | virtual void Run() { |
172 | Util::SetRandomSeed(static_cast<uint32>(Util::GetTime())); | |
173 | Util::SetRandomSeed(static_cast<uint32>(Clock::GetTime())); | |
173 | 174 | RegisterPepperInstanceForHTTPClient(instance_); |
174 | 175 | #ifdef GOOGLE_JAPANESE_INPUT_BUILD |
175 | 176 | const bool filesystem_available = |
32 | 32 | |
33 | 33 | #include <algorithm> |
34 | 34 | |
35 | #include "base/clock.h" | |
35 | 36 | #include "base/config_file_stream.h" |
36 | 37 | #include "base/logging.h" |
37 | 38 | #include "base/number_util.h" |
39 | 40 | #include "base/scoped_ptr.h" |
40 | 41 | #include "base/singleton.h" |
41 | 42 | #include "base/system_util.h" |
42 | #include "base/util.h" | |
43 | 43 | #include "base/version.h" |
44 | 44 | #include "protocol/config.pb.h" |
45 | 45 | |
322 | 322 | void ConfigHandler::SetMetaData(Config *config) { |
323 | 323 | GeneralConfig *general_config = config->mutable_general_config(); |
324 | 324 | general_config->set_config_version(CONFIG_VERSION); |
325 | general_config->set_last_modified_time(Util::GetTime()); | |
325 | general_config->set_last_modified_time(Clock::GetTime()); | |
326 | 326 | general_config->set_last_modified_product_version(Version::GetMozcVersion()); |
327 | 327 | general_config->set_platform(SystemUtil::GetOSVersionString()); |
328 | 328 | } |
31 | 31 | #include <algorithm> |
32 | 32 | #include <climits> |
33 | 33 | |
34 | #include "base/clock.h" | |
34 | 35 | #include "base/file_stream.h" |
35 | 36 | #include "base/hash.h" |
36 | 37 | #include "base/logging.h" |
32 | 32 | #include <QtCore/QMutexLocker> |
33 | 33 | #include <QtGui/QtGui> |
34 | 34 | |
35 | #include "base/clock.h" | |
35 | 36 | #include "base/logging.h" |
36 | #include "base/util.h" | |
37 | 37 | #include "config/stats_config_util.h" |
38 | 38 | |
39 | 39 | namespace { |
94 | 94 | void HandWritingThread::SetStrokes(const handwriting::Strokes &strokes) { |
95 | 95 | CopyStrokes(strokes, &strokes_, &strokes_mutex_); |
96 | 96 | // This is absolutely thread-unsafe but practically no problems. |
97 | Util::GetTimeOfDay(&strokes_sec_, &strokes_usec_); | |
97 | Clock::GetTimeOfDay(&strokes_sec_, &strokes_usec_); | |
98 | 98 | } |
99 | 99 | |
100 | 100 | void HandWritingThread::GetCandidates(vector<string> *candidates) { |
28 | 28 | |
29 | 29 | #include "ipc/process_watch_dog.h" |
30 | 30 | |
31 | #include "base/clock.h" | |
31 | 32 | #include "base/logging.h" |
32 | 33 | #include "base/port.h" |
33 | 34 | #include "base/util.h" |
42 | 43 | public: |
43 | 44 | void Signaled(ProcessWatchDog::SignalType type) { |
44 | 45 | EXPECT_EQ(type, ProcessWatchDog::PROCESS_SIGNALED); |
45 | const uint64 diff = Util::GetTime() - g_current_time; | |
46 | const uint64 diff = Clock::GetTime() - g_current_time; | |
46 | 47 | EXPECT_EQ(2, diff); // allow 1-sec error |
47 | 48 | } |
48 | 49 | }; |
49 | 50 | |
50 | 51 | TEST(ProcessWatchDog, ProcessWatchDogTest) { |
51 | g_current_time = Util::GetTime(); | |
52 | g_current_time = Clock::GetTime(); | |
52 | 53 | |
53 | 54 | #ifndef OS_WIN |
54 | 55 | // revoke myself with different parameter |
0 | 0 | MAJOR=2 |
1 | 1 | MINOR=17 |
2 | BUILD=2144 | |
2 | BUILD=2145 | |
3 | 3 | REVISION=102 |
4 | 4 | # NACL_DICTIONARY_VERSION is the target version of the system dictionary to be |
5 | 5 | # downloaded by NaCl Mozc. |
34 | 34 | #include <string> |
35 | 35 | #include <unordered_set> |
36 | 36 | |
37 | #include "base/clock.h" | |
37 | 38 | #include "base/config_file_stream.h" |
38 | 39 | #include "base/flags.h" |
39 | 40 | #include "base/hash.h" |
1543 | 1544 | return; |
1544 | 1545 | } |
1545 | 1546 | |
1546 | const uint64 last_access_time = Util::GetTime(); | |
1547 | const uint64 last_access_time = Clock::GetTime(); | |
1547 | 1548 | const uint32 dic_key = Fingerprint("", "", type); |
1548 | 1549 | |
1549 | 1550 | CHECK(dic_.get()); |
1653 | 1654 | } |
1654 | 1655 | |
1655 | 1656 | const bool is_suggestion = segments->request_type() != Segments::CONVERSION; |
1656 | const uint64 last_access_time = Util::GetTime(); | |
1657 | const uint64 last_access_time = Clock::GetTime(); | |
1657 | 1658 | |
1658 | 1659 | // If user inputs a punctuation just after some long sentence, |
1659 | 1660 | // we make a new candidate by concatinating the top element in LRU and |
32 | 32 | #include <climits> |
33 | 33 | #include <string> |
34 | 34 | |
35 | #include "base/clock.h" | |
35 | 36 | #include "base/logging.h" |
36 | 37 | #include "base/mutex.h" |
37 | 38 | #include "base/process.h" |
94 | 95 | case RendererLauncher::RENDERER_TIMEOUT: |
95 | 96 | case RendererLauncher::RENDERER_TERMINATED: |
96 | 97 | if (error_times_ <= kMaxErrorTimes && |
97 | Util::GetTime() - last_launch_time_ >= kRetryIntervalTime) { | |
98 | Clock::GetTime() - last_launch_time_ >= kRetryIntervalTime) { | |
98 | 99 | return true; |
99 | 100 | } |
100 | 101 | VLOG(1) << "never re-launch renderer"; |
126 | 127 | } |
127 | 128 | |
128 | 129 | void Run() { |
129 | last_launch_time_ = Util::GetTime(); | |
130 | last_launch_time_ = Clock::GetTime(); | |
130 | 131 | |
131 | 132 | NamedEventListener listener(name_.c_str()); |
132 | 133 | const bool listener_is_available = listener.IsAvailable(); |
42 | 42 | #include <string> |
43 | 43 | #include <vector> |
44 | 44 | |
45 | #include "base/clock.h" | |
45 | 46 | #include "base/logging.h" |
46 | 47 | #include "base/number_util.h" |
47 | 48 | #include "base/util.h" |
1862 | 1863 | // Checks given date is valid or not in this year |
1863 | 1864 | bool IsValidDateInThisYear(uint32 month, uint32 day) { |
1864 | 1865 | struct tm t_st; |
1865 | if (!Util::GetTmWithOffsetSecond(&t_st, 0)) { | |
1866 | if (!Clock::GetTmWithOffsetSecond(&t_st, 0)) { | |
1866 | 1867 | LOG(ERROR) << "GetTmWithOffsetSecond() failed"; |
1867 | 1868 | return false; |
1868 | 1869 | } |
2025 | 2026 | vector<string> era; |
2026 | 2027 | switch (type) { |
2027 | 2028 | case REWRITE_DATE: { |
2028 | if (!Util::GetTmWithOffsetSecond(&t_st, diff * 86400)) { | |
2029 | if (!Clock::GetTmWithOffsetSecond(&t_st, diff * 86400)) { | |
2029 | 2030 | LOG(ERROR) << "GetTmWithOffsetSecond() failed"; |
2030 | 2031 | return false; |
2031 | 2032 | } |
2050 | 2051 | } |
2051 | 2052 | |
2052 | 2053 | case REWRITE_MONTH: { |
2053 | if (!Util::GetCurrentTm(&t_st)) { | |
2054 | if (!Clock::GetCurrentTm(&t_st)) { | |
2054 | 2055 | LOG(ERROR) << "GetCurrentTm failed"; |
2055 | 2056 | return false; |
2056 | 2057 | } |
2064 | 2065 | } |
2065 | 2066 | |
2066 | 2067 | case REWRITE_YEAR: { |
2067 | if (!Util::GetCurrentTm(&t_st)) { | |
2068 | if (!Clock::GetCurrentTm(&t_st)) { | |
2068 | 2069 | LOG(ERROR) << "GetCurrentTm failed"; |
2069 | 2070 | return false; |
2070 | 2071 | } |
2084 | 2085 | } |
2085 | 2086 | |
2086 | 2087 | case REWRITE_CURRENT_TIME: { |
2087 | if (!Util::GetCurrentTm(&t_st)) { | |
2088 | if (!Clock::GetCurrentTm(&t_st)) { | |
2088 | 2089 | LOG(ERROR) << "GetCurrentTm failed"; |
2089 | 2090 | return false; |
2090 | 2091 | } |
2098 | 2099 | } |
2099 | 2100 | |
2100 | 2101 | case REWRITE_DATE_AND_CURRENT_TIME: { |
2101 | if (!Util::GetCurrentTm(&t_st)) { | |
2102 | if (!Clock::GetCurrentTm(&t_st)) { | |
2102 | 2103 | LOG(ERROR) << "GetCurrentTm failed"; |
2103 | 2104 | return false; |
2104 | 2105 | } |
2165 | 2166 | |
2166 | 2167 | bool DateRewriter::RewriteWeekday(Segment *segment) const { |
2167 | 2168 | struct tm t_st; |
2168 | if (!Util::GetCurrentTm(&t_st)) { | |
2169 | if (!Clock::GetCurrentTm(&t_st)) { | |
2169 | 2170 | LOG(ERROR) << "GetCurrentTm failed"; |
2170 | 2171 | return false; |
2171 | 2172 | } |
30 | 30 | |
31 | 31 | #include <cstddef> |
32 | 32 | |
33 | #include "base/clock.h" | |
33 | 34 | #include "base/clock_mock.h" |
34 | 35 | #include "base/port.h" |
35 | 36 | #include "base/scoped_ptr.h" |
221 | 222 | TEST_F(DateRewriterTest, DateRewriteTest) { |
222 | 223 | scoped_ptr<ClockMock> mock_clock( |
223 | 224 | new ClockMock(kTestSeconds, kTestMicroSeconds)); |
224 | Util::SetClockHandler(mock_clock.get()); | |
225 | Clock::SetClockForUnitTest(mock_clock.get()); | |
225 | 226 | |
226 | 227 | DateRewriter rewriter; |
227 | 228 | Segments segments; |
470 | 471 | } |
471 | 472 | } |
472 | 473 | |
473 | Util::SetClockHandler(NULL); | |
474 | Clock::SetClockForUnitTest(nullptr); | |
474 | 475 | } |
475 | 476 | |
476 | 477 | TEST_F(DateRewriterTest, ADToERA) { |
32 | 32 | #include <ctime> |
33 | 33 | #include <string> |
34 | 34 | |
35 | #include "base/clock.h" | |
35 | 36 | #include "base/logging.h" |
36 | 37 | #include "base/singleton.h" |
37 | 38 | #include "base/util.h" |
74 | 75 | void ChangeFortune() { |
75 | 76 | const int *levels = kNormalLevels; |
76 | 77 | tm today; |
77 | if (Util::GetCurrentTm(&today)) { | |
78 | if (Clock::GetCurrentTm(&today)) { | |
78 | 79 | // Modify once per one day |
79 | 80 | if (today.tm_yday == last_update_yday_ && |
80 | 81 | today.tm_year == last_update_year_) { |
30 | 30 | |
31 | 31 | #include <string> |
32 | 32 | |
33 | #include "base/clock.h" | |
33 | 34 | #include "base/clock_mock.h" |
34 | 35 | #include "base/file_util.h" |
35 | 36 | #include "base/logging.h" |
161 | 162 | ConfigHandler::SetConfig(config); |
162 | 163 | CharacterFormManager::GetCharacterFormManager()->Reload(); |
163 | 164 | |
164 | Util::SetClockHandler(NULL); | |
165 | Clock::SetClockForUnitTest(NULL); | |
165 | 166 | |
166 | 167 | pos_matcher_ = mock_data_manager_.GetPOSMatcher(); |
167 | 168 | pos_group_.reset(new PosGroup(mock_data_manager_.GetPosGroupData())); |
170 | 171 | } |
171 | 172 | |
172 | 173 | virtual void TearDown() { |
173 | Util::SetClockHandler(NULL); | |
174 | Clock::SetClockForUnitTest(NULL); | |
174 | 175 | |
175 | 176 | scoped_ptr<UserSegmentHistoryRewriter> rewriter( |
176 | 177 | CreateUserSegmentHistoryRewriter()); |
472 | 473 | const uint64 kSeconds = 0; |
473 | 474 | const uint32 kMicroSeconds = 0; |
474 | 475 | ClockMock clock(kSeconds, kMicroSeconds); |
475 | Util::SetClockHandler(&clock); | |
476 | Clock::SetClockForUnitTest(&clock); | |
476 | 477 | |
477 | 478 | { |
478 | 479 | InitSegments(&segments, 1); |
576 | 577 | const uint64 kSeconds = 0; |
577 | 578 | const uint32 kMicroSeconds = 0; |
578 | 579 | ClockMock clock(kSeconds, kMicroSeconds); |
579 | Util::SetClockHandler(&clock); | |
580 | Clock::SetClockForUnitTest(&clock); | |
580 | 581 | |
581 | 582 | { |
582 | 583 | InitSegments(&segments, 1); |
1470 | 1471 | const uint64 kSeconds = 0; |
1471 | 1472 | const uint32 kMicroSeconds = 0; |
1472 | 1473 | ClockMock clock(kSeconds, kMicroSeconds); |
1473 | Util::SetClockHandler(&clock); | |
1474 | Clock::SetClockForUnitTest(&clock); | |
1474 | 1475 | |
1475 | 1476 | InitSegments(&segments, 1); |
1476 | 1477 | segments.mutable_segment(0)->move_candidate(2, 0); |
1614 | 1615 | const uint64 kSeconds = 0; |
1615 | 1616 | const uint32 kMicroSeconds = 0; |
1616 | 1617 | ClockMock clock(kSeconds, kMicroSeconds); |
1617 | Util::SetClockHandler(&clock); | |
1618 | Clock::SetClockForUnitTest(&clock); | |
1618 | 1619 | |
1619 | 1620 | rewriter->Clear(); |
1620 | 1621 | for (int i = 0; i < 5; ++i) { |
33 | 33 | #include <string> |
34 | 34 | #include <vector> |
35 | 35 | |
36 | #include "base/clock.h" | |
36 | 37 | #include "base/logging.h" |
37 | 38 | #include "base/port.h" |
38 | 39 | #include "base/process.h" |
218 | 219 | Session::~Session() {} |
219 | 220 | |
220 | 221 | void Session::InitContext(ImeContext *context) const { |
221 | context->set_create_time(Util::GetTime()); | |
222 | context->set_create_time(Clock::GetTime()); | |
222 | 223 | context->set_last_command_time(0); |
223 | 224 | context->set_composer(new composer::Composer(NULL, &context->GetRequest())); |
224 | 225 | context->set_converter( |
2844 | 2845 | } |
2845 | 2846 | |
2846 | 2847 | void Session::UpdateTime() { |
2847 | context_->set_last_command_time(Util::GetTime()); | |
2848 | context_->set_last_command_time(Clock::GetTime()); | |
2848 | 2849 | } |
2849 | 2850 | |
2850 | 2851 | void Session::TransformInput(commands::Input *input) { |
30 | 30 | |
31 | 31 | #include <string> |
32 | 32 | |
33 | #include "base/clock.h" | |
33 | 34 | #include "base/port.h" |
34 | 35 | #include "base/system_util.h" |
35 | 36 | #include "base/util.h" |
64 | 65 | public: |
65 | 66 | SessionConverterStressTest() { |
66 | 67 | if (!FLAGS_test_deterministic) { |
67 | FLAGS_test_srand_seed = static_cast<int32>(Util::GetTime()); | |
68 | FLAGS_test_srand_seed = static_cast<int32>(Clock::GetTime()); | |
68 | 69 | } |
69 | 70 | Util::SetRandomSeed(static_cast<uint32>(FLAGS_test_srand_seed)); |
70 | 71 | } |
35 | 35 | #include <string> |
36 | 36 | #include <vector> |
37 | 37 | |
38 | #include "base/clock.h" | |
38 | 39 | #include "base/flags.h" |
39 | 40 | #include "base/init.h" |
40 | 41 | #include "base/logging.h" |
141 | 142 | SessionHandler::SessionHandler(EngineInterface *engine) |
142 | 143 | : is_available_(false), |
143 | 144 | max_session_size_(0), |
144 | last_session_empty_time_(Util::GetTime()), | |
145 | last_session_empty_time_(Clock::GetTime()), | |
145 | 146 | last_cleanup_time_(0), |
146 | 147 | last_create_session_time_(0), |
147 | 148 | engine_(engine), |
595 | 596 | const int create_session_minimum_interval = |
596 | 597 | max(0, min(FLAGS_create_session_min_interval, 10)); |
597 | 598 | |
598 | uint64 current_time = Util::GetTime(); | |
599 | uint64 current_time = Clock::GetTime(); | |
599 | 600 | if (last_create_session_time_ != 0 && |
600 | 601 | (current_time - last_create_session_time_) < |
601 | 602 | create_session_minimum_interval) { |
641 | 642 | session->set_application_info(command->input().application_info()); |
642 | 643 | #ifdef __native_client__ |
643 | 644 | if (command->input().application_info().has_timezone_offset()) { |
644 | Util::SetTimezoneOffset( | |
645 | Clock::SetTimezoneOffset( | |
645 | 646 | command->input().application_info().timezone_offset()); |
646 | 647 | } |
647 | 648 | #endif // __native_client__ |
673 | 674 | // no active session and client doesn't send any conversion |
674 | 675 | // request to the server for FLAGS_timeout sec. |
675 | 676 | bool SessionHandler::Cleanup(commands::Command *command) { |
676 | const uint64 current_time = Util::GetTime(); | |
677 | const uint64 current_time = Clock::GetTime(); | |
677 | 678 | |
678 | 679 | // suspend/hibernation may happen |
679 | 680 | uint64 suspend_time = 0; |
794 | 795 | // if session gets empty, save the timestamp |
795 | 796 | if (last_session_empty_time_ == 0 && |
796 | 797 | session_map_->Size() == 0) { |
797 | last_session_empty_time_ = Util::GetTime(); | |
798 | last_session_empty_time_ = Clock::GetTime(); | |
798 | 799 | } |
799 | 800 | |
800 | 801 | return true; |
66 | 66 | protected: |
67 | 67 | virtual void SetUp() { |
68 | 68 | SessionHandlerTestBase::SetUp(); |
69 | Util::SetClockHandler(NULL); | |
69 | Clock::SetClockForUnitTest(NULL); | |
70 | 70 | GenericStorageManagerFactory::SetGenericStorageManager(NULL); |
71 | 71 | } |
72 | 72 | |
73 | 73 | virtual void TearDown() { |
74 | 74 | GenericStorageManagerFactory::SetGenericStorageManager(NULL); |
75 | Util::SetClockHandler(NULL); | |
75 | Clock::SetClockForUnitTest(NULL); | |
76 | 76 | SessionHandlerTestBase::TearDown(); |
77 | 77 | } |
78 | 78 | }; |
81 | 81 | uint32 expected_session_created_num = 0; |
82 | 82 | const int32 interval_time = FLAGS_create_session_min_interval = 10; // 10 sec |
83 | 83 | ClockMock clock(1000, 0); |
84 | Util::SetClockHandler(&clock); | |
84 | Clock::SetClockForUnitTest(&clock); | |
85 | 85 | |
86 | 86 | // The oldest item is remvoed |
87 | 87 | const size_t session_size = 3; |
146 | 146 | TEST_F(SessionHandlerTest, CreateSessionMinInterval) { |
147 | 147 | const int32 interval_time = FLAGS_create_session_min_interval = 10; // 10 sec |
148 | 148 | ClockMock clock(1000, 0); |
149 | Util::SetClockHandler(&clock); | |
149 | Clock::SetClockForUnitTest(&clock); | |
150 | 150 | |
151 | 151 | scoped_ptr<EngineInterface> engine(MockDataEngineFactory::Create()); |
152 | 152 | SessionHandler handler(engine.get()); |
165 | 165 | TEST_F(SessionHandlerTest, LastCreateSessionTimeout) { |
166 | 166 | const int32 timeout = FLAGS_last_create_session_timeout = 10; // 10 sec |
167 | 167 | ClockMock clock(1000, 0); |
168 | Util::SetClockHandler(&clock); | |
168 | Clock::SetClockForUnitTest(&clock); | |
169 | 169 | |
170 | 170 | scoped_ptr<EngineInterface> engine(MockDataEngineFactory::Create()); |
171 | 171 | SessionHandler handler(engine.get()); |
182 | 182 | TEST_F(SessionHandlerTest, LastCommandTimeout) { |
183 | 183 | const int32 timeout = FLAGS_last_command_timeout = 10; // 10 sec |
184 | 184 | ClockMock clock(1000, 0); |
185 | Util::SetClockHandler(&clock); | |
185 | Clock::SetClockForUnitTest(&clock); | |
186 | 186 | |
187 | 187 | scoped_ptr<EngineInterface> engine(MockDataEngineFactory::Create()); |
188 | 188 | SessionHandler handler(engine.get()); |
272 | 272 | uint64 id = 0; |
273 | 273 | |
274 | 274 | ClockMock clock(1000, 0); |
275 | Util::SetClockHandler(&clock); | |
275 | Clock::SetClockForUnitTest(&clock); | |
276 | 276 | EXPECT_TRUE(CreateSession(&handler, &id)); |
277 | 277 | EXPECT_TIMING_STATS("ElapsedTimeUSec", 0, 1, 0, 0); |
278 | 278 | } |
33 | 33 | #include <string> |
34 | 34 | #include <vector> |
35 | 35 | |
36 | #include "base/clock.h" | |
36 | 37 | #include "base/logging.h" |
37 | 38 | #include "base/mutex.h" |
38 | 39 | #include "base/number_util.h" |
39 | 40 | #include "base/port.h" |
40 | 41 | #include "base/scheduler.h" |
41 | #include "base/util.h" | |
42 | 42 | #include "config/stats_config_util.h" |
43 | 43 | #include "protocol/commands.pb.h" |
44 | 44 | #include "protocol/state.pb.h" |
72 | 72 | uint64 GetTimeInMilliSecond() { |
73 | 73 | uint64 second = 0; |
74 | 74 | uint32 micro_second = 0; |
75 | Util::GetTimeOfDay(&second, µ_second); | |
75 | Clock::GetTimeOfDay(&second, µ_second); | |
76 | 76 | return second * 1000 + micro_second / 1000; |
77 | 77 | } |
78 | 78 |
30 | 30 | |
31 | 31 | #include <string> |
32 | 32 | |
33 | #include "base/clock.h" | |
33 | 34 | #include "base/clock_mock.h" |
34 | 35 | #include "base/logging.h" |
35 | 36 | #include "base/scheduler.h" |
59 | 60 | SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir); |
60 | 61 | UsageStats::ClearAllStatsForTest(); |
61 | 62 | |
62 | Util::SetClockHandler(NULL); | |
63 | Clock::SetClockForUnitTest(nullptr); | |
63 | 64 | |
64 | 65 | scheduler_stub_.reset(new SchedulerStub); |
65 | 66 | Scheduler::SetSchedulerHandler(scheduler_stub_.get()); |
69 | 70 | } |
70 | 71 | |
71 | 72 | virtual void TearDown() { |
72 | Util::SetClockHandler(NULL); | |
73 | Scheduler::SetSchedulerHandler(NULL); | |
74 | config::StatsConfigUtil::SetHandler(NULL); | |
73 | Clock::SetClockForUnitTest(nullptr); | |
74 | Scheduler::SetSchedulerHandler(nullptr); | |
75 | config::StatsConfigUtil::SetHandler(nullptr); | |
75 | 76 | |
76 | 77 | UsageStats::ClearAllStatsForTest(); |
77 | 78 | } |
150 | 151 | const uint64 kSeconds = 0; |
151 | 152 | const uint32 kMicroSeconds = 0; |
152 | 153 | ClockMock clock(kSeconds, kMicroSeconds); |
153 | Util::SetClockHandler(&clock); | |
154 | Clock::SetClockForUnitTest(&clock); | |
154 | 155 | |
155 | 156 | // prepare command |
156 | 157 | commands::Command orig_show_command, orig_hide_command; |
33 | 33 | #include <numeric> |
34 | 34 | #include <string> |
35 | 35 | |
36 | #include "base/clock.h" | |
36 | 37 | #include "base/cpu_stats.h" |
37 | 38 | #include "base/logging.h" |
38 | 39 | #include "base/port.h" |
39 | 40 | #include "base/scoped_ptr.h" |
40 | 41 | #include "base/system_util.h" |
41 | 42 | #include "base/unnamed_event.h" |
42 | #include "base/util.h" | |
43 | 43 | #include "client/client_interface.h" |
44 | 44 | |
45 | 45 | namespace mozc { |
137 | 137 | |
138 | 138 | fill(cpu_loads, cpu_loads + arraysize(cpu_loads), 0.0); |
139 | 139 | |
140 | uint64 last_cleanup_time = Util::GetTime(); | |
140 | uint64 last_cleanup_time = Clock::GetTime(); | |
141 | 141 | |
142 | 142 | while (true) { |
143 | 143 | VLOG(1) << "Start sleeping " << idle_interval_msec; |
170 | 170 | |
171 | 171 | DCHECK_GT(cpu_loads_index, 0); |
172 | 172 | |
173 | const uint64 current_cleanup_time = Util::GetTime(); | |
173 | const uint64 current_cleanup_time = Clock::GetTime(); | |
174 | 174 | if (!CanSendCleanupCommand(cpu_loads, |
175 | 175 | cpu_loads_index, |
176 | 176 | current_cleanup_time, |
37 | 37 | #include <string> |
38 | 38 | #include <vector> |
39 | 39 | |
40 | #include "base/clock.h" | |
40 | 41 | #include "base/file_stream.h" |
41 | 42 | #include "base/file_util.h" |
42 | 43 | #include "base/hash.h" |
72 | 73 | } |
73 | 74 | |
74 | 75 | void Update(char *ptr) { |
75 | const uint32 last_access_time = static_cast<uint32>(Util::GetTime()); | |
76 | const uint32 last_access_time = static_cast<uint32>(Clock::GetTime()); | |
76 | 77 | memcpy(ptr + 8, reinterpret_cast<const char *>(&last_access_time), 4); |
77 | 78 | } |
78 | 79 | |
79 | 80 | void Update(char *ptr, uint64 fp, const char *value, size_t value_size) { |
80 | const uint32 last_access_time = static_cast<uint32>(Util::GetTime()); | |
81 | const uint32 last_access_time = static_cast<uint32>(Clock::GetTime()); | |
81 | 82 | memcpy(ptr, reinterpret_cast<const char *>(&fp), 8); |
82 | 83 | memcpy(ptr + 8, reinterpret_cast<const char *>(&last_access_time), 4); |
83 | 84 | memcpy(ptr + 12, value, value_size); |
36 | 36 | #include <sstream> |
37 | 37 | #include <string> |
38 | 38 | |
39 | #include "base/clock.h" | |
39 | 40 | #include "base/const.h" |
40 | 41 | #include "base/file_util.h" |
41 | 42 | #include "base/flags.h" |
243 | 244 | } // namespace |
244 | 245 | |
245 | 246 | MozcEngine::MozcEngine() |
246 | : last_sync_time_(Util::GetTime()), | |
247 | : last_sync_time_(Clock::GetTime()), | |
247 | 248 | key_event_handler_(new KeyEventHandler), |
248 | 249 | client_(CreateAndConfigureClient()), |
249 | 250 | #ifdef MOZC_ENABLE_X11_SELECTION_MONITOR |
640 | 641 | return; |
641 | 642 | } |
642 | 643 | |
643 | const uint64 current_time = Util::GetTime(); | |
644 | const uint64 current_time = Clock::GetTime(); | |
644 | 645 | if (force || |
645 | 646 | (current_time >= last_sync_time_ && |
646 | 647 | current_time - last_sync_time_ >= kSyncDataInterval)) { |
34 | 34 | #ifdef OS_ANDROID |
35 | 35 | #include "base/android_util.h" |
36 | 36 | #endif // OS_ANDROID |
37 | #include "base/clock.h" | |
37 | 38 | #include "base/encryptor.h" |
38 | 39 | #include "base/mutex.h" |
39 | 40 | #include "base/port.h" |
295 | 296 | |
296 | 297 | bool UsageStatsUploader::Send(void *data) { |
297 | 298 | const string upload_key = string(kRegistryPrefix) + kLastUploadKey; |
298 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
299 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
299 | 300 | uint32 last_upload_sec = 0; |
300 | 301 | const string mozc_version_key = string(kRegistryPrefix) + kMozcVersionKey; |
301 | 302 | const string ¤t_mozc_version = Version::GetMozcVersion(); |
38 | 38 | #include <utility> |
39 | 39 | #include <vector> |
40 | 40 | |
41 | #include "base/clock.h" | |
41 | 42 | #include "base/port.h" |
42 | 43 | #include "base/singleton.h" |
43 | 44 | #include "base/system_util.h" |
215 | 216 | }; |
216 | 217 | |
217 | 218 | TEST_F(UsageStatsUploaderTest, SendTest) { |
218 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
219 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
219 | 220 | const uint32 last_upload_sec = current_sec - kOneDaySec; |
220 | 221 | SetUpMetaData(last_upload_sec); |
221 | 222 | SetValidResult(); |
237 | 238 | } |
238 | 239 | |
239 | 240 | TEST_F(UsageStatsUploaderTest, FirstTimeSendTest) { |
240 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
241 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
241 | 242 | // Don't call SetUpMetaData().. |
242 | 243 | SetValidResult(); |
243 | 244 | |
261 | 262 | } |
262 | 263 | |
263 | 264 | TEST_F(UsageStatsUploaderTest, SendFailTest) { |
264 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
265 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
265 | 266 | const uint32 last_upload_sec = current_sec - kHalfDaySec; |
266 | 267 | SetUpMetaData(last_upload_sec); |
267 | 268 | SetValidResult(); |
277 | 278 | } |
278 | 279 | |
279 | 280 | TEST_F(UsageStatsUploaderTest, InvalidLastUploadTest) { |
280 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
281 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
281 | 282 | // future time |
282 | 283 | // for example: time zone has changed |
283 | 284 | const uint32 invalid_sec = current_sec + kHalfDaySec; |
296 | 297 | } |
297 | 298 | |
298 | 299 | TEST_F(UsageStatsUploaderTest, MozcVersionMismatchTest) { |
299 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
300 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
300 | 301 | const uint32 last_upload_sec = current_sec - kOneDaySec; |
301 | 302 | SetUpMetaDataWithMozcVersion(last_upload_sec, "invalid_mozc_version"); |
302 | 303 | SetValidResult(); |
331 | 332 | }; |
332 | 333 | |
333 | 334 | TEST_F(UsageStatsUploaderTest, SaveMetadataFailTest) { |
334 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
335 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
335 | 336 | const uint32 last_upload_sec = current_sec - kOneDaySec; |
336 | 337 | const string current_version = Version::GetMozcVersion(); |
337 | 338 | SetUpMetaData(last_upload_sec); |
364 | 365 | } |
365 | 366 | |
366 | 367 | TEST_F(UsageStatsUploaderTest, UploadFailTest) { |
367 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
368 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
368 | 369 | const uint32 last_upload_sec = current_sec - kOneDaySec; |
369 | 370 | SetUpMetaData(last_upload_sec); |
370 | 371 | SetValidResult(); |
389 | 390 | } |
390 | 391 | |
391 | 392 | TEST_F(UsageStatsUploaderTest, UploadRetryTest) { |
392 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
393 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
393 | 394 | const uint32 last_upload_sec = current_sec - kOneDaySec; |
394 | 395 | SetUpMetaData(last_upload_sec); |
395 | 396 | SetValidResult(); |
426 | 427 | } |
427 | 428 | |
428 | 429 | TEST_F(UsageStatsUploaderTest, UploadDataTest) { |
429 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
430 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
430 | 431 | const uint32 last_upload_sec = current_sec - kOneDaySec; |
431 | 432 | SetUpMetaData(last_upload_sec); |
432 | 433 | SetValidResult(); |
492 | 493 | |
493 | 494 | TEST_F(UsageStatsUploaderTest, UploadTouchEventStats) { |
494 | 495 | // save last_upload |
495 | const uint32 current_sec = static_cast<uint32>(Util::GetTime()); | |
496 | const uint32 current_sec = static_cast<uint32>(Clock::GetTime()); | |
496 | 497 | const uint32 last_upload_sec = current_sec - kOneDaySec; |
497 | 498 | SetUpMetaData(last_upload_sec); |
498 | 499 | SetValidResult(); |
30 | 30 | |
31 | 31 | #include <memory> |
32 | 32 | |
33 | #include "base/clock.h" | |
34 | #include "base/clock_mock.h" | |
33 | 35 | #include "testing/base/public/googletest.h" |
34 | 36 | #include "testing/base/public/gunit.h" |
35 | ||
36 | #include "base/clock_mock.h" | |
37 | #include "base/util.h" | |
38 | 37 | |
39 | 38 | namespace mozc { |
40 | 39 | namespace win32 { |
53 | 52 | clock_mock_.reset(new ClockMock(0, 0)); |
54 | 53 | // 1 kHz (Accuracy = 1msec) |
55 | 54 | clock_mock_->SetFrequency(1000uLL); |
56 | Util::SetClockHandler(clock_mock_.get()); | |
55 | Clock::SetClockForUnitTest(clock_mock_.get()); | |
57 | 56 | } |
58 | 57 | |
59 | 58 | virtual void TearDown() { |
60 | Util::SetClockHandler(NULL); | |
59 | Clock::SetClockForUnitTest(nullptr); | |
61 | 60 | } |
62 | 61 | |
63 | 62 | void PutForwardMilliseconds(uint64 milli_sec) { |