@@ -357,6 +357,13 @@ def func():
357
357
with self .assertRaises (raises [i ]):
358
358
func ()
359
359
360
+ def test_proxy_object_reuse_is_allowed (self ):
361
+ client = self .get_client ()
362
+ sub_service_proxy = client .sub_service
363
+ result = sub_service_proxy .subtract (5 , 10 )
364
+ self .assertTrue (result == - 5 )
365
+ result = sub_service_proxy .add (21 , 21 )
366
+ self .assertTrue (result == 42 )
360
367
361
368
if jsonrpc .USE_UNIX_SOCKETS :
362
369
# We won't do these tests unless Unix Sockets are supported
@@ -412,36 +419,43 @@ def tearDown(self):
412
419
jsonrpc .USE_UNIX_SOCKETS = self .original_value
413
420
414
421
415
- """ Test Methods """
416
-
417
-
418
- def subtract (minuend , subtrahend ):
419
- """ Using the keywords from the JSON-RPC v2 doc """
420
- return minuend - subtrahend
421
-
422
-
423
- def add (x , y ):
424
- return x + y
422
+ class ExampleService (object ):
423
+ @staticmethod
424
+ def subtract (minuend , subtrahend ):
425
+ """ Using the keywords from the JSON-RPC v2 doc """
426
+ return minuend - subtrahend
425
427
428
+ @staticmethod
429
+ def add (x , y ):
430
+ return x + y
426
431
427
- def update (* args ):
428
- return args
432
+ @staticmethod
433
+ def update (* args ):
434
+ return args
429
435
436
+ @staticmethod
437
+ def summation (* args ):
438
+ return sum (args )
430
439
431
- def summation (* args ):
432
- return sum (args )
440
+ @staticmethod
441
+ def notify_hello (* args ):
442
+ return args
433
443
444
+ @staticmethod
445
+ def get_data ():
446
+ return ['hello' , 5 ]
434
447
435
- def notify_hello (* args ):
436
- return args
448
+ @staticmethod
449
+ def ping ():
450
+ return True
437
451
438
452
439
- def get_data ( ):
440
- return [ 'hello' , 5 ]
441
-
442
-
443
- def ping ( ):
444
- return True
453
+ class ExampleAggregateService ( ExampleService ):
454
+ """
455
+ Exposes the inherited ExampleService and a second copy as sub_service
456
+ """
457
+ def __init__ ( self ):
458
+ self . sub_service = ExampleService ()
445
459
446
460
447
461
def server_set_up (addr , address_family = socket .AF_INET ):
@@ -452,15 +466,13 @@ def log_request(self, *args, **kwargs):
452
466
pass
453
467
SimpleJSONRPCRequestHandler .log_request = log_request
454
468
server = SimpleJSONRPCServer (addr , address_family = address_family )
455
- server .register_function (summation , 'sum' )
456
- server .register_function (summation , 'notify_sum' )
457
- server .register_function (notify_hello )
458
- server .register_function (subtract )
459
- server .register_function (update )
460
- server .register_function (get_data )
461
- server .register_function (add )
462
- server .register_function (ping )
463
- server .register_function (summation , 'namespace.sum' )
469
+ service = ExampleAggregateService ()
470
+ # Expose an instance of the service
471
+ server .register_instance (service , allow_dotted_names = True )
472
+ # Expose some aliases for service methods
473
+ server .register_function (service .summation , 'sum' )
474
+ server .register_function (service .summation , 'notify_sum' )
475
+ server .register_function (service .summation , 'namespace.sum' )
464
476
server_proc = Thread (target = server .serve_forever )
465
477
server_proc .daemon = True
466
478
server_proc .start ()
0 commit comments