-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_server.py
More file actions
822 lines (636 loc) · 24.1 KB
/
test_server.py
File metadata and controls
822 lines (636 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
import copy
import textwrap
from concurrent import futures
from contextlib import contextmanager
from dataclasses import dataclass
from functools import partial
from pathlib import Path
from typing import Any, Iterator, List, Optional, cast
import grpc
import pytest
from isolate.backends.settings import IsolateSettings
from isolate.connections.grpc.configuration import get_default_options
from isolate.logs import Log, LogLevel, LogSource
from isolate.server import definitions, health
from isolate.server.health_server import HealthServicer
from isolate.server.interface import from_grpc, to_serialized_object
from isolate.server.server import (
BridgeManager,
IsolateServicer,
ServerBoundInterceptor,
SingleTaskInterceptor,
)
REPO_DIR = Path(__file__).parent.parent
assert (
REPO_DIR.exists() and REPO_DIR.name == "isolate"
), "This test should have access to isolate as an installable package."
def inherit_from_local(monkeypatch: Any, value: bool = True) -> None:
"""Enables the inherit from local mode for the isolate server."""
monkeypatch.setattr("isolate.server.server.INHERIT_FROM_LOCAL", value)
@dataclass
class Stubs:
isolate_stub: definitions.IsolateStub
health_stub: health.HealthStub
@pytest.fixture
def interceptors():
return []
@contextmanager
def make_server(
tmp_path: Path, interceptors: Optional[List[ServerBoundInterceptor]] = None
) -> Iterator[Stubs]:
interceptors = interceptors or []
server = grpc.server(
futures.ThreadPoolExecutor(max_workers=1),
options=get_default_options(),
interceptors=interceptors, # type: ignore
)
for interceptor in interceptors:
interceptor.register_server(server)
test_settings = IsolateSettings(cache_dir=tmp_path / "cache")
with BridgeManager() as bridge:
servicer = IsolateServicer(bridge, test_settings)
for interceptor in interceptors:
interceptor.register_servicer(servicer)
definitions.register_isolate(servicer, server)
health.register_health(HealthServicer(), server)
host, port = "localhost", server.add_insecure_port("[::]:0")
server.start()
try:
isolate_stub = definitions.IsolateStub(
grpc.insecure_channel(
f"{host}:{port}",
options=get_default_options(),
)
)
health_stub = health.HealthStub(
grpc.insecure_channel(
f"{host}:{port}",
options=get_default_options(),
)
)
yield Stubs(isolate_stub=isolate_stub, health_stub=health_stub)
finally:
server.stop(None)
servicer.cancel_tasks()
@pytest.fixture
def stub(tmp_path, interceptors):
with make_server(tmp_path, interceptors) as stubs:
yield stubs.isolate_stub
@pytest.fixture
def health_stub(tmp_path, interceptors):
with make_server(tmp_path, interceptors) as stubs:
yield stubs.health_stub
def define_environment(kind: str, **kwargs: Any) -> definitions.EnvironmentDefinition:
struct = definitions.Struct()
struct.update(kwargs)
return definitions.EnvironmentDefinition(
kind=kind,
configuration=struct,
)
_NOT_SET = object()
def run_request(
stub: definitions.IsolateStub,
request: definitions.BoundFunction,
*,
build_logs: Optional[List[Log]] = None,
bridge_logs: Optional[List[Log]] = None,
user_logs: Optional[List[Log]] = None,
) -> definitions.SerializedObject:
log_store = {
LogSource.BUILDER: build_logs if build_logs is not None else [],
LogSource.BRIDGE: bridge_logs if bridge_logs is not None else [],
LogSource.USER: user_logs if user_logs is not None else [],
}
return_value = _NOT_SET
for result in stub.Run(request):
for _log in result.logs:
log = from_grpc(_log)
log_store[log.source].append(log)
if result.is_complete:
if return_value is _NOT_SET:
return_value = result.result
else:
raise ValueError("Sent the result twice")
if return_value is _NOT_SET:
raise ValueError("Never sent the result")
else:
return cast(definitions.SerializedObject, return_value)
def prepare_request(function, *args, **kwargs):
import dill
import __main__
dill.settings["recurse"] = True
# Make it seem like it originated from __main__
setattr(__main__, function.__name__, function)
function.__module__ = "__main__"
function.__qualname__ = f"__main__.{function.__name__}"
basic_function = partial(function, *args, **kwargs)
environment = define_environment("virtualenv", requirements=[])
return definitions.BoundFunction(
function=to_serialized_object(basic_function, method="dill"),
environments=[environment],
)
def run_function(stub, function, *args, log_handler=None, **kwargs):
request = prepare_request(function, *args, **kwargs)
user_logs: List[Log] = [] if log_handler is None else log_handler
result = run_request(stub, request, user_logs=user_logs)
raw_user_logs = [log.message for log in user_logs if log.message]
return from_grpc(result), raw_user_logs
@pytest.mark.parametrize("inherit_local", [True, False])
def test_server_basic_communication(
stub: definitions.IsolateStub,
monkeypatch: Any,
inherit_local: bool,
) -> None:
inherit_from_local(monkeypatch, inherit_local)
requirements = ["pyjokes==0.6.0"]
if not inherit_local:
# The agent process needs dill (and isolate) to actually
# deserialize the given function, so they need to be installed
# when we are not inheriting the local environment.
requirements.append("dill==0.3.5.1")
requirements.append(f"{REPO_DIR}")
env_definition = define_environment("virtualenv", requirements=requirements)
request = definitions.BoundFunction(
function=to_serialized_object(
partial(
eval,
"__import__('pyjokes').__version__",
),
method="dill",
),
environments=[env_definition],
)
raw_result = run_request(stub, request)
assert from_grpc(raw_result) == "0.6.0"
def test_server_builder_error(stub: definitions.IsolateStub, monkeypatch: Any) -> None:
inherit_from_local(monkeypatch)
# $$$$ as a package can't exist on PyPI since PEP 508 explicitly defines
# what is considered to be a legit package name.
#
# https://peps.python.org/pep-0508/#names
env_definition = define_environment("virtualenv", requirements=["$$$$"])
request = definitions.BoundFunction(
function=to_serialized_object(
partial(
eval,
"__import__('pyjokes').__version__",
),
method="dill",
),
environments=[env_definition],
)
build_logs: List[Log] = []
with pytest.raises(grpc.RpcError) as exc:
run_request(stub, request, build_logs=build_logs)
assert "Failure during 'pip install': Command" in exc.value.details()
raw_logs = [log.message for log in build_logs]
assert any("ERROR: Invalid requirement: '$$$$'" in raw_log for raw_log in raw_logs)
def test_user_logs_immediate(stub: definitions.IsolateStub, monkeypatch: Any) -> None:
inherit_from_local(monkeypatch)
env_definition = define_environment("virtualenv", requirements=["pyjokes==0.6.0"])
request = definitions.BoundFunction(
function=to_serialized_object(
partial(
exec,
textwrap.dedent(
"""
import sys, pyjokes
print(pyjokes.__version__)
print("error error!", file=sys.stderr)
print("[debug] error!", file=sys.stderr)
"""
),
),
method="dill",
),
environments=[env_definition],
)
user_logs: List[Log] = []
run_request(stub, request, user_logs=user_logs)
assert len(user_logs) == 3
by_stream = {log.level: log.message for log in user_logs}
assert by_stream[LogLevel.INFO] == "0.6.0"
assert by_stream[LogLevel.ERROR] == "error error!"
assert by_stream[LogLevel.DEBUG] == "[debug] error!"
def test_unknown_environment(stub: definitions.IsolateStub, monkeypatch: Any) -> None:
inherit_from_local(monkeypatch)
env_definition = define_environment("unknown")
request = definitions.BoundFunction(
function=to_serialized_object(
partial(
eval,
"__import__('pyjokes').__version__",
),
method="dill",
),
environments=[env_definition],
)
with pytest.raises(grpc.RpcError) as exc:
run_request(stub, request)
assert exc.match("Unknown environment kind")
def test_invalid_param(stub: definitions.IsolateStub, monkeypatch: Any) -> None:
inherit_from_local(monkeypatch)
env_definition = define_environment("virtualenv", packages=["pyjokes==1.0"])
request = definitions.BoundFunction(
function=to_serialized_object(
partial(
eval,
"__import__('pyjokes').__version__",
),
method="dill",
),
environments=[env_definition],
)
with pytest.raises(grpc.RpcError) as exc:
run_request(stub, request)
assert exc.match("unexpected keyword argument 'packages'")
@pytest.mark.parametrize("inherit_local", [True, False])
def test_server_multiple_envs(
stub: definitions.IsolateStub,
monkeypatch: Any,
inherit_local: bool,
) -> None:
inherit_from_local(monkeypatch, inherit_local)
xtra_requirements = ["python-dateutil==2.8.2"]
requirements = ["pyjokes==0.6.0"]
if not inherit_local:
# The agent process needs dill (and isolate) to actually
# deserialize the given function, so they need to be installed
# when we are not inheriting the local environment.
requirements.append("dill==0.3.5.1")
# TODO: apparently [server] doesn't work but [grpc] does work (not sure why
# needs further investigation, probably poetry related).
requirements.append(f"{REPO_DIR}[grpc]")
env_definition = define_environment("virtualenv", requirements=requirements)
xtra_env_definition = define_environment(
"virtualenv", requirements=xtra_requirements
)
request = definitions.BoundFunction(
function=to_serialized_object(
partial(
eval,
(
"__import__('pyjokes').__version__ + "
"' ' + "
"__import__('dateutil').__version__"
),
),
method="dill",
),
environments=[env_definition, xtra_env_definition],
)
raw_result = run_request(stub, request)
assert from_grpc(raw_result) == "0.6.0 2.8.2"
@pytest.mark.parametrize("python_version", ["3.8"])
def test_agent_requirements_custom_version(
stub: definitions.IsolateStub,
monkeypatch: Any,
python_version: str,
) -> None:
requirements = ["pyjokes==0.6.0"]
agent_requirements = ["dill==0.3.5.1", f"{REPO_DIR}[grpc]"]
monkeypatch.setattr("isolate.server.server.AGENT_REQUIREMENTS", agent_requirements)
env_definition = define_environment(
"virtualenv",
requirements=requirements,
python_version=python_version,
)
request = definitions.BoundFunction(
function=to_serialized_object(
partial(
eval,
(
"__import__('sysconfig').get_python_version(), "
"__import__('pyjokes').__version__"
),
),
method="dill",
),
environments=[env_definition],
)
raw_result = run_request(stub, request)
assert from_grpc(raw_result) == ("3.8", "0.6.0")
def test_agent_show_logs_from_agent_requirements(
stub: definitions.IsolateStub,
monkeypatch: Any,
) -> None:
requirements = ["pyjokes==0.6.0"]
agent_requirements = ["$$$$", f"{REPO_DIR}[grpc]"]
monkeypatch.setattr("isolate.server.server.AGENT_REQUIREMENTS", agent_requirements)
env_definition = define_environment(
"virtualenv",
requirements=requirements,
)
request = definitions.BoundFunction(
function=to_serialized_object(
partial(
eval,
(
"__import__('sysconfig').get_python_version(), "
"__import__('pyjokes').__version__"
),
),
method="dill",
),
environments=[env_definition],
)
build_logs: List[Log] = []
with pytest.raises(grpc.RpcError) as exc:
run_request(stub, request, build_logs=build_logs)
assert "Failure during 'pip install': Command" in exc.value.details()
raw_logs = [log.message for log in build_logs]
assert any("ERROR: Invalid requirement: '$$$$'" in raw_log for raw_log in raw_logs)
def test_bridge_connection_reuse(
stub: definitions.IsolateStub, monkeypatch: Any
) -> None:
inherit_from_local(monkeypatch)
first_env = define_environment(
"virtualenv",
requirements=["pyjokes==0.6.0"],
)
request = definitions.BoundFunction(
setup_func=to_serialized_object(
lambda: __import__("os").getpid(), method="cloudpickle"
),
function=to_serialized_object(
lambda process_pid: process_pid, method="cloudpickle"
),
environments=[first_env],
)
initial_process_pid = from_grpc(run_request(stub, request))
secondary_process_pid = from_grpc(run_request(stub, request))
# Both of the functions should run in the same agent process
assert initial_process_pid == secondary_process_pid
# But if we run a third function that has a different environment
# then its process should be different
second_env = define_environment(
"virtualenv",
requirements=["pyjokes==0.5.0"],
)
request_2 = copy.deepcopy(request)
request_2.environments.remove(first_env)
request_2.environments.append(second_env)
third_process_pid = from_grpc(run_request(stub, request_2))
assert third_process_pid != initial_process_pid
# As long as the environments are same, they are cached
fourth_process_pid = from_grpc(run_request(stub, request_2))
assert fourth_process_pid == third_process_pid
@pytest.mark.flaky(max_runs=3)
def test_bridge_connection_reuse_logs(
stub: definitions.IsolateStub, monkeypatch: Any
) -> None:
inherit_from_local(monkeypatch)
first_env = define_environment(
"virtualenv",
requirements=["pyjokes==0.6.0"],
)
request = definitions.BoundFunction(
setup_func=to_serialized_object(
lambda: print("setup"),
method="cloudpickle",
),
function=to_serialized_object(
lambda _: print("run"),
method="cloudpickle",
),
environments=[first_env],
)
logs: List[Log] = []
run_request(stub, request, user_logs=logs)
run_request(stub, request, user_logs=logs)
run_request(stub, request, user_logs=logs)
str_logs = [log.message for log in logs if log.message]
assert str_logs == [
"setup",
"run",
"run",
"run",
]
def print_logs_no_delay(num_lines, should_flush):
for i in range(num_lines):
print(i, flush=should_flush)
return num_lines
@pytest.mark.parametrize("num_lines", [0, 1, 10, 100, 1000])
@pytest.mark.parametrize("should_flush", [True, False])
@pytest.mark.flaky(max_runs=3)
def test_receive_complete_logs(
stub: definitions.IsolateStub,
monkeypatch: Any,
num_lines: int,
should_flush: bool,
) -> None:
inherit_from_local(monkeypatch)
result, logs = run_function(stub, print_logs_no_delay, num_lines, should_flush)
assert result == num_lines
assert logs == [str(i) for i in range(num_lines)]
def take_buffer(buffer):
return buffer
def test_grpc_option_configuration(tmp_path, monkeypatch):
inherit_from_local(monkeypatch)
with monkeypatch.context() as ctx:
ctx.setenv("ISOLATE_GRPC_CALL_MAX_SEND_MESSAGE_LENGTH", "100")
ctx.setenv("ISOLATE_GRPC_CALL_MAX_RECEIVE_MESSAGE_LENGTH", "100")
with pytest.raises(grpc.RpcError, match="Sent message larger than max"):
with make_server(tmp_path) as stubs:
run_function(stubs.isolate_stub, take_buffer, b"0" * 200)
with monkeypatch.context() as ctx:
ctx.setenv("ISOLATE_GRPC_CALL_MAX_SEND_MESSAGE_LENGTH", "5000")
ctx.setenv("ISOLATE_GRPC_CALL_MAX_RECEIVE_MESSAGE_LENGTH", "5000")
with make_server(tmp_path) as stubs:
result, _ = run_function(stubs.isolate_stub, take_buffer, b"0" * 200)
assert result == b"0" * 200
def test_health_check(health_stub: health.HealthStub) -> None:
resp: health.HealthCheckResponse = health_stub.Check(
health.HealthCheckRequest(service="")
)
assert resp.status == health.HealthCheckResponse.SERVING
def check_machine():
import os
return os.getpid()
def kill_machine():
import os
os._exit(1)
def get_pid_as_exc():
import os
raise ValueError(os.getpid())
def test_bridge_caching_when_undeerlying_channel_fails(
stub: definitions.IsolateStub, monkeypatch: Any
) -> None:
import os
import time
inherit_from_local(monkeypatch)
pid_1, _ = run_function(stub, check_machine)
pid_2, _ = run_function(stub, check_machine)
assert pid_1 == pid_2 # Same bridge
# Now send some faulty code that breaks the
# running agent and thus invalidatathing the
# bridge
with pytest.raises(grpc.RpcError):
run_function(stub, kill_machine)
# Now we should get a new bridge
pid_3, _ = run_function(stub, check_machine)
assert pid_1 != pid_3
# Even if there is a normal exception, the bridge
# should be reused (since we can capture it and it
# does not affect it badly).
with pytest.raises(ValueError) as exc_info:
run_function(stub, get_pid_as_exc)
[pid_4] = exc_info.value.args
assert pid_3 == pid_4
# Ensure that outside factors are also accounted for
# and the bridge is not reused
os.kill(pid_4, 9)
# And channels are kept fresh for a while (according
# to gRPC spec they might fall into idle when there is
# no exchange between client and server for a while but
# that doesn't seem to happen to us? If it did, we would
# add keepalive pings to the channel but not sure if we need
# it now).
pid_5 = run_function(stub, check_machine)
assert pid_4 != pid_5
time.sleep(10) # I've tried up to 90, and it seems to work fine?
# using 10 as it is the default keepalive time
# which would mean the channel would normally be
# fallen into the idle status?
pid_6 = run_function(stub, check_machine)
assert pid_5 == pid_6
def test_server_minimum_viable_proto_version(stub: definitions.IsolateStub) -> None:
# The agent process needs dill (and isolate) to actually
# deserialize the given function, so they need to be installed
# when we are not inheriting the local environment.
# protobuf<3 (the 2.x series) seems to use Python 2 only?
requirements = ["protobuf>3,<4"]
requirements.append("dill==0.3.5.1")
requirements.append(f"{REPO_DIR}")
env_definition = define_environment("virtualenv", requirements=requirements)
request = definitions.BoundFunction(
function=to_serialized_object(
partial(eval, "1+2"),
method="dill",
),
environments=[env_definition],
)
raw_result = run_request(stub, request)
assert from_grpc(raw_result) == 3
def send_unserializable_object():
import sys
return sys._getframe()
def raise_unserializable_object():
import sys
raise Exception("relevant information", sys._getframe())
def test_server_proper_error_delegation(
stub: definitions.IsolateStub, monkeypatch: Any
) -> None:
inherit_from_local(monkeypatch)
user_logs: List[Any] = []
with pytest.raises(grpc.RpcError) as exc_info:
run_function(stub, send_unserializable_object, log_handler=user_logs)
assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT
assert (
"Error while serializing the execution result "
"(object of type <class 'frame'>)."
) in exc_info.value.details()
assert not user_logs
user_logs = []
with pytest.raises(grpc.RpcError) as exc_info:
run_function(stub, raise_unserializable_object, log_handler=user_logs)
assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT
assert (
"Error while serializing the execution result "
"(object of type <class 'Exception'>)."
) in exc_info.value.details()
assert "relevant information" in "\n".join(log.message for log in user_logs)
def myfunc(path):
import time
with open(path, "w") as fobj:
for _ in range(10):
time.sleep(0.1)
fobj.write("still alive")
fobj.write("completed")
def test_server_submit(
stub: definitions.IsolateStub,
monkeypatch: Any,
tmp_path: Path,
) -> None:
import time
inherit_from_local(monkeypatch)
file = tmp_path / "file"
request = definitions.SubmitRequest(
function=prepare_request(myfunc, str(file)),
)
stub.Submit(request)
time.sleep(5)
assert "completed" in file.read_text()
assert not list(stub.List(definitions.ListRequest()).tasks)
def myserver():
import time
while True:
print("running")
time.sleep(1)
def test_server_submit_server(
stub: definitions.IsolateStub,
monkeypatch: Any,
) -> None:
inherit_from_local(monkeypatch)
request = definitions.SubmitRequest(function=prepare_request(myserver))
task_id = stub.Submit(request).task_id
tasks = [task.task_id for task in stub.List(definitions.ListRequest()).tasks]
assert task_id in tasks
stub.Cancel(definitions.CancelRequest(task_id=task_id))
assert not list(stub.List(definitions.ListRequest()).tasks)
@pytest.mark.parametrize(
"interceptors",
[
[SingleTaskInterceptor()],
],
)
def test_server_single_use_submit(
stub: definitions.IsolateStub,
monkeypatch: Any,
) -> None:
import time
inherit_from_local(monkeypatch)
request = definitions.SubmitRequest(function=prepare_request(myserver))
task_id = stub.Submit(request).task_id
tasks = [task.task_id for task in stub.List(definitions.ListRequest()).tasks]
assert task_id in tasks
# Now try to Submit again
with pytest.raises(grpc.RpcError) as exc_info:
stub.Submit(request)
assert exc_info.value.code() == grpc.StatusCode.RESOURCE_EXHAUSTED
# And try to Run a task
with pytest.raises(grpc.RpcError) as exc_info:
run_request(stub, prepare_request(myserver))
assert exc_info.value.code() == grpc.StatusCode.RESOURCE_EXHAUSTED
stub.Cancel(definitions.CancelRequest(task_id=task_id))
time.sleep(1)
with pytest.raises(grpc.RpcError) as exc_info:
stub.List(definitions.ListRequest())
# Server should be shutting down
assert exc_info.value.code() == grpc.StatusCode.UNAVAILABLE
@pytest.mark.parametrize(
"interceptors",
[
[SingleTaskInterceptor()],
],
)
def test_server_single_use_run(
stub: definitions.IsolateStub,
monkeypatch: Any,
) -> None:
import time
inherit_from_local(monkeypatch)
run_function(stub, check_machine)
time.sleep(1)
# Now try to Submit again
with pytest.raises(grpc.RpcError) as exc_info:
submit_request = definitions.SubmitRequest(function=prepare_request(myserver))
stub.Submit(submit_request)
assert exc_info.value.code() == grpc.StatusCode.UNAVAILABLE
# And try to Run a task
with pytest.raises(grpc.RpcError) as exc_info:
run_function(stub, check_machine)
assert exc_info.value.code() == grpc.StatusCode.UNAVAILABLE
with pytest.raises(grpc.RpcError) as exc_info:
stub.List(definitions.ListRequest())
assert exc_info.value.code() == grpc.StatusCode.UNAVAILABLE