Example #1
0
    def test_ioloop_schedule_action(self):
        loop = ioloop.IOLoop.instance()

        scheduler = IOLoopScheduler(loop)
        ran = [False]

        def action(scheduler, state):
            ran[0] = True
        scheduler.schedule(action)

        def done():
            assert(ran[0] == True)
            loop.stop()
        loop.call_later(0.1, done)

        loop.start()
Example #2
0
    def test_coroutine():
        protocol_client = protocol_client_cls()

        yield servient.shutdown()

        future_err = tornado.concurrent.Future()

        # noinspection PyUnusedLocal
        def on_next(item):
            future_err.set_exception(Exception("Should not have emitted any items"))

        def on_error(err):
            future_err.set_result(err)

        observable = protocol_client.on_property_change(td, prop_name)

        subscribe_kwargs = {
            "on_next": on_next,
            "on_error": on_error
        }

        subscription = observable.subscribe_on(IOLoopScheduler()).subscribe(**subscribe_kwargs)

        observe_err = yield future_err

        assert isinstance(observe_err, Exception)

        subscription.dispose()
Example #3
0
    def test_coroutine():
        protocol_client = protocol_client_cls()

        payloads = [Faker().pyint() for _ in range(10)]
        future_payloads = {key: tornado.concurrent.Future() for key in payloads}

        @tornado.gen.coroutine
        def emit_next():
            next_value = next(val for val, fut in six.iteritems(future_payloads) if not fut.done())
            exposed_thing.events[event_name].emit(next_value)

        def on_next(ev):
            if ev.data in future_payloads and not future_payloads[ev.data].done():
                future_payloads[ev.data].set_result(True)

        observable = protocol_client.on_event(td, event_name)

        subscription = observable.subscribe_on(IOLoopScheduler()).subscribe(on_next)

        periodic_emit = tornado.ioloop.PeriodicCallback(emit_next, 10)
        periodic_emit.start()

        yield list(future_payloads.values())

        periodic_emit.stop()
        subscription.dispose()
Example #4
0
    def test_coroutine():
        protocol_client = protocol_client_cls()

        values = [Faker().sentence() for _ in range(10)]
        values_observed = {value: tornado.concurrent.Future() for value in values}

        @tornado.gen.coroutine
        def write_next():
            try:
                next_value = next(val for val, fut in six.iteritems(values_observed) if not fut.done())
                yield exposed_thing.properties[prop_name].write(next_value)
            except StopIteration:
                pass

        def on_next(ev):
            prop_value = ev.data.value
            if prop_value in values_observed and not values_observed[prop_value].done():
                values_observed[prop_value].set_result(True)

        observable = protocol_client.on_property_change(td, prop_name)

        subscription = observable.subscribe_on(IOLoopScheduler()).subscribe(on_next)

        periodic_emit = tornado.ioloop.PeriodicCallback(write_next, 10)
        periodic_emit.start()

        yield list(values_observed.values())

        periodic_emit.stop()
        subscription.dispose()
Example #5
0
 def __init__(self):
     scheduler = IOLoopScheduler(IOLoop.current())
     self._app = make_app()
     self.messages = Subject()
     only_messages = self.messages.filter(lambda msg: msg[
         0] == 'message').map(lambda msg: msg[1]).publish()
     only_messages.subscribe(lambda msg: print(msg))
     only_messages.connect()
Example #6
0
    def test_ioloop_schedule_action_cancel(self):
        loop = ioloop.IOLoop.instance()

        ran = [False]
        scheduler = IOLoopScheduler(loop)

        def action(scheduler, state):
            ran[0] = True
        d = scheduler.schedule_relative(0.01, action)
        d.dispose()

        def done():
            assert(not ran[0])
            loop.stop()
        loop.call_later(0.1, done)

        loop.start()
Example #7
0
    def test_ioloop_schedule_action(self):
        loop = ioloop.IOLoop.instance()

        scheduler = IOLoopScheduler(loop)
        ran = [False]

        def action(scheduler, state):
            ran[0] = True

        scheduler.schedule(action)

        def done():
            assert (ran[0] == True)
            loop.stop()

        loop.call_later(0.1, done)

        loop.start()
