def test_clients_subset(): """Although all clients are enabled by default, the user may only enable a subset.""" ws_client = WebsocketClient() servient_01 = Servient() servient_02 = Servient(clients=[ws_client]) td = ThingDescription(TD_DICT_01) prop_name = next(six.iterkeys(TD_DICT_01["properties"])) assert servient_01.select_client(td, prop_name) is not ws_client assert servient_02.select_client(td, prop_name) is ws_client
def test_all_protocols_combined(all_protocols_servient): """Protocol bindings work as expected when multiple servers are combined within the same Servient.""" exposed_thing = next(all_protocols_servient.exposed_things) td = ThingDescription.from_thing(exposed_thing.thing) clients = [WebsocketClient(), HTTPClient()] if is_coap_supported(): from wotpy.protocols.coap.client import CoAPClient clients.append(CoAPClient()) if is_mqtt_supported(): from tests.protocols.mqtt.broker import is_test_broker_online from wotpy.protocols.mqtt.client import MQTTClient if is_test_broker_online(): clients.append(MQTTClient()) prop_name = next(six.iterkeys(td.properties)) @tornado.gen.coroutine def read_property(the_client): prop_value = Faker().sentence() curr_value = yield the_client.read_property(td, prop_name) assert curr_value != prop_value yield exposed_thing.properties[prop_name].write(prop_value) curr_value = yield the_client.read_property(td, prop_name) assert curr_value == prop_value @tornado.gen.coroutine def write_property(the_client): updated_value = Faker().sentence() curr_value = yield exposed_thing.properties[prop_name].read() assert curr_value != updated_value yield the_client.write_property(td, prop_name, updated_value) curr_value = yield exposed_thing.properties[prop_name].read() assert curr_value == updated_value @tornado.gen.coroutine def test_coroutine(): for client in clients: yield read_property(client) yield write_property(client) run_test_coroutine(test_coroutine)
def build_protocol_client(protocol): """Factory function to build the protocol client for the given protocol.""" if protocol == Protocols.HTTP: return HTTPClient() elif protocol == Protocols.WEBSOCKETS: return WebsocketClient() elif protocol == Protocols.COAP: from wotpy.protocols.coap.client import CoAPClient return CoAPClient() elif protocol == Protocols.MQTT: from wotpy.protocols.mqtt.client import MQTTClient return MQTTClient()
def _build_default_clients(self): """Builds the default Protocol Binding clients.""" self._clients = self._clients if self._clients else {} conf = self._clients_config if self._clients_config else {} self._clients.update({ Protocols.WEBSOCKETS: WebsocketClient(**conf.get(Protocols.WEBSOCKETS, {})), Protocols.HTTP: HTTPClient(**conf.get(Protocols.HTTP, {})) }) if is_coap_supported(): from wotpy.protocols.coap.client import CoAPClient self._clients.update( {Protocols.COAP: CoAPClient(**conf.get(Protocols.COAP, {}))}) if is_mqtt_supported(): from wotpy.protocols.mqtt.client import MQTTClient self._clients.update( {Protocols.MQTT: MQTTClient(**conf.get(Protocols.MQTT, {}))})
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()
def test_coroutine(): ws_client = WebsocketClient() with pytest.raises(ProtocolClientException): yield ws_client.read_property(td, uuid.uuid4().hex)