本文整理汇总了Python中katcp.testutils.DeviceTestServer类的典型用法代码示例。如果您正苦于以下问题:Python DeviceTestServer类的具体用法?Python DeviceTestServer怎么用?Python DeviceTestServer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DeviceTestServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
def setUp(self):
self.server = DeviceTestServer('', 0)
self.server.start(timeout=0.1)
host, port = self.server._sock.getsockname()
self.client = BlockingTestClient(self, host, port)
self.client.start(timeout=0.1)
示例2: setUp
def setUp(self):
self.server = DeviceTestServer('', 0)
self.server.start(timeout=0.1)
host, port = self.server._sock.getsockname()
self.client = katcp.DeviceClient(host, port)
self.client.start(timeout=0.1)
示例3: setUp
def setUp(self):
super(test_KATCPClientResource_IntegratedTimewarp, self).setUp()
self.server = DeviceTestServer('', 0)
start_thread_with_cleanup(self, self.server)
self.host, self.port = self.server.bind_address
self.default_resource_spec = dict(
name='thething',
address=self.server.bind_address,
controlled=True)
示例4: setUp
def setUp(self):
super(TestInspectingClientAsync, self).setUp()
self.server = DeviceTestServer('', 0)
start_thread_with_cleanup(self, self.server, start_timeout=1)
self.host, self.port = self.server.bind_address
self.client = InspectingClientAsync(self.host, self.port,
ioloop=self.io_loop)
self.io_loop.add_callback(self.client.connect)
示例5: setUp
def setUp(self):
self.server = DeviceTestServer('', 0)
self.server.start(timeout=0.1)
host, port = self.server._sock.getsockname()
self.server_addr = (host, port)
self.client = BlockingTestClient(self, host, port)
self.client.start(timeout=0.1)
self.assertTrue(self.client.wait_protocol(timeout=0.1))
示例6: setUp
def setUp(self):
self.server = DeviceTestServer('', 0)
start_thread_with_cleanup(self, self.server, start_timeout=1)
host, port = self.server.bind_address
self.client = katcp.DeviceClient(host, port)
self.client.enable_thread_safety()
start_thread_with_cleanup(self, self.client, start_timeout=1)
self.client.wait_connected(timeout=1)
示例7: test_DeviceServer
class test_DeviceServer(unittest.TestCase, TestUtilMixin):
def setUp(self):
self.server = DeviceTestServer('', 0)
def test_on_client_connect(self):
fake_sock = mock.Mock()
mock_conn = mock.Mock(
spec=katcp.server.ClientConnection(self.server._server, fake_sock))
self.server.BUILD_INFO = ('buildy', 1, 2, 'g')
self.server.VERSION_INFO = ('deviceapi', 5, 6)
# Hack around ioloop thread asserts
self.server._server.ioloop_thread_id = thread.get_ident()
# Test call
self.server.on_client_connect(mock_conn)
# we are expecting 3 inform messages
no_msgs = 3
self.assertEqual(mock_conn.inform.call_count, no_msgs)
# Get all the messages sent to _send_message
msgs = [str(call[0][0]) for call in mock_conn.inform.call_args_list]
katcp_version = __version__
self._assert_msgs_equal(msgs, (
r'#version-connect katcp-protocol 5.0-IM',
# Will have to be updated for every library version bump
r'#version-connect katcp-library katcp-python-%s' % katcp_version,
r'#version-connect katcp-device deviceapi-5.6 buildy-1.2g'))
def test_request_sensor_sampling_clear(self):
self.server.clear_strategies = mock.Mock()
start_thread_with_cleanup(self, self.server, start_timeout=1)
client_connection = ClientConnectionTest()
self.server.ioloop.make_current()
tf = self.server.handle_message(
client_connection, katcp.Message.request('sensor-sampling-clear'))
# tf may be a tornado (not thread-safe) future, so we wrap it in a thread-safe
# future object
f = Future()
self.server.ioloop.add_callback(gen.chain_future, tf, f)
f.result(timeout=1)
# Ensure that the tornado future has finished running its callbacks
self.server.sync_with_ioloop()
self._assert_msgs_equal(client_connection.messages, [
'!sensor-sampling-clear ok'])
self.server.clear_strategies.assert_called_once_with(client_connection)
def test_has_sensor(self):
self.assertFalse(self.server.has_sensor('blaah'))
self.server.add_sensor(katcp.Sensor.boolean('blaah', 'blaah sens'))
self.assertTrue(self.server.has_sensor('blaah'))
示例8: test_ThreadSafeKATCPClientResourceWrapper
class test_ThreadSafeKATCPClientResourceWrapper(unittest.TestCase):
def setUp(self):
self.server = DeviceTestServer('', 0)
start_thread_with_cleanup(self, self.server)
self.ioloop_manager = ioloop_manager.IOLoopManager(managed_default=True)
self.io_loop = self.ioloop_manager.get_ioloop()
self.host, self.port = self.server.bind_address
self.default_resource_spec = dict(
name='thething',
address=self.server.bind_address,
controlled=True)
self.client_resource = resource_client.KATCPClientResource(
self.default_resource_spec)
self.client_resource.set_ioloop(self.io_loop)
self.io_loop.add_callback(self.client_resource.start)
self.ioloop_thread_wrapper = resource_client.IOLoopThreadWrapper(self.io_loop)
start_thread_with_cleanup(self, self.ioloop_manager, start_timeout=1)
self.ioloop_thread_wrapper.default_timeout = 1
self.DUT = resource_client.ThreadSafeKATCPClientResourceWrapper(
self.client_resource, self.ioloop_thread_wrapper)
self.DUT.until_synced()
def test_wrapped_timeout(self):
self.assertEqual(self.client_resource.state, 'synced')
# Test timeout
self.ioloop_thread_wrapper.default_timeout = 0.001
t0 = time.time()
with self.assertRaises(TimeoutError):
self.DUT.until_state('disconnected')
self.assertLess(time.time() - t0, 0.2)
# Now make sure we can actualy still wait on the state
self.ioloop_thread_wrapper.default_timeout = 1
self.server.stop()
self.server.join()
self.DUT.until_state('disconnected')
self.assertEqual(self.client_resource.state, 'disconnected')
self.server.start()
self.DUT.until_state('synced')
self.assertEqual(self.client_resource.state, 'synced')
def test_request(self):
reply = self.DUT.req.sensor_value('an.int')
last_server_msg = self.server.messages[-1]
self.assertTrue(reply.succeeded)
self.assertEqual(str(last_server_msg),
'?sensor-value[{}] an.int'.format(reply.reply.mid))
def test_sensor(self):
server_sensor = self.server.get_sensor('an.int')
reading = self.DUT.sensor.an_int.get_reading()
self.assertEqual(reading.value, server_sensor.read().value)
server_sensor.set_value(server_sensor.read().value + 5)
reading = self.DUT.sensor.an_int.get_reading()
self.assertEqual(reading.value, server_sensor.read().value)
示例9: setUp
def setUp(self):
super(test_AsyncClientTimeoutsIntegrated, self).setUp()
self.server = DeviceTestServer('', 0)
self.server.set_ioloop(self.io_loop)
self.server.set_concurrency_options(thread_safe=False, handler_thread=False)
self.server.start()
host, port = self.server.bind_address
self.client = katcp.CallbackClient(host, port)
self.client.set_ioloop(self.io_loop)
self.client.start()
示例10: test_AsyncClientIntegrated
class test_AsyncClientIntegrated(tornado.testing.AsyncTestCase, TestUtilMixin):
def setUp(self):
super(test_AsyncClientIntegrated, self).setUp()
self.server = DeviceTestServer('', 0)
self.server.set_ioloop(self.io_loop)
self.server.set_concurrency_options(thread_safe=False, handler_thread=False)
self.server.start()
host, port = self.server.bind_address
self.client = katcp.CallbackClient(host, port)
self.client.set_ioloop(self.io_loop)
self.client.start()
@tornado.testing.gen_test
def test_future_request_simple(self):
yield self.client.until_connected()
reply, informs = yield self.client.future_request(Message.request('watchdog'))
self.assertEqual(len(informs), 0)
self.assertEqual(reply.name, "watchdog")
self.assertEqual(reply.arguments, ["ok"])
@tornado.testing.gen_test
def test_future_request_with_informs(self):
yield self.client.until_connected()
reply, informs = yield self.client.future_request(Message.request('help'))
self.assertEqual(reply.name, "help")
self.assertEqual(reply.arguments, ["ok", "%d" % NO_HELP_MESSAGES])
self.assertEqual(len(informs), NO_HELP_MESSAGES)
@tornado.testing.gen_test
def test_disconnect_cleanup(self):
yield self.client.until_protocol()
mid = 55
future_reply = self.client.future_request(Message.request(
'slow-command', 1, mid=mid))
# Force a disconnect
self.client._disconnect()
reply, informs = yield future_reply
self.assertEqual(reply, Message.reply(
'slow-command', 'fail', 'Connection closed before reply was received',
mid=mid))
@tornado.testing.gen_test
def test_stop_cleanup(self):
yield self.client.until_protocol()
mid = 564
future_reply = self.client.future_request(Message.request(
'slow-command', 1, mid=mid))
# Stop client
self.client.stop()
reply, informs = yield future_reply
self.assertEqual(reply, Message.reply(
'slow-command', 'fail', 'Client stopped before reply was received', mid=mid))
示例11: test_AsyncClientTimeoutsIntegrated
class test_AsyncClientTimeoutsIntegrated(TimewarpAsyncTestCase):
def setUp(self):
super(test_AsyncClientTimeoutsIntegrated, self).setUp()
self.server = DeviceTestServer('', 0)
self.server.set_ioloop(self.io_loop)
self.server.set_concurrency_options(thread_safe=False, handler_thread=False)
self.server.start()
host, port = self.server.bind_address
self.client = katcp.CallbackClient(host, port)
self.client.set_ioloop(self.io_loop)
self.client.start()
@tornado.testing.gen_test(timeout=10)
# We are using time-warping, so the timeout should be longer than the fake-duration
def test_future_request_default_timeout(self):
# Test the default timeout of 5s
yield self._test_timeout(5)
@tornado.testing.gen_test()
def test_future_request_change_default_timeout(self):
self.client._request_timeout = 3
yield self._test_timeout(3)
@tornado.testing.gen_test()
def test_future_request_request_timeout(self):
yield self._test_timeout(1, set_request_timeout=True)
@gen.coroutine
def _test_timeout(self, timeout, set_request_timeout=False):
request_timeout = timeout if set_request_timeout else None
yield self.client.until_connected()
t0 = self.io_loop.time()
reply_future = self.client.future_request(Message.request('slow-command', timeout + 1),
timeout=request_timeout)
# Warp to just before the timeout expires and check that the future is not yet
# resolved
self.set_ioloop_time(t0 + timeout*0.9999)
yield self.wake_ioloop()
self.assertFalse(reply_future.done())
# Warp to just after the timeout expires, and check that it gives us a timeout
# error reply
self.set_ioloop_time(t0 + timeout*1.0001)
yield self.wake_ioloop()
self.assertTrue(reply_future.done())
reply, informs = reply_future.result()
self.assertFalse(reply.reply_ok())
self.assertRegexpMatches(
reply.arguments[1],
r"Request slow-command timed out after .* seconds.")
示例12: test_DeviceServer
class test_DeviceServer(unittest.TestCase, TestUtilMixin):
def setUp(self):
self.server = DeviceTestServer('', 0)
def test_on_client_connect(self):
conn = katcp.server.ClientConnectionTCP(self.server, 'fake-sock')
m_sm = self.server._send_message = mock.Mock()
self.server.BUILD_INFO = ('buildy', 1, 2, 'g')
self.server.VERSION_INFO = ('deviceapi', 5, 6)
self.server.on_client_connect(conn)
# we are expecting 3 inform messages
no_msgs = 3
self.assertEqual(m_sm.call_count, no_msgs)
# Check that calls were syntactically valid
self.assertEqual(m_sm.call_args_list,
[mock.call('fake-sock', mock.ANY)]*no_msgs)
# Get all the messages sent to _send_message
msgs = [str(call[0][1]) for call in m_sm.call_args_list]
self._assert_msgs_equal(msgs, (
r'#version-connect katcp-protocol 5.0-IM',
# Will have to be updated for every library version bump
r'#version-connect katcp-library katcp-python-0.5.4',
r'#version-connect katcp-device deviceapi-5.6 buildy-1.2g') )
def test_request_sensor_sampling_clear(self):
self.server.clear_strategies = mock.Mock()
client_connection = ClientConnectionTest()
self.server.handle_message(
client_connection, katcp.Message.request('sensor-sampling-clear'))
self._assert_msgs_equal(client_connection.messages, [
'!sensor-sampling-clear ok'])
self.server.clear_strategies.assert_called_once_with(client_connection)
def test_has_sensor(self):
self.assertFalse(self.server.has_sensor('blaah'))
self.server.add_sensor(katcp.Sensor.boolean('blaah', 'blaah sens'))
self.assertTrue(self.server.has_sensor('blaah'))
示例13: TestBlockingClient
class TestBlockingClient(unittest.TestCase):
def setUp(self):
self.server = DeviceTestServer('', 0)
self.server.start(timeout=0.1)
host, port = self.server._sock.getsockname()
self.client = katcp.BlockingClient(host, port)
self.client.start(timeout=0.1)
def tearDown(self):
if self.client.running():
self.client.stop()
self.client.join()
if self.server.running():
self.server.stop()
self.server.join()
def test_blocking_request(self):
"""Test blocking_request."""
reply, informs = self.client.blocking_request(
katcp.Message.request("watchdog"))
assert reply.name == "watchdog"
assert reply.arguments == ["ok"]
assert informs == []
reply, informs = self.client.blocking_request(
katcp.Message.request("help"))
assert reply.name == "help"
assert reply.arguments == ["ok", "13"]
assert len(informs) == int(reply.arguments[1])
def test_timeout(self):
"""Test calling blocking_request with a timeout."""
try:
self.client.blocking_request(
katcp.Message.request("slow-command", "0.5"),
timeout=0.001)
except RuntimeError, e:
self.assertEqual(str(e), "Request slow-command timed out after 0.001 seconds.")
else:
示例14: TestInspectingClientAsync
class TestInspectingClientAsync(tornado.testing.AsyncTestCase):
def setUp(self):
super(TestInspectingClientAsync, self).setUp()
self.server = DeviceTestServer('', 0)
start_thread_with_cleanup(self, self.server, start_timeout=1)
self.host, self.port = self.server.bind_address
self.client = InspectingClientAsync(self.host, self.port,
ioloop=self.io_loop)
self.io_loop.add_callback(self.client.connect)
@tornado.testing.gen_test
def test_simple_request(self):
"""Perform a basic request."""
yield self.client.until_synced()
reply, informs = yield self.client.simple_request('help', 'watchdog')
self.assertIn('ok', str(reply))
self.assertEquals(len(informs), 1)
@tornado.testing.gen_test
def test_sensor(self):
"""Access the sensor with the Async client."""
yield self.client.until_synced()
sensor_name = 'an.int'
sensor = yield self.client.future_get_sensor(sensor_name)
self.assertEquals(sensor.name, sensor_name)
self.assertEquals(sensor.stype, 'integer')
# Unknown sensor requests return a None.
sensor_name = 'thing.unknown_sensor'
sensor = yield self.client.future_get_sensor(sensor_name)
self.assertIsNone(sensor)
@tornado.testing.gen_test
def test_request_access(self):
"""Test access to requests."""
yield self.client.until_synced()
request_name = 'watchdog'
self.assertIn(request_name, self.client.requests)
request = yield self.client.future_get_request(request_name)
self.assertEqual(request.name, request_name)
self.assertTrue(request.description,
'Expected an description: got nothing.')
# Unknown request return a None.
request_name = 'watchcat'
self.assertNotIn(request_name, self.client.requests)
request = yield self.client.future_get_request(request_name)
self.assertIsNone(request)
@tornado.testing.gen_test
def test_sensor_add_remove(self):
"""Test a sensor being added and then remove it."""
yield self.client.until_synced()
sensor = DeviceTestSensor(Sensor.INTEGER, "another.int",
"An Integer.",
"count", [-5, 5], timestamp=time.time(),
status=Sensor.NOMINAL, value=3)
# Check that the sensor does not exist currently
self.assertNotIn(sensor.name, self.client.sensors)
# Add a sensor.
self.server.add_sensor(sensor)
self.server.mass_inform(Message.inform('interface-changed'))
# Do a blocking request to ensure #interface-changed has been received
yield self.client.simple_request('watchdog')
yield self.client.until_synced()
self.assertIn('another.int', self.client.sensors)
# Remove a sensor.
self.server.remove_sensor(sensor)
self.server.mass_inform(Message.inform('interface-changed'))
# Do a blocking request to ensure #interface-changed has been received
yield self.client.simple_request('watchdog')
yield self.client.until_synced()
self.assertNotIn('another.int', self.client.sensors)
@tornado.testing.gen_test
def test_request_add_remove(self):
"""Test a request being added and then remove it."""
yield self.client.until_synced()
def request_sparkling_new(self, req, msg):
"""A new command."""
return Message.reply(msg.name, "ok", "bling1", "bling2")
# Check that the request did not exist before
self.assertNotIn('sparkling-new', self.client.requests)
# Add a request.
self.server.request_sparkling_new = request_sparkling_new
self.server._request_handlers['sparkling-new'] = request_sparkling_new
self.server.mass_inform(Message.inform('interface-changed'))
# Do a blocking request to ensure #interface-changed has been received
#.........这里部分代码省略.........
示例15: TestCallbackClient
class TestCallbackClient(unittest.TestCase, TestUtilMixin):
def setUp(self):
self.addCleanup(self.stop_server_client)
self.server = DeviceTestServer('', 0)
self.server.start(timeout=0.1)
host, port = self.server._sock.getsockname()
self.client = katcp.CallbackClient(host, port)
self.client.start(timeout=0.1)
self.assertTrue(self.client.wait_protocol(timeout=1))
def stop_server_client(self):
if hasattr(self, 'client') and self.client.running():
self.client.stop()
self.client.join()
if hasattr(self, 'server') and self.server.running():
self.server.stop()
self.server.join()
def test_callback_request(self):
"""Test callback request."""
watchdog_replies = []
watchdog_replied = threading.Event()
def watchdog_reply(reply):
self.assertEqual(reply.name, "watchdog")
self.assertEqual(reply.arguments, ["ok"])
watchdog_replies.append(reply)
watchdog_replied.set()
self.assertTrue(self.client.wait_protocol(0.2))
self.client.callback_request(
katcp.Message.request("watchdog"),
reply_cb=watchdog_reply,
)
watchdog_replied.wait(0.2)
self.assertTrue(watchdog_replies)
help_replies = []
help_informs = []
help_replied = threading.Event()
def help_reply(reply):
self.assertEqual(reply.name, "help")
self.assertEqual(reply.arguments, ["ok", "%d" % NO_HELP_MESSAGES])
self.assertEqual(len(help_informs), int(reply.arguments[1]))
help_replies.append(reply)
help_replied.set()
def help_inform(inform):
self.assertEqual(inform.name, "help")
self.assertEqual(len(inform.arguments), 2)
help_informs.append(inform)
self.client.callback_request(
katcp.Message.request("help"),
reply_cb=help_reply,
inform_cb=help_inform,
)
help_replied.wait(1)
self.assertTrue(help_replied.isSet())
help_replied.clear()
help_replied.wait(0.05) # Check if (unwanted) late help replies arrive
self.assertFalse(help_replied.isSet())
self.assertEqual(len(help_replies), 1)
self.assertEqual(len(help_informs), NO_HELP_MESSAGES)
def test_callback_request_mid(self):
## Test that the client does the right thing with message identifiers
# Wait for the client to detect the server protocol. Server should
# support message identifiers
self.assertTrue(self.client.wait_protocol(0.2))
# Replace send_message so that we can check the message
self.client.send_message = mock.Mock()
# By default message identifiers should be enabled, and should start
# counting at 1
self.client.callback_request(katcp.Message.request('watchdog'))
self.client.callback_request(katcp.Message.request('watchdog'))
self.client.callback_request(katcp.Message.request('watchdog'))
# Extract katcp.Message object .mid attributes from the mock calls to
# send_message
mids = [args[0].mid # arg[0] should be the Message() object
for args, kwargs in self.client.send_message.call_args_list]
self.assertEqual(mids, ['1','2','3'])
self.client.send_message.reset_mock()
# Explicitly ask for no mid to be used
self.client.callback_request(
katcp.Message.request('watchdog'), use_mid=False)
mid = self.client.send_message.call_args[0][0].mid
self.assertEqual(mid, None)
# Ask for a specific mid to be used
#.........这里部分代码省略.........