Skip to content

Commit a7a167a

Browse files
committed
Added an overloaded method of untilAsserted(..) that takes a supplier and a consumer
1 parent ef8f663 commit a7a167a

10 files changed

Lines changed: 337 additions & 17 deletions

File tree

awaitility-kotlin/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
</dependency>
6060
</dependencies>
6161
<build>
62-
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
6362
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
6463
<plugins>
6564
<plugin>

awaitility-kotlin/src/main/kotlin/org/awaitility/kotlin/AwaitilityKt.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.hamcrest.Matchers.nullValue
1616
import java.time.Duration
1717
import java.util.concurrent.ExecutorService
1818
import java.util.concurrent.atomic.AtomicBoolean
19+
import java.util.function.Predicate
1920
import kotlin.reflect.KClass
2021
import kotlin.time.toJavaDuration
2122

@@ -51,7 +52,7 @@ data class AwaitilityKtUntilFunCondition<out T> internal constructor(internal va
5152
* @param pred The predicate that determines whether or not the condition is fulfilled.
5253
*/
5354
@Suppress("HasPlatformType")
54-
infix fun <T> AwaitilityKtUntilFunCondition<T?>.matches(pred: (T?) -> Boolean) = factory.until(fn, pred)
55+
infix fun <T> AwaitilityKtUntilFunCondition<T?>.matches(pred: (T?) -> Boolean): T? = factory.until(fn, Predicate { pred.invoke(it) })
5556

5657

5758
/**
@@ -74,13 +75,13 @@ infix fun <T> AwaitilityKtUntilFunCondition<T?>.matches(pred: (T?) -> Boolean) =
7475
* @param pred The predicate that determines whether or not the condition is fulfilled.
7576
* @since 3.1.5
7677
*/
77-
infix fun <T> AwaitilityKtUntilFunCondition<T?>.has(pred: T.() -> Boolean) = factory.until(fn) { t: T? ->
78+
infix fun <T> AwaitilityKtUntilFunCondition<T?>.has(pred: T.() -> Boolean): T = factory.until(fn, Predicate { t: T? ->
7879
if (t == null) {
7980
false
8081
} else {
8182
pred(t)
8283
}
83-
}!!
84+
})!!
8485

8586
/**
8687
* An extension function to `ConditionFactory` that allows you do write conditions such as:

awaitility-kotlin/src/test/kotlin/org/awaitility/kotlin/KotlinTest.kt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.junit.Before
3232
import org.junit.Rule
3333
import org.junit.Test
3434
import org.junit.rules.ExpectedException
35+
import java.lang.Thread.sleep
3536
import java.time.Duration
3637
import java.util.concurrent.TimeUnit.MILLISECONDS
3738
import java.util.concurrent.TimeUnit.SECONDS
@@ -65,7 +66,7 @@ class KotlinTest {
6566
@Test
6667
fun forever() {
6768
Asynch(fakeRepository).perform()
68-
await.forever until { fakeRepository.value == 1 }
69+
await.forever until { fakeRepository.value == 1 }
6970
}
7071

7172
@Test
@@ -126,6 +127,28 @@ class KotlinTest {
126127
}
127128
}
128129

130+
@Test
131+
fun untilAssertedWithConsumerMatcher() {
132+
Asynch(fakeRepository).perform()
133+
134+
await.untilAsserted(fakeRepository::getValue) {
135+
assertThat(it).isEqualTo(1)
136+
}
137+
}
138+
139+
@Test
140+
fun untilAtomicAssertedWhenReferenceIsNullByDefault() {
141+
val atomicReference = AtomicReference<String>()
142+
Thread {
143+
sleep(500)
144+
atomicReference.set("world")
145+
}.start()
146+
147+
await.untilAtomic(atomicReference) { string ->
148+
assertThat(string).isEqualTo("world")
149+
}
150+
}
151+
129152
@Test
130153
fun untilAssertedWithConditionEvaluationListener() {
131154
var value = false
@@ -182,7 +205,7 @@ class KotlinTest {
182205
val fakeObjectRepository = FakeGenericRepository(Data("Before"))
183206
AsynchObject(fakeObjectRepository, Data("After")).perform()
184207

185-
val data : Data = await untilCallTo { fakeObjectRepository.data } has {
208+
val data: Data = await untilCallTo { fakeObjectRepository.data } has {
186209
state == "After"
187210
}
188211
assertThat(data.state).isEqualTo("After")

awaitility/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
OF ANY KIND, either express or implied. ~ See the License for the specific
88
language governing permissions and ~ limitations under the License. -->
99

10-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
10+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
1112
<modelVersion>4.0.0</modelVersion>
1213
<parent>
1314
<groupId>org.awaitility</groupId>
@@ -82,5 +83,10 @@
8283
<artifactId>awaitility-test-support</artifactId>
8384
<scope>test</scope>
8485
</dependency>
86+
<dependency>
87+
<groupId>org.assertj</groupId>
88+
<artifactId>assertj-core</artifactId>
89+
<scope>test</scope>
90+
</dependency>
8591
</dependencies>
8692
</project>

awaitility/src/main/java/org/awaitility/core/ConditionFactory.java

Lines changed: 166 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,35 @@ public void untilAsserted(final ThrowingRunnable assertion) {
790790
until(new AssertionCondition(assertion, generateConditionSettings()));
791791
}
792792

793+
/**
794+
* Await until a callable returns a value that when passed to a {@link java.util.function.Consumer} ends without throwing an exception. E.g. with Java 8:
795+
* <p>&nbsp;</p>
796+
* <pre>
797+
* public class MyBean {
798+
*
799+
* public String myFunction() {
800+
* // Imagine stuff being executed in asynchrinously here and the result of this
801+
* // operation is a string called "my value"
802+
* return "my value"
803+
* }
804+
* }
805+
*
806+
* // Then in your test you can wait for the "myFunction" to return "my value"
807+
* await().untilAsserted(myBean::myFunction, value -&gt; Assertions.assertThat(value).isEqualTo("my value"));
808+
* </pre>
809+
*
810+
* @param supplier the supplier that is responsible for executing the assertion and throwing AssertionError on failure.
811+
* @param assertConsumer The consumer that will assert that the supplied value is correct, or throw an exception.
812+
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
813+
* @since 4.3.2
814+
*/
815+
public <T> void untilAsserted(final Callable<T> supplier, final Consumer<? super T> assertConsumer) {
816+
untilAsserted(() -> {
817+
T value = supplier.call();
818+
assertConsumer.accept(value);
819+
});
820+
}
821+
793822
/**
794823
* Await until a Atomic variable has a value matching the specified
795824
* {@link org.hamcrest.Matcher}. E.g.
@@ -805,7 +834,23 @@ public void untilAsserted(final ThrowingRunnable assertion) {
805834
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
806835
*/
807836
public Integer untilAtomic(final AtomicInteger atomic, final Matcher<? super Integer> matcher) {
808-
return until(new CallableHamcrestCondition<>(() -> (Integer) atomic.get(), matcher, generateConditionSettings()));
837+
return until(new CallableHamcrestCondition<>(atomic::get, matcher, generateConditionSettings()));
838+
}
839+
840+
/**
841+
* Await until a Atomic integer is asserted by the {@code matcher} consumer
842+
* E.g.
843+
* <p>&nbsp;</p>
844+
* <pre>
845+
* await().untilAtomic(myAtomic, value -&gt; Assertions.assertThat(value).isEqualTo(123));
846+
* </pre>
847+
*
848+
* @param atomic the atomic variable
849+
* @param matcher the matcher The consumer that validates that the atomic reference value is correct, or throws an exception
850+
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
851+
*/
852+
public void untilAtomic(final AtomicInteger atomic, final Consumer<? super Integer> matcher) {
853+
untilAsserted(atomic::get, matcher);
809854
}
810855

811856
/**
@@ -823,7 +868,23 @@ public Integer untilAtomic(final AtomicInteger atomic, final Matcher<? super Int
823868
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
824869
*/
825870
public Long untilAtomic(final AtomicLong atomic, final Matcher<? super Long> matcher) {
826-
return until(new CallableHamcrestCondition<>(() -> (Long) atomic.get(), matcher, generateConditionSettings()));
871+
return until(new CallableHamcrestCondition<>(atomic::get, matcher, generateConditionSettings()));
872+
}
873+
874+
/**
875+
* Await until a Atomic long is asserted by the {@code matcher} consumer
876+
* E.g.
877+
* <p>&nbsp;</p>
878+
* <pre>
879+
* await().untilAtomic(myAtomic, value -&gt; Assertions.assertThat(value).isEqualTo(123L));
880+
* </pre>
881+
*
882+
* @param atomic the atomic variable
883+
* @param matcher the matcher The consumer that validates that the atomic reference value is correct, or throws an exception
884+
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
885+
*/
886+
public void untilAtomic(final AtomicLong atomic, final Consumer<? super Long> matcher) {
887+
untilAsserted(atomic::get, matcher);
827888
}
828889

829890
/**
@@ -840,7 +901,23 @@ public Long untilAtomic(final AtomicLong atomic, final Matcher<? super Long> mat
840901
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
841902
*/
842903
public void untilAtomic(final AtomicBoolean atomic, final Matcher<? super Boolean> matcher) {
843-
until(new CallableHamcrestCondition<>(() -> (Boolean) atomic.get(), matcher, generateConditionSettings()));
904+
until(new CallableHamcrestCondition<>(atomic::get, matcher, generateConditionSettings()));
905+
}
906+
907+
/**
908+
* Await until a AtomicBoolean is asserted by the {@code matcher} consumer
909+
* E.g.
910+
* <p>&nbsp;</p>
911+
* <pre>
912+
* await().untilAtomic(myAtomicBoolean, value -&gt; Assertions.assertThat(value).isTrue());
913+
* </pre>
914+
*
915+
* @param atomic the atomic variable
916+
* @param matcher the matcher The consumer that validates that the atomic boolean value is correct, or throws an exception
917+
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
918+
*/
919+
public void untilAtomic(final AtomicBoolean atomic, final Consumer<? super Boolean> matcher) {
920+
untilAsserted(atomic::get, matcher);
844921
}
845922

846923
/**
@@ -875,7 +952,23 @@ public void untilFalse(final AtomicBoolean atomic) {
875952
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
876953
*/
877954
public void untilAdder(final LongAdder adder, final Matcher<? super Long> matcher) {
878-
until(new CallableHamcrestCondition<>(() -> (Long) adder.longValue(), matcher, generateConditionSettings()));
955+
until(new CallableHamcrestCondition<>(adder::longValue, matcher, generateConditionSettings()));
956+
}
957+
958+
/**
959+
* Await until a LongAdder is asserted by the {@code matcher} consumer
960+
* E.g.
961+
* <p>&nbsp;</p>
962+
* <pre>
963+
* await().untilAdder(myAdder, value -&gt; Assertions.assertThat(value).isEqualTo(123L));
964+
* </pre>
965+
*
966+
* @param adder the atomic variable
967+
* @param matcher the matcher The consumer that validates that the atomic reference value is correct, or throws an exception
968+
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
969+
*/
970+
public void untilAdder(final LongAdder adder, final Consumer<? super Long> matcher) {
971+
untilAsserted(adder::longValue, matcher);
879972
}
880973

881974
/**
@@ -890,7 +983,23 @@ public void untilAdder(final LongAdder adder, final Matcher<? super Long> matche
890983
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
891984
*/
892985
public void untilAdder(final DoubleAdder adder, final Matcher<? super Double> matcher) {
893-
until(new CallableHamcrestCondition<>(() -> (Double) adder.doubleValue(), matcher, generateConditionSettings()));
986+
until(new CallableHamcrestCondition<>(adder::doubleValue, matcher, generateConditionSettings()));
987+
}
988+
989+
/**
990+
* Await until a DoubleAdder is asserted by the {@code matcher} consumer
991+
* E.g.
992+
* <p>&nbsp;</p>
993+
* <pre>
994+
* await().untilAdder(myAdder, value -&gt; Assertions.assertThat(value).isEqualTo(12.3d));
995+
* </pre>
996+
*
997+
* @param adder the atomic variable
998+
* @param matcher the matcher The consumer that validates that the atomic reference value is correct, or throws an exception
999+
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
1000+
*/
1001+
public void untilAdder(final DoubleAdder adder, final Consumer<? super Double> matcher) {
1002+
untilAsserted(adder::doubleValue, matcher);
8941003
}
8951004

8961005
/**
@@ -905,7 +1014,23 @@ public void untilAdder(final DoubleAdder adder, final Matcher<? super Double> ma
9051014
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
9061015
*/
9071016
public void untilAccumulator(final LongAccumulator accumulator, final Matcher<? super Long> matcher) {
908-
until(new CallableHamcrestCondition<>(() -> (Long) accumulator.longValue(), matcher, generateConditionSettings()));
1017+
until(new CallableHamcrestCondition<>(accumulator::longValue, matcher, generateConditionSettings()));
1018+
}
1019+
1020+
/**
1021+
* Await until a LongAccumulator is asserted by the {@code matcher} consumer
1022+
* E.g.
1023+
* <p>&nbsp;</p>
1024+
* <pre>
1025+
* await().untilAccumulator(myAdder, value -&gt; Assertions.assertThat(value).isEqualTo(123L));
1026+
* </pre>
1027+
*
1028+
* @param accumulator the atomic variable
1029+
* @param matcher the matcher The consumer that validates that the atomic reference value is correct, or throws an exception
1030+
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
1031+
*/
1032+
public void untilAccumulator(final LongAccumulator accumulator, final Consumer<? super Long> matcher) {
1033+
untilAsserted(accumulator::longValue, matcher);
9091034
}
9101035

9111036
/**
@@ -920,7 +1045,23 @@ public void untilAccumulator(final LongAccumulator accumulator, final Matcher<?
9201045
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
9211046
*/
9221047
public void untilAccumulator(final DoubleAccumulator accumulator, final Matcher<? super Double> matcher) {
923-
until(new CallableHamcrestCondition<>(() -> (Double) accumulator.doubleValue(), matcher, generateConditionSettings()));
1048+
until(new CallableHamcrestCondition<>(accumulator::doubleValue, matcher, generateConditionSettings()));
1049+
}
1050+
1051+
/**
1052+
* Await until a DoubleAccumulator is asserted by the {@code matcher} consumer
1053+
* E.g.
1054+
* <p>&nbsp;</p>
1055+
* <pre>
1056+
* await().untilAccumulator(myAdder, value -&gt; Assertions.assertThat(value).isEqualTo(12.3d));
1057+
* </pre>
1058+
*
1059+
* @param accumulator the atomic variable
1060+
* @param matcher the matcher The consumer that validates that the atomic reference value is correct, or throws an exception
1061+
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
1062+
*/
1063+
public void untilAccumulator(final DoubleAccumulator accumulator, final Consumer<? super Double> matcher) {
1064+
untilAsserted(accumulator::doubleValue, matcher);
9241065
}
9251066

9261067
/**
@@ -942,6 +1083,23 @@ public <V> V untilAtomic(final AtomicReference<V> atomic, final Matcher<? super
9421083
return until(new CallableHamcrestCondition<>(atomic::get, matcher, generateConditionSettings()));
9431084
}
9441085

1086+
/**
1087+
* Await until a Atomic variable is asserted by the {@code matcher} consumer
1088+
* E.g.
1089+
* <p>&nbsp;</p>
1090+
* <pre>
1091+
* await().untilAtomic(myAtomic, value -&gt; Assertions.assertThat(value).isEqualTo("something"));
1092+
* </pre>
1093+
*
1094+
* @param atomic the atomic variable
1095+
* @param matcher the matcher The consumer that validates that the atomic reference value is correct, or throws an exception
1096+
* @param <V> a V object.
1097+
* @throws org.awaitility.core.ConditionTimeoutException If condition was not fulfilled within the given time period.
1098+
*/
1099+
public <V> void untilAtomic(final AtomicReference<V> atomic, final Consumer<? super V> matcher) {
1100+
untilAsserted(atomic::get, matcher);
1101+
}
1102+
9451103
/**
9461104
* Await until a {@link java.util.concurrent.Callable} returns <code>true</code>. This is method
9471105
* is not as generic as the other variants of "until" but it allows for a
@@ -1026,5 +1184,4 @@ private Duration definePollDelay(Duration pollDelay, PollInterval pollInterval)
10261184
}
10271185
return pollDelayToUse;
10281186
}
1029-
1030-
}
1187+
}

0 commit comments

Comments
 (0)