@@ -161,18 +161,70 @@ def test_create_datagram_endpoint_sock(self):
161
161
@unittest .skipIf (sys .version_info < (3 , 5 , 1 ),
162
162
"asyncio in 3.5.0 doesn't have the 'sock' argument" )
163
163
def test_create_datagram_endpoint_sock_unix_domain (self ):
164
+
165
+ class Proto (asyncio .DatagramProtocol ):
166
+ done = None
167
+
168
+ def __init__ (self , loop ):
169
+ self .state = 'INITIAL'
170
+ self .addrs = set ()
171
+ self .done = asyncio .Future (loop = loop )
172
+ self .data = b''
173
+
174
+ def connection_made (self , transport ):
175
+ self .transport = transport
176
+ assert self .state == 'INITIAL' , self .state
177
+ self .state = 'INITIALIZED'
178
+
179
+ def datagram_received (self , data , addr ):
180
+ assert self .state == 'INITIALIZED' , self .state
181
+ self .addrs .add (addr )
182
+ self .data += data
183
+ if self .data == b'STOP' and not self .done .done ():
184
+ self .done .set_result (True )
185
+
186
+ def error_received (self , exc ):
187
+ assert self .state == 'INITIALIZED' , self .state
188
+ if not self .done .done ():
189
+ self .done .set_exception (exc or RuntimeError ())
190
+
191
+ def connection_lost (self , exc ):
192
+ assert self .state == 'INITIALIZED' , self .state
193
+ self .state = 'CLOSED'
194
+ if self .done and not self .done .done ():
195
+ self .done .set_result (None )
196
+
164
197
tmp_file = os .path .join (tempfile .gettempdir (), str (uuid .uuid4 ()))
165
198
sock = socket .socket (socket .AF_UNIX , type = socket .SOCK_DGRAM )
166
199
sock .bind (tmp_file )
167
200
168
201
with sock :
202
+ pr = Proto (loop = self .loop )
169
203
f = self .loop .create_datagram_endpoint (
170
- lambda : MyDatagramProto (loop = self .loop ), sock = sock )
171
- tr , pr = self .loop .run_until_complete (f )
172
- self .assertIsInstance (pr , MyDatagramProto )
173
- tr .sendto (b'HELLO' , tmp_file )
174
- tr .close ()
175
- self .loop .run_until_complete (pr .done )
204
+ lambda : pr , sock = sock )
205
+ tr , pr_prime = self .loop .run_until_complete (f )
206
+ self .assertIs (pr , pr_prime )
207
+
208
+ tmp_file2 = os .path .join (tempfile .gettempdir (), str (uuid .uuid4 ()))
209
+ sock2 = socket .socket (socket .AF_UNIX , type = socket .SOCK_DGRAM )
210
+ sock2 .bind (tmp_file2 )
211
+
212
+ with sock2 :
213
+ f2 = self .loop .create_datagram_endpoint (
214
+ asyncio .DatagramProtocol , sock = sock2 )
215
+ tr2 , pr2 = self .loop .run_until_complete (f2 )
216
+
217
+ tr2 .sendto (b'STOP' , tmp_file )
218
+
219
+ self .loop .run_until_complete (pr .done )
220
+
221
+ tr .close ()
222
+ tr2 .close ()
223
+
224
+ # Let transports close
225
+ self .loop .run_until_complete (asyncio .sleep (0.2 ))
226
+
227
+ self .assertIn (tmp_file2 , pr .addrs )
176
228
177
229
178
230
class Test_UV_UDP (_TestUDP , tb .UVTestCase ):
0 commit comments