forked from schananas/practical-reactor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c6_CombiningPublishers.java
377 lines (326 loc) · 12.6 KB
/
c6_CombiningPublishers.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import org.junit.jupiter.api.*;
import reactor.blockhound.BlockHound;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Hooks;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.Objects;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
/**
* In this important chapter we are going to cover different ways of combining publishers.
*
* Read first:
*
* https://projectreactor.io/docs/core/release/reference/#which.values
*
* Useful documentation:
*
* https://projectreactor.io/docs/core/release/reference/#which-operator
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html
*
* @author Stefan Dragisic
*/
public class c6_CombiningPublishers extends CombiningPublishersBase {
/**
* Goal of this exercise is to retrieve e-mail of currently logged-in user.
* `getCurrentUser()` method retrieves currently logged-in user
* and `getUserEmail()` will return e-mail for given user.
*
* No blocking operators, no subscribe operator!
* You may only use `flatMap()` operator.
*/
@Test
public void behold_flatmap() {
Hooks.enableContextLossTracking(); //used for testing - detects if you are cheating!
//todo: feel free to change code as you need
Mono<String> currentUserEmail = null;
Mono<String> currentUserMono = getCurrentUser();
getUserEmail(null);
//don't change below this line
StepVerifier.create(currentUserEmail)
.expectNext("[email protected]")
.verifyComplete();
}
/**
* `taskExecutor()` returns tasks that should execute important work.
* Get all the tasks and execute them.
*
* Answer:
* - Is there a difference between Mono.flatMap() and Flux.flatMap()?
*/
@Test
public void task_executor() {
//todo: feel free to change code as you need
Flux<Void> tasks = null;
taskExecutor();
//don't change below this line
StepVerifier.create(tasks)
.verifyComplete();
Assertions.assertEquals(taskCounter.get(), 10);
}
/**
* `streamingService()` opens a connection to the data provider.
* Once connection is established you will be able to collect messages from stream.
*
* Establish connection and get all messages from data provider stream!
*/
@Test
public void streaming_service() {
//todo: feel free to change code as you need
Flux<Message> messageFlux = null;
streamingService();
//don't change below this line
StepVerifier.create(messageFlux)
.expectNextCount(10)
.verifyComplete();
}
/**
* Join results from services `numberService1()` and `numberService2()` end-to-end.
* First `numberService1` emits elements and then `numberService2`. (no interleaving)
*
* Bonus: There are two ways to do this, check out both!
*/
@Test
public void i_am_rubber_you_are_glue() {
//todo: feel free to change code as you need
Flux<Integer> numbers = null;
numberService1();
numberService2();
//don't change below this line
StepVerifier.create(numbers)
.expectNext(1, 2, 3, 4, 5, 6, 7)
.verifyComplete();
}
/**
* Similar to previous task:
*
* `taskExecutor()` returns tasks that should execute important work.
* Get all the tasks and execute each of them.
*
* Instead of flatMap() use concatMap() operator.
*
* Answer:
* - What is difference between concatMap() and flatMap()?
* - What is difference between concatMap() and flatMapSequential()?
* - Why doesn't Mono have concatMap() operator?
*/
@Test
public void task_executor_again() {
//todo: feel free to change code as you need
Flux<Void> tasks = null;
taskExecutor();
//don't change below this line
StepVerifier.create(tasks)
.verifyComplete();
Assertions.assertEquals(taskCounter.get(), 10);
}
/**
* You are writing software for broker house. You can retrieve current stock prices by calling either
* `getStocksGrpc()` or `getStocksRest()`.
* Since goal is the best response time, invoke both services but use result only from the one that responds first.
*/
@Test
public void need_for_speed() {
//todo: feel free to change code as you need
Flux<String> stonks = null;
getStocksGrpc();
getStocksRest();
//don't change below this line
StepVerifier.create(stonks)
.expectNextCount(5)
.verifyComplete();
}
/**
* As part of your job as software engineer for broker house, you have also introduced quick local cache to retrieve
* stocks from. But cache may not be formed yet or is empty. If cache is empty, switch to a live source:
* `getStocksRest()`.
*/
@Test
public void plan_b() {
//todo: feel free to change code as you need
Flux<String> stonks = null;
getStocksLocalCache();
getStocksRest();
//don't change below this line
StepVerifier.create(stonks)
.expectNextCount(6)
.verifyComplete();
Assertions.assertTrue(localCacheCalled.get());
}
/**
* You are checking mail in your mailboxes. Check first mailbox, and if first message contains spam immediately
* switch to a second mailbox. Otherwise, read all messages from first mailbox.
*/
@Test
public void mail_box_switcher() {
//todo: feel free to change code as you need
Flux<Message> myMail = null;
mailBoxPrimary();
mailBoxSecondary();
//don't change below this line
StepVerifier.create(myMail)
.expectNextMatches(m -> !m.metaData.equals("spam"))
.expectNextMatches(m -> !m.metaData.equals("spam"))
.verifyComplete();
Assertions.assertEquals(1, consumedSpamCounter.get());
}
/**
* You are implementing instant search for software company.
* When user types in a text box results should appear in near real-time with each keystroke.
*
* Call `autoComplete()` function for each user input
* but if newer input arrives, cancel previous `autoComplete()` call and call it for latest input.
*/
@Test
public void instant_search() {
//todo: feel free to change code as you need
autoComplete(null);
Flux<String> suggestions = userSearchInput()
//todo: use one operator only
;
//don't change below this line
StepVerifier.create(suggestions)
.expectNext("reactor project", "reactive project")
.verifyComplete();
}
/**
* Code should work, but it should also be easy to read and understand.
* Orchestrate file writing operations in a self-explanatory way using operators like `when`,`and`,`then`...
* If all operations have been executed successfully return boolean value `true`.
*/
@Test
public void prettify() {
//todo: feel free to change code as you need
//todo: use when,and,then...
Mono<Boolean> successful = null;
openFile();
writeToFile("0x3522285912341");
closeFile();
//don't change below this line
StepVerifier.create(successful)
.expectNext(true)
.verifyComplete();
Assertions.assertTrue(fileOpened.get());
Assertions.assertTrue(writtenToFile.get());
Assertions.assertTrue(fileClosed.get());
}
/**
* Before reading from a file we need to open file first.
*/
@Test
public void one_to_n() {
//todo: feel free to change code as you need
Flux<String> fileLines = null;
openFile();
readFile();
StepVerifier.create(fileLines)
.expectNext("0x1", "0x2", "0x3")
.verifyComplete();
}
/**
* Execute all tasks sequentially and after each task have been executed, commit task changes. Don't lose id's of
* committed tasks, they are needed to further processing!
*/
@Test
public void acid_durability() {
//todo: feel free to change code as you need
Flux<String> committedTasksIds = null;
tasksToExecute();
commitTask(null);
//don't change below this line
StepVerifier.create(committedTasksIds)
.expectNext("task#1", "task#2", "task#3")
.verifyComplete();
Assertions.assertEquals(3, committedTasksCounter.get());
}
/**
* News have come that Microsoft is buying Blizzard and there will be a major merger.
* Merge two companies, so they may still produce titles in individual pace but as a single company.
*/
@Test
public void major_merger() {
//todo: feel free to change code as you need
Flux<String> microsoftBlizzardCorp =
microsoftTitles();
blizzardTitles();
//don't change below this line
StepVerifier.create(microsoftBlizzardCorp)
.expectNext("windows12",
"wow2",
"bing2",
"overwatch3",
"office366",
"warcraft4")
.verifyComplete();
}
/**
* Your job is to produce cars. To produce car you need chassis and engine that are produced by a different
* manufacturer. You need both parts before you can produce a car.
* Also, engine factory is located further away and engines are more complicated to produce, therefore it will be
* slower part producer.
* After both parts arrive connect them to a car.
*/
@Test
public void car_factory() {
//todo: feel free to change code as you need
Flux<Car> producedCars = null;
carChassisProducer();
carEngineProducer();
//don't change below this line
StepVerifier.create(producedCars)
.recordWith(ConcurrentLinkedDeque::new)
.expectNextCount(3)
.expectRecordedMatches(cars -> cars.stream()
.allMatch(car -> Objects.equals(car.chassis.getSeqNum(),
car.engine.getSeqNum())))
.verifyComplete();
}
/**
* When `chooseSource()` method is used, based on current value of sourceRef, decide which source should be used.
*/
//only read from sourceRef
AtomicReference<String> sourceRef = new AtomicReference<>("X");
//todo: implement this method based on instructions
public Mono<String> chooseSource() {
sourceA(); //<- choose if sourceRef == "A"
sourceB(); //<- choose if sourceRef == "B"
return Mono.empty(); //otherwise, return empty
}
@Test
public void deterministic() {
//don't change below this line
Mono<String> source = chooseSource();
sourceRef.set("A");
StepVerifier.create(source)
.expectNext("A")
.verifyComplete();
sourceRef.set("B");
StepVerifier.create(source)
.expectNext("B")
.verifyComplete();
}
/**
* Sometimes you need to clean up after your self.
* Open a connection to a streaming service and after all elements have been consumed,
* close connection (invoke closeConnection()), without blocking.
*
* This may look easy...
*/
@Test
public void cleanup() {
BlockHound.install(); //don't change this line, blocking = cheating!
//todo: feel free to change code as you need
Flux<String> stream = StreamingConnection.startStreaming()
.flatMapMany(Function.identity());
StreamingConnection.closeConnection();
//don't change below this line
StepVerifier.create(stream)
.then(()-> Assertions.assertTrue(StreamingConnection.isOpen.get()))
.expectNextCount(20)
.verifyComplete();
Assertions.assertTrue(StreamingConnection.cleanedUp.get());
}
}