def test_dump(self, stdout, stderr): fh = MyBytesIO() utils.emergency_dump_state({'foo': 'bar'}, open_file=lambda n, m: fh) self.assertDictEqual(pickle.loads(fh.getvalue()), {'foo': 'bar'}) self.assertTrue(stderr.getvalue()) self.assertFalse(stdout.getvalue())
def restore_unacked_once(self): """Restores all uncknowledged message at shutdown/gc collect. Will only be done once for each instance. """ self._on_collect.cancel() self._flush() state = self._delivered if not self.channel.do_restore or getattr(state, "restored"): assert not state return try: if state: say("Restoring unacknowledged messages: %s", state) unrestored = self.restore_unacked() if unrestored: errors, messages = zip(*unrestored) say("UNABLE TO RESTORE %s MESSAGES: %s", len(errors), errors) emergency_dump_state(messages) finally: state.restored = True
def restore_unacked_once(self, stderr=None): """Restores all unacknowledged messages at shutdown/gc collect. Will only be done once for each instance. """ self._on_collect.cancel() self._flush() stderr = sys.stderr if stderr is None else stderr state = self._delivered if not self.restore_at_shutdown or not self.channel.do_restore: return if getattr(state, 'restored', None): assert not state return try: if state: print(RESTORING_FMT.format(len(self._delivered)), file=stderr) unrestored = self.restore_unacked() if unrestored: errors, messages = list(zip(*unrestored)) print(RESTORE_PANIC_FMT.format(len(errors), errors), file=stderr) emergency_dump_state(messages, stderr=stderr) finally: state.restored = True
def restore_unacked_once(self): """Restores all unacknowledged messages at shutdown/gc collect. Will only be done once for each instance. """ self._on_collect.cancel() self._flush() state = self._delivered if not self.restore_at_shutdown or not self.channel.do_restore: return if getattr(state, 'restored', None): assert not state return try: if state: say('Restoring {0!r} unacknowledged message(s).', len(self._delivered)) unrestored = self.restore_unacked() if unrestored: errors, messages = list(zip(*unrestored)) say('UNABLE TO RESTORE {0} MESSAGES: {1}', len(errors), errors) emergency_dump_state(messages) finally: state.restored = True
def test_dump(self, stdout, stderr): fh = MyStringIO() utils.emergency_dump_state({"foo": "bar"}, open_file=lambda n, m: fh) self.assertDictEqual(pickle.loads(fh.getvalue()), {"foo": "bar"}) self.assertTrue(stderr.getvalue()) self.assertFalse(stdout.getvalue())
def test_dump_second_strategy(self, stdout, stderr): fh = MyStringIO() def raise_something(*args, **kwargs): raise KeyError('foo') utils.emergency_dump_state({'foo': 'bar'}, open_file=lambda n, m: fh, dump=raise_something) self.assertIn("'foo': 'bar'", fh.getvalue()) self.assertTrue(stderr.getvalue()) self.assertFalse(stdout.getvalue())
def test_dump_second_strategy(self, stdout, stderr): fh = MyStringIO() def raise_something(*args, **kwargs): raise KeyError("foo") utils.emergency_dump_state({"foo": "bar"}, open_file=lambda n, m: fh, dump=raise_something) self.assertIn("'foo': 'bar'", fh.getvalue()) self.assertTrue(stderr.getvalue()) self.assertFalse(stdout.getvalue())
def test_dump_second_strategy(self, stdout, stderr): fh = MyStringIO() def raise_something(*args, **kwargs): raise KeyError('foo') utils.emergency_dump_state({'foo': 'bar'}, open_file=lambda n, m: fh, dump=raise_something) # replace at the end removes u' from repr on Py2 self.assertIn("'foo': 'bar'", fh.getvalue().replace("u'", "'")) self.assertTrue(stderr.getvalue()) self.assertFalse(stdout.getvalue())