1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 #include <log4cxx/helpers/thread.h> 18 #include "../insertwide.h" 19 #include "../logunit.h" 20 #include <apr_time.h> 21 #include <log4cxx/helpers/exception.h> 22 23 using namespace log4cxx; 24 using namespace log4cxx::helpers; 25 26 27 /** 28 Unit test for Thread. 29 30 */ LOGUNIT_CLASS(ThreadTestCase)31LOGUNIT_CLASS(ThreadTestCase) 32 { 33 LOGUNIT_TEST_SUITE(ThreadTestCase); 34 LOGUNIT_TEST(testInterrupt); 35 LOGUNIT_TEST_SUITE_END(); 36 37 public: 38 /** 39 * Start a thread that will wait for a minute, then interrupt it. 40 */ 41 void testInterrupt() 42 { 43 Thread thread1; 44 bool interrupted = false; 45 thread1.run(sleep, &interrupted); 46 apr_sleep(100000); 47 apr_time_t start = apr_time_now(); 48 thread1.interrupt(); 49 thread1.join(); 50 LOGUNIT_ASSERT_EQUAL(true, interrupted); 51 apr_time_t elapsed = apr_time_now() - start; 52 LOGUNIT_ASSERT(elapsed < 1000000); 53 } 54 55 private: 56 static void* LOG4CXX_THREAD_FUNC sleep(apr_thread_t* thread, void* data) 57 { 58 try 59 { 60 Thread::sleep(60000); 61 } 62 catch (InterruptedException& ex) 63 { 64 *(reinterpret_cast<bool*>(data)) = true; 65 } 66 67 return NULL; 68 } 69 70 }; 71 72 LOGUNIT_TEST_SUITE_REGISTRATION(ThreadTestCase); 73 74