diff --git a/exercises/src/test/java/c1_Introduction.java b/exercises/src/test/java/c1_Introduction.java index 92ac5493..2a8201af 100644 --- a/exercises/src/test/java/c1_Introduction.java +++ b/exercises/src/test/java/c1_Introduction.java @@ -1,14 +1,14 @@ -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.Test; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.time.Duration; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicReference; -import static java.util.Collections.emptyList; import static org.junit.jupiter.api.Assertions.*; /** @@ -41,7 +41,7 @@ public class c1_Introduction extends IntroductionBase { public void hello_world() { Mono serviceResult = hello_world_service(); - String result = null; //todo: change this line only + String result = serviceResult.block(); //todo: change this line only assertEquals("Hello World!", result); } @@ -55,7 +55,7 @@ public void unresponsive_service() { Exception exception = assertThrows(IllegalStateException.class, () -> { Mono serviceResult = unresponsiveService(); - String result = null; //todo: change this line only + String result = serviceResult.block(Duration.ofSeconds(1)); //todo: change this line only }); String expectedMessage = "Timeout on blocking read for 1"; @@ -72,7 +72,7 @@ public void unresponsive_service() { public void empty_service() { Mono serviceResult = emptyService(); - Optional optionalServiceResult = null; //todo: change this line only + Optional optionalServiceResult = serviceResult.blockOptional(); //todo: change this line only assertTrue(optionalServiceResult.isEmpty()); assertTrue(emptyServiceIsCalled.get()); @@ -89,7 +89,7 @@ public void empty_service() { public void multi_result_service() { Flux serviceResult = multiResultService(); - String result = serviceResult.toString(); //todo: change this line only + String result = serviceResult.blockFirst(); //todo: change this line only assertEquals("valid result", result); } @@ -103,7 +103,8 @@ public void multi_result_service() { public void fortune_top_five() { Flux serviceResult = fortuneTop5(); - List results = emptyList(); //todo: change this line only +// List results = serviceResult.take(5).toStream().toList(); //emptyList(); //todo: change this line only + List results = serviceResult.toStream().toList(); //emptyList(); //todo: change this line only assertEquals(Arrays.asList("Walmart", "Amazon", "Apple", "CVS Health", "UnitedHealth Group"), results); assertTrue(fortuneTop5ServiceIsCalled.get()); @@ -126,13 +127,17 @@ public void nothing_happens_until_you_() throws InterruptedException { Flux serviceResult = fortuneTop5(); - serviceResult + var fluxHandle = serviceResult .doOnNext(companyList::add) - //todo: add an operator here, don't use any blocking operator! + .subscribe() //todo: add an operator here, don't use any blocking operator! ; + //wait for the flux our handler 'companyList.add' Thread.sleep(1000); //bonus: can you explain why this line is needed? + fluxHandle.dispose(); + + assertEquals(Arrays.asList("Walmart", "Amazon", "Apple", "CVS Health", "UnitedHealth Group"), companyList); } @@ -153,6 +158,7 @@ public void leaving_blocking_world_behind() throws InterruptedException { fortuneTop5() //todo: change this line only + .subscribe(companyList::add,null, () -> serviceCallCompleted.set(true)) ; Thread.sleep(1000);