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 intptr_t count = 0; |
20 ThreadInterrupter::Unregister(); | 20 Thread::EnsureInit(); |
| 21 Thread* thread = Thread::Current(); |
| 22 thread->SetThreadInterrupter(IncrementCallback, &count); |
21 ThreadInterrupter::SetInterruptPeriod(period); | 23 ThreadInterrupter::SetInterruptPeriod(period); |
22 ThreadInterrupter::Register(IncrementCallback, &count); | |
23 OS::Sleep(run_time * kMillisecondsPerSecond); | 24 OS::Sleep(run_time * kMillisecondsPerSecond); |
24 ThreadInterrupter::Unregister(); | 25 thread->SetThreadInterrupter(NULL, NULL); |
25 intptr_t run_time_micros = run_time * kMicrosecondsPerSecond; | 26 intptr_t run_time_micros = run_time * kMicrosecondsPerSecond; |
26 intptr_t expected_interrupts = run_time_micros / period; | 27 intptr_t expected_interrupts = run_time_micros / period; |
27 intptr_t error = allowed_error * expected_interrupts; | 28 intptr_t error = allowed_error * expected_interrupts; |
28 intptr_t low_bar = expected_interrupts - error; | 29 intptr_t low_bar = expected_interrupts - error; |
29 intptr_t high_bar = expected_interrupts + error; | 30 intptr_t high_bar = expected_interrupts + error; |
30 EXPECT_GE(count, low_bar); | 31 EXPECT_GE(count, low_bar); |
31 EXPECT_LE(count, high_bar); | 32 EXPECT_LE(count, high_bar); |
32 } | 33 } |
33 | 34 |
34 static void IncrementCallback(const InterruptedThreadState& state, | 35 static void IncrementCallback(const InterruptedThreadState& state, |
(...skipping 21 matching lines...) Expand all Loading... |
56 | 57 |
57 TEST_CASE(ThreadInterrupterLow) { | 58 TEST_CASE(ThreadInterrupterLow) { |
58 const intptr_t kRunTimeSeconds = 5; | 59 const intptr_t kRunTimeSeconds = 5; |
59 const intptr_t kInterruptPeriodMicros = 1000; | 60 const intptr_t kInterruptPeriodMicros = 1000; |
60 ThreadInterrupterTestHelper::InterruptTest(kRunTimeSeconds, | 61 ThreadInterrupterTestHelper::InterruptTest(kRunTimeSeconds, |
61 kInterruptPeriodMicros); | 62 kInterruptPeriodMicros); |
62 } | 63 } |
63 | 64 |
64 | 65 |
65 } // namespace dart | 66 } // namespace dart |
OLD | NEW |