7
7
8
8
9
9
class ExampleService :
10
+ def __init__ (self , test_hook = None ):
11
+ # This lets us pass assertions to the servicer ;)
12
+ self .test_hook = test_hook
10
13
11
- async def DoThing (self , stream : 'grpclib.server.Stream[DoThingRequest, DoThingResponse]' ):
14
+ async def DoThing (
15
+ self , stream : "grpclib.server.Stream[DoThingRequest, DoThingResponse]"
16
+ ):
12
17
request = await stream .recv_message ()
18
+ print ("self.test_hook" , self .test_hook )
19
+ if self .test_hook is not None :
20
+ self .test_hook (stream )
13
21
for iteration in range (request .iterations ):
14
22
pass
15
23
await stream .send_message (DoThingResponse (request .iterations ))
16
24
17
-
18
25
def __mapping__ (self ) -> Dict [str , grpclib .const .Handler ]:
19
26
return {
20
- ' /service.ExampleService/DoThing' : grpclib .const .Handler (
27
+ " /service.ExampleService/DoThing" : grpclib .const .Handler (
21
28
self .DoThing ,
22
29
grpclib .const .Cardinality .UNARY_UNARY ,
23
30
DoThingRequest ,
@@ -26,10 +33,91 @@ def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
26
33
}
27
34
28
35
36
+ async def _test_stub (stub , iterations = 42 , ** kwargs ):
37
+ response = await stub .do_thing (iterations = iterations )
38
+ assert response .successful_iterations == iterations
39
+
40
+
41
+ def _get_server_side_test (deadline , metadata ):
42
+ def server_side_test (stream ):
43
+ assert stream .deadline ._timestamp == pytest .approx (
44
+ deadline ._timestamp , 1
45
+ ), "The provided deadline should be recieved serverside"
46
+ assert (
47
+ stream .metadata ["authorization" ] == metadata ["authorization" ]
48
+ ), "The provided authorization metadata should be recieved serverside"
49
+
50
+ return server_side_test
51
+
52
+
29
53
@pytest .mark .asyncio
30
54
async def test_simple_service_call ():
31
- ITERATIONS = 42
32
55
async with ChannelFor ([ExampleService ()]) as channel :
33
- stub = ExampleServiceStub (channel )
34
- response = await stub .do_thing (iterations = ITERATIONS )
56
+ await _test_stub (ExampleServiceStub (channel ))
57
+
58
+
59
+ @pytest .mark .asyncio
60
+ async def test_service_call_with_upfront_request_params ():
61
+ # Setting deadline
62
+ deadline = grpclib .metadata .Deadline .from_timeout (22 )
63
+ metadata = {"authorization" : "12345" }
64
+ async with ChannelFor (
65
+ [ExampleService (test_hook = _get_server_side_test (deadline , metadata ))]
66
+ ) as channel :
67
+ await _test_stub (
68
+ ExampleServiceStub (channel , deadline = deadline , metadata = metadata )
69
+ )
70
+
71
+ # Setting timeout
72
+ timeout = 99
73
+ deadline = grpclib .metadata .Deadline .from_timeout (timeout )
74
+ metadata = {"authorization" : "12345" }
75
+ async with ChannelFor (
76
+ [ExampleService (test_hook = _get_server_side_test (deadline , metadata ))]
77
+ ) as channel :
78
+ await _test_stub (
79
+ ExampleServiceStub (channel , timeout = timeout , metadata = metadata )
80
+ )
81
+
82
+
83
+ @pytest .mark .asyncio
84
+ async def test_service_call_lower_level_with_overrides ():
85
+ ITERATIONS = 99
86
+
87
+ # Setting deadline
88
+ deadline = grpclib .metadata .Deadline .from_timeout (22 )
89
+ metadata = {"authorization" : "12345" }
90
+ kwarg_deadline = grpclib .metadata .Deadline .from_timeout (28 )
91
+ kwarg_metadata = {"authorization" : "12345" }
92
+ async with ChannelFor (
93
+ [ExampleService (test_hook = _get_server_side_test (deadline , metadata ))]
94
+ ) as channel :
95
+ stub = ExampleServiceStub (channel , deadline = deadline , metadata = metadata )
96
+ response = await stub ._unary_unary (
97
+ "/service.ExampleService/DoThing" , DoThingRequest (ITERATIONS ), DoThingResponse ,
98
+ deadline = kwarg_deadline ,
99
+ metadata = kwarg_metadata ,
100
+ )
101
+ assert response .successful_iterations == ITERATIONS
102
+
103
+ # Setting timeout
104
+ timeout = 99
105
+ deadline = grpclib .metadata .Deadline .from_timeout (timeout )
106
+ metadata = {"authorization" : "12345" }
107
+ kwarg_timeout = 9000
108
+ kwarg_deadline = grpclib .metadata .Deadline .from_timeout (kwarg_timeout )
109
+ kwarg_metadata = {"authorization" : "09876" }
110
+ async with ChannelFor (
111
+ [
112
+ ExampleService (
113
+ test_hook = _get_server_side_test (kwarg_deadline , kwarg_metadata )
114
+ )
115
+ ]
116
+ ) as channel :
117
+ stub = ExampleServiceStub (channel , deadline = deadline , metadata = metadata )
118
+ response = await stub ._unary_unary (
119
+ "/service.ExampleService/DoThing" , DoThingRequest (ITERATIONS ), DoThingResponse ,
120
+ timeout = kwarg_timeout ,
121
+ metadata = kwarg_metadata ,
122
+ )
35
123
assert response .successful_iterations == ITERATIONS
0 commit comments