MCPcopy Index your code
hub / github.com/python/cpython / _test_send

Method _test_send

Lib/test/_test_eintr.py:252–296  ·  view source on GitHub ↗
(self, send_func)

Source from the content-addressed store, hash-verified

250 self._test_recv(lambda sock, data: sock.recvmsg(data)[0])
251
252 def _test_send(self, send_func):
253 rd, wr = socket.socketpair()
254 self.addCleanup(wr.close)
255 # rd closed explicitly by parent
256
257 # we must send enough data for the send() to block
258 data = b"xyz" * (support.SOCK_MAX_SIZE // 3)
259
260 code = '\n'.join((
261 'import os, socket, sys, time',
262 '',
263 'fd = int(sys.argv[1])',
264 f'family = {int(rd.family)}',
265 f'sock_type = {int(rd.type)}',
266 f'sleep_time = {self.sleep_time!r}',
267 f'data = b"xyz" * {support.SOCK_MAX_SIZE // 3}',
268 'data_len = len(data)',
269 '',
270 'rd = socket.fromfd(fd, family, sock_type)',
271 'os.close(fd)',
272 '',
273 'with rd:',
274 ' # let the parent block on send()',
275 ' time.sleep(sleep_time)',
276 '',
277 ' received_data = bytearray(data_len)',
278 ' n = 0',
279 ' while n < data_len:',
280 ' n += rd.recv_into(memoryview(received_data)[n:])',
281 '',
282 'if received_data != data:',
283 ' raise Exception(f"recv error: {len(received_data)}'
284 ' vs {data_len} bytes")',
285 ))
286
287 fd = rd.fileno()
288 proc = self.subprocess(code, str(fd), pass_fds=[fd])
289 with kill_on_error(proc):
290 rd.close()
291 written = 0
292 while written < len(data):
293 sent = send_func(wr, memoryview(data)[written:])
294 # sendall() returns None
295 written += len(data) if sent is None else sent
296 self.assertEqual(proc.wait(), 0)
297
298 def test_send(self):
299 self._test_send(socket.socket.send)

Callers 3

test_sendMethod · 0.95
test_sendallMethod · 0.95
test_sendmsgMethod · 0.95

Calls 10

strFunction · 0.85
addCleanupMethod · 0.80
subprocessMethod · 0.80
kill_on_errorFunction · 0.70
socketpairMethod · 0.45
joinMethod · 0.45
filenoMethod · 0.45
closeMethod · 0.45
assertEqualMethod · 0.45
waitMethod · 0.45

Tested by

no test coverage detected