/
DemoApplicationTests.java
47 lines (37 loc) · 1.46 KB
/
DemoApplicationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.example;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoApplicationTests {
@LocalServerPort
Integer port;
@Test
public void test() throws Exception {
Flux<Map> result = WebClient.create()
.get()
.uri("http://localhost:" + port + "/")
.accept(MediaType.TEXT_EVENT_STREAM)
.exchange()
.flatMap(response -> response.bodyToFlux(Map.class));
StepVerifier.create(result)
.expectNext(Collections.singletonMap("i", 1))
.then(() -> Assert.assertTrue("should subscribe", DemoApplication.subscribed.get()))
.thenCancel()
.verify();
// Give it some time...
TimeUnit.SECONDS.sleep(1);
Assert.assertFalse("should unsubscribe", DemoApplication.subscribed.get());
}
}