-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathDataServiceTest.java
141 lines (116 loc) · 5.65 KB
/
DataServiceTest.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package itx.rxjava.test;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Single;
import itx.rxjava.DataService;
import itx.rxjava.DataServiceImpl;
import itx.rxjava.dto.DataItem;
import itx.rxjava.dto.DataQuery;
import itx.rxjava.dto.SingleDataQuery;
import itx.rxjava.producer.CompletableDataItem;
import itx.rxjava.test.consumer.SynchronousCompletableObserver;
import itx.rxjava.test.consumer.SynchronousDataObserver;
import itx.rxjava.test.consumer.SynchronousDataSubscriber;
import itx.rxjava.test.consumer.SynchronousMaybeObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class DataServiceTest {
private static final Logger LOG = LoggerFactory.getLogger(DataServiceTest.class);
private ExecutorService executor;
@BeforeClass
public void init() {
this.executor = Executors.newFixedThreadPool(4);
LOG.info("test init");
}
@Test
public void testDataServiceWithBackPressureComplete() throws InterruptedException {
DataService dataService = new DataServiceImpl(executor);
Flowable<DataItem> dataFlow = dataService.getDataFlowWithBackPressure(new DataQuery("query1-back-pressure-complete", 10));
LOG.info("query submitted");
SynchronousDataSubscriber dataSubscriber = new SynchronousDataSubscriber();
dataFlow.subscribe(dataSubscriber);
dataSubscriber.request(10);
dataSubscriber.await(10, TimeUnit.SECONDS);
LOG.info("evaluating test results");
Assert.assertTrue(dataSubscriber.getErrors().size() == 0);
Assert.assertTrue(dataSubscriber.getResults().size() == 10);
Assert.assertTrue(dataSubscriber.isCompleted());
Assert.assertNotNull(dataSubscriber.getSubscription());
}
@Test
public void testDataServiceWithBackPressureIncomplete() throws InterruptedException {
DataService dataService = new DataServiceImpl(executor);
Flowable<DataItem> dataFlow = dataService.getDataFlowWithBackPressure(new DataQuery("query2-back-pressure-incomplete", 10));
LOG.info("query submitted");
SynchronousDataSubscriber dataSubscriber = new SynchronousDataSubscriber();
dataFlow.subscribe(dataSubscriber);
dataSubscriber.request(5);
dataSubscriber.await(2, TimeUnit.SECONDS);
LOG.info("evaluating test results");
Assert.assertTrue(dataSubscriber.getErrors().size() == 0);
Assert.assertTrue(dataSubscriber.getResults().size() == 5);
Assert.assertFalse(dataSubscriber.isCompleted());
Assert.assertNotNull(dataSubscriber.getSubscription());
}
@Test
public void testDataService() throws InterruptedException {
DataService dataService = new DataServiceImpl(executor);
Observable<DataItem> dataFlow = dataService.getDataFlow(new DataQuery("query3-complete", 10));
LOG.info("query submitted");
SynchronousDataObserver dataObserver = new SynchronousDataObserver();
dataFlow.subscribe(dataObserver);
dataObserver.await(10, TimeUnit.SECONDS);
LOG.info("evaluating test results");
Assert.assertTrue(dataObserver.getErrors().size() == 0);
Assert.assertTrue(dataObserver.getResults().size() == 10);
Assert.assertTrue(dataObserver.isCompleted());
}
@Test
public void testDataServiceSingle() {
DataService dataService = new DataServiceImpl(executor);
Single<DataItem> single = dataService.getSingle(new SingleDataQuery("single-query"));
DataItem dataItem = single.blockingGet();
Assert.assertNotNull(dataItem);
Assert.assertEquals(dataItem.getRequest(), "single-query");
Assert.assertEquals(dataItem.getResult(), "single-data-result");
Assert.assertTrue(dataItem.getOrdinal() == 1);
}
@Test
public void testCompletable() throws InterruptedException {
DataService dataService = new DataServiceImpl(executor);
CompletableDataItem completable = dataService.getCompletable(new SingleDataQuery("completable-query"));
SynchronousCompletableObserver completableObserver = new SynchronousCompletableObserver();
completable.subscribe(completableObserver);
completableObserver.await(10, TimeUnit.SECONDS);
Assert.assertTrue(completableObserver.isSubscribed());
Assert.assertTrue(completableObserver.isCompleted());
Assert.assertFalse(completableObserver.hasErrors());
}
@Test
public void testMaybe() throws InterruptedException {
DataService dataService = new DataServiceImpl(executor);
Maybe<DataItem> maybe = dataService.getMaybe(new SingleDataQuery("maybe-query"));
SynchronousMaybeObserver maybeObserver = new SynchronousMaybeObserver();
maybe.subscribe(maybeObserver);
maybeObserver.await(10, TimeUnit.SECONDS);
Assert.assertTrue(maybeObserver.isCompleted());
Assert.assertFalse(maybeObserver.hasErrors());
Assert.assertNotNull(maybeObserver.getDataItem());
Assert.assertEquals(maybeObserver.getDataItem().getRequest(), "maybe-query");
Assert.assertEquals(maybeObserver.getDataItem().getResult(), "maybe-data-result");
Assert.assertTrue(maybeObserver.getDataItem().getOrdinal() == 1);
}
@AfterClass
public void shutdown() {
LOG.info("test shutdown");
executor.shutdown();
}
}