OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 | 6 |
7 #include "vm/dart_api_impl.h" | 7 #include "vm/dart_api_impl.h" |
8 #include "vm/dart_api_state.h" | 8 #include "vm/dart_api_state.h" |
9 #include "vm/globals.h" | 9 #include "vm/globals.h" |
10 #include "vm/thread_interrupter.h" | 10 #include "vm/thread_interrupter.h" |
11 #include "vm/unit_test.h" | 11 #include "vm/unit_test.h" |
12 | 12 |
13 namespace dart { | 13 namespace dart { |
14 | 14 |
15 class ThreadInterrupterTestHelper : public AllStatic { | 15 class ThreadInterrupterTestHelper : public AllStatic { |
16 public: | 16 public: |
17 static void InterruptTest(const intptr_t run_time, const intptr_t period) { | 17 static void InterruptTest(const intptr_t run_time, const intptr_t period) { |
18 const double allowed_error = 0.25; // +/- 25% | 18 const double allowed_error = 0.25; // +/- 25% |
19 intptr_t count = 0; | 19 counter = 0; |
20 Thread::EnsureInit(); | 20 Thread::EnsureInit(); |
21 Thread* thread = Thread::Current(); | 21 Thread* thread = Thread::Current(); |
22 thread->SetThreadInterrupter(IncrementCallback, &count); | 22 thread->SetThreadInterruptCallback(IncrementCallback); |
23 ThreadInterrupter::SetInterruptPeriod(period); | 23 ThreadInterrupter::SetInterruptPeriod(period); |
24 OS::Sleep(run_time * kMillisecondsPerSecond); | 24 OS::Sleep(run_time * kMillisecondsPerSecond); |
25 thread->SetThreadInterrupter(NULL, NULL); | 25 thread->SetThreadInterruptCallback(NULL); |
26 intptr_t run_time_micros = run_time * kMicrosecondsPerSecond; | 26 intptr_t run_time_micros = run_time * kMicrosecondsPerSecond; |
27 intptr_t expected_interrupts = run_time_micros / period; | 27 intptr_t expected_interrupts = run_time_micros / period; |
28 intptr_t error = allowed_error * expected_interrupts; | 28 intptr_t error = allowed_error * expected_interrupts; |
29 intptr_t low_bar = expected_interrupts - error; | 29 intptr_t low_bar = expected_interrupts - error; |
30 intptr_t high_bar = expected_interrupts + error; | 30 intptr_t high_bar = expected_interrupts + error; |
31 EXPECT_GE(count, low_bar); | 31 EXPECT_GE(counter, low_bar); |
32 EXPECT_LE(count, high_bar); | 32 EXPECT_LE(counter, high_bar); |
33 } | 33 } |
34 | 34 |
35 static void IncrementCallback(const InterruptedThreadState& state, | 35 static intptr_t counter; |
36 void* data) { | 36 |
37 ASSERT(data != NULL); | 37 static void IncrementCallback(Thread* thread, |
38 intptr_t* counter = reinterpret_cast<intptr_t*>(data); | 38 const InterruptedThreadState& state) { |
39 *counter = *counter + 1; | 39 ASSERT(thread != NULL); |
| 40 counter++; |
40 } | 41 } |
41 }; | 42 }; |
42 | 43 |
| 44 intptr_t ThreadInterrupterTestHelper::counter = 0; |
43 | 45 |
44 TEST_CASE(ThreadInterrupterHigh) { | 46 TEST_CASE(ThreadInterrupterHigh) { |
45 const intptr_t kRunTimeSeconds = 5; | 47 const intptr_t kRunTimeSeconds = 5; |
46 const intptr_t kInterruptPeriodMicros = 250; | 48 const intptr_t kInterruptPeriodMicros = 250; |
47 ThreadInterrupterTestHelper::InterruptTest(kRunTimeSeconds, | 49 ThreadInterrupterTestHelper::InterruptTest(kRunTimeSeconds, |
48 kInterruptPeriodMicros); | 50 kInterruptPeriodMicros); |
49 } | 51 } |
50 | 52 |
51 TEST_CASE(ThreadInterrupterMedium) { | 53 TEST_CASE(ThreadInterrupterMedium) { |
52 const intptr_t kRunTimeSeconds = 5; | 54 const intptr_t kRunTimeSeconds = 5; |
53 const intptr_t kInterruptPeriodMicros = 500; | 55 const intptr_t kInterruptPeriodMicros = 500; |
54 ThreadInterrupterTestHelper::InterruptTest(kRunTimeSeconds, | 56 ThreadInterrupterTestHelper::InterruptTest(kRunTimeSeconds, |
55 kInterruptPeriodMicros); | 57 kInterruptPeriodMicros); |
56 } | 58 } |
57 | 59 |
58 TEST_CASE(ThreadInterrupterLow) { | 60 TEST_CASE(ThreadInterrupterLow) { |
59 const intptr_t kRunTimeSeconds = 5; | 61 const intptr_t kRunTimeSeconds = 5; |
60 const intptr_t kInterruptPeriodMicros = 1000; | 62 const intptr_t kInterruptPeriodMicros = 1000; |
61 ThreadInterrupterTestHelper::InterruptTest(kRunTimeSeconds, | 63 ThreadInterrupterTestHelper::InterruptTest(kRunTimeSeconds, |
62 kInterruptPeriodMicros); | 64 kInterruptPeriodMicros); |
63 } | 65 } |
64 | 66 |
65 | 67 |
66 } // namespace dart | 68 } // namespace dart |
OLD | NEW |