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