1 /******************************************************************************* 2 * Copyright (c) 2010, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 15 package org.eclipse.ua.tests.help.util; 16 17 import org.junit.Assert; 18 19 20 public class ParallelTestSupport { 21 22 public interface ITestCase { 23 /** 24 * Runs a test case one time 25 * @return null if the test passes, otherwise a string representing 26 * the reason for the failure 27 * @throws Exception 28 */ runTest()29 public String runTest() throws Exception; 30 } 31 testSingleCase(ITestCase testCase, int repetitions)32 public static void testSingleCase(ITestCase testCase, int repetitions) { 33 String result = null; 34 for (int i = 0; i < repetitions && result == null; i++) { 35 try { 36 result = testCase.runTest(); 37 } catch (Exception e) { 38 result = e.getMessage(); 39 } 40 if (result != null) { 41 Assert.fail(result); 42 } 43 } 44 } 45 testInParallel(ITestCase[] testCases, int repetitions)46 public static void testInParallel(ITestCase[] testCases, int repetitions) { 47 int numberOfThreads = testCases.length; 48 TestThread[] testThreads = new TestThread[numberOfThreads]; 49 for (int i = 0; i < numberOfThreads; i++) { 50 testThreads[i] = new TestThread(i, testCases[i], repetitions); 51 testThreads[i].start(); 52 } 53 // Now wait for the threads to complete 54 boolean complete = false; 55 do { 56 complete = true; 57 for (int i = 0; i < numberOfThreads && complete; i++) { 58 if (testThreads[i].isAlive()) { 59 complete = false; 60 try { 61 Thread.sleep(100); 62 } catch (InterruptedException e) { 63 Assert.fail("Interrupted Exception"); 64 } 65 } 66 } 67 } while (!complete); 68 for (int i = 0; i < numberOfThreads; i++) { 69 if (testThreads[i].failureReason != null) { 70 Assert.fail(testThreads[i].failureReason); 71 } 72 } 73 } 74 75 private static class TestThread extends Thread { 76 77 private int index; 78 private ITestCase testCase; 79 private int repetitions; 80 TestThread(int index, ITestCase testCase, int repetitions)81 public TestThread(int index, ITestCase testCase, int repetitions) { 82 this.index = index; 83 this.testCase = testCase; 84 this.repetitions = repetitions; 85 } 86 87 public String failureReason = null; 88 89 @Override run()90 public void run() { 91 for (int j = 0; j <= repetitions; j++) { 92 try { 93 String unexpected = testCase.runTest(); 94 if (unexpected != null) { 95 failureReason = "Thread " + index + ", iteration " + j + ": " + unexpected; 96 return; 97 } 98 } catch (Exception e) { 99 failureReason = e.getMessage(); 100 return; 101 } 102 } 103 } 104 } 105 106 } 107