PythonSTB commited on
Commit
4cc0907
·
verified ·
1 Parent(s): c5ea81f

Upload grpcio/Test_Grpcio.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. grpcio/Test_Grpcio.py +163 -0
grpcio/Test_Grpcio.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Test_Grpcio.py - device test for grpcio 1.83.1 Android wheels.
2
+
3
+ Runs on-device via PipManager Scripts folder (needs the grpcio wheel with
4
+ the bundled C++ core + Cython cygrpc extension; needs `typing_extensions`
5
+ installed from PyPI/official wheel).
6
+ Exit-code contract: exit 0 iff every check PASSes (SKIPs allowed), else 1.
7
+ Each check prints [PASS] / [FAIL] / [SKIP] with the measured values.
8
+
9
+ Generated by RIMI
10
+ """
11
+ import sys
12
+ import traceback
13
+
14
+ PASS = 0
15
+ FAIL = 0
16
+ SKIP = 0
17
+
18
+
19
+ def check(name, cond, detail=""):
20
+ global PASS, FAIL
21
+ if cond:
22
+ PASS += 1
23
+ print("[PASS] %s %s" % (name, detail))
24
+ else:
25
+ FAIL += 1
26
+ print("[FAIL] %s %s" % (name, detail))
27
+
28
+
29
+ def skip(name, detail=""):
30
+ global SKIP
31
+ SKIP += 1
32
+ print("[SKIP] %s %s" % (name, detail))
33
+
34
+
35
+ def is_net_restricted(exc):
36
+ msg = repr(exc)
37
+ for token in ("EACCES", "EPERM", "Permission denied", "Network is unreachable",
38
+ "EADDRNOTAVAIL", "ENETUNREACH", "FutureTimeoutError",
39
+ "TimeoutError", "timed out", "Deadline Exceeded",
40
+ "UNAVAILABLE"):
41
+ if token in msg:
42
+ return True
43
+ return False
44
+
45
+
46
+ print("grpcio device test - python %s" % sys.version.split()[0])
47
+
48
+ # 1. import + native extension present
49
+ try:
50
+ import grpc
51
+ import grpc._cython.cygrpc as cygrpc
52
+ check("import grpc", True, "version=%s" % grpc.__version__)
53
+ check("native cygrpc", True, cygrpc.__name__)
54
+ except Exception as e:
55
+ check("import grpc", False, repr(e))
56
+ print("RESULT: %d PASS, %d FAIL, %d SKIP" % (PASS, FAIL, SKIP))
57
+ traceback.print_exc()
58
+ sys.exit(1)
59
+
60
+ check("grpc version 1.83.1", grpc.__version__ == "1.83.1",
61
+ "got=%s" % grpc.__version__)
62
+
63
+ # 2. runtime dep present
64
+ try:
65
+ import typing_extensions
66
+ try:
67
+ from importlib.metadata import version as _dv
68
+ _tev = _dv("typing_extensions")
69
+ except Exception:
70
+ _tev = getattr(typing_extensions, "__version__", "unknown")
71
+ check("import typing_extensions", True,
72
+ "version=%s" % _tev)
73
+ except Exception as e:
74
+ check("import typing_extensions", False, repr(e))
75
+
76
+ # 3. channel object creation (no network traffic)
77
+ try:
78
+ ch0 = grpc.insecure_channel("127.0.0.1:1")
79
+ check("insecure_channel create", True, "target=127.0.0.1:1")
80
+ ch0.close()
81
+ except Exception as e:
82
+ check("insecure_channel create", False, repr(e))
83
+
84
+ # 4. loopback unary-unary echo roundtrip (raw bytes, no protobuf, no
85
+ # external network). Generic handler echoes b'echo:' + request.
86
+ server = None
87
+ try:
88
+ from concurrent import futures
89
+ import socket
90
+
91
+ def _echo_handler(request, context):
92
+ return b"echo:" + request
93
+
94
+ handler = grpc.unary_unary_rpc_method_handler(
95
+ _echo_handler,
96
+ request_deserializer=lambda b: b,
97
+ response_serializer=lambda b: b,
98
+ )
99
+ generic = grpc.method_handlers_generic_handler(
100
+ "test.TestService", {"Echo": handler})
101
+ server = grpc.server(futures.ThreadPoolExecutor(max_workers=2))
102
+ server.add_generic_rpc_handlers((generic,))
103
+ port = server.add_insecure_port("127.0.0.1:0")
104
+ check("loopback server bind", port > 0, "port=%d" % port)
105
+ server.start()
106
+ try:
107
+ ch = grpc.insecure_channel("127.0.0.1:%d" % port)
108
+ try:
109
+ fut = grpc.channel_ready_future(ch)
110
+ fut.result(timeout=15)
111
+ check("channel ready (loopback)", True, "port=%d" % port)
112
+ except Exception as e:
113
+ if is_net_restricted(e):
114
+ skip("channel ready (loopback)", repr(e))
115
+ else:
116
+ check("channel ready (loopback)", False, repr(e))
117
+ try:
118
+ resp = ch.unary_unary("/test.TestService/Echo")(b"ping", timeout=15)
119
+ check("unary echo roundtrip", resp == b"echo:ping",
120
+ "got=%r" % (resp,))
121
+ except Exception as e:
122
+ if is_net_restricted(e):
123
+ skip("unary echo roundtrip", repr(e))
124
+ else:
125
+ check("unary echo roundtrip", False, repr(e))
126
+ try:
127
+ resp2 = ch.unary_unary("/test.TestService/Echo")(
128
+ b"hello-grpc", timeout=15)
129
+ check("unary echo roundtrip #2", resp2 == b"echo:hello-grpc",
130
+ "got=%r" % (resp2,))
131
+ except Exception as e:
132
+ if is_net_restricted(e):
133
+ skip("unary echo roundtrip #2", repr(e))
134
+ else:
135
+ check("unary echo roundtrip #2", False, repr(e))
136
+ ch.close()
137
+ finally:
138
+ server.stop(None)
139
+ server = None
140
+ except Exception as e:
141
+ if is_net_restricted(e):
142
+ skip("loopback server bind", repr(e))
143
+ skip("channel ready (loopback)", "server unavailable")
144
+ skip("unary echo roundtrip", "server unavailable")
145
+ skip("unary echo roundtrip #2", "server unavailable")
146
+ else:
147
+ check("loopback server bind", False, repr(e))
148
+ finally:
149
+ if server is not None:
150
+ try:
151
+ server.stop(None)
152
+ except Exception:
153
+ pass
154
+
155
+ # 5. status-code API surface (pure python, no network)
156
+ try:
157
+ check("StatusCode.OK", grpc.StatusCode.OK.value[0] == 0,
158
+ "name=%s" % grpc.StatusCode.OK.name)
159
+ except Exception as e:
160
+ check("StatusCode.OK", False, repr(e))
161
+
162
+ print("RESULT: %d PASS, %d FAIL, %d SKIP" % (PASS, FAIL, SKIP))
163
+ sys.exit(0 if FAIL == 0 else 1)