2019-02-18 17:50:28 +00:00
|
|
|
import testlib
|
|
|
|
|
|
|
|
import mitogen.os_fork
|
|
|
|
import mitogen.service
|
|
|
|
|
|
|
|
|
|
|
|
class CorkTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
klass = mitogen.os_fork.Corker
|
|
|
|
|
|
|
|
def ping(self, latch):
|
|
|
|
latch.put('pong')
|
|
|
|
|
|
|
|
def test_cork_broker(self):
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
self.broker.defer(self.ping, latch)
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual('pong', latch.get())
|
2019-02-18 17:50:28 +00:00
|
|
|
|
|
|
|
corker = self.klass(brokers=(self.broker,))
|
|
|
|
corker.cork()
|
|
|
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
self.broker.defer(self.ping, latch)
|
|
|
|
self.assertRaises(mitogen.core.TimeoutError,
|
|
|
|
lambda: latch.get(timeout=0.5))
|
|
|
|
corker.uncork()
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual('pong', latch.get())
|
2019-02-18 17:50:28 +00:00
|
|
|
|
|
|
|
def test_cork_pool(self):
|
|
|
|
pool = mitogen.service.Pool(self.router, services=(), size=4)
|
|
|
|
try:
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
pool.defer(self.ping, latch)
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual('pong', latch.get())
|
2019-02-18 17:50:28 +00:00
|
|
|
|
|
|
|
corker = self.klass(pools=(pool,))
|
|
|
|
corker.cork()
|
|
|
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
pool.defer(self.ping, latch)
|
|
|
|
self.assertRaises(mitogen.core.TimeoutError,
|
|
|
|
lambda: latch.get(timeout=0.5))
|
|
|
|
corker.uncork()
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual('pong', latch.get())
|
2019-02-18 17:50:28 +00:00
|
|
|
finally:
|
|
|
|
pool.stop(join=True)
|