Example #8
0
    def test_ioloop_schedule_action_cancel(self):
        loop = ioloop.IOLoop.instance()

        ran = [False]
        scheduler = IOLoopScheduler(loop)

        def action(scheduler, state):
            ran[0] = True

        d = scheduler.schedule_relative(0.01, action)
        d.dispose()

        def done():
            assert (not ran[0])
            loop.stop()

        loop.call_later(0.1, done)

        loop.start()
Example #9
0
    def subscribe(self, *args, **kwargs):
        """Subscribe to an stream of events emitted when the property value changes."""

        client_kwargs = kwargs.pop("client_kwargs", None)

        observable = self._consumed_thing.on_property_change(
            self._name, client_kwargs=client_kwargs)

        return observable.subscribe_on(IOLoopScheduler()).subscribe(
            *args, **kwargs)
Example #10
0
    def subscribe(self, *args, **kwargs):
        """Subscribe to an stream of emissions of this event."""

        client_kwargs = kwargs.pop("client_kwargs", None)

        observable = self._consumed_thing.on_event(self._name,
                                                   client_kwargs=client_kwargs)

        return observable.subscribe_on(IOLoopScheduler()).subscribe(
            *args, **kwargs)
Example #11
0
    def test_ioloop_schedule_action_due(self):
        loop = ioloop.IOLoop.instance()

        scheduler = IOLoopScheduler(loop)
        starttime = loop.time()
        endtime = [None]

        def action(scheduler, state):
            endtime[0] = loop.time()

        scheduler.schedule_relative(0.2, action)

        def done():
            diff = endtime[0]-starttime
            assert(diff > 0.18)
            loop.stop()
        loop.call_later(0.3, done)

        loop.start()
    def open(self):
        Server().messages.on_next(['opened', self.request])
        scheduler = IOLoopScheduler(IOLoop.current())

        def send_order(i):
            for order in Server().posted_orders:
                self.write_message("{}".format(order))

        timer = Observable.interval(1000, scheduler=scheduler)
        self.order_sender = timer.subscribe(send_order)
Example #13
0
 def __init__(self):
     scheduler = IOLoopScheduler(IOLoop.current())
     self.messages = Subject()
     only_messages = self.messages.filter(lambda msg: msg[
         0] == 'message').map(lambda msg: msg[1]).publish()
     only_messages.subscribe(lambda msg: print(msg))
     only_messages.connect()
     self._app = Application([
         (r'/exchange', ExchangeHandler),
         (r'/', MainHandler),
     ])
Example #14
0
    def test_ioloop_schedule_action_due(self):
        loop = ioloop.IOLoop.instance()

        scheduler = IOLoopScheduler(loop)
        starttime = loop.time()
        endtime = [None]

        def action(scheduler, state):
            endtime[0] = loop.time()

        scheduler.schedule_relative(0.2, action)

        def done():
            diff = endtime[0] - starttime
            assert (diff > 0.18)
            loop.stop()

        loop.call_later(0.3, done)

        loop.start()
