Change rpc server example to use simple restore function
This commit is contained in:
@@ -130,17 +130,9 @@ given address/port ADDRESS may be '*' to bind to all local addresses.\
|
|||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
class CalcRestorer:
|
def restore(ref):
|
||||||
|
assert ref.as_text() == 'calculator'
|
||||||
'''A RPC Restorer. This requires a `restore` function to be defined, and
|
return CalculatorImpl()
|
||||||
will be passed in a SturdyRef by your client'''
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.calc = CalculatorImpl()
|
|
||||||
|
|
||||||
def restore(self, ref):
|
|
||||||
assert ref.as_text() == 'calculator'
|
|
||||||
return CalculatorImpl()
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -169,8 +161,7 @@ def main():
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
(clientsocket, address) = s.accept()
|
(clientsocket, address) = s.accept()
|
||||||
restorer = CalcRestorer()
|
server = capnp.TwoPartyServer(clientsocket, restore)
|
||||||
server = capnp.TwoPartyServer(clientsocket, restorer)
|
|
||||||
|
|
||||||
server.run_forever()
|
server.run_forever()
|
||||||
print("client disconnected")
|
print("client disconnected")
|
||||||
|
|||||||
35
test/test_rpc_calculator.py
Normal file
35
test/test_rpc_calculator.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import capnp
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import gc
|
||||||
|
|
||||||
|
import sys # add examples dir to sys.path
|
||||||
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'examples'))
|
||||||
|
import calculator_client
|
||||||
|
import calculator_server
|
||||||
|
|
||||||
|
|
||||||
|
def test_calculator():
|
||||||
|
read, write = socket.socketpair(socket.AF_UNIX)
|
||||||
|
|
||||||
|
server = capnp.TwoPartyServer(write, calculator_server.restore)
|
||||||
|
calculator_client.main(read)
|
||||||
|
|
||||||
|
|
||||||
|
def test_calculator_gc():
|
||||||
|
def new_evaluate_impl(old_evaluate_impl):
|
||||||
|
def call(*args, **kwargs):
|
||||||
|
gc.collect()
|
||||||
|
return old_evaluate_impl(*args, **kwargs)
|
||||||
|
return call
|
||||||
|
|
||||||
|
read, write = socket.socketpair(socket.AF_UNIX)
|
||||||
|
|
||||||
|
# inject a gc.collect to the beginning of every evaluate_impl call
|
||||||
|
evaluate_impl_orig = calculator_server.evaluate_impl
|
||||||
|
calculator_server.evaluate_impl = new_evaluate_impl(evaluate_impl_orig)
|
||||||
|
|
||||||
|
server = capnp.TwoPartyServer(write, calculator_server.restore)
|
||||||
|
calculator_client.main(read)
|
||||||
|
|
||||||
|
calculator_server.evaluate_impl = evaluate_impl_orig
|
||||||
Reference in New Issue
Block a user