Improved fix for the failed unit test
authorFrancois Chouinard <fchouinard@gmail.com>
Wed, 9 Jan 2013 20:30:58 +0000 (15:30 -0500)
committerFrancois Chouinard <fchouinard@gmail.com>
Wed, 9 Jan 2013 20:51:02 +0000 (15:51 -0500)
Change-Id: Ib16a748e631a93b8faf440394be86db0bc6d0b06
Signed-off-by: Francois Chouinard <fchouinard@gmail.com>
Reviewed-on: https://git.eclipse.org/r/9567
Tested-by: Hudson CI
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/request/TmfRequestExecutorTest.java

index 0b5729f7212aee636d51d38389b86157b01f9bff..bed7cdd609c0e79492b67fd1eceaa8afdcac2afc 100644 (file)
@@ -14,7 +14,17 @@ package org.eclipse.linuxtools.tmf.core.tests.request;
 
 import junit.framework.TestCase;
 
+import org.eclipse.linuxtools.internal.tmf.core.component.TmfEventThread;
 import org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor;
+import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider;
+import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
+import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
+import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
+import org.eclipse.linuxtools.tmf.core.request.ITmfRequest;
+import org.eclipse.linuxtools.tmf.core.request.TmfRequest;
+import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
 
 /**
  * Test suite for the TmfRequestExecutor class.
@@ -76,176 +86,210 @@ public class TmfRequestExecutorTest extends TestCase {
        }
 
        // ------------------------------------------------------------------------
-       // execute - works fine in my environment but has timing issues at Eclipse
-       // Disabled for now until I have a decent synchronization solution
+       // execute with pre-emption
        // ------------------------------------------------------------------------
 
-//     // Dummy context
-//     private static class MyContext implements ITmfContext {
-//         private long fNbRequested;
-//        private long fRank;
-//
-//        public MyContext(long requested) {
-//            fNbRequested = requested;
-//            fRank = 0;
-//        }
-//        @Override
-//        public long getRank() {
-//            return (fRank <= fNbRequested) ? fRank : -1;
-//        }
-//        @Override
-//        public ITmfLocation getLocation() {
-//            return null;
-//        }
-//        @Override
-//        public boolean hasValidRank() {
-//            return true;
-//        }
-//        @Override
-//        public void setLocation(ITmfLocation location) {
-//        }
-//        @Override
-//        public void setRank(long rank) {
-//            fRank = rank;
-//        }
-//        @Override
-//        public void increaseRank() {
-//            fRank++;
-//        }
-//        @Override
-//        public void dispose() {
-//        }
-//        @Override
-//        public MyContext clone() {
-//            return this;
-//        }
-//     }
-//
-//     // Dummy provider
-//     private static class MyProvider extends TmfDataProvider {
-//         private ITmfEvent fEvent = new TmfEvent();
-//
-//        @Override
-//        public String getName() {
-//            return null;
-//        }
-//        @Override
-//        public void dispose() {
-//        }
-//        @Override
-//        public void broadcast(TmfSignal signal) {
-//        }
-//        @Override
-//        public void sendRequest(ITmfRequest request) {
-//        }
-//        @Override
-//        public void fireRequest() {
-//        }
-//        @Override
-//        public void notifyPendingRequest(boolean isIncrement) {
-//        }
-//        @Override
-//        public ITmfEvent getNext(ITmfContext context) {
-//            context.increaseRank();
-//            return context.getRank() >= 0 ? fEvent : null;
-//        }
-//        @Override
-//        public ITmfContext armRequest(ITmfRequest request) {
-//            return new MyContext(request.getNbRequested());
-//        }
-//     }
-//
-//     // Dummy request
-//    private static class MyRequest extends TmfRequest {
-//        public MyRequest(TmfRequestPriority priority, int requested) {
-//            super(TmfTimeRange.ETERNITY, 0, requested, priority);
-//        }
-//        @Override
-//        public synchronized void done() {
-//            synchronized (monitor) {
-//                monitor.notifyAll();
-//            }
-//        }
-//    }
-//
-//    // Dummy thread
-//    private static class MyThread extends TmfEventThread {
-//        public MyThread(TmfDataProvider provider, ITmfRequest request) {
-//            super(provider, request);
-//        }
-//    }
-//
-//    private final static Object monitor = new Object();
-//
-//    /**
-//      * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#execute(java.lang.Runnable)}.
-//      */
-//     public void testExecute() {
-//        MyProvider provider = new MyProvider();
-//        MyRequest  request1 = new MyRequest(ITmfRequest.TmfRequestPriority.NORMAL, Integer.MAX_VALUE /  50);
-//        MyThread   thread1  = new MyThread(provider, request1);
-//        MyRequest  request2 = new MyRequest(ITmfRequest.TmfRequestPriority.HIGH, Integer.MAX_VALUE / 100);
-//        MyThread   thread2  = new MyThread(provider, request2);
-//        MyRequest  request3 = new MyRequest(ITmfRequest.TmfRequestPriority.HIGH, Integer.MAX_VALUE / 100);
-//        MyThread   thread3  = new MyThread(provider, request3);
-//
-//        // Start thread1
-//        fExecutor.execute(thread1);
-//        try {
-//            Thread.sleep(1000);
-//        } catch (InterruptedException e) {
-//        }
-//       assertTrue("isRunning", thread1.isRunning());
-//
-//        // Start higher priority thread2
-//        fExecutor.execute(thread2);
-//        try {
-//            Thread.sleep(1000);
-//        } catch (InterruptedException e) {
-//        }
-//        assertFalse("isRunning", thread1.isRunning());
-//        assertTrue("isRunning", thread2.isRunning());
-//
-//        // Wait for end of thread2
-//        try {
-//            synchronized (monitor) {
-//                monitor.wait();
-//                Thread.sleep(1000);
-//            }
-//        } catch (InterruptedException e) {
-//        }
-//        assertTrue("isCompleted", thread2.isCompleted());
-//        assertTrue("isRunning", thread1.isRunning());
-//
-//        // Start higher priority thread3
-//        fExecutor.execute(thread3);
-//        try {
-//            Thread.sleep(500);
-//        } catch (InterruptedException e) {
-//        }
-//        assertFalse("isRunning", thread1.isRunning());
-//        assertTrue("isRunning", thread3.isRunning());
-//
-//        // Wait for end of thread3
-//        try {
-//            synchronized (monitor) {
-//                monitor.wait();
-//                Thread.sleep(500);
-//            }
-//        } catch (InterruptedException e) {
-//        }
-//        assertTrue("isCompleted", thread3.isCompleted());
-//        assertTrue("isRunning", thread1.isRunning());
-//
-//        // Wait for thread1 completion
-//        try {
-//            synchronized (monitor) {
-//                monitor.wait();
-//            }
-//        } catch (InterruptedException e) {
-//        }
-//        assertTrue("isCompleted", thread1.isCompleted());
-//    }
+       // Dummy context
+       private static class MyContext implements ITmfContext {
+           private long fNbRequested;
+        private long fRank;
+
+        public MyContext(long requested) {
+            fNbRequested = requested;
+            fRank = 0;
+        }
+        @Override
+        public long getRank() {
+            return (fRank <= fNbRequested) ? fRank : -1;
+        }
+        @Override
+        public ITmfLocation getLocation() {
+            return null;
+        }
+        @Override
+        public boolean hasValidRank() {
+            return true;
+        }
+        @Override
+        public void setLocation(ITmfLocation location) {
+        }
+        @Override
+        public void setRank(long rank) {
+            fRank = rank;
+        }
+        @Override
+        public void increaseRank() {
+            fRank++;
+        }
+        @Override
+        public void dispose() {
+        }
+        @Override
+        public MyContext clone() {
+            return this;
+        }
+       }
+
+       // Dummy provider
+       private static class MyProvider extends TmfDataProvider {
+           private ITmfEvent fEvent = new TmfEvent();
+
+        @Override
+        public String getName() {
+            return null;
+        }
+        @Override
+        public void dispose() {
+        }
+        @Override
+        public void broadcast(TmfSignal signal) {
+        }
+        @Override
+        public void sendRequest(ITmfRequest request) {
+        }
+        @Override
+        public void fireRequest() {
+        }
+        @Override
+        public void notifyPendingRequest(boolean isIncrement) {
+        }
+        @Override
+        public ITmfEvent getNext(ITmfContext context) {
+            context.increaseRank();
+            return context.getRank() >= 0 ? fEvent : null;
+        }
+        @Override
+        public ITmfContext armRequest(ITmfRequest request) {
+            return new MyContext(request.getNbRequested());
+        }
+       }
+
+       // Dummy request
+    private static class MyRequest extends TmfRequest {
+        private final Object monitor;
+        public MyRequest(TmfRequestPriority priority, int requested, Object mon) {
+            super(TmfTimeRange.ETERNITY, 0, requested, priority);
+            monitor = mon;
+        }
+        @Override
+        public synchronized void done() {
+            super.done();
+            synchronized (monitor) {
+                monitor.notifyAll();
+            }
+        }
+        @Override
+        public synchronized void handleStarted() {
+            super.handleStarted();
+            synchronized (monitor) {
+                monitor.notifyAll();
+            }
+        }
+    }
+
+    // Dummy thread
+    private static class MyThread extends TmfEventThread {
+        private final Object monitor;
+        public MyThread(TmfDataProvider provider, ITmfRequest request, Object mon) {
+            super(provider, request);
+            monitor = mon;
+        }
+        @Override
+        public synchronized void resume() {
+            super.resume();
+            monitor.notifyAll();
+        }
+    }
+
+    private final static Object reqmon1 = new Object();
+    private final static Object reqmon2 = new Object();
+    private final static Object reqmon3 = new Object();
+
+    private final static Object thrmon1 = new Object();
+    private final static Object thrmon2 = new Object();
+    private final static Object thrmon3 = new Object();
+
+    /**
+        * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#execute(java.lang.Runnable)}.
+        */
+       public void testExecute() {
+           final long TIMEOUT = 100;
+
+        MyProvider provider = new MyProvider();
+        MyRequest  request1 = new MyRequest(ITmfRequest.TmfRequestPriority.NORMAL, Integer.MAX_VALUE /  10, reqmon1);
+        MyThread   thread1  = new MyThread(provider, request1, thrmon1);
+        MyRequest  request2 = new MyRequest(ITmfRequest.TmfRequestPriority.HIGH,   Integer.MAX_VALUE / 100, reqmon2);
+        MyThread   thread2  = new MyThread(provider, request2, thrmon2);
+        MyRequest  request3 = new MyRequest(ITmfRequest.TmfRequestPriority.HIGH,   Integer.MAX_VALUE / 100, reqmon3);
+        MyThread   thread3  = new MyThread(provider, request3, thrmon3);
+
+        // Start thread1
+        fExecutor.execute(thread1);
+        synchronized (reqmon1) {
+            try {
+                reqmon1.wait();
+                assertTrue("isRunning", thread1.isRunning());
+            } catch (InterruptedException e) {
+            }
+        }
+
+        // Start higher priority thread2
+        fExecutor.execute(thread2);
+        synchronized (reqmon2) {
+            try {
+                reqmon2.wait();
+                assertFalse("isRunning", thread1.isRunning());
+                assertTrue("isRunning", thread2.isRunning());
+            } catch (InterruptedException e) {
+            }
+        }
+
+        // Wait for end of thread2
+        try {
+            synchronized (reqmon2) {
+                reqmon2.wait();
+                assertTrue("isCompleted", thread2.isCompleted());
+            }
+            synchronized (thrmon1) {
+                thrmon1.wait(TIMEOUT); // Use a timeout in case we already missed the signal
+                assertTrue("isRunning", thread1.isRunning());
+            }
+        } catch (InterruptedException e) {
+        }
+
+        // Start higher priority thread3
+        fExecutor.execute(thread3);
+        synchronized (reqmon3) {
+            try {
+                reqmon3.wait();
+                assertFalse("isRunning", thread1.isRunning());
+                assertTrue("isRunning", thread3.isRunning());
+            } catch (InterruptedException e) {
+            }
+        }
+
+        // Wait for end of thread3
+        try {
+            synchronized (reqmon3) {
+                reqmon3.wait();
+                assertTrue("isCompleted", thread3.isCompleted());
+            }
+            synchronized (thrmon1) {
+                thrmon1.wait(TIMEOUT); // Use a timeout in case we already missed the signal
+                assertTrue("isRunning", thread1.isRunning());
+            }
+        } catch (InterruptedException e) {
+        }
+
+        // Wait for thread1 completion
+        try {
+            synchronized (reqmon1) {
+                reqmon1.wait();
+                assertTrue("isCompleted", thread1.isCompleted());
+            }
+        } catch (InterruptedException e) {
+        }
+    }
 
        // ------------------------------------------------------------------------
        // toString
This page took 0.029247 seconds and 5 git commands to generate.