Example #15
0
        def __init__(self):
            scheduler = IOLoopScheduler(IOLoop.current())
            self._define_app_handlers()
            self.orders = PriorityQueue()

            self.posted_orders = []

            self.fulfilled_orders = []

            self.messages = Subject()

            self.only_messages = self.messages \
                .filter(lambda msg: msg[0] == 'message') \
                .map(lambda msg: msg[1].split(',')) \
                .publish()

            def queue_order(msg):
                print('queueing order: {}'.format(msg))
                self.orders.put(Order.from_list(msg))

            self.only_messages \
                .filter(lambda msg: msg[0] == 'order') \
                .map(lambda msg: msg[1:]) \
                .subscribe(queue_order)
            self.only_messages.connect()

            def process_order(time):
                try:
                    order = self.orders.get_nowait()
                    print('processing order: {} [{}]'.format(
                        order, order.timestamp))
                    matching = None
                    for posted in self.posted_orders:
                        if posted.matches(order):
                            matching = posted
                            break

                    if matching is None:
                        self.posted_orders.append(order)
                        print('could not find match, posted order count is {}'.
                              format(len(self.posted_orders)))
                    else:
                        self.posted_orders.remove(posted)
                        self.fulfilled_orders.append(posted)
                        self.fulfilled_orders.append(order)
                        print('order fulfilled: {}'.format(order))
                        print('fulfilled by: {}'.format(posted))
                except QueueEmpty:
                    pass

            Observable.interval(100,
                                scheduler=scheduler).subscribe(process_order)
    def open(self):
        print('server open')
        Server().messages.on_next(['opened', self.request])
        scheduler = IOLoopScheduler(IOLoop.current())

        def send_order_snapshot(i):
            for order in Server().posted_orders:
                self.write_message('posted: {}'.format(order))
            for order in Server().fulfilled_orders:
                self.write_message('fulfilled: {}'.format(order))

        timer = Observable.interval(1000, scheduler=scheduler)
        self.order_sender = timer.subscribe(
            send_order_snapshot)  # => Disposable
Example #17
0
        def __init__(self):
            scheduler = IOLoopScheduler(IOLoop.current())
            self._app = Application([
                (r'/exchange', ExchangeHandler),
            ])
            self._servers = [
                ['localhost', '8888'],
                ['localhost', '7777'],
            ]
            self._current_server = 0

            self.messages = Subject()

            def passthrough_to_matching_server(msg):
                if self.matching_server_connection is None:
                    print('server is DOWN')
                    return
                self.matching_server_connection.write_message(msg)

            self.only_messages = self.messages.filter(lambda msg: msg[0] == 'message').map(lambda msg: msg[1]).subscribe(passthrough_to_matching_server)
            self.opened.dispose()

        def on_message_callback(message):
            self.messages.on_next(message)

        future = websocket_connect(
            self._url,
            on_message_callback=on_message_callback
        )
        Observable.from_future(future).subscribe(on_connect)

    def write_message(self, message):
        self.conn.write_message(message)

if __name__ == '__main__':
    scheduler = IOLoopScheduler(IOLoop.current())

    def make_say_hello(client, client_id):
        def say_hello():
            print('{} client #{} is sending orders'.format(
                datetime.datetime.now(), client_id))
            client.write_message(
                'order,{},ABC,buy,100,1.23'.format(client_id))
            client.write_message(
                'order,{},ABC,sell,110,1.23'.format(client_id))

        def schedule_say_hello(conn):
            sleep = choice([500, 600, 700])
            Observable \
                .interval(sleep, scheduler=scheduler) \
                .subscribe(lambda value: say_hello())
Example #19
0
    def _fetch_appium_sessions(self, appium_url) -> Observable:
        def on_error(e):
            logger.error("fail to fetch appium sessions by url: %s, %s",
                         appium_url, str(e))
            return Observable.empty()

        future = convert_yielded(self._appium_client.get_sessions(appium_url))
        return Observable.from_future(future) \
            .map(lambda res: json.loads(res.body)) \
            .catch_exception(handler=on_error) \
            .flat_map(lambda data: Observable.from_(data['value'])) \
            .map(lambda cap: (appium_url, cap))

    @staticmethod
    def _get_base_url(url):
        return get_base_url(url)

    @staticmethod
    def _get_current_time():
        return int(time.time())


_scheduler = IOLoopScheduler()

selenium_grid_service = SeleniumGridService(secret=LOCK_SECRET)
# start background job
selenium_grid_service.update_capabilities_in_background(10)
selenium_grid_service.release_expired_lock_in_background(1)
selenium_grid_service.refresh_capabilities_in_background(5)
Example #20
0
 def __init__(self, *args, **kwargs):
     self._server = kwargs.pop("websocket_server", None)
     self._scheduler = IOLoopScheduler()
     self._subscriptions = {}
     self._exposed_thing_name = None
     super(WebsocketHandler, self).__init__(*args, **kwargs)
