@@ -284,6 +284,50 @@ async def run():
284
284
285
285
self .loop .run_until_complete (run ())
286
286
287
+ def test_socketpair (self ):
288
+ peername = asyncio .Future (loop = self .loop )
289
+
290
+ class Proto (MyDatagramProto ):
291
+ def datagram_received (self , data , addr ):
292
+ super ().datagram_received (data , addr )
293
+ peername .set_result (addr )
294
+
295
+ s1 , s2 = socket .socketpair (socket .AF_UNIX , socket .SOCK_DGRAM , 0 )
296
+
297
+ with s1 , s2 :
298
+ try :
299
+ f = self .loop .create_datagram_endpoint (
300
+ lambda : Proto (loop = self .loop ), sock = s1 )
301
+ except TypeError as ex :
302
+ # asyncio in 3.5.0 doesn't have the 'sock' argument
303
+ if 'got an unexpected keyword argument' not in ex .args [0 ]:
304
+ raise
305
+ else :
306
+ tr , pr = self .loop .run_until_complete (f )
307
+ self .assertIsInstance (pr , Proto )
308
+
309
+ s2 .send (b'hello, socketpair' )
310
+ addr = self .loop .run_until_complete (
311
+ asyncio .wait_for (peername , 1 , loop = self .loop ))
312
+ if sys .platform .startswith ('linux' ):
313
+ self .assertEqual (addr , None )
314
+ else :
315
+ self .assertEqual (addr , '' )
316
+ self .assertEqual (pr .nbytes , 17 )
317
+
318
+ if not self .is_asyncio_loop ():
319
+ # asyncio doesn't support sendto(xx) on UDP sockets
320
+ # https://git.io/Jfqbw
321
+ data = b'from uvloop'
322
+ tr .sendto (data )
323
+ result = self .loop .run_until_complete (asyncio .wait_for (
324
+ self .loop .run_in_executor (None , s2 .recv , 1024 ),
325
+ 1 , loop = self .loop ))
326
+ self .assertEqual (data , result )
327
+
328
+ tr .close ()
329
+ self .loop .run_until_complete (pr .done )
330
+
287
331
288
332
class Test_UV_UDP (_TestUDP , tb .UVTestCase ):
289
333
0 commit comments