11package org .sterl .test ;
22
33import static org .assertj .core .api .Assertions .assertThat ;
4- import static org .junit . jupiter .api .Assertions .fail ;
4+ import static org .assertj . core .api .Assertions .fail ;
55
66import java .time .Duration ;
77import java .time .Instant ;
1313
1414import org .assertj .core .api .ListAssert ;
1515
16+ import lombok .Getter ;
1617import lombok .Setter ;
1718
1819public class AsyncAsserts {
1920
2021 private final List <String > values = Collections .synchronizedList (new ArrayList <String >());
2122 private final Map <String , Integer > counts = new ConcurrentHashMap <>();
22- @ Setter
23- private Duration defaultTimeout = Duration .ofSeconds (3 );
2423
24+ @ Getter @ Setter
25+ private int maxStepCount = 100 ;
26+
27+ @ Getter @ Setter
28+ private Duration defaultTimeout = Duration .ofSeconds (3 );
29+
2530 public synchronized void clear () {
2631 values .clear ();
2732 counts .clear ();
2833 }
29-
30- public int add (String value ) {
34+ public synchronized int add (String value ) {
3135 values .add (value );
3236 final int count = getCount (value ) + 1 ;
3337 counts .put (value , count );
34- if (values .size () > 100 ) {
35- throw new IllegalStateException ("Workflow has already more than 100 steps, assuming error!" );
38+ if (values .size () > maxStepCount ) {
39+ throw new IllegalStateException ("Flow has already more than " + maxStepCount + " steps, assuming error!" );
3640 }
3741 return count ;
3842 }
39-
4043 /**
4144 * @return how often this value has been already added ...
4245 */
@@ -53,53 +56,55 @@ public int info(String value) {
5356 System .err .println (size + ". " + value );
5457 return count ;
5558 }
56-
57- public int getCount (String value ) {
58- return counts .getOrDefault (value , 0 );
59- }
6059 public int getCount () {
6160 return counts .size ();
6261 }
63-
62+
63+ public int getCount (String value ) {
64+ return counts .getOrDefault (value , 0 );
65+ }
6466 public void awaitValue (String value ) {
65- final var start = Instant .now ();
67+ awaitValue (null , value );
68+ }
69+ /**
70+ * Wait for the given value, if not found call the given method
71+ * @param fn the optional function to call after each wait
72+ * @param value the value to wait for
73+ */
74+ public void awaitValue (Runnable fn , String value ) {
75+ final var start = System .currentTimeMillis ();
6676 while (!values .contains (value )
67- && (System .currentTimeMillis () - start . toEpochMilli () <= defaultTimeout .toMillis ())) {
77+ && (System .currentTimeMillis () - start <= defaultTimeout .toMillis ())) {
6878 try {
69- Thread .sleep (100 );
79+ Thread .sleep (50 );
80+ if (fn != null ) fn .run ();
7081 } catch (InterruptedException e ) {
71- if (Thread .interrupted ()) {
72- break ;
73- }
82+ if (Thread .interrupted ()) break ;
7483 }
7584 }
7685 assertValue (value );
7786 }
78-
79- public ListAssert < String > assertValue ( String value ) {
80- return assertThat ( new ArrayList <>( values )). contains ( value );
81- }
82-
83- public void awaitValue (String value , String ... values ) {
84- awaitValue (value );
87+ /**
88+ * Wait for the given value, if not found call the given method
89+ * @param fn the optional function to call after each wait
90+ * @param value the value to wait for
91+ */
92+ public void awaitValue (Runnable fn , String value , String ... values ) {
93+ awaitValue (fn , value );
8594 if (values != null && values .length > 0 ) {
8695 for (String v : values ) {
87- awaitValue (v );
96+ awaitValue (fn , v );
8897 }
8998 }
9099 }
91-
92- public void awaitValueOnce (String value ) {
93- awaitValue (value );
94- assertThat (values ).contains (value );
95- var occurrences = values .stream ().filter (e -> value .equals (e )).count ();
96- if (occurrences > 1 ) {
97- fail ("Expected " + value + " to be present once but was present " + occurrences + " times." );
98- }
100+ public void awaitValue (String value , String ... values ) {
101+ awaitValue (null , value , values );
99102 }
100-
101103 public void awaitOrdered (String value , String ... values ) {
102- awaitValue (value , values );
104+ awaitOrdered (null , value , values );
105+ }
106+ public void awaitOrdered (Runnable fn , String value , String ... values ) {
107+ awaitValue (fn , value , values );
103108
104109 assertThat (this .values .indexOf (value )).isEqualTo (0 );
105110 if (values != null && values .length > 0 ) {
@@ -108,7 +113,11 @@ public void awaitOrdered(String value, String... values) {
108113 }
109114 }
110115 }
111-
116+
117+ public ListAssert <String > assertValue (String value ) {
118+ return assertThat (new ArrayList <>(values )).contains (value );
119+ }
120+
112121 public void assertMissing (String value ) {
113122 assertThat (values ).doesNotContain (value );
114123 }
@@ -118,4 +127,13 @@ public void assertMissing(String value, String... inValues) {
118127 assertThat (values ).doesNotContain (s );
119128 }
120129 }
130+
131+ public void awaitValueOnce (String value ) {
132+ awaitValue (null , value );
133+ assertThat (values ).contains (value );
134+ var occurrences = values .stream ().filter (e -> value .equals (e )).count ();
135+ if (occurrences > 1 ) {
136+ fail ("Expected " + value + " to be present once but was present " + occurrences + " times." );
137+ }
138+ }
121139}
0 commit comments