Example #21
0
    def test_ioloop_schedule_now(self):
        loop = ioloop.IOLoop.instance()

        scheduler = IOLoopScheduler(loop)
        res = datetime.fromtimestamp(scheduler.now()) - datetime.utcnow()
        assert(res < timedelta(seconds=1))
Example #22
0
 def subscribe_func(event_name, on_next):
     observable = consumed_thing.on_event(event_name)
     return observable.subscribe_on(IOLoopScheduler()).subscribe(on_next)
Example #23
0
    def subscribe(self, *args, **kwargs):
        """Subscribes to changes on the TD of this thing."""

        observable = self.on_td_change()
        return observable.subscribe_on(IOLoopScheduler()).subscribe(*args, **kwargs)
Example #24
0
 def subscribe_func(prop_name, on_next):
     observable = consumed_thing.on_property_change(prop_name)
     return observable.subscribe_on(IOLoopScheduler()).subscribe(on_next)
Example #25
0
    def subscribe(self, *args, **kwargs):
        """Subscribe to an stream of emissions of this event."""

        observable = self._exposed_thing.on_event(self._name)
        return observable.subscribe_on(IOLoopScheduler()).subscribe(*args, **kwargs)
Example #26
0
    def subscribe(self, *args, **kwargs):
        """Subscribe to an stream of events emitted when the property value changes."""

        observable = self._exposed_thing.on_property_change(self._name)
        return observable.subscribe_on(IOLoopScheduler()).subscribe(*args, **kwargs)
Example #27
0
    def test_coroutine():
        ws_client = WebsocketClient()

        prop_names = list(td.properties.keys())
        prop_name_01 = prop_names[0]
        prop_name_02 = prop_names[1]

        obsv_01 = ws_client.on_property_change(td, prop_name_01)
        obsv_02 = ws_client.on_property_change(td, prop_name_02)

        prop_values_01 = [uuid.uuid4().hex for _ in range(10)]
        prop_values_02 = [uuid.uuid4().hex for _ in range(90)]

        future_values_01 = {key: Future() for key in prop_values_01}
        future_values_02 = {key: Future() for key in prop_values_02}

        future_conn_01 = Future()
        future_conn_02 = Future()

        def build_on_next(fut_conn, fut_vals):
            def on_next(ev):
                if not fut_conn.done():
                    fut_conn.set_result(True)

                if ev.data.value in fut_vals:
                    fut_vals[ev.data.value].set_result(True)

            return on_next

        on_next_01 = build_on_next(future_conn_01, future_values_01)
        on_next_02 = build_on_next(future_conn_02, future_values_02)

        subscription_01 = obsv_01.subscribe_on(
            IOLoopScheduler()).subscribe(on_next_01)
        subscription_02 = obsv_02.subscribe_on(
            IOLoopScheduler()).subscribe(on_next_02)

        while not future_conn_01.done() or not future_conn_02.done():
            yield exposed_thing.write_property(prop_name_01, uuid.uuid4().hex)
            yield exposed_thing.write_property(prop_name_02, uuid.uuid4().hex)
            yield tornado.gen.sleep(0)

        assert len(prop_values_01) < len(prop_values_02)

        for idx in range(len(prop_values_01)):
            yield exposed_thing.write_property(prop_name_01,
                                               prop_values_01[idx])
            yield exposed_thing.write_property(prop_name_02,
                                               prop_values_02[idx])

        yield list(future_values_01.values())

        assert next(fut for fut in six.itervalues(future_values_02)
                    if not fut.done())

        subscription_01.dispose()

        for val in prop_values_02[len(prop_values_01):]:
            yield exposed_thing.write_property(prop_name_02, val)

        yield list(future_values_02.values())

        subscription_02.dispose()
Example #28
0
    def test_ioloop_schedule_now(self):
        loop = ioloop.IOLoop.instance()

        scheduler = IOLoopScheduler(loop)
        res = scheduler.now() - datetime.now()
        assert (res < timedelta(seconds=1))