MultiThreadMatchers.java

  1. /**
  2.  * Powerunit - A JDK1.8 test framework
  3.  * Copyright (C) 2014 Mathieu Boretti.
  4.  *
  5.  * This file is part of Powerunit
  6.  *
  7.  * Powerunit is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * Powerunit is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
  19.  */
  20. package ch.powerunit;

  21. import java.util.concurrent.Future;
  22. import java.util.concurrent.TimeUnit;

  23. import org.hamcrest.Matcher;

  24. import ch.powerunit.matchers.future.FutureMatchers;

  25. /**
  26.  * @author borettim
  27.  * @since 0.4.0
  28.  */
  29. interface MultiThreadMatchers {
  30.     /**
  31.      * Verify if a {@link Future} is cancelled.
  32.      *
  33.      * @param expected
  34.      *            the expected value.
  35.      * @return the Matcher on {@link Future}
  36.      * @param <T>
  37.      *            The type of the return value of the {@link Future}.
  38.      * @since 0.4.0
  39.      *
  40.      */
  41.     default <T> Matcher<Future<T>> futureIsCancelled(boolean expected) {
  42.         return FutureMatchers.futureIsCancelled(expected);
  43.     }

  44.     /**
  45.      * Verify if a {@link Future} is done.
  46.      *
  47.      * @param expected
  48.      *            the expected value.
  49.      * @return the Matcher on {@link Future}
  50.      * @param <T>
  51.      *            The type of the return value of the {@link Future}.
  52.      * @since 0.4.0
  53.      */
  54.     default <T> Matcher<Future<T>> futureIsDone(boolean expected) {
  55.         return FutureMatchers.futureIsDone(expected);
  56.     }

  57.     /**
  58.      * Retrieve the value of a {@link Future}, with timeout support and validate
  59.      * the value.
  60.      * <p>
  61.      * For instance :
  62.      *
  63.      * <pre>
  64.      * assertThat(Executors.newSingleThreadExecutor().submit(DemoFutureTest::run1))
  65.      *      .is(futureIsBeforeTimeout(notNullValue(), 10, TimeUnit.SECONDS));
  66.      * </pre>
  67.      *
  68.      * @param matching
  69.      *            the matching on the value
  70.      * @param timeout
  71.      *            the timeout to get the value.
  72.      * @param unit
  73.      *            the timeunit for the value.
  74.      * @return the Matcher on {@link Future}
  75.      * @param <T>
  76.      *            The type of the return value of the {@link Future}.
  77.      * @since 0.4.0
  78.      */
  79.     default <T> Matcher<Future<T>> futureIsBeforeTimeout(
  80.             Matcher<? super T> matching, long timeout, TimeUnit unit) {
  81.         return FutureMatchers.futureIsBeforeTimeout(matching, timeout, unit);
  82.     }
  83. }