This allows e.g. utils.utils.run_async to offload long-running functions to a completely different subprocess entirely, offering real parallelism. Implementation is still experimental, notably not all objects can be transferred safely across the wire; also there is no concept of updating caches yet - so adding an object from the subprocess side will not be known in the main thread yet (since caches cannot yet tell the underlying database has changed).
49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
from twisted.internet import defer, reactor
|
|
from twisted.internet.protocol import ClientFactory
|
|
from twisted.trial import unittest
|
|
from twisted.protocols import amp
|
|
|
|
from src.utils.ampoule import service, child, pool, main
|
|
from src.utils.ampoule.commands import Echo
|
|
|
|
class ClientAMP(amp.AMP):
|
|
factory = None
|
|
def connectionMade(self):
|
|
if self.factory is not None:
|
|
self.factory.theProto = self
|
|
if hasattr(self.factory, 'onMade'):
|
|
self.factory.onMade.callback(None)
|
|
|
|
class TestAMPProxy(unittest.TestCase):
|
|
def setUp(self):
|
|
"""
|
|
Setup the proxy service and the client connection to the proxy
|
|
service in order to run call through them.
|
|
|
|
Inspiration comes from twisted.test.test_amp
|
|
"""
|
|
self.pp = pool.ProcessPool()
|
|
self.svc = service.AMPouleService(self.pp, child.AMPChild, 0, "")
|
|
self.svc.startService()
|
|
self.proxy_port = self.svc.server.getHost().port
|
|
self.clientFactory = ClientFactory()
|
|
self.clientFactory.protocol = ClientAMP
|
|
d = self.clientFactory.onMade = defer.Deferred()
|
|
self.clientConn = reactor.connectTCP("127.0.0.1",
|
|
self.proxy_port,
|
|
self.clientFactory)
|
|
self.addCleanup(self.clientConn.disconnect)
|
|
self.addCleanup(self.svc.stopService)
|
|
def setClient(_):
|
|
self.client = self.clientFactory.theProto
|
|
return d.addCallback(setClient)
|
|
|
|
def test_forwardCall(self):
|
|
"""
|
|
Test that a call made from a client is correctly forwarded to
|
|
the process pool and the result is correctly reported.
|
|
"""
|
|
DATA = "hello"
|
|
return self.client.callRemote(Echo, data=DATA).addCallback(
|
|
self.assertEquals, {'response': DATA}
|
|
)
|