2023-04-16 16:52:06 +02:00
|
|
|
import time
|
2023-04-16 03:06:55 +02:00
|
|
|
import traceback
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from cpl_core.console import Console
|
2023-04-16 16:52:06 +02:00
|
|
|
from cpl_reactive_extensions import Subject
|
2023-04-16 03:06:55 +02:00
|
|
|
from cpl_reactive_extensions.interval import Interval
|
|
|
|
from cpl_reactive_extensions.operators.take import take
|
2023-04-16 16:52:06 +02:00
|
|
|
from cpl_reactive_extensions.operators.take_until import take_until
|
2023-04-16 03:06:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ObservableOperatorTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self._error = False
|
|
|
|
self._completed = False
|
|
|
|
|
|
|
|
def _on_error(self, ex: Exception):
|
|
|
|
tb = traceback.format_exc()
|
|
|
|
Console.error(f"Got error from observable: {ex}", tb)
|
|
|
|
self._error = True
|
|
|
|
|
|
|
|
def _on_complete(self):
|
|
|
|
self._completed = True
|
|
|
|
|
|
|
|
def test_take_two(self):
|
2023-04-16 16:52:06 +02:00
|
|
|
count = 0
|
|
|
|
|
|
|
|
def sub(x):
|
|
|
|
nonlocal count
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
observable = Interval(0.1)
|
|
|
|
observable.pipe(take(2)).subscribe(sub)
|
|
|
|
time.sleep(0.5)
|
|
|
|
self.assertEqual(count, 2)
|
|
|
|
|
|
|
|
def test_take_five(self):
|
|
|
|
count = 0
|
|
|
|
|
2023-04-16 03:06:55 +02:00
|
|
|
def sub(x):
|
2023-04-16 16:52:06 +02:00
|
|
|
nonlocal count
|
|
|
|
|
|
|
|
count += 1
|
2023-04-16 03:06:55 +02:00
|
|
|
|
2023-04-16 15:48:33 +02:00
|
|
|
observable = Interval(0.1)
|
2023-04-16 16:52:06 +02:00
|
|
|
observable.pipe(take(5)).subscribe(sub)
|
|
|
|
time.sleep(1)
|
|
|
|
self.assertEqual(count, 5)
|
|
|
|
|
|
|
|
def test_take_until(self):
|
|
|
|
count = 0
|
|
|
|
unsubscriber = Subject(None)
|
|
|
|
|
|
|
|
def sub(x):
|
|
|
|
nonlocal count
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
observable = Interval(0.1)
|
|
|
|
observable.pipe(take_until(unsubscriber)).subscribe(sub)
|
|
|
|
|
|
|
|
timer = 2
|
|
|
|
time.sleep(timer)
|
|
|
|
unsubscriber.next(None)
|
|
|
|
unsubscriber.complete()
|
|
|
|
self.assertEqual(count, timer * 10 - 1)
|