本文整理汇总了Python中katcp.testutils.DeviceTestServer.running方法的典型用法代码示例。如果您正苦于以下问题:Python DeviceTestServer.running方法的具体用法?Python DeviceTestServer.running怎么用?Python DeviceTestServer.running使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类katcp.testutils.DeviceTestServer
的用法示例。
在下文中一共展示了DeviceTestServer.running方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestBlockingClient
# 需要导入模块: from katcp.testutils import DeviceTestServer [as 别名]
# 或者: from katcp.testutils.DeviceTestServer import running [as 别名]
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:
示例2: TestCallbackClient
# 需要导入模块: from katcp.testutils import DeviceTestServer [as 别名]
# 或者: from katcp.testutils.DeviceTestServer import running [as 别名]
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
#.........这里部分代码省略.........
示例3: TestDeviceClientIntegrated
# 需要导入模块: from katcp.testutils import DeviceTestServer [as 别名]
# 或者: from katcp.testutils.DeviceTestServer import running [as 别名]
class TestDeviceClientIntegrated(unittest.TestCase, TestUtilMixin):
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)
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_request(self):
"""Test request method."""
self.assertTrue(self.client.wait_protocol(1))
self.client.send_request(katcp.Message.request("watchdog"))
self.client.send_request(katcp.Message.request("watchdog", mid=55))
self.client._server_supports_ids = False
with self.assertRaises(katcp.core.KatcpVersionError):
self.client.send_request(katcp.Message.request("watchdog", mid=56))
time.sleep(0.1)
msgs = self.server.messages()
self._assert_msgs_equal(msgs, [
r"?watchdog",
r"?watchdog[55]",
])
def test_send_message(self):
"""Test send_message method."""
self.client.send_message(katcp.Message.inform("random-inform"))
time.sleep(0.1)
msgs = self.server.messages()
self._assert_msgs_equal(msgs, [
r"#random-inform",
])
def test_stop_and_restart(self):
"""Test stopping and then restarting a client."""
self.client.stop(timeout=0.1)
# timeout needs to be longer than select sleep.
self.client.join(timeout=1.5)
self.assertEqual(self.client._thread, None)
self.assertFalse(self.client._running.isSet())
self.client.start(timeout=0.1)
def test_is_connected(self):
"""Test is_connected method."""
self.assertTrue(self.client.is_connected())
self.server.stop(timeout=0.1)
# timeout needs to be longer than select sleep.
self.server.join(timeout=1.5)
self.assertFalse(self.client.is_connected())
def test_wait_connected(self):
"""Test wait_connected method."""
start = time.time()
self.assertTrue(self.client.wait_connected(1.0))
self.assertTrue(time.time() - start < 1.0)
self.server.stop(timeout=0.1)
# timeout needs to be longer than select sleep.
self.server.join(timeout=1.5)
start = time.time()
self.assertFalse(self.client.wait_connected(0.2))
self.assertTrue(0.15 < time.time() - start < 0.25)
def test_bad_socket(self):
"""Test what happens when select is called on a dead socket."""
# wait for client to connect
time.sleep(0.1)
# close socket while the client isn't looking
# then wait for the client to notice
sock = self.client._sock
sockname = sock.getpeername()
sock.close()
time.sleep(1.25)
# check that client reconnected
self.assertTrue(sock is not self.client._sock,
"Expected %r to not be %r" % (sock, self.client._sock))
self.assertEqual(sockname, self.client._sock.getpeername())
def test_daemon_value(self):
"""Test passing in a daemon value to client start method."""
self.client.stop(timeout=0.1)
# timeout needs to be longer than select sleep.
self.client.join(timeout=1.5)
self.client.start(timeout=0.1, daemon=True)
self.assertTrue(self.client._thread.isDaemon())
#.........这里部分代码省略.........
示例4: TestCallbackClient
# 需要导入模块: from katcp.testutils import DeviceTestServer [as 别名]
# 或者: from katcp.testutils.DeviceTestServer import running [as 别名]
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.bind_address
self.client = katcp.CallbackClient(host, port)
self.client.start(timeout=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.5))
self.client.callback_request(
Message.request("watchdog"),
reply_cb=watchdog_reply,
)
watchdog_replied.wait(0.5)
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(
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(wraps=self.client.send_message)
# By default message identifiers should be enabled, and should start
# counting at 1
cb = counting_callback(number_of_calls=3)(lambda *x: x)
self.client.callback_request(Message.request('watchdog'), reply_cb=cb)
self.client.callback_request(Message.request('watchdog'), reply_cb=cb)
self.client.callback_request(Message.request('watchdog'), reply_cb=cb)
cb.assert_wait()
# Extract Message object .mid attributes from the mock calls to
# send_message
mids = [args[0].mid # args[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
cb = counting_callback(number_of_calls=1)(lambda *x: x)
self.client.callback_request(
Message.request('watchdog'), use_mid=False, reply_cb=cb)
#.........这里部分代码省略.........
示例5: TestDeviceServerClientIntegrated
# 需要导入模块: from katcp.testutils import DeviceTestServer [as 别名]
# 或者: from katcp.testutils.DeviceTestServer import running [as 别名]
class TestDeviceServerClientIntegrated(unittest.TestCase, TestUtilMixin):
BLACKLIST = ("version-connect", "version", "build-state")
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))
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_log(self):
get_msgs = self.client.message_recorder(
blacklist=self.BLACKLIST,
replies=True)
with mock.patch('katcp.server.time.time') as m_time:
m_time.return_value = 1234
self.server.log.error('An error')
get_msgs.wait_number(1)
self._assert_msgs_equal(
get_msgs(), [r"#log error 1234.000000 root An\_error"])
def test_simple_connect(self):
"""Test a simple server setup and teardown with client connect."""
get_msgs = self.client.message_recorder(
blacklist=self.BLACKLIST,
replies=True)
# basic send
self.client.request(katcp.Message.request("foo"), use_mid=False)
# pipe-lined send
self.client.raw_send("?bar-boom\r\n?baz\r")
# broken up sends
self.client.raw_send("?boo")
self.client.raw_send("m arg1 arg2")
self.client.raw_send("\n")
time.sleep(0.1)
self._assert_msgs_equal(get_msgs(), [
r"!foo invalid Unknown\_request.",
r"!bar-boom invalid Unknown\_request.",
r"!baz invalid Unknown\_request.",
r"!boom invalid Unknown\_request.",
])
def test_bad_requests(self):
"""Test request failure paths in device server."""
get_msgs = self.client.message_recorder(
blacklist=self.BLACKLIST, replies=True)
self.client.raw_send("bad msg\n")
# wait for reply
self.client.blocking_request(
katcp.Message.request("watchdog"), use_mid=False)
self._assert_msgs_like(get_msgs(), [
(r"#log error", "KatcpSyntaxError:"
"\_Bad\_type\_character\_'b'.\\n"),
(r"!watchdog ok", ""),
])
def test_slow_client(self):
# Test that server does not choke sending messages to slow clients
self.server.send_timeout = 0.1 # Set a short sending timeout
self.client.wait_protocol(1)
slow_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
slow_sock.connect(self.server_addr)
slow_sock.settimeout(0.1)
# Send a bunch of request to the server, but don't read anything from
# the server
try:
slow_sock.sendall('?help\n'*100000)
except socket.timeout:
pass
t0 = time.time()
# Request should not have taken a very long time.
self.client.assert_request_succeeds('help', informs_count=NO_HELP_MESSAGES)
self.assertTrue(time.time() - t0 < 1)
def test_server_ignores_informs_and_replies(self):
"""Test server ignores informs and replies."""
get_msgs = self.client.message_recorder(
blacklist=self.BLACKLIST, replies=True)
self.client.raw_send("#some inform\n")
#.........这里部分代码省略.........
示例6: TestCallbackClient
# 需要导入模块: from katcp.testutils import DeviceTestServer [as 别名]
# 或者: from katcp.testutils.DeviceTestServer import running [as 别名]
class TestCallbackClient(unittest.TestCase, TestUtilMixin):
def setUp(self):
self.server = DeviceTestServer('', 0)
self.server.start(timeout=0.1)
host, port = self.server._sock.getsockname()
self.client = CallbackTestClient(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_callback_request(self):
"""Test callback request."""
watchdog_replies = []
def watchdog_reply(reply):
self.assertEqual(reply.name, "watchdog")
self.assertEqual(reply.arguments, ["ok"])
watchdog_replies.append(reply)
self.client.request(
katcp.Message.request("watchdog"),
reply_cb=watchdog_reply,
)
time.sleep(0.1)
self.assertTrue(watchdog_replies)
help_replies = []
help_informs = []
def help_reply(reply):
self.assertEqual(reply.name, "help")
self.assertEqual(reply.arguments, ["ok", "13"])
self.assertEqual(len(help_informs), int(reply.arguments[1]))
help_replies.append(reply)
def help_inform(inform):
self.assertEqual(inform.name, "help")
self.assertEqual(len(inform.arguments), 2)
help_informs.append(inform)
self.client.request(
katcp.Message.request("help"),
reply_cb=help_reply,
inform_cb=help_inform,
)
time.sleep(0.2)
self.assertEqual(len(help_replies), 1)
self.assertEqual(len(help_informs), 13)
def test_no_callback(self):
"""Test request without callback."""
self.client.request(
katcp.Message.request("help")
)
time.sleep(0.1)
msgs = self.client.messages()
self._assert_msgs_like(msgs,
[("#version ", "")] +
[("#build-state ", "")] +
[("#help ", "")]*13 +
[("!help ok 13", "")]
)
def test_timeout(self):
"""Test requests that timeout."""
replies = []
informs = []
timeout = 0.001
def reply_cb(msg):
replies.append(msg)
def inform_cb(msg):
informs.append(msg)
self.client.request(
katcp.Message.request("slow-command", "0.1"),
reply_cb=reply_cb,
inform_cb=inform_cb,
timeout=timeout,
)
time.sleep(0.2)
self.assertEqual(len(replies), 1)
self.assertEqual(len(informs), 0)
#.........这里部分代码省略.........
示例7: TestDeviceServer
# 需要导入模块: from katcp.testutils import DeviceTestServer [as 别名]
# 或者: from katcp.testutils.DeviceTestServer import running [as 别名]
class TestDeviceServer(unittest.TestCase, TestUtilMixin):
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)
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_simple_connect(self):
"""Test a simple server setup and teardown with client connect."""
# basic send
self.client.request(katcp.Message.request("foo"))
# pipe-lined send
self.client.raw_send("?bar-boom\r\n?baz\r")
# broken up sends
self.client.raw_send("?boo")
self.client.raw_send("m arg1 arg2")
self.client.raw_send("\n")
time.sleep(0.1)
msgs = self.client.messages()
self._assert_msgs_equal(msgs, [
r"#version device_stub-0.1",
r"#build-state name-0.1",
r"!foo invalid Unknown\_request.",
r"!bar-boom invalid Unknown\_request.",
r"!baz invalid Unknown\_request.",
r"!boom invalid Unknown\_request.",
])
def test_bad_requests(self):
"""Test request failure paths in device server."""
self.client.raw_send("bad msg\n")
# wait for reply
self.client.blocking_request(katcp.Message.request("watchdog"))
msgs = self.client.messages()
self._assert_msgs_like(msgs, [
(r"#version device_stub-0.1", ""),
(r"#build-state name-0.1", ""),
(r"#log error", "KatcpSyntaxError:\_Bad\_type\_character\_'b'.\\n"),
(r"!watchdog ok", ""),
])
def test_server_ignores_informs_and_replies(self):
"""Test server ignores informs and replies."""
self.client.raw_send("#some inform\n")
self.client.raw_send("!some reply\n")
time.sleep(0.1)
msgs = self.client.messages()
self._assert_msgs_like(msgs, [
(r"#version device_stub-0.1", ""),
(r"#build-state name-0.1", ""),
])
def test_standard_requests(self):
"""Test standard request and replies."""
self.client.request(katcp.Message.request("watchdog"))
self.client.request(katcp.Message.request("restart"))
self.client.request(katcp.Message.request("log-level"))
self.client.request(katcp.Message.request("log-level", "trace"))
self.client.request(katcp.Message.request("log-level", "unknown"))
self.client.request(katcp.Message.request("help"))
self.client.request(katcp.Message.request("help", "watchdog"))
self.client.request(katcp.Message.request("help", "unknown-request"))
self.client.request(katcp.Message.request("client-list"))
self.client.request(katcp.Message.request("sensor-list"))
self.client.request(katcp.Message.request("sensor-list", "an.int"))
self.client.request(katcp.Message.request("sensor-list", "an.unknown"))
self.client.request(katcp.Message.request("sensor-value"))
self.client.request(katcp.Message.request("sensor-value", "an.int"))
self.client.request(katcp.Message.request("sensor-value", "an.unknown"))
self.client.request(katcp.Message.request("sensor-sampling", "an.int"))
self.client.request(katcp.Message.request("sensor-sampling", "an.int", "differential", "2"))
self.client.request(katcp.Message.request("sensor-sampling"))
self.client.request(katcp.Message.request("sensor-sampling", "an.unknown", "auto"))
self.client.blocking_request(katcp.Message.request("sensor-sampling", "an.int", "unknown"))
time.sleep(0.1)
self.server.log.trace("trace-msg")
self.server.log.debug("debug-msg")
self.server.log.info("info-msg")
self.server.log.warn("warn-msg")
self.server.log.error("error-msg")
#.........这里部分代码省略.........