You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ParallelScheduler.java 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package com.vaadin.tests.tb3;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.concurrent.Callable;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Future;
  7. import org.junit.runners.model.RunnerScheduler;
  8. /**
  9. * JUnit scheduler capable of running multiple tets in parallel. Each test is
  10. * run in its own thread. Uses an {@link ExecutorService} to manage the threads.
  11. *
  12. * @author Vaadin Ltd
  13. */
  14. public class ParallelScheduler implements RunnerScheduler {
  15. private final List<Future<Object>> fResults = new ArrayList<Future<Object>>();
  16. private ExecutorService fService;
  17. /**
  18. * Creates a parallel scheduler which will use the given executor service
  19. * when submitting test jobs.
  20. *
  21. * @param service
  22. * The service to use for tests
  23. */
  24. public ParallelScheduler(ExecutorService service) {
  25. fService = service;
  26. }
  27. @Override
  28. public void schedule(final Runnable childStatement) {
  29. fResults.add(fService.submit(new Callable<Object>() {
  30. @Override
  31. public Object call() throws Exception {
  32. childStatement.run();
  33. return null;
  34. }
  35. }));
  36. }
  37. @Override
  38. public void finished() {
  39. for (Future<Object> each : fResults) {
  40. try {
  41. each.get();
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. }