1 package org.objectweb.petals.jbi.component.thread; 2 3 import junit.framework.TestCase; 4 5 public class AbstractThreadTest extends TestCase { 6 7 10 protected AbstractThread threadTest; 11 12 17 public void testSubmissionFailure() { 18 threadTest.execute(0); 19 assertNotNull(threadTest.getJbiException()); 20 } 21 22 26 public void testMultipleConsumers() { 27 int loopA = 10; 28 long sleepA = 100; 29 int loopB = 5; 30 long sleepB = 120; 31 32 threadTest.start(); 33 34 ConsumerThread consumerA = new ConsumerThread(threadTest, loopA, sleepA); 35 ConsumerThread consumerB = new ConsumerThread(threadTest, loopB, sleepB); 36 37 consumerA.start(); 38 consumerB.start(); 39 40 try { 42 consumerA.join(loopA * sleepA * 2); 43 consumerB.join(loopB * sleepB * 2); 44 } catch (InterruptedException e) { 45 fail("Task submission failure"); 46 } 47 48 } 49 50 55 protected void setUp() throws Exception { 56 super.setUp(); 57 58 61 threadTest = new AbstractThread() { 62 @Override 63 protected int doTask(int action) { 64 int result = 0; 65 switch (action) { 66 case AbstractThread.SHUTDOWNTHREAD: 67 result = -1; 68 break; 69 70 default: 71 result = action; 72 break; 73 } 74 return result; 75 } 76 }; 77 } 78 79 84 protected class ConsumerThread extends Thread { 85 86 private AbstractThread abstractThread; 87 88 private long sleepTime; 89 90 private int loop; 91 92 99 public ConsumerThread(AbstractThread abstractThread, int loop, 100 long sleepTime) { 101 this.abstractThread = abstractThread; 102 this.loop = loop; 103 this.sleepTime = sleepTime; 104 } 105 106 111 @Override 112 public void run() { 113 for (int i = 0; i < loop; i++) { 114 abstractThread.execute(i); 115 try { 116 Thread.sleep(sleepTime); 117 } catch (InterruptedException e) { 118 } 119 } 120 } 121 } 122 123 } 124 | Popular Tags |