本文整理汇总了Python中ubuntuone.devtools.handlers.MementoHandler.check_warning方法的典型用法代码示例。如果您正苦于以下问题:Python MementoHandler.check_warning方法的具体用法?Python MementoHandler.check_warning怎么用?Python MementoHandler.check_warning使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ubuntuone.devtools.handlers.MementoHandler
的用法示例。
在下文中一共展示了MementoHandler.check_warning方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: OSWrapperTests
# 需要导入模块: from ubuntuone.devtools.handlers import MementoHandler [as 别名]
# 或者: from ubuntuone.devtools.handlers.MementoHandler import check_warning [as 别名]
class OSWrapperTests(test_os_helper.OSWrapperTests):
"""Tests for os wrapper functions."""
@defer.inlineCallbacks
def setUp(self, test_dir_name=None, test_file_name=None,
valid_file_path_builder=None):
"""Set up."""
yield super(OSWrapperTests, self).setUp(
test_dir_name=test_dir_name, test_file_name=test_file_name,
valid_file_path_builder=None)
self.handler = MementoHandler()
self.handler.setLevel(logging.DEBUG)
self._logger = logging.getLogger('ubuntuone.SyncDaemon')
self._logger.addHandler(self.handler)
self.addCleanup(self._logger.removeHandler, self.handler)
self.patch(darwin.shutil, "move", self._fake_move)
def _fake_move(*args):
"""Fake shutil move."""
raise Exception("Fail fake move")
def test_stat_symlink(self):
"""Test that it doesn't follow symlinks.
We compare the inode only (enough to see if it's returning info
from the link or the linked), as we can not compare the full stat
because the st_mode will be different.
"""
link = os.path.join(self.basedir, 'foo')
os.symlink(self.testfile, link)
self.assertNotEqual(os.stat(link).st_ino, stat_path(link).st_ino)
self.assertEqual(os.lstat(link).st_ino, stat_path(link).st_ino)
def test_movetotrash_file_bad(self):
"""Something bad happen when moving to trash, removed anyway."""
path = os.path.join(self.basedir, 'foo')
open_file(path, 'w').close()
move_to_trash(path)
self.assertFalse(os.path.exists(path))
self.assertTrue(self.handler.check_warning("Problems moving to trash!",
"Removing anyway", "foo"))
def test_movetotrash_file_not_exists(self):
"""Something bad happen when moving to trash, removed anyway."""
path = os.path.join(self.basedir, 'foo2')
self.assertFalse(os.path.exists(path))
self.assertRaises(OSError, move_to_trash, path)
def test_movetotrash_dir_bad(self):
"""Something bad happen when moving to trash, removed anyway."""
path = os.path.join(self.basedir, 'foo')
os.mkdir(path)
open_file(os.path.join(path, 'file inside directory'), 'w').close()
move_to_trash(path)
self.assertFalse(os.path.exists(path))
self.assertTrue(self.handler.check_warning("Problems moving to trash!",
"Removing anyway", "foo"))
示例2: test_gsettings_cannot_parse
# 需要导入模块: from ubuntuone.devtools.handlers import MementoHandler [as 别名]
# 或者: from ubuntuone.devtools.handlers.MementoHandler import check_warning [as 别名]
def test_gsettings_cannot_parse(self):
"""Some weird setting that cannot be parsed is logged with warning."""
memento = MementoHandler()
memento.setLevel(logging.DEBUG)
gsettings.logger.addHandler(memento)
self.addCleanup(gsettings.logger.removeHandler, memento)
troublesome_value = "#bang"
template_values = dict(BASE_GSETTINGS_VALUES)
template_values["ignore_hosts"] = troublesome_value
fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
self.patch(gsettings.subprocess, "check_output",
lambda _: fake_output)
ps = gsettings.get_proxy_settings()
self.assertTrue(memento.check_warning(gsettings.CANNOT_PARSE_WARNING %
troublesome_value))
self.assertEqual(ps, {})
示例3: ClientDummyAuthTests
# 需要导入模块: from ubuntuone.devtools.handlers import MementoHandler [as 别名]
# 或者: from ubuntuone.devtools.handlers.MementoHandler import check_warning [as 别名]
class ClientDummyAuthTests(AuthenticationBaseTestCase):
"""Client authentication tests using the dummy auth provider."""
auth_provider_class = DummyAuthProvider
@defer.inlineCallbacks
def setUp(self):
yield super(ClientDummyAuthTests, self).setUp()
self.creds = "open sesame"
self.bad_creds = "not my secret"
self.handler = MementoHandler()
logger = logging.getLogger("storage.server")
logger.addHandler(self.handler)
self.addCleanup(logger.removeHandler, self.handler)
self.handler.setLevel(logging.DEBUG)
def assert_auth_ok_logging(self):
self.assertTrue(self.handler.check_debug("authenticated user", "OK", self.usr0.username))
self.assertFalse(self.handler.check_warning("missing user"))
def assert_auth_ok_missing_user(self):
self.assertTrue(self.handler.check_debug("missing user", "(id=%s)" % self.usr0.id))
self.assertFalse(self.handler.check_info("authenticated user"))
@defer.inlineCallbacks
def test_auth_ok_user_ok(self):
"""Correct authentication must succeed."""
yield self.callback_test(self.do_auth, credentials=self.creds, add_default_callbacks=True)
self.assert_auth_ok_logging()
@defer.inlineCallbacks
def test_auth_ok_bad_user(self):
"""Non existing user must fail authentication."""
# make the user getter fail
self.patch(self.service.factory.content, "get_user_by_id", lambda *a, **k: defer.fail(DoesNotExist()))
d = self.callback_test(self.do_auth, credentials=self.creds, add_default_callbacks=True)
yield self.assertFailure(d, protocol_errors.AuthenticationFailedError)
self.assert_auth_ok_missing_user()
@defer.inlineCallbacks
def test_auth_ok_with_session_id(self):
"""Correct authentication must succeed and include the session_id."""
auth_request = yield self.callback_test(self.do_auth, credentials=self.creds, add_default_callbacks=True)
protocol = self.service.factory.protocols[0]
self.assertEqual(auth_request.session_id, str(protocol.session_id))
@defer.inlineCallbacks
def test_auth_ok_with_metadata(self):
"""Correct authentication must succeed and include metadata."""
m_called = []
self.service.factory.metrics.meter = lambda *a: m_called.append(a)
metadata = {u"platform": u"linux2", u"version": u"1.0", u"foo": u"bar"}
yield self.callback_test(self.do_auth, credentials=self.creds, metadata=metadata, add_default_callbacks=True)
self.assertTrue(self.handler.check_info("Client metadata: %s" % metadata))
self.assertIn(("client.platform.linux2", 1), m_called)
self.assertIn(("client.version.1_0", 1), m_called)
self.assertNotIn(("client.foo.bar", 1), m_called)
def test_auth_fail(self):
"""Wrong secret must fail."""
def test(client, **kwargs):
d = self.do_auth(client, credentials=self.bad_creds)
d.addCallbacks(
lambda _: client.test_fail(Exception("Should not succeed.")), lambda _: client.test_done("ok")
)
return self.callback_test(test)
def test_get_root(self):
"""Must receive the root after authentication."""
@defer.inlineCallbacks
def test(client, **kwargs):
yield self.do_auth(client, credentials=self.creds)
root_id = yield client.get_root()
self.assertIsNotNone(root_id)
return self.callback_test(test, add_default_callbacks=True)
def test_get_root_twice(self):
"""Get root must keep the root id."""
@defer.inlineCallbacks
def test(client, **kwargs):
yield self.do_auth(client, credentials=self.creds)
root_id1 = yield client.get_root()
root_id2 = yield client.get_root()
self.assertEqual(root_id1, root_id2)
return self.callback_test(test, add_default_callbacks=True)
def test_user_becomes_inactive(self):
"""After StorageUser authentication ok it becomes inactive."""
#.........这里部分代码省略.........
示例4: HeartbeatWriterTest
# 需要导入模块: from ubuntuone.devtools.handlers import MementoHandler [as 别名]
# 或者: from ubuntuone.devtools.handlers.MementoHandler import check_warning [as 别名]
class HeartbeatWriterTest(TwistedTestCase):
"""Tests for HeartbeatWriter."""
interval = 5
@defer.inlineCallbacks
def setUp(self):
yield super(HeartbeatWriterTest, self).setUp()
self.logger = logging.Logger("HeartbeatWriter.test")
self.handler = MementoHandler()
self.logger.addHandler(self.handler)
self.addCleanup(self.logger.removeHandler, self.handler)
self.clock = task.Clock()
self.hw = HeartbeatWriter(self.interval, self.logger,
reactor=self.clock)
def test_send_no_transport(self):
"""Log a warning when there is no transport."""
self.hw.send()
self.assertTrue(self.handler.check_warning(
"Can't send heartbeat without a transport"))
def test_send_loop(self):
"""Send heartbeats in the LoopingCall."""
# first connect to something
transport = StringIO()
self.clock.advance(2)
self.hw.makeConnection(transport)
self.clock.advance(5)
self.clock.advance(5)
# we should have 3 heartbeats in the transport, get them
raw_events = transport.getvalue().split(BEGIN_TOKEN, 3)
events = []
for raw_event in raw_events:
if raw_event:
events.append(json.loads(raw_event.strip(END_TOKEN)))
# strip the tokens
for i, timestamp in [(0, 2), (1, 7), (2, 12)]:
self.assertEqual(events[i]['type'], "heartbeat")
self.assertEqual(events[i]['time'], timestamp)
def test_send_on_connectionMade(self):
"""On connectionMade start the loop and send."""
# first connect to something
transport = StringIO()
self.clock.advance(0.1)
self.hw.makeConnection(transport)
self.assertTrue(self.hw.loop.running)
raw_event = transport.getvalue()
self.assertTrue(raw_event.startswith(BEGIN_TOKEN))
self.assertTrue(raw_event.endswith(END_TOKEN))
# strip the tokens
payload = json.loads(raw_event.strip(BEGIN_TOKEN).strip(END_TOKEN))
self.assertEqual(payload['type'], "heartbeat")
self.assertEqual(payload['time'], self.clock.seconds())
def test_connectionLost(self):
"""On connectionLost cleanup everything."""
self.hw.makeConnection(None)
called = []
self.patch(self.hw.loop, 'stop', lambda: called.append(True))
self.hw.connectionLost(protocol.connectionDone)
self.assertTrue(self.handler.check_info(
"HeartbeatWriter connectionLost: %s" % (protocol.connectionDone,)))
self.assertTrue(called)
self.assertEqual(self.hw.loop, None)
self.assertEqual(self.hw.reactor, None)
self.assertEqual(self.hw.logger, None)
示例5: OSWrapperTests
# 需要导入模块: from ubuntuone.devtools.handlers import MementoHandler [as 别名]
# 或者: from ubuntuone.devtools.handlers.MementoHandler import check_warning [as 别名]
class OSWrapperTests(test_os_helper.OSWrapperTests):
"""Tests for os wrapper functions."""
@defer.inlineCallbacks
def setUp(self):
"""Set up."""
yield super(OSWrapperTests, self).setUp()
self.handler = MementoHandler()
self.handler.setLevel(logging.DEBUG)
self._logger = logging.getLogger('ubuntuone.SyncDaemon')
self._logger.addHandler(self.handler)
self.addCleanup(self._logger.removeHandler, self.handler)
def test_stat_symlink(self):
"""Test that it doesn't follow symlinks.
We compare the inode only (enough to see if it's returning info
from the link or the linked), as we can not compare the full stat
because the st_mode will be different.
"""
link = os.path.join(self.basedir, 'foo')
os.symlink(self.testfile, link)
self.assertNotEqual(os.stat(link).st_ino, stat_path(link).st_ino)
self.assertEqual(os.lstat(link).st_ino, stat_path(link).st_ino)
def test_movetotrash_file_bad(self):
"""Something bad happen when moving to trash, removed anyway."""
FakeGIOFile._bad_trash_call = False # error
self.patch(gio, "File", FakeGIOFile)
path = os.path.join(self.basedir, 'foo')
open_file(path, 'w').close()
move_to_trash(path)
self.assertFalse(os.path.exists(path))
self.assertTrue(self.handler.check_warning("Problems moving to trash!",
"Removing anyway", "foo"))
def test_movetotrash_dir_bad(self):
"""Something bad happen when moving to trash, removed anyway."""
FakeGIOFile._bad_trash_call = False # error
self.patch(gio, "File", FakeGIOFile)
path = os.path.join(self.basedir, 'foo')
os.mkdir(path)
open_file(os.path.join(path, 'file inside directory'), 'w').close()
move_to_trash(path)
self.assertFalse(os.path.exists(path))
self.assertTrue(self.handler.check_warning("Problems moving to trash!",
"Removing anyway", "foo"))
def test_movetotrash_file_systemnotcapable(self):
"""The system is not capable of moving into trash."""
FakeGIOFile._bad_trash_call = GIO_NOT_SUPPORTED
self.patch(gio, "File", FakeGIOFile)
path = os.path.join(self.basedir, 'foo')
open_file(path, 'w').close()
move_to_trash(path)
self.assertFalse(os.path.exists(path))
self.assertTrue(self.handler.check_warning("Problems moving to trash!",
"Removing anyway", "foo",
"ERROR_NOT_SUPPORTED"))
def test_movetotrash_dir_systemnotcapable(self):
"""The system is not capable of moving into trash."""
FakeGIOFile._bad_trash_call = GIO_NOT_SUPPORTED
self.patch(gio, "File", FakeGIOFile)
path = os.path.join(self.basedir, 'foo')
os.mkdir(path)
open_file(os.path.join(path, 'file inside directory'), 'w').close()
move_to_trash(path)
self.assertFalse(os.path.exists(path))
self.assertTrue(self.handler.check_warning("Problems moving to trash!",
"Removing anyway", "foo",
"ERROR_NOT_SUPPORTED"))
示例6: HeartbeatListenerTestCase
# 需要导入模块: from ubuntuone.devtools.handlers import MementoHandler [as 别名]
# 或者: from ubuntuone.devtools.handlers.MementoHandler import check_warning [as 别名]
#.........这里部分代码省略.........
state=RUNNING))
self.listener.processes = ['bar']
# 2 process to restart
self.listener.data['foo'] = {
'time': time.time() - (self.listener.timeout + 2)}
self.listener.data['bar'] = {
'time': time.time() - (self.listener.timeout + 3)}
self.listener.data['p-1'] = {
'time': time.time() - (self.listener.timeout - 1)}
expect(self.rpc.supervisor.getAllProcessInfo()).result(self.processes)
expect(self.rpc.supervisor.stopProcess("foo:"))
expect(self.rpc.supervisor.startProcess("foo:"))
expect(self.rpc.supervisor.stopProcess("bar:bar"))
expect(self.rpc.supervisor.startProcess("bar:bar"))
with self.mocker:
self.listener.check_processes()
def test_check_processes_no_data(self):
"""Test the check_processes method with no data of a process."""
# add the fake process to the process list
self.processes.append(dict(name="foo", group="foo", pid="42",
state=RUNNING))
self.processes.append(dict(name="bar", group="bar", pid="43",
state=RUNNING))
self.listener.processes = ['bar']
expect(self.rpc.supervisor.getAllProcessInfo()).result(self.processes)
expect(self.rpc.supervisor.stopProcess("foo:"))
expect(self.rpc.supervisor.startProcess("foo:"))
expect(self.rpc.supervisor.stopProcess("bar:bar"))
expect(self.rpc.supervisor.startProcess("bar:bar"))
with self.mocker:
# one process to restart
self.listener.check_processes()
self.assertTrue(self.handler.check_warning(
"Restarting process foo:foo (42), as we never received a hearbeat"
" event from it"))
self.assertTrue(self.handler.check_warning(
"Restarting process bar:bar (43), as we never received a hearbeat"
" event from it"))
def test_check_processes_untracked(self):
"""Test the check_processes method with a untracked proccess."""
# add the fake process to the process list
self.processes.append(dict(name="foo-untracked", group="untracked",
pid="43", state=RUNNING))
# add a new tracked process from an untracked group
self.processes.append(dict(name="bar-untracked", group="bar", pid="44",
state=RUNNING))
self.listener.processes = ['bar']
expect(self.rpc.supervisor.getAllProcessInfo()).result(self.processes)
with self.mocker:
self.listener.check_processes()
self.assertTrue(self.handler.check_info(
"Ignoring untracked:foo-untracked (43) as isn't tracked."))
self.assertTrue(self.handler.check_info(
"Ignoring bar:bar-untracked (44) as isn't tracked."))
def test_check_processes_not_running(self):
"""Test the check_processes method if the proccess isn't running."""
# add the fake process to the process list
self.processes.append(dict(name="foo", group="foo", pid="42",
state=states.ProcessStates.STARTING))
# add a new tracked process from an untracked group
self.processes.append(dict(name="bar", group="bar", pid="43",
state=states.ProcessStates.STARTING))
self.listener.processes = ['bar']
示例7: SignalBroadcasterTestCase
# 需要导入模块: from ubuntuone.devtools.handlers import MementoHandler [as 别名]
# 或者: from ubuntuone.devtools.handlers.MementoHandler import check_warning [as 别名]
class SignalBroadcasterTestCase(TestCase):
"""Test the signal broadcaster code."""
@defer.inlineCallbacks
def setUp(self):
yield super(SignalBroadcasterTestCase, self).setUp()
self.client = FakeRemoteClient()
self.sb = ipc.SignalBroadcaster()
self.memento = MementoHandler()
ipc.logger.addHandler(self.memento)
ipc.logger.setLevel(logging.DEBUG)
self.addCleanup(ipc.logger.removeHandler, self.memento)
def test_remote_register_to_signals(self):
"""Assert that the client was added."""
signals = ["demo_signal1", "demo_signal2"]
self.sb.remote_register_to_signals(self.client, signals)
for signal in signals:
clients = self.sb.clients_per_signal[signal]
self.assertTrue(self.client in clients)
def test_emit_signal(self):
"""Assert that the client method was called."""
first = 1
second = 2
word = 'word'
signal_name = 'on_test'
self.client.callRemote(signal_name, first, second, word=word)
signals = [signal_name]
self.sb.remote_register_to_signals(self.client, signals)
self.sb.emit_signal(signal_name, first, second, foo=word)
self.assertEqual(self.client.called, ((first, second), dict(foo=word)))
def test_emit_signal_dead_reference(self):
"""Test dead reference while emitting a signal."""
sample_signal = "sample_signal"
fake_remote_client = FakeRemoteClient(dead_remote=True)
self.sb.remote_register_to_signals(fake_remote_client, [sample_signal])
self.assertIn(fake_remote_client,
self.sb.clients_per_signal[sample_signal])
self.sb.emit_signal(sample_signal)
self.assertNotIn(fake_remote_client,
self.sb.clients_per_signal[sample_signal])
def test_emit_signal_some_dead_some_not(self):
"""Test a clean reference after a dead one."""
sample_signal = "sample_signal"
fake_dead_remote = FakeRemoteClient(dead_remote=True)
fake_alive_remote = FakeRemoteClient()
self.sb.remote_register_to_signals(fake_dead_remote, [sample_signal])
self.sb.remote_register_to_signals(fake_alive_remote, [sample_signal])
self.sb.emit_signal(sample_signal)
self.assertTrue(fake_alive_remote.called, "The alive must be called.")
def test_emit_signal_ignore_missing_handlers(self):
"""A missing signal handler should just log a debug line."""
fake_remote_client = FakeRemoteClient()
signal = fake_remote_client.missing_signal
signals = [signal]
self.sb.remote_register_to_signals(fake_remote_client, signals)
sb_clients = self.sb.clients_per_signal[signal]
self.assertIn(fake_remote_client, sb_clients)
self.sb.emit_signal(signal)
expected = ipc.SignalBroadcaster.MSG_NO_SIGNAL_HANDLER % (
signal,
fake_remote_client,
)
self.assertTrue(self.memento.check_debug(*expected))
def test_emit_signal_log_other_errors(self):
"""Other errors should be logged as warnings."""
fake_remote_client = FakeRemoteClient()
signal = fake_remote_client.failing_signal
signals = [signal]
self.sb.remote_register_to_signals(fake_remote_client, signals)
sb_clients = self.sb.clients_per_signal[signal]
self.assertIn(fake_remote_client, sb_clients)
self.sb.emit_signal(signal)
expected = ipc.SignalBroadcaster.MSG_COULD_NOT_EMIT_SIGNAL % (
signal,
fake_remote_client,
fake_remote_client.random_exception,
)
self.assertTrue(self.memento.check_warning(*expected))
示例8: NodeStructureTestCase
# 需要导入模块: from ubuntuone.devtools.handlers import MementoHandler [as 别名]
# 或者: from ubuntuone.devtools.handlers.MementoHandler import check_warning [as 别名]
#.........这里部分代码省略.........
self.qc.set_content([('Unlink', '89', {'path': '/a/b/foo/fighters'})])
node = self.qc._node_ops[''].children['a']
node = node.children['b']
self.assertTrue(isinstance(node.last_modified, float))
self.assertEqual(node.kind, KIND_DIR)
expected = [('12', 'MakeDir', {'path': '/a/b', '__done__': False})]
self.assertEqual(node.operations, expected)
self.assertEqual(node.done, False)
self.assertEqual(len(node.children), 2)
node = node.children['foo']
self.assertTrue(isinstance(node.last_modified, float))
self.assertEqual(node.kind, KIND_DIR)
expected = [('34', 'MakeDir', {'path': '/a/b/foo', '__done__': False})]
self.assertEqual(node.operations, expected)
self.assertEqual(node.done, False)
self.assertEqual(len(node.children), 1)
node = node.children['fighters']
self.assertTrue(isinstance(node.last_modified, float))
self.assertEqual(node.kind, KIND_FILE)
p = '/a/b/foo/fighters'
expected = [('67', 'MakeFile', {'path': p, '__done__': False}),
('89', 'Unlink', {'path': p, '__done__': False})]
self.assertEqual(node.operations, expected)
self.assertEqual(node.done, False)
self.assertEqual(len(node.children), 0)
def test_finishing_nothing(self):
"""Finish something that is not there."""
r = self.qc.remove('MakeDir', '34', {'path': '/a/bar'})
self.assertEqual(r, None)
self.assertTrue(self.handler.check_warning(
"Element ''", "['', 'a', 'bar']", 'not in children'))
def test_operation_error_nothing(self):
"""Finish an operation that is not there."""
# create a node and break it on purpose
self.qc.add('MakeDir', '12', {'path': '/a'})
self.assertEqual(len(self.qc._node_ops), 1)
node = self.qc._node_ops[''].children['a']
node.operations = []
# remove the operation and check
r = self.qc.remove('MakeDir', '12', {'path': '/a'})
self.assertEqual(r, None)
self.assertTrue(self.handler.check_error(
"found 0 times", "MakeDir", "12"))
def test_operation_error_several(self):
"""Finish an operation that is more than once."""
# create a node and break it on purpose
self.qc.add('MakeDir', '12', {'path': '/a'})
self.assertEqual(len(self.qc._node_ops), 1)
node = self.qc._node_ops[''].children['a']
node.operations = node.operations * 2
# remove the operation and check
r = self.qc.remove('MakeDir', '12', {'path': '/a'})
self.assertEqual(r, None)
self.assertTrue(self.handler.check_error(
"found 2 times", "MakeDir", "12"))
def test_two_ops_finishing_one(self):
"""Add some nodes with different combinations."""
示例9: ReactorInspectorTestCase
# 需要导入模块: from ubuntuone.devtools.handlers import MementoHandler [as 别名]
# 或者: from ubuntuone.devtools.handlers.MementoHandler import check_warning [as 别名]
#.........这里部分代码省略.........
# Reactor will stop after call_count calls, thanks to helper
if join:
self.ri.join()
def test_stop(self):
"""It stops."""
self.run_ri(1000, join=False)
assert self.ri.is_alive()
self.ri.stop()
self.ri.join()
self.assertFalse(self.ri.is_alive())
@defer.inlineCallbacks
def test_dump_frames(self):
"""Test how frames are dumped.
Rules:
- own frame must not be logged
- must log all other threads
- main reactor thread must have special title
"""
# other thread, whose frame must be logged
waitingd = defer.Deferred()
def waiting_function():
"""Function with funny name to be checked later."""
reactor.callFromThread(waitingd.callback, True)
# wait have a default value
event.wait()
event = threading.Event()
threading.Thread(target=waiting_function).start()
# Make sure the thread has entered the waiting_function
yield waitingd
# Set reactor_thread since we're not starting the ReactorInspector
# thread here.
self.ri.reactor_thread = threading.currentThread().ident
# dump frames in other thread, also
def dumping_function():
"""Function with funny name to be checked later."""
time.sleep(.1)
self.ri.dump_frames()
reactor.callFromThread(d.callback, True)
d = defer.Deferred()
threading.Thread(target=dumping_function).start()
yield d
event.set()
# check
self.assertFalse(self.handler.check_debug("dumping_function"))
self.assertTrue(self.handler.check_debug("Dumping Python frame",
"waiting_function"))
self.assertTrue(self.handler.check_debug("Dumping Python frame",
"reactor main thread"))
def test_reactor_ok(self):
"""Reactor working fast."""
self.run_ri()
ok_line = self.handler.check(TRACE, "ReactorInspector: ok")
self.assertTrue(ok_line)
self.assertTrue(ok_line.args[-1] >= 0) # Should be near zero delay
# Check the metrics
expected_metric = ("gauge", "delay", ok_line.args[-1])
self.assertEqual([expected_metric], self.fake_metrics.calls)
self.assertTrue(self.ri.last_responsive_ts >= self.start_ts)
@defer.inlineCallbacks
def test_reactor_blocked(self):
"""Reactor not working fast."""
dump_frames_called = defer.Deferred()
self.ri.dump_frames = lambda: dump_frames_called.callback(True)
self.run_ri(0)
yield dump_frames_called
log_line = self.handler.check(logging.CRITICAL, "ReactorInspector",
"detected unresponsive")
self.assertTrue(log_line)
self.assertTrue(log_line.args[-1] >= .1) # waited for entire loop time
# Check the metrics
expected_metric = ("gauge", "delay", log_line.args[-1])
self.assertEqual([expected_metric], self.fake_metrics.calls)
self.assertTrue(self.ri.last_responsive_ts < self.start_ts)
def test_reactor_back_alive(self):
"""Reactor resurrects after some loops."""
self.run_ri(3)
late_line = self.handler.check_warning("ReactorInspector: late",
"got: 0")
self.assertTrue(late_line)
self.assertTrue(late_line.args[-1] >= .2) # At least 2 cycles of delay
# Check the metrics
expected_metric = ("gauge", "delay", late_line.args[-1])
self.assertEqual(expected_metric, self.fake_metrics.calls[-1])
self.assertTrue(self.ri.queue.empty())
# A late reactor is not considered responsive (until a successful loop)
self.assertTrue(self.ri.last_responsive_ts < self.start_ts)
示例10: OffloadQueueTestCase
# 需要导入模块: from ubuntuone.devtools.handlers import MementoHandler [as 别名]
# 或者: from ubuntuone.devtools.handlers.MementoHandler import check_warning [as 别名]
#.........这里部分代码省略.........
results.append(self.oq.pop())
# push the rest of the data, it should rotate at some point
for d in data[1:]:
self.oq.push(d)
assert self.oq._tempfile != orig_temp
# pop everything and compare
while len(self.oq):
results.append(self.oq.pop())
self.assertEqual(data, results)
def test_rotate_removes_old_file(self):
"""Rotation should start a new file and remove the previous one."""
data, item_size = self._get_data()
self.oq._rotation_soft_limit = item_size * 2.5
orig_fname = self.oq._tempfile_name
self.oq.push(data)
self.oq.pop()
self.oq.push(data)
self.oq.push(data)
self.assertFalse(os.path.exists(orig_fname))
def test_log_init_tempfile(self):
"""Log the initial temp file used."""
self.assertTrue(self.handler.check_debug("Using temporary file", repr(self.oq._tempfile_name)))
def test_log_rotate(self):
"""Log new file in rotation."""
data, item_size = self._get_data()
self.oq._rotation_soft_limit = item_size * 2.5
self.oq.push(data)
self.oq.pop()
self.oq.push(data)
self.oq.push(data)
self.assertTrue(self.handler.check_debug("Rotation into", "moving", repr(self.oq._tempfile_name)))
def test_safe_rotate_crash(self):
"""All is ok even after rotation crashes when getting temp file."""
def crash(*a):
"""Will crash."""
raise NameError("ugly")
self.patch(tempfile, "mkstemp", crash)
# do a lot of things, rotating in the middle, checking all is ok
self.test_rotate_keep_working()
self.assertTrue(self.handler.check_exception(NameError))
self.assertTrue(self.oq._in_memory)
def test_safe_rotate_unlink(self):
"""All is ok after failing to unlink old file."""
def crash(*a):
"""Will crash."""
raise NameError("ugly")
self.patch(os, "unlink", crash)
# do a lot of things, rotating in the middle, checking all is ok
self.test_rotate_keep_working()
self.assertTrue(self.handler.check_warning("Error when removing old tempfile", "NameError"))
def _test_safe_push_write(self, count):
"""Fail when pushing an item will leave it all ok."""
class CrashingFile(StringIO.StringIO):
"""File-like object that crashes in second write."""
def __init__(self):
self._fail_counter = 0
StringIO.StringIO.__init__(self)
def write(self, *a):
"""Crashing write."""
self._fail_counter += 1
if self._fail_counter == count:
raise ValueError("broken")
else:
StringIO.StringIO.write(self, *a)
self.oq._tempfile = CrashingFile()
# will try three items, checking all is ok
self.test_fifo_mixed()
self.assertTrue(self.handler.check_exception(ValueError))
self.assertTrue(self.oq._in_memory)
def test_safe_push_write_first(self):
"""Fail when pushing an item, on first write."""
self._test_safe_push_write(1)
def test_safe_push_write_second(self):
"""Fail when pushing an item, on second write."""
self._test_safe_push_write(2)