2018-10-26 12:55:25 +00:00
|
|
|
|
2019-03-09 00:21:25 +00:00
|
|
|
import time
|
2018-10-26 12:55:25 +00:00
|
|
|
import threading
|
|
|
|
|
2018-11-03 12:39:29 +00:00
|
|
|
import mock
|
2018-10-26 12:55:25 +00:00
|
|
|
import unittest2
|
|
|
|
|
|
|
|
import testlib
|
|
|
|
|
|
|
|
import mitogen.core
|
|
|
|
|
|
|
|
|
2018-11-03 12:39:29 +00:00
|
|
|
class ShutdownTest(testlib.TestCase):
|
|
|
|
klass = mitogen.core.Broker
|
|
|
|
|
|
|
|
def test_poller_closed(self):
|
|
|
|
broker = self.klass()
|
|
|
|
actual_close = broker.poller.close
|
|
|
|
broker.poller.close = mock.Mock()
|
|
|
|
broker.shutdown()
|
|
|
|
broker.join()
|
|
|
|
self.assertEquals(1, len(broker.poller.close.mock_calls))
|
|
|
|
actual_close()
|
|
|
|
|
|
|
|
|
2018-12-14 05:30:20 +00:00
|
|
|
class DeferTest(testlib.TestCase):
|
|
|
|
klass = mitogen.core.Broker
|
|
|
|
|
|
|
|
def test_defer(self):
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
broker = self.klass()
|
|
|
|
try:
|
|
|
|
broker.defer(lambda: latch.put(123))
|
|
|
|
self.assertEquals(123, latch.get())
|
|
|
|
finally:
|
|
|
|
broker.shutdown()
|
|
|
|
broker.join()
|
|
|
|
|
|
|
|
def test_defer_after_shutdown(self):
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
broker = self.klass()
|
|
|
|
broker.shutdown()
|
|
|
|
broker.join()
|
|
|
|
|
|
|
|
e = self.assertRaises(mitogen.core.Error,
|
|
|
|
lambda: broker.defer(lambda: latch.put(123)))
|
2018-12-14 14:49:34 +00:00
|
|
|
self.assertEquals(e.args[0], mitogen.core.Waker.broker_shutdown_msg)
|
2018-12-14 05:30:20 +00:00
|
|
|
|
|
|
|
|
2018-10-26 12:55:25 +00:00
|
|
|
class DeferSyncTest(testlib.TestCase):
|
|
|
|
klass = mitogen.core.Broker
|
|
|
|
|
|
|
|
def test_okay(self):
|
|
|
|
broker = self.klass()
|
|
|
|
try:
|
|
|
|
th = broker.defer_sync(lambda: threading.currentThread())
|
|
|
|
self.assertEquals(th, broker._thread)
|
|
|
|
finally:
|
|
|
|
broker.shutdown()
|
2018-11-03 20:55:30 +00:00
|
|
|
broker.join()
|
2018-10-26 12:55:25 +00:00
|
|
|
|
|
|
|
def test_exception(self):
|
|
|
|
broker = self.klass()
|
|
|
|
try:
|
|
|
|
self.assertRaises(ValueError,
|
|
|
|
broker.defer_sync, lambda: int('dave'))
|
|
|
|
finally:
|
|
|
|
broker.shutdown()
|
2018-11-03 20:55:30 +00:00
|
|
|
broker.join()
|
2018-10-26 12:55:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest2.main()
|