WorkerThreadTest.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:6k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.threading;
  2. import junit.framework.TestCase;
  3. import net.javacoding.jspider.core.task.WorkerTask;
  4. import net.javacoding.jspider.core.util.config.ConfigurationFactory;
  5. import net.javacoding.jspider.mockobjects.*;
  6. import java.net.URL;
  7. /**
  8.  * $Id: WorkerThreadTest.java,v 1.6 2003/04/09 17:08:14 vanrogu Exp $
  9.  */
  10. public class WorkerThreadTest extends TestCase {
  11.     protected WorkerThread thread;
  12.     protected WorkerThreadPool pool;
  13.     public WorkerThreadTest ( ) {
  14.         super ( "WorkerThreadTest" );
  15.         // make sure we're using the 'unittest' configuration
  16.         ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST);
  17.     }
  18.     protected void setUp() throws Exception {
  19.         pool = new MockWorkerThreadPool ( );
  20.         thread = new WorkerThread(pool, "testThread", 1);
  21.     }
  22.     public void testDoubleAssign ( ) {
  23.         thread.start();
  24.         sleep();
  25.         try {
  26.             thread.assign(new WaitTask(null, 1000));
  27.         } catch (Exception e) {
  28.             e.printStackTrace();
  29.             fail("thread throw an exception during the first assign");
  30.         }
  31.         try {
  32.             thread.assign(new WaitTask(null, 1000));
  33.         } catch (Exception e) {
  34.             // thread telling us he's already busy doing something else
  35.             return;
  36.         }
  37.         fail("thread threw no exception during the first assign");
  38.     }
  39.     public void testAvailable ( ) {
  40.         thread.start();
  41.         sleep();
  42.         assertTrue ( "idle thread claiming not to be available", thread.isAvailable() );
  43.         assertFalse ( "idle thread claiming not to be occupied", thread.isOccupied() );
  44.         try {
  45.             thread.assign(new WaitTask(null, 1000));
  46.             assertFalse ( "busy thread claiming to be available", thread.isAvailable() );
  47.             assertTrue ( "busy thread claiming to be occupied", thread.isOccupied() );
  48.         } catch (Exception e) {
  49.             fail("thread throw an exception during the first assign");
  50.         }
  51.         try {
  52.             thread.assign(new WaitTask(null, 1000));
  53.         } catch (Exception e) {
  54.             assertFalse ( "busy thread claiming to be available", thread.isAvailable() );
  55.             assertTrue ( "busy thread claiming to be occupied", thread.isOccupied() );
  56.             // thread telling us he's already busy doing something else
  57.             return;
  58.         }
  59.         fail("thread threw no exception during the first assign");
  60.     }
  61.     public void testStopRunning ( ) {
  62.         thread.start();
  63.         sleep();
  64.         thread.stopRunning();
  65.         assertFalse ( "stopped thread claiming to be available", thread.isAvailable() );
  66.         assertFalse ( "stopped thread claiming to be occupied", thread.isOccupied() );
  67.     }
  68.     public void testStopRunningNotStarted ( ) {
  69.         try {
  70.             thread.stopRunning();
  71.         } catch (Exception e) {
  72.             return;
  73.         }
  74.         fail ( "not started thread accepted to be stopped" );
  75.     }
  76.     public void testAssignNotStarted ( ) {
  77.         try {
  78.             thread.assign(new WaitTask(null, 1000));
  79.         } catch (Exception e) {
  80.             return;
  81.         }
  82.         fail("not started thread accepted an assigned task");
  83.     }
  84.     public void testAssignAfterStop ( ) {
  85.         thread.start();
  86.         sleep();
  87.         thread.stopRunning();
  88.         try {
  89.             thread.assign(new WaitTask(null, 1000));
  90.         } catch (Exception e) {
  91.             return;
  92.         }
  93.         fail("stopped thread accepted an assigned task");
  94.     }
  95.     public void testInitialState ( ) {
  96.         int state = thread.getState();
  97.         int expected = WorkerThread.WORKERTHREAD_IDLE;
  98.         assertEquals ( "newly created thread reports another state than IDLE", expected, state);
  99.     }
  100.     public void testBlockedState ( ) throws Exception {
  101.         URL url = new URL ("http://j-spider.sourceforge.net");
  102.         WorkerTask task = new WaitTask ( new SimpleSpiderContext(url), 1000, 1000 );
  103.         thread.start();
  104.         sleep();
  105.         thread.assign(task);
  106.         try {
  107.             Thread.sleep(100);
  108.         } catch (InterruptedException e) {
  109.             Thread.currentThread().interrupt();
  110.         }
  111.         int state = thread.getState();
  112.         int expected = WorkerThread.WORKERTHREAD_BLOCKED;
  113.         assertEquals ( "thread waiting in prepare() did report another state than BLOCKED", expected, state);
  114.     }
  115.     public void testBusyState ( ) throws Exception {
  116.         URL url = new URL ("http://j-spider.sourceforge.net");
  117.         WorkerTask task = new WaitTask ( new SimpleSpiderContext(url), 1000, 0 );
  118.         thread.start();
  119.         sleep();
  120.         thread.assign(task);
  121.         sleep();
  122.         int state = thread.getState();
  123.         int expected = WorkerThread.WORKERTHREAD_BUSY;
  124.         assertEquals ( "thread waiting in execute() did report another state than BUSY", expected, state);
  125.     }
  126.     public void testIdleStateAfterTask ( ) throws Exception {
  127.         URL url = new URL ("http://j-spider.sourceforge.net");
  128.         WorkerTask task = new WaitTask ( new SimpleSpiderContext(url), 10, 0 );
  129.         thread.start();
  130.         sleep();
  131.         thread.assign(task);
  132.         sleep();
  133.         int state = thread.getState();
  134.         int expected = WorkerThread.WORKERTHREAD_IDLE;
  135.         assertEquals ( "thread that should have completed it's job reported another state than IDLE", expected, state);
  136.     }
  137.     // give the thread the time to start the run() method.
  138.     protected void sleep(){
  139.         try {
  140.             Thread.sleep(100);
  141.         } catch (InterruptedException e) {
  142.             Thread.currentThread().interrupt();
  143.         }
  144.     }
  145. }