File: test_debounce.py

package info (click to toggle)
python-rx 4.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,056 kB
  • sloc: python: 39,070; javascript: 77; makefile: 24
file content (445 lines) | stat: -rw-r--r-- 13,723 bytes parent folder | download
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import unittest

from reactivex import empty, never
from reactivex import operators as _
from reactivex import throw
from reactivex.testing import ReactiveTest, TestScheduler

on_next = ReactiveTest.on_next
on_completed = ReactiveTest.on_completed
on_error = ReactiveTest.on_error
subscribe = ReactiveTest.subscribe
subscribed = ReactiveTest.subscribed
disposed = ReactiveTest.disposed
created = ReactiveTest.created


class RxException(Exception):
    pass


# Helper function for raising exceptions within lambdas
def _raise(ex):
    raise RxException(ex)


class TestDebounce(unittest.TestCase):
    def test_debounce_timespan_allpass(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(200, 2),
            on_next(250, 3),
            on_next(300, 4),
            on_next(350, 5),
            on_next(400, 6),
            on_next(450, 7),
            on_next(500, 8),
            on_completed(550),
        )

        def create():
            return xs.pipe(_.debounce(40))

        results = scheduler.start(create)

        assert results.messages == [
            on_next(290, 3),
            on_next(340, 4),
            on_next(390, 5),
            on_next(440, 6),
            on_next(490, 7),
            on_next(540, 8),
            on_completed(550),
        ]

    def test_debounce_timespan_allpass_error_end(self):
        ex = "ex"
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(200, 2),
            on_next(250, 3),
            on_next(300, 4),
            on_next(350, 5),
            on_next(400, 6),
            on_next(450, 7),
            on_next(500, 8),
            on_error(550, ex),
        )

        def create():
            return xs.pipe(_.debounce(40))

        results = scheduler.start(create)
        assert results.messages == [
            on_next(290, 3),
            on_next(340, 4),
            on_next(390, 5),
            on_next(440, 6),
            on_next(490, 7),
            on_next(540, 8),
            on_error(550, ex),
        ]

    def test_debounce_timespan_alldrop(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(200, 2),
            on_next(250, 3),
            on_next(300, 4),
            on_next(350, 5),
            on_next(400, 6),
            on_next(450, 7),
            on_next(500, 8),
            on_completed(550),
        )

        def create():
            return xs.pipe(_.debounce(60))

        results = scheduler.start(create)
        assert results.messages == [on_next(550, 8), on_completed(550)]

    def test_debounce_timespan_alldrop_error_end(self):
        ex = "ex"
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(200, 2),
            on_next(250, 3),
            on_next(300, 4),
            on_next(350, 5),
            on_next(400, 6),
            on_next(450, 7),
            on_next(500, 8),
            on_error(550, ex),
        )

        def create():
            return xs.pipe(_.debounce(60))

        results = scheduler.start(create)
        assert results.messages == [on_error(550, ex)]

    def test_debounce_timespan_some_drop(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(250, 2),
            on_next(350, 3),
            on_next(370, 4),
            on_next(421, 5),
            on_next(480, 6),
            on_next(490, 7),
            on_next(500, 8),
            on_completed(600),
        )

        def create():
            return xs.pipe(_.debounce(50))

        results = scheduler.start(create)
        assert results.messages == [
            on_next(300, 2),
            on_next(420, 4),
            on_next(471, 5),
            on_next(550, 8),
            on_completed(600),
        ]

    def test_debounce_empty(self):
        scheduler = TestScheduler()

        def create():
            return empty().pipe(_.debounce(10))

        results = scheduler.start(create)

        assert results.messages == [on_completed(200)]

    def test_debounce_error(self):
        ex = "ex"
        scheduler = TestScheduler()

        def create():
            return throw(ex).pipe(_.debounce(10))

        results = scheduler.start(create)
        assert results.messages == [on_error(200, ex)]

    def test_debounce_never(self):
        scheduler = TestScheduler()

        def create():
            return never().pipe(_.debounce(10))

        results = scheduler.start(create)
        assert results.messages == []

    def test_debounce_duration_delay_behavior(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, -1),
            on_next(250, 0),
            on_next(280, 1),
            on_next(310, 2),
            on_next(350, 3),
            on_next(400, 4),
            on_completed(550),
        )
        ys = [
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
        ]

        def create():
            def mapper(x):
                return ys[x]

            return xs.pipe(_.throttle_with_mapper(mapper))

        results = scheduler.start(create)

        assert results.messages == [
            on_next(250 + 20, 0),
            on_next(280 + 20, 1),
            on_next(310 + 20, 2),
            on_next(350 + 20, 3),
            on_next(400 + 20, 4),
            on_completed(550),
        ]
        assert xs.subscriptions == [subscribe(200, 550)]
        assert ys[0].subscriptions == [subscribe(250, 250 + 20)]
        assert ys[1].subscriptions == [subscribe(280, 280 + 20)]
        assert ys[2].subscriptions == [subscribe(310, 310 + 20)]
        assert ys[3].subscriptions == [subscribe(350, 350 + 20)]
        assert ys[4].subscriptions == [subscribe(400, 400 + 20)]

    def test_debounce_duration_throttle_behavior(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, -1),
            on_next(250, 0),
            on_next(280, 1),
            on_next(310, 2),
            on_next(350, 3),
            on_next(400, 4),
            on_completed(550),
        )
        ys = [
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
            scheduler.create_cold_observable(on_next(40, 42), on_next(45, 99)),
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
            scheduler.create_cold_observable(on_next(60, 42), on_next(65, 99)),
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
        ]

        def create():
            def mapper(x):
                return ys[x]

            return xs.pipe(_.throttle_with_mapper(mapper))

        results = scheduler.start(create)

        assert results.messages == [
            on_next(250 + 20, 0),
            on_next(310 + 20, 2),
            on_next(400 + 20, 4),
            on_completed(550),
        ]
        assert xs.subscriptions == [subscribe(200, 550)]
        assert ys[0].subscriptions == [subscribe(250, 250 + 20)]
        assert ys[1].subscriptions == [subscribe(280, 310)]
        assert ys[2].subscriptions == [subscribe(310, 310 + 20)]
        assert ys[3].subscriptions == [subscribe(350, 400)]
        assert ys[4].subscriptions == [subscribe(400, 400 + 20)]

    def test_debounce_duration_early_completion(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, -1),
            on_next(250, 0),
            on_next(280, 1),
            on_next(310, 2),
            on_next(350, 3),
            on_next(400, 4),
            on_completed(410),
        )
        ys = [
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
            scheduler.create_cold_observable(on_next(40, 42), on_next(45, 99)),
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
            scheduler.create_cold_observable(on_next(60, 42), on_next(65, 99)),
            scheduler.create_cold_observable(on_next(20, 42), on_next(25, 99)),
        ]

        def create():
            def mapper(x):
                return ys[x]

            return xs.pipe(_.throttle_with_mapper(mapper))

        results = scheduler.start(create)

        assert results.messages == [
            on_next(250 + 20, 0),
            on_next(310 + 20, 2),
            on_next(410, 4),
            on_completed(410),
        ]
        assert xs.subscriptions == [subscribe(200, 410)]
        assert ys[0].subscriptions == [subscribe(250, 250 + 20)]
        assert ys[1].subscriptions == [subscribe(280, 310)]
        assert ys[2].subscriptions == [subscribe(310, 310 + 20)]
        assert ys[3].subscriptions == [subscribe(350, 400)]
        assert ys[4].subscriptions == [subscribe(400, 410)]

    def test_debounce_duration_inner_error(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(250, 2),
            on_next(350, 3),
            on_next(450, 4),
            on_completed(550),
        )
        ex = "ex"

        def create():
            def mapper(x):
                if x < 4:
                    return scheduler.create_cold_observable(
                        on_next(x * 10, "Ignore"), on_next(x * 10 + 5, "Aargh!")
                    )
                else:
                    return scheduler.create_cold_observable(on_error(x * 10, ex))

            return xs.pipe(_.throttle_with_mapper(mapper))

        results = scheduler.start(create)

        assert results.messages == [
            on_next(250 + 2 * 10, 2),
            on_next(350 + 3 * 10, 3),
            on_error(450 + 4 * 10, ex),
        ]
        assert xs.subscriptions == [subscribe(200, 490)]

    def test_debounce_duration_outer_error(self):
        ex = "ex"
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(250, 2),
            on_next(350, 3),
            on_next(450, 4),
            on_error(460, ex),
        )

        def create():
            def mapper(x):
                return scheduler.create_cold_observable(
                    on_next(x * 10, "Ignore"), on_next(x * 10 + 5, "Aargh!")
                )

            return xs.pipe(_.throttle_with_mapper(mapper))

        results = scheduler.start(create)

        assert results.messages == [
            on_next(250 + 2 * 10, 2),
            on_next(350 + 3 * 10, 3),
            on_error(460, ex),
        ]
        assert xs.subscriptions == [subscribe(200, 460)]

    def test_debounce_duration_mapper_throws(self):
        ex = "ex"
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(250, 2),
            on_next(350, 3),
            on_next(450, 4),
            on_completed(550),
        )

        def create():
            def mapper(x):
                if x < 4:
                    return scheduler.create_cold_observable(
                        on_next(x * 10, "Ignore"), on_next(x * 10 + 5, "Aargh!")
                    )
                else:
                    _raise(ex)

            return xs.pipe(_.throttle_with_mapper(mapper))

        results = scheduler.start(create)

        assert results.messages == [
            on_next(250 + 2 * 10, 2),
            on_next(350 + 3 * 10, 3),
            on_error(450, ex),
        ]
        assert xs.subscriptions == [subscribe(200, 450)]

    def test_debounce_duration_inner_done_delay_behavior(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(250, 2),
            on_next(350, 3),
            on_next(450, 4),
            on_completed(550),
        )

        def create():
            def mapper(x):
                return scheduler.create_cold_observable(on_completed(x * 10))

            return xs.pipe(_.throttle_with_mapper(mapper))

        results = scheduler.start(create)

        assert results.messages == [
            on_next(250 + 2 * 10, 2),
            on_next(350 + 3 * 10, 3),
            on_next(450 + 4 * 10, 4),
            on_completed(550),
        ]
        assert xs.subscriptions == [subscribe(200, 550)]

    def test_debounce_duration_inner_done_throttle_behavior(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(250, 2),
            on_next(280, 3),
            on_next(300, 4),
            on_next(400, 5),
            on_next(410, 6),
            on_completed(550),
        )

        def create():
            def mapper(x):
                return scheduler.create_cold_observable(on_completed(x * 10))

            return xs.pipe(_.throttle_with_mapper(mapper))

        results = scheduler.start(create)

        assert results.messages == [
            on_next(250 + 2 * 10, 2),
            on_next(300 + 4 * 10, 4),
            on_next(410 + 6 * 10, 6),
            on_completed(550),
        ]
        assert xs.subscriptions == [subscribe(200, 550)]