Skip to content

Commit

Permalink
Merge pull request #2 from reactor/master
Browse files Browse the repository at this point in the history
Update with base
  • Loading branch information
dcardozo authored Nov 5, 2016
2 parents 09ee3da + 2f4d9fc commit 25f21b1
Show file tree
Hide file tree
Showing 16 changed files with 517 additions and 1,525 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Lite Rx API Hands-on

This Hands-on (slides available [here](https://speakerdeck.com/sdeleuze/a-lite-rx-api-for-the-jvm)) is designed to help you to learn easily the lite Rx API provided by [Reactor Core 3.x](https://github.com/reactor/reactor-core/).
This Hands-on is designed to help you to learn easily the lite Rx API provided by [Reactor Core 3.x](https://github.com/reactor/reactor-core/).

You will mostly need these 2 classes Javadoc:
You will mostly need these 3 classes Javadoc:

- [Flux](http://projectreactor.io/core/docs/api/reactor/core/publisher/Flux.html)
- [Mono](http://projectreactor.io/core/docs/api/reactor/core/publisher/Mono.html)
- [StepVerifier](http://next.projectreactor.io/ext/docs/api/reactor/test/StepVerifier.html)

To do this Hands-on, you just have to:

- Have [Java 8](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) and a Java IDE ([IntelliJ IDEA](https://www.jetbrains.com/idea/) for example) installed with Maven support
- Clone this repository (or your fork)
- Import the project as a Maven one in your IDE
- Make sure that the language level is set to Java 8 in your IDE project settings
- Fix the TODO one by one in Part01 to Part09 test classes to make unit tests green
- Fix the TODO one by one in Part01 to Part11 test classes to make unit tests green

The solution is available in the `complete` branch to compare, when you have finished, with what you have done.

Expand Down
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<reactor.core.version>3.0.3.RELEASE</reactor.core.version>
</properties>

<dependencies>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.0.2.RELEASE</version>
<version>${reactor.core.version}</version>
</dependency>
<dependency>
<groupId>io.projectreactor.addons</groupId>
<artifactId>reactor-test</artifactId>
<version>${reactor.core.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
Expand All @@ -34,9 +40,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>1.1.9</version>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -54,5 +60,5 @@
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package io.pivotal.literx;

import java.time.Duration;
import java.util.Arrays;

import org.junit.Test;
import reactor.core.publisher.Flux;
import io.pivotal.literx.test.TestSubscriber;
import reactor.test.StepVerifier;

/**
* Learn how to create Flux instances.
*
* @author Sebastien Deleuze
* @see <a href="http://projectreactor.io/core/docs/api/reactor/core/publisher/Flux.html">Flux Javadoc</a>
*
*/
public class Part01CreateFlux {
public class Part01Flux {

//========================================================================================

@Test
public void empty() {
Flux<String> flux = emptyFlux();
TestSubscriber
.subscribe(flux)
.assertValueCount(0)
.assertComplete();

StepVerifier.create(flux)
.expectComplete()
.verify();
}

// TODO Return an empty Flux
Expand All @@ -34,10 +36,10 @@ Flux<String> emptyFlux() {
@Test
public void fromValues() {
Flux<String> flux = fooBarFluxFromValues();
TestSubscriber
.subscribe(flux)
.assertValues("foo", "bar")
.assertComplete();
StepVerifier.create(flux)
.expectNext("foo", "bar")
.expectComplete()
.verify();
}

// TODO Return a Flux that contains 2 values "foo" and "bar" without using an array or a collection
Expand All @@ -50,10 +52,10 @@ Flux<String> fooBarFluxFromValues() {
@Test
public void fromList() {
Flux<String> flux = fooBarFluxFromList();
TestSubscriber
.subscribe(flux)
.assertValues("foo", "bar")
.assertComplete();
StepVerifier.create(flux)
.expectNext("foo", "bar")
.expectComplete()
.verify();
}

// TODO Create a Flux from a List that contains 2 values "foo" and "bar"
Expand All @@ -66,12 +68,10 @@ Flux<String> fooBarFluxFromList() {
@Test
public void error() {
Flux<String> flux = errorFlux();
TestSubscriber
.subscribe(flux)
.assertError(IllegalStateException.class)
.assertNotComplete();
StepVerifier.create(flux)
.expectError(IllegalStateException.class)
.verify();
}

// TODO Create a Flux that emits an IllegalStateException
Flux<String> errorFlux() {
return null;
Expand All @@ -80,30 +80,15 @@ Flux<String> errorFlux() {
//========================================================================================

@Test
public void neverTerminates() {
Flux<String> flux = neverTerminatedFlux();
TestSubscriber
.subscribe(flux)
.assertNotTerminated();
}

// TODO Create a Flux that never terminates
Flux<String> neverTerminatedFlux() {
return null;
}

//========================================================================================

@Test
public void countEachSecond() {
public void countEach100ms() {
Flux<Long> flux = counter();
TestSubscriber
.subscribe(flux)
.assertNotTerminated()
.awaitAndAssertNextValues(0L, 1L, 2L);
StepVerifier.create(flux)
.expectNext(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L)
.expectComplete()
.verify();
}

// TODO Create a Flux that emits an increasing value each 100ms
// TODO Create a Flux that emits increasing values from 0 to 9 each 100ms
Flux<Long> counter() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,61 @@
package io.pivotal.literx;

import java.time.Duration;

import org.junit.Test;
import reactor.core.publisher.Mono;
import io.pivotal.literx.test.TestSubscriber;
import reactor.test.StepVerifier;

/**
* Learn how to create Mono instances.
*
* @author Sebastien Deleuze
* @see <a href="http://projectreactor.io/core/docs/api/reactor/core/publisher/Mono.html">Mono Javadoc</a>
*
*/
public class Part02CreateMono {
public class Part02Mono {

//========================================================================================

@Test
public void empty() {
Mono<String> mono = emptyMono();
TestSubscriber
.subscribe(mono)
.assertValueCount(0)
.assertComplete();
StepVerifier.create(mono)
.expectComplete()
.verify();
}

// TODO Return an empty Mono
Mono<String> emptyMono() {
return null;
}

//========================================================================================

@Test
public void noSignal() {
Mono<String> mono = monoWithNoSignal();
StepVerifier
.create(mono)
.expectSubscription()
.expectNoEvent(Duration.ofSeconds(1))
.thenCancel()
.verify();
}

// TODO Return an Mono that never emit any signal
Mono<String> monoWithNoSignal() {
return null;
}

//========================================================================================

@Test
public void fromValue() {
Mono<String> mono = fooMono();
TestSubscriber
.subscribe(mono)
.assertValues("foo")
.assertComplete();
StepVerifier.create(mono)
.expectNext("foo")
.expectComplete()
.verify();
}

// TODO Return a Mono that contains a "foo" value
Expand All @@ -50,10 +68,9 @@ Mono<String> fooMono() {
@Test
public void error() {
Mono<String> mono = errorMono();
TestSubscriber
.subscribe(mono)
.assertError(IllegalStateException.class)
.assertNotComplete();
StepVerifier.create(mono)
.expectError(IllegalStateException.class)
.verify();
}

// TODO Create a Mono that emits an IllegalStateException
Expand Down
97 changes: 97 additions & 0 deletions src/test/java/io/pivotal/literx/Part03StepVerifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.pivotal.literx;

import java.time.Duration;
import java.util.function.Supplier;

import io.pivotal.literx.domain.User;
import static org.junit.Assert.fail;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

/**
* Learn how to use StepVerifier to test Mono, Flux or any other kind of Reactive Streams Publisher.
*
* @author Sebastien Deleuze
* @see <a href="http://next.projectreactor.io/ext/docs/api/reactor/test/StepVerifier.html">StepVerifier Javadoc</a>
*/
public class Part03StepVerifier {

//========================================================================================

@Test
public void expectElementsThenComplete() {
expectFooBarComplete(Flux.just("foo", "bar"));
}

// TODO Use StepVerifier to check that the flux parameter emits "foo" and "bar" elements then completes successfully.
void expectFooBarComplete(Flux<String> flux) {
fail();
}

//========================================================================================

@Test
public void expect2ElementsThenError() {
expectFooBarError(Flux.just("foo", "bar").concatWith(Mono.error(new RuntimeException())));
}

// TODO Use StepVerifier to check that the flux parameter emits "foo" and "bar" elements then a RuntimeException error.
void expectFooBarError(Flux<String> flux) {
fail();
}

//========================================================================================

@Test
public void expectElementsWithThenComplete() {
expectSkylerJesseComplete(Flux.just(new User("swhite", null, null), new User("jpinkman", null, null)));
}

// TODO Use StepVerifier to check that the flux parameter emits a User with "swhite" username and another one with "jpinkman" then completes successfully.
void expectSkylerJesseComplete(Flux<User> flux) {
fail();
}

//========================================================================================

@Test
public void count() {
expect10Elements(Flux.interval(Duration.ofSeconds(1)).take(10));
}

// TODO Expect 10 elements then complete and notice how long it takes for running the test
void expect10Elements(Flux<Long> flux) {
fail();
}

//========================================================================================

@Test
public void countWithVirtualTime() {
expect3600Elements(() -> Flux.interval(Duration.ofSeconds(1)).take(3600));
}

// TODO Expect 3600 elements then complete using the virtual time capabilities provided via StepVerifier.with() and notice how long it takes for running the test
void expect3600Elements(Supplier<Flux<Long>> supplier) {
fail();
}

}
Loading

0 comments on commit 25f21b1

Please sign in to comment.