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_mergeconcat_innererror(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_hot_observable( on_next(210, scheduler.create_cold_observable( on_next(50, 1), on_next(100, 2), on_next(120, 3), on_completed(140))), on_next(260, scheduler.create_cold_observable( on_next(20, 4), on_next(70, 5), on_completed(200))), on_next(270, scheduler.create_cold_observable( on_next(10, 6), on_next(90, 7), on_next(110, 8), on_error(140, ex))), on_next(320, scheduler.create_cold_observable( on_next(210, 9), on_next(240, 10), on_completed(300))), on_completed(400) ) def create(): return xs.pipe(ops.merge(max_concurrent=2)) results = scheduler.start(create) assert results.messages == [ on_next(260, 1), on_next(280, 4), on_next(310, 2), on_next(330, 3), on_next(330, 5), on_next(360, 6), on_next(440, 7), on_next(460, 8), on_error(490, ex)] assert xs.subscriptions == [subscribe(200, 400)]
def test_select_many_then_complete_complete_2(): scheduler = TestScheduler() xs = scheduler.create_cold_observable( on_next(100, 4), on_next(200, 2), on_next(300, 3), on_next(400, 1), on_completed(700) ) ys = scheduler.create_cold_observable( on_next(50, "foo"), on_next(100, "bar"), on_next(150, "baz"), on_next(200, "qux"), on_completed(250) ) def factory(): return xs.select_many(ys) results = scheduler.start(factory) results.messages.assert_equal( on_next(350, "foo"), on_next(400, "bar"), on_next(450, "baz"), on_next(450, "foo"), on_next(500, "qux"), on_next(500, "bar"), on_next(550, "baz"), on_next(550, "foo"), on_next(600, "qux"), on_next(600, "bar"), on_next(650, "baz"), on_next(650, "foo"), on_next(700, "qux"), on_next(700, "bar"), on_next(750, "baz"), on_next(800, "qux"), on_completed(900), ) xs.subscriptions.assert_equal(subscribe(200, 900)) ys.subscriptions.assert_equal(subscribe(300, 550), subscribe(400, 650), subscribe(500, 750), subscribe(600, 850))
def test_switch_inner_throws(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_hot_observable( on_next(300, scheduler.create_cold_observable( on_next(10, 101), on_next(20, 102), on_next(110, 103), on_next(120, 104), on_next(210, 105), on_next(220, 106), on_completed(230))), on_next(400, scheduler.create_cold_observable( on_next(10, 201), on_next(20, 202), on_next(30, 203), on_next(40, 204), on_error(50, ex))), on_next(500, scheduler.create_cold_observable( on_next(10, 301), on_next(20, 302), on_next(30, 303), on_next(40, 304), on_completed(150))), on_completed(600)) def create(): return xs.pipe(ops.switch_latest()) results = scheduler.start(create) assert results.messages == [ on_next(310, 101), on_next(320, 102), on_next(410, 201), on_next(420, 202), on_next(430, 203), on_next(440, 204), on_error(450, ex)]
def test_select_many_then_error_complete(): ex = "ex" scheduler = TestScheduler() xs = scheduler.create_cold_observable( on_next(100, 4), on_next(200, 2), on_next(300, 3), on_next(400, 1), on_error(500, ex) ) ys = scheduler.create_cold_observable( on_next(50, "foo"), on_next(100, "bar"), on_next(150, "baz"), on_next(200, "qux"), on_completed(250) ) results = scheduler.start(lambda: xs.select_many(ys)) results.messages.assert_equal( on_next(350, "foo"), on_next(400, "bar"), on_next(450, "baz"), on_next(450, "foo"), on_next(500, "qux"), on_next(500, "bar"), on_next(550, "baz"), on_next(550, "foo"), on_next(600, "qux"), on_next(600, "bar"), on_next(650, "baz"), on_next(650, "foo"), on_error(700, ex), ) xs.subscriptions.assert_equal(subscribe(200, 700)) ys.subscriptions.assert_equal(subscribe(300, 550), subscribe(400, 650), subscribe(500, 700), subscribe(600, 700))
def test_flat_map_then_complete_complete(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable( on_next(100, 4), on_next(200, 2), on_next(300, 3), on_next(400, 1), on_completed(500)) ys = scheduler.create_cold_observable( on_next(50, "foo"), on_next(100, "bar"), on_next(150, "baz"), on_next(200, "qux"), on_completed(250)) def factory(): return xs.pipe(ops.flat_map(ys)) results = scheduler.start(factory) assert results.messages == [ on_next(350, "foo"), on_next(400, "bar"), on_next(450, "baz"), on_next(450, "foo"), on_next(500, "qux"), on_next(500, "bar"), on_next(550, "baz"), on_next(550, "foo"), on_next(600, "qux"), on_next(600, "bar"), on_next(650, "baz"), on_next(650, "foo"), on_next(700, "qux"), on_next(700, "bar"), on_next(750, "baz"), on_next(800, "qux"), on_completed(850)] assert xs.subscriptions == [subscribe(200, 700)] assert ys.subscriptions == [ subscribe(300, 550), subscribe(400, 650), subscribe(500, 750), subscribe(600, 850)]
def test_merge_observable_of_observable_outer_throws(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(300, scheduler.create_cold_observable(on_next(10, 101), on_next(20, 102), on_completed(230))), on_next( 400, scheduler.create_cold_observable(on_next(10, 201), on_next(20, 202), on_next(30, 203), on_next(40, 200), on_completed(50))), on_error(500, ex)) def create(): return xs.pipe(ops.merge_all()) results = scheduler.start(create) assert results.messages == [on_next(310, 101), on_next(320, 102), on_next( 410, 201), on_next(420, 202), on_next(430, 203), on_next(440, 200), on_error(500, ex)]
def test_mergeconcat_basic_late(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(210, scheduler.create_cold_observable(on_next(50, 1), on_next(100, 2), on_next(120, 3), on_completed(140))), on_next(260, scheduler.create_cold_observable(on_next(20, 4), on_next(70, 5), on_completed( 300))), on_next(270, scheduler.create_cold_observable(on_next(10, 6), on_next(90, 7), on_next(110, 8), on_completed(130))), on_next(420, scheduler.create_cold_observable(on_next(210, 9), on_next(240, 10), on_completed(300))), on_completed(750)) def create(): return xs.pipe(ops.merge(max_concurrent=3)) results = scheduler.start(create) assert results.messages == [on_next(260, 1), on_next(280, 4), on_next(280, 6), on_next(310, 2), on_next( 330, 3), on_next(330, 5), on_next(360, 7), on_next(380, 8), on_next(630, 9), on_next(660, 10), on_completed(750)] assert xs.subscriptions == [subscribe(200, 750)]
def test_timeout_duration_simple_firstthrows(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(310, 1), on_next(350, 2), on_next(420, 3), on_completed(450)) ys = scheduler.create_cold_observable(on_error(50, ex)) zs = scheduler.create_cold_observable() def create(): return xs.timeout_with_selector(ys, lambda _: zs) results = scheduler.start(create) results.messages.assert_equal(on_error(250, ex)) xs.subscriptions.assert_equal(subscribe(200, 250)) ys.subscriptions.assert_equal(subscribe(200, 250)) zs.subscriptions.assert_equal()
def test_retry_observable_basic(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3), on_completed(250)) results = scheduler.start(lambda: xs.retry()) results.messages.assert_equal(on_next(300, 1), on_next(350, 2), on_next(400, 3), on_completed(450)) xs.subscriptions.assert_equal(subscribe(200, 450))
def test_retry_observable_infinite(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3)) results = scheduler.start(lambda: xs.retry()) results.messages.assert_equal(on_next(300, 1), on_next(350, 2), on_next(400, 3)) return xs.subscriptions.assert_equal(subscribe(200, 1000))
def test_retry_observable_error(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3), on_error(250, ex)) results = scheduler.start(lambda: xs.retry(), disposed=1100) results.messages.assert_equal(on_next(300, 1), on_next(350, 2), on_next(400, 3), on_next(550, 1), on_next(600, 2), on_next(650, 3), on_next(800, 1), on_next(850, 2), on_next(900, 3), on_next(1050, 1)) return xs.subscriptions.assert_equal(subscribe(200, 450), subscribe(450, 700), subscribe(700, 950), subscribe(950, 1100))
def test_retry_observable_retry_count_dispose(self): scheduler = TestScheduler() ex = 'ex' xs = scheduler.create_cold_observable(on_next(5, 1), on_next(10, 2), on_next(15, 3), on_error(20, ex)) results = scheduler.start(lambda: xs.retry(3), disposed=231) results.messages.assert_equal(on_next(205, 1), on_next(210, 2), on_next(215, 3), on_next(225, 1), on_next(230, 2)) xs.subscriptions.assert_equal(subscribe(200, 220), subscribe(220, 231))
def test_timeout_duration_simple_never(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable( on_next(310, 1), on_next(350, 2), on_next(420, 3), on_completed(450) ) ys = scheduler.create_cold_observable() def create(): return xs.timeout_with_selector(ys, lambda _: ys) results = scheduler.start(create) results.messages.assert_equal( on_next(310, 1), on_next(350, 2), on_next(420, 3), on_completed(450) ) xs.subscriptions.assert_equal( subscribe(200, 450) ) ys.subscriptions.assert_equal( subscribe(200, 310), subscribe(310, 350), subscribe(350, 420), subscribe(420, 450) )
def test_dowhile_sometimes_true(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable( self.on_next(50, 1), self.on_next(100, 2), self.on_next(150, 3), self.on_next(200, 4), self.on_completed(250)) n = [0] def create(): def condition(x): n[0] += 1 return n[0]<3 return xs.pipe(ops.do_while(condition)) results = scheduler.start(create=create) assert results.messages == [ self.on_next(250, 1), self.on_next(300, 2), self.on_next(350, 3), self.on_next(400, 4), self.on_next(500, 1), self.on_next(550, 2), self.on_next(600, 3), self.on_next(650, 4), self.on_next(750, 1), self.on_next(800, 2), self.on_next(850, 3), self.on_next(900, 4), self.on_completed(950)] assert xs.subscriptions == [ self.subscribe(200, 450), self.subscribe(450, 700), self.subscribe(700, 950)]
def test_timeout_duration_simple_timeoutbycompletion(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(310, 1), on_next(350, 2), on_next(420, 3), on_completed(450)) ys = scheduler.create_cold_observable() zs = scheduler.create_cold_observable() def create(): def selector(x): if x < 3: return zs else: raise Exception(ex) return xs.timeout_with_selector(ys, selector) results = scheduler.start(create) results.messages.assert_equal( on_next(310, 1), on_next(350, 2), on_next(420, 3), on_error(420, ex) ) xs.subscriptions.assert_equal(subscribe(200, 420)) ys.subscriptions.assert_equal(subscribe(200, 310)) zs.subscriptions.assert_equal(subscribe(310, 350), subscribe(350, 420))
def test_repeat_observable_infinite(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3)) results = scheduler.start(lambda: xs.pipe(ops.repeat())) assert results.messages == [on_next(300, 1), on_next(350, 2), on_next(400, 3)] assert xs.subscriptions == [subscribe(200, 1000)]
def test_dowhile_always_true(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable( self.on_next(50, 1), self.on_next(100, 2), self.on_next(150, 3), self.on_next(200, 4), self.on_completed(250)) def create(): return xs.pipe(ops.do_while(lambda _: True)) results = scheduler.start(create=create) assert results.messages == [ self.on_next(250, 1), self.on_next(300, 2), self.on_next(350, 3), self.on_next(400, 4), self.on_next(500, 1), self.on_next(550, 2), self.on_next(600, 3), self.on_next(650, 4), self.on_next(750, 1), self.on_next(800, 2), self.on_next(850, 3), self.on_next(900, 4)] assert xs.subscriptions == [ self.subscribe(200, 450), self.subscribe(450, 700), self.subscribe(700, 950), self.subscribe(950, 1000)]
def test_repeat_observable_repeat_count_basic(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(5, 1), on_next(10, 2), on_next(15, 3), on_completed(20)) results = scheduler.start(lambda: xs.repeat(3)) results.messages.assert_equal(on_next(205, 1), on_next(210, 2), on_next(215, 3), on_next(225, 1), on_next(230, 2), on_next(235, 3), on_next(245, 1), on_next(250, 2), on_next(255, 3), on_completed(260)) xs.subscriptions.assert_equal(subscribe(200, 220), subscribe(220, 240), subscribe(240, 260))
def test_repeat_observable_repeat_count_error(): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3), on_error(250, ex)) results = scheduler.start(lambda: xs.repeat(3)) results.messages.assert_equal(on_next(300, 1), on_next(350, 2), on_next(400, 3), on_error(450, ex)) return xs.subscriptions.assert_equal(subscribe(200, 450))
def test_retry_observable_retry_count_basic(): scheduler = TestScheduler() ex = 'ex' xs = scheduler.create_cold_observable(on_next(5, 1), on_next(10, 2), on_next(15, 3), on_error(20, ex)) results = scheduler.start(lambda: xs.retry(3)) results.messages.assert_equal(on_next(205, 1), on_next(210, 2), on_next(215, 3), on_next(225, 1), on_next(230, 2), on_next(235, 3), on_next(245, 1), on_next(250, 2), on_next(255, 3), on_error(260, ex)) xs.subscriptions.assert_equal(subscribe(200, 220), subscribe(220, 240), subscribe(240, 260))
def test_repeat_observable_repeat_count_error(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3), on_error(250, ex)) results = scheduler.start(lambda: xs.pipe(ops.repeat(3))) assert results.messages == [on_next(300, 1), on_next(350, 2), on_next(400, 3), on_error(450, ex)] assert xs.subscriptions == [subscribe(200, 450)]
def test_merge_observable_of_observable_data(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(300, scheduler.create_cold_observable(on_next(10, 101), on_next(20, 102), on_next(110, 103), on_next(120, 104), on_next(210, 105), on_next(220, 106), on_completed(230))), on_next(400, scheduler.create_cold_observable(on_next(10, 201), on_next(20, 202), on_next(30, 203), on_next(40, 204), on_completed(50))), on_next(500, scheduler.create_cold_observable(on_next(10, 301), on_next(20, 302), on_next(30, 303), on_next(40, 304), on_next(120, 305), on_completed(150))), on_completed(600)) def create(): return xs.merge_observable() results = scheduler.start(create) results.messages.assert_equal(on_next(310, 101), on_next(320, 102), on_next(410, 103), on_next(410, 201), on_next(420, 104), on_next(420, 202), on_next(430, 203), on_next(440, 204), on_next(510, 105), on_next(510, 301), on_next(520, 106), on_next(520, 302), on_next(530, 303), on_next(540, 304), on_next(620, 305), on_completed(650))
def test_timeout_duration_simple_timeoutbycompletion(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(310, 1), on_next(350, 2), on_next(420, 3), on_completed(450)) ys = scheduler.create_cold_observable() zs = scheduler.create_cold_observable(on_completed(50)) def create(): return xs.timeout_with_selector(ys, lambda _: zs) results = scheduler.start(create) self.assertEqual(3, len(results.messages)) assert(on_next(310, 1).equals(results.messages[0])) assert(on_next(350, 2).equals(results.messages[1])) assert(results.messages[2].time == 400 and results.messages[2].value.exception) xs.subscriptions.assert_equal(subscribe(200, 400)) ys.subscriptions.assert_equal(subscribe(200, 310)) zs.subscriptions.assert_equal(subscribe(310, 350), subscribe(350, 400))
def test_retry_observable_retry_count_dispose_ii(self): scheduler = TestScheduler() ex = 'ex' xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3)) results = scheduler.start(lambda: xs.retry(3)) results.messages.assert_equal(on_next(300, 1), on_next(350, 2), on_next(400, 3)) xs.subscriptions.assert_equal(subscribe(200, 1000))
def test_select_many_error_outer(): ex = "ex" scheduler = TestScheduler() xs = scheduler.create_hot_observable( on_next(5, scheduler.create_cold_observable(on_error(1, "ex1"))), on_next(105, scheduler.create_cold_observable(on_error(1, "ex2"))), on_next( 300, scheduler.create_cold_observable( on_next(10, 102), on_next(90, 103), on_next(110, 104), on_next(190, 105), on_next(440, 106), on_completed(460), ), ), on_next(400, scheduler.create_cold_observable(on_next(180, 202), on_next(190, 203), on_completed(205))), on_next( 550, scheduler.create_cold_observable( on_next(10, 301), on_next(50, 302), on_next(70, 303), on_next(260, 304), on_next(310, 305), on_completed(410), ), ), on_next(750, scheduler.create_cold_observable(on_completed(40))), on_next(850, scheduler.create_cold_observable(on_next(80, 401), on_next(90, 402), on_completed(100))), on_error(900, ex), ) def factory(): return xs.select_many(lambda x: x) results = scheduler.start(factory) results.messages.assert_equal( on_next(310, 102), on_next(390, 103), on_next(410, 104), on_next(490, 105), on_next(560, 301), on_next(580, 202), on_next(590, 203), on_next(600, 302), on_next(620, 303), on_next(740, 106), on_next(810, 304), on_next(860, 305), on_error(900, ex), ) xs.subscriptions.assert_equal(subscribe(200, 900)) xs.messages[2].value.value.subscriptions.assert_equal(subscribe(300, 760)) xs.messages[3].value.value.subscriptions.assert_equal(subscribe(400, 605)) xs.messages[4].value.value.subscriptions.assert_equal(subscribe(550, 900)) xs.messages[5].value.value.subscriptions.assert_equal(subscribe(750, 790)) xs.messages[6].value.value.subscriptions.assert_equal(subscribe(850, 900))
def test_switch_outer_throws(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(300, scheduler.create_cold_observable(on_next(10, 101), on_next(20, 102), on_next(110, 103), on_next(120, 104), on_next(210, 105), on_next(220, 106), on_completed(230))), on_next(400, scheduler.create_cold_observable(on_next(10, 201), on_next(20, 202), on_next(30, 203), on_next(40, 204), on_completed(50))), on_error(500, ex)) def create(): return xs.switch_latest() results = scheduler.start(create) results.messages.assert_equal(on_next(310, 101), on_next(320, 102), on_next(410, 201), on_next(420, 202), on_next(430, 203), on_next(440, 204), on_error(500, ex))
def test_select_many_then_never_complete(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 4), on_next(200, 2), on_next(300, 3), on_next(400, 1), on_next(500, 5), on_next(700, 0)) ys = scheduler.create_cold_observable(on_next(50, "foo"), on_next(100, "bar"), on_next(150, "baz"), on_next(200, "qux"), on_completed(250)) results = scheduler.start(lambda: xs.select_many(ys)) results.messages.assert_equal(on_next(350, "foo"), on_next(400, "bar"), on_next(450, "baz"), on_next(450, "foo"), on_next(500, "qux"), on_next(500, "bar"), on_next(550, "baz"), on_next(550, "foo"), on_next(600, "qux"), on_next(600, "bar"), on_next(650, "baz"), on_next(650, "foo"), on_next(700, "qux"), on_next(700, "bar"), on_next(750, "baz"), on_next(750, "foo"), on_next(800, "qux"), on_next(800, "bar"), on_next(850, "baz"), on_next(900, "qux"), on_next(950, "foo")) xs.subscriptions.assert_equal(subscribe(200, 1000)) ys.subscriptions.assert_equal(subscribe(300, 550), subscribe(400, 650), subscribe(500, 750), subscribe(600, 850), subscribe(700, 950), subscribe(900, 1000))
def test_switch_inner_completes(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(300, scheduler.create_cold_observable(on_next(10, 101), on_next(20, 102), on_next(110, 103), on_next(120, 104), on_next(210, 105), on_next(220, 106), on_completed(230))), on_completed(540)) def create(): return xs.switch_latest() results = scheduler.start(create) results.messages.assert_equal(on_next(310, 101), on_next(320, 102), on_next(410, 103), on_next(420, 104), on_next(510, 105), on_next(520, 106), on_completed(540))
def test_while_always_true_infinite(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(50, 1)) def create(): return Observable.while_do(lambda _: True, xs) results = scheduler.start(create) results.messages.assert_equal(on_next(250, 1)) xs.subscriptions.assert_equal(subscribe(200, 1000))
def test_mergeconcat_disposed(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable( on_next( 210, scheduler.create_cold_observable(on_next(50, 1), on_next(100, 2), on_next(120, 3), on_completed(140))), on_next( 260, scheduler.create_cold_observable(on_next(20, 4), on_next(70, 5), on_completed(200))), on_next( 270, scheduler.create_cold_observable(on_next(10, 6), on_next(90, 7), on_next(110, 8), on_completed(130))), on_next( 320, scheduler.create_cold_observable(on_next(210, 9), on_next(240, 10), on_completed(300))), on_completed(400)) def create(): return xs.merge(2) results = scheduler.start(create, disposed=450) results.messages.assert_equal(on_next(260, 1), on_next(280, 4), on_next(310, 2), on_next(330, 3), on_next(330, 5), on_next(360, 6), on_next(440, 7)) xs.subscriptions.assert_equal(subscribe(200, 450))
def test_retry_observable_error(): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3), on_error(250, ex)) results = scheduler.start(lambda: xs.retry(), disposed=1100) results.messages.assert_equal(on_next(300, 1), on_next(350, 2), on_next(400, 3), on_next(550, 1), on_next(600, 2), on_next(650, 3), on_next(800, 1), on_next(850, 2), on_next(900, 3), on_next(1050, 1)) return xs.subscriptions.assert_equal(subscribe(200, 450), subscribe(450, 700), subscribe(700, 950), subscribe(950, 1100))
def test_flat_map_then_complete_never(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 4), on_next(200, 2), on_next(300, 3), on_next(400, 1), on_completed(500)) ys = scheduler.create_cold_observable(on_next(50, "foo"), on_next(100, "bar"), on_next(150, "baz"), on_next(200, "qux")) results = scheduler.start(lambda: xs.pipe(ops.flat_map(ys))) assert results.messages == [ on_next(350, "foo"), on_next(400, "bar"), on_next(450, "baz"), on_next(450, "foo"), on_next(500, "qux"), on_next(500, "bar"), on_next(550, "baz"), on_next(550, "foo"), on_next(600, "qux"), on_next(600, "bar"), on_next(650, "baz"), on_next(650, "foo"), on_next(700, "qux"), on_next(700, "bar"), on_next(750, "baz"), on_next(800, "qux") ] assert xs.subscriptions == [subscribe(200, 700)] assert ys.subscriptions == [ subscribe(300, 1000), subscribe(400, 1000), subscribe(500, 1000), subscribe(600, 1000) ]
def test_zip_non_empty_partial_sequential(self): scheduler = TestScheduler() msgs1 = [on_next(210, 1), on_next(215, 2), on_completed(230)] msgs2 = [on_next(240, 1), on_completed(250)] e1 = scheduler.create_cold_observable(msgs1) e2 = scheduler.create_cold_observable(msgs2) def create(): return e1.pipe(ops.zip(e2), ops.map(sum)) results = scheduler.start(create) assert results.messages == [ on_next(200 + 240, 1 + 1), on_completed(200 + 250) ]
def test_select_many_then_complete_never(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 4), on_next(200, 2), on_next(300, 3), on_next(400, 1), on_completed(500)) ys = scheduler.create_cold_observable(on_next(50, "foo"), on_next(100, "bar"), on_next(150, "baz"), on_next(200, "qux")) results = scheduler.start(lambda: xs.select_many(ys)) results.messages.assert_equal(on_next(350, "foo"), on_next(400, "bar"), on_next(450, "baz"), on_next(450, "foo"), on_next(500, "qux"), on_next(500, "bar"), on_next(550, "baz"), on_next(550, "foo"), on_next(600, "qux"), on_next(600, "bar"), on_next(650, "baz"), on_next(650, "foo"), on_next(700, "qux"), on_next(700, "bar"), on_next(750, "baz"), on_next(800, "qux")) xs.subscriptions.assert_equal(subscribe(200, 700)) ys.subscriptions.assert_equal(subscribe(300, 1000), subscribe(400, 1000), subscribe(500, 1000), subscribe(600, 1000))
def test_timeout_timeout_occurs_never(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(70, 1), on_next(130, 2), on_next(240, 3), on_next(310, 4), on_next(430, 5), on_completed(500)) ys = scheduler.create_cold_observable() def create(): return xs.timeout(100, ys, scheduler=scheduler) results = scheduler.start(create) results.messages.assert_equal(on_next(240, 3), on_next(310, 4)) xs.subscriptions.assert_equal(subscribe(200, 410)) ys.subscriptions.assert_equal(subscribe(410, 1000))
def test_timeout_datetime_offset_timeout_occur_3(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(310, 1), on_next(350, 2), on_next(420, 3), on_completed(450)) ys = scheduler.create_cold_observable() def create(): return xs.timeout(datetime.fromtimestamp(400 / 1000.0), ys, scheduler) results = scheduler.start(create) results.messages.assert_equal(on_next(310, 1), on_next(350, 2)) xs.subscriptions.assert_equal(subscribe(200, 400)) ys.subscriptions.assert_equal(subscribe(400, 1000))
def test_flat_map_error_inner(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(5, scheduler.create_cold_observable(on_error(1, 'ex1'))), on_next(105, scheduler.create_cold_observable(on_error(1, 'ex2'))), on_next(300, scheduler.create_cold_observable(on_next(10, 102), on_next(90, 103), on_next(110, 104), on_next(190, 105), on_next(440, 106), on_error(460, ex))), on_next(400, scheduler.create_cold_observable(on_next(180, 202), on_next(190, 203), on_completed(205))), on_next(550, scheduler.create_cold_observable(on_next(10, 301), on_next(50, 302), on_next(70, 303), on_next(260, 304), on_next(310, 305), on_completed(410))), on_next(750, scheduler.create_cold_observable(on_completed(40))), on_next(850, scheduler.create_cold_observable(on_next(80, 401), on_next(90, 402), on_completed(100))), on_completed(900)) def factory(): return xs.flat_map(lambda x: x) results = scheduler.start(factory) assert results.messages == [on_next(310, 102), on_next(390, 103), on_next(410, 104), on_next(490, 105), on_next(560, 301), on_next(580, 202), on_next(590, 203), on_next(600, 302), on_next(620, 303), on_next(740, 106), on_error(760, ex)] assert xs.subscriptions == [subscribe(200, 760)] assert xs.messages[2].value.value.subscriptions == [subscribe(300, 760)] assert xs.messages[3].value.value.subscriptions == [subscribe(400, 605)] assert xs.messages[4].value.value.subscriptions == [subscribe(550, 760)] assert xs.messages[5].value.value.subscriptions == [subscribe(750, 760)] assert xs.messages[6].value.value.subscriptions == []
def test_select_many_dispose(): scheduler = TestScheduler() xs = scheduler.create_hot_observable( on_next(5, scheduler.create_cold_observable(on_error(1, 'ex1'))), on_next(105, scheduler.create_cold_observable(on_error(1, 'ex2'))), on_next( 300, scheduler.create_cold_observable(on_next(10, 102), on_next(90, 103), on_next(110, 104), on_next(190, 105), on_next(440, 106), on_completed(460))), on_next( 400, scheduler.create_cold_observable(on_next(180, 202), on_next(190, 203), on_completed(205))), on_next( 550, scheduler.create_cold_observable(on_next(10, 301), on_next(50, 302), on_next(70, 303), on_next(260, 304), on_next(310, 305), on_completed(410))), on_next(750, scheduler.create_cold_observable(on_completed(40))), on_next( 850, scheduler.create_cold_observable(on_next(80, 401), on_next(90, 402), on_completed(100))), on_completed(900)) def create(): return xs.select_many(lambda x: x) results = scheduler.start(create, disposed=700) results.messages.assert_equal(on_next(310, 102), on_next(390, 103), on_next(410, 104), on_next(490, 105), on_next(560, 301), on_next(580, 202), on_next(590, 203), on_next(600, 302), on_next(620, 303)) xs.subscriptions.assert_equal(subscribe(200, 700)) xs.messages[2].value.value.subscriptions.assert_equal(subscribe(300, 700)) xs.messages[3].value.value.subscriptions.assert_equal(subscribe(400, 605)) xs.messages[4].value.value.subscriptions.assert_equal(subscribe(550, 700)) xs.messages[5].value.value.subscriptions.assert_equal() xs.messages[6].value.value.subscriptions.assert_equal()
def test_repeat_observable_error(self): results = None scheduler = TestScheduler() ex = 'ex' xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3), on_error(250, ex)) results = scheduler.start(lambda: xs.pipe(ops.repeat())) assert results.messages == [ on_next(300, 1), on_next(350, 2), on_next(400, 3), on_error(450, ex) ] assert xs.subscriptions == [subscribe(200, 450)]
def test_timeout_datetime_offset_timeout_does_not_occur_error(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(310, 1), on_error(390, ex)) ys = scheduler.create_cold_observable(on_next(100, -1)) def create(): return xs.timeout(datetime.fromtimestamp(400 / 1000.0), ys, scheduler=scheduler) results = scheduler.start(create) results.messages.assert_equal(on_next(310, 1), on_error(390, ex)) xs.subscriptions.assert_equal(subscribe(200, 390)) ys.subscriptions.assert_equal()
def test_timeout_duration_simple_never(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(310, 1), on_next(350, 2), on_next(420, 3), on_completed(450)) ys = scheduler.create_cold_observable() def create(): return xs.timeout_with_selector(ys, lambda _: ys) results = scheduler.start(create) results.messages.assert_equal(on_next(310, 1), on_next(350, 2), on_next(420, 3), on_completed(450)) xs.subscriptions.assert_equal(subscribe(200, 450)) ys.subscriptions.assert_equal(subscribe(200, 310), subscribe(310, 350), subscribe(350, 420), subscribe(420, 450))
def test_switch_inner_completes(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable( on_next(300, scheduler.create_cold_observable( on_next(10, 101), on_next(20, 102), on_next(110, 103), on_next(120, 104), on_next(210, 105), on_next(220, 106), on_completed(230))), on_completed(540)) def create(): return xs.pipe(ops.switch_latest()) results = scheduler.start(create) assert results.messages == [ on_next(310, 101), on_next(320, 102), on_next(410, 103), on_next(420, 104), on_next(510, 105), on_next(520, 106), on_completed(540)]
def test_dowhile_sometimes_throws(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(50, 1), on_next(100, 2), on_next(150, 3), on_next(200, 4), on_completed(250)) n = [0] def create(): def condition(x): n[0] += 1 if n[0]<3: return True else: raise Exception(ex) return xs.do_while(condition) results = scheduler.start(create=create) results.messages.assert_equal(on_next(250, 1), on_next(300, 2), on_next(350, 3), on_next(400, 4), on_next(500, 1), on_next(550, 2), on_next(600, 3), on_next(650, 4), on_next(750, 1), on_next(800, 2), on_next(850, 3), on_next(900, 4), on_error(950, ex)) xs.subscriptions.assert_equal(subscribe(200, 450), subscribe(450, 700), subscribe(700, 950))
def test_delay_duration_dispose1(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(150, 1), on_next(210, 2), on_next(220, 3), on_next(230, 4), on_next(240, 5), on_next(250, 6), on_completed(300)) ys = scheduler.create_cold_observable(on_next(200, '!')) def create(): return xs.delay_with_selector(lambda _: ys) results = scheduler.start(create, disposed=425) results.messages.assert_equal(on_next(210 + 200, 2), on_next(220 + 200, 3)) xs.subscriptions.assert_equal(subscribe(200, 300)) ys.subscriptions.assert_equal(subscribe(210, 410), subscribe(220, 420), subscribe(230, 425), subscribe(240, 425), subscribe(250, 425))
def test_timeout_datetime_offset_timeout_occur_2(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(310, 1), on_next(350, 2), on_next(420, 3), on_completed(450)) ys = scheduler.create_cold_observable(on_next(100, -1)) def create(): return xs.timeout(datetime.utcfromtimestamp(400 / 1000.0), ys) results = scheduler.start(create) assert results.messages == [ on_next(310, 1), on_next(350, 2), on_next(500, -1) ] assert xs.subscriptions == [subscribe(200, 400)] assert ys.subscriptions == [subscribe(400, 1000)]
def test_while_always_true(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(50, 1), on_next(100, 2), on_next(150, 3), on_next(200, 4), on_completed(250)) def create(): return Observable.while_do(lambda _: True, xs) results = scheduler.start(create) results.messages.assert_equal(on_next(250, 1), on_next(300, 2), on_next(350, 3), on_next(400, 4), on_next(500, 1), on_next(550, 2), on_next(600, 3), on_next(650, 4), on_next(750, 1), on_next(800, 2), on_next(850, 3), on_next(900, 4)) xs.subscriptions.assert_equal(subscribe(200, 450), subscribe(450, 700), subscribe(700, 950), subscribe(950, 1000))
def test_timeout_duration_simple_source_throws(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(310, 1), on_next(350, 2), on_next(420, 3), on_error(450, ex)) ys = scheduler.create_cold_observable() zs = scheduler.create_cold_observable() def create(): return xs.timeout_with_selector(ys, lambda _: zs) results = scheduler.start(create) results.messages.assert_equal(on_next(310, 1), on_next(350, 2), on_next(420, 3), on_error(450, ex)) xs.subscriptions.assert_equal(subscribe(200, 450)) ys.subscriptions.assert_equal(subscribe(200, 310)) zs.subscriptions.assert_equal(subscribe(310, 350), subscribe(350, 420), subscribe(420, 450))
def test_dowhile_always_false(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable( self.on_next(50, 1), self.on_next(100, 2), self.on_next(150, 3), self.on_next(200, 4), self.on_completed(250)) def create(): return xs.pipe(ops.do_while(lambda _: False)) results = scheduler.start(create=create) assert results.messages == [ self.on_next(250, 1), self.on_next(300, 2), self.on_next(350, 3), self.on_next(400, 4), self.on_completed(450)] assert xs.subscriptions == [self.subscribe(200, 450)]
def test_timeout_timeout_occurs_1(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(70, 1), on_next(130, 2), on_next(310, 3), on_next(400, 4), on_completed(500)) ys = scheduler.create_cold_observable(on_next(50, -1), on_next(200, -2), on_next(310, -3), on_completed(320)) def create(): return xs.timeout(100, ys) results = scheduler.start(create) assert results.messages == [ on_next(350, -1), on_next(500, -2), on_next(610, -3), on_completed(620) ] assert xs.subscriptions == [subscribe(200, 300)] assert ys.subscriptions == [subscribe(300, 620)]
def test_delay_duration_simple4_inner_empty(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(150, 1), on_next(210, 2), on_next(220, 3), on_next(230, 4), on_next(240, 5), on_next(250, 6), on_completed(300)) ys = scheduler.create_cold_observable(on_completed(100)) def create(): return xs.delay_with_selector(lambda _: ys) results = scheduler.start(create) results.messages.assert_equal(on_next(210 + 100, 2), on_next(220 + 100, 3), on_next(230 + 100, 4), on_next(240 + 100, 5), on_next(250 + 100, 6), on_completed(350)) xs.subscriptions.assert_equal(subscribe(200, 300)) ys.subscriptions.assert_equal(subscribe(210, 310), subscribe(220, 320), subscribe(230, 330), subscribe(240, 340), subscribe(250, 350))
def test_flat_map_on_error(self): invoked = [0] ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(5, scheduler.create_cold_observable(on_error(1, 'ex1'))), on_next(105, scheduler.create_cold_observable(on_error(1, 'ex2'))), on_next(300, scheduler.create_cold_observable(on_next(10, 102), on_next(90, 103), on_next(110, 104), on_next(190, 105), on_next(440, 106), on_completed(460))), on_next(400, scheduler.create_cold_observable(on_next(180, 202), on_next(190, 203), on_completed(205))), on_next(550, scheduler.create_cold_observable(on_next(10, 301), on_next(50, 302), on_next(70, 303), on_next(260, 304), on_next(310, 305), on_completed(410))), on_next(750, scheduler.create_cold_observable(on_completed(40))), on_next(850, scheduler.create_cold_observable(on_next(80, 401), on_next(90, 402), on_completed(100))), on_completed(900)) def factory(): def projection(x): invoked[0] += 1 if invoked[0] == 3: raise Exception(ex) return x return xs.flat_map(projection) results = scheduler.start(factory) assert results.messages == [on_next(310, 102), on_next(390, 103), on_next(410, 104), on_next(490, 105), on_error(550, ex)] assert xs.subscriptions == [subscribe(200, 550)] assert xs.messages[2].value.value.subscriptions == [subscribe(300, 550)] assert xs.messages[3].value.value.subscriptions == [subscribe(400, 550)] assert xs.messages[4].value.value.subscriptions == [] assert xs.messages[5].value.value.subscriptions == [] assert xs.messages[6].value.value.subscriptions == []
def test_while_sometimes_true(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(50, 1), on_next(100, 2), on_next(150, 3), on_next(200, 4), on_completed(250)) n = [0] def create(): def predicate(x): n[0] += 1 return n[0] < 3 return Observable.while_do(predicate, xs) results = scheduler.start(create) results.messages.assert_equal(on_next(250, 1), on_next(300, 2), on_next(350, 3), on_next(400, 4), on_next(500, 1), on_next(550, 2), on_next(600, 3), on_next(650, 4), on_completed(700)) xs.subscriptions.assert_equal(subscribe(200, 450), subscribe(450, 700))
def test_dowhile_sometimes_true(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(self.on_next(50, 1), self.on_next(100, 2), self.on_next(150, 3), self.on_next(200, 4), self.on_completed(250)) n = [0] def create(): def condition(x): n[0] += 1 return n[0] < 3 return xs.do_while(condition) results = scheduler.start(create=create) assert results.messages == [ self.on_next(250, 1), self.on_next(300, 2), self.on_next(350, 3), self.on_next(400, 4), self.on_next(500, 1), self.on_next(550, 2), self.on_next(600, 3), self.on_next(650, 4), self.on_next(750, 1), self.on_next(800, 2), self.on_next(850, 3), self.on_next(900, 4), self.on_completed(950) ] assert xs.subscriptions == [ self.subscribe(200, 450), self.subscribe(450, 700), self.subscribe(700, 950) ]
def test_repeat_observable_repeat_count_basic(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(5, 1), on_next(10, 2), on_next(15, 3), on_completed(20)) results = scheduler.start(lambda: xs.pipe(ops.repeat(3))) assert results.messages == [ on_next(205, 1), on_next(210, 2), on_next(215, 3), on_next(225, 1), on_next(230, 2), on_next(235, 3), on_next(245, 1), on_next(250, 2), on_next(255, 3), on_completed(260) ] assert xs.subscriptions == [ subscribe(200, 220), subscribe(220, 240), subscribe(240, 260) ]
def test_repeat_observable_basic(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(200, 3), on_next(150, 2), on_completed(250)) results = scheduler.start(lambda: xs.pipe(ops.repeat())) assert results.messages == [ on_next(300, 1), on_next(350, 2), on_next(400, 3), on_next(550, 1), on_next(600, 2), on_next(650, 3), on_next(800, 1), on_next(850, 2), on_next(900, 3) ] assert xs.subscriptions == [ subscribe(200, 450), subscribe(450, 700), subscribe(700, 950), subscribe(950, 1000) ]