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 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.tb3;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.concurrent.Callable;
  20. import java.util.concurrent.ExecutorService;
  21. import java.util.concurrent.Future;
  22. import org.junit.runners.model.RunnerScheduler;
  23. /**
  24. * JUnit scheduler capable of running multiple tets in parallel. Each test is
  25. * run in its own thread. Uses an {@link ExecutorService} to manage the threads.
  26. *
  27. * @author Vaadin Ltd
  28. */
  29. public class ParallelScheduler implements RunnerScheduler {
  30. private final List<Future<Object>> fResults = new ArrayList<Future<Object>>();
  31. private ExecutorService fService;
  32. /**
  33. * Creates a parallel scheduler which will use the given executor service
  34. * when submitting test jobs.
  35. *
  36. * @param service
  37. * The service to use for tests
  38. */
  39. public ParallelScheduler(ExecutorService service) {
  40. fService = service;
  41. }
  42. @Override
  43. public void schedule(final Runnable childStatement) {
  44. fResults.add(fService.submit(new Callable<Object>() {
  45. @Override
  46. public Object call() throws Exception {
  47. childStatement.run();
  48. return null;
  49. }
  50. }));
  51. }
  52. @Override
  53. public void finished() {
  54. for (Future<Object> each : fResults) {
  55. try {
  56. each.get();
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. }
  62. }