@@ -195,47 +195,50 @@ def _():
195195 assert invocations == 3
196196
197197
198- tmpfile = tempfile .NamedTemporaryFile ()
199-
200-
201198@pytest .mark .asyncio
202199async def test_file_reader ():
203- async with OnEndedSessionCallbacks ():
204- invocations = 0
200+ tmpfile = tempfile .NamedTemporaryFile (delete = False )
201+ try :
202+ async with OnEndedSessionCallbacks ():
203+ invocations = 0
205204
206- tmpfile .write ("hello\n " .encode ())
207- tmpfile .flush ()
205+ tmpfile .write ("hello\n " .encode ())
206+ tmpfile .flush ()
207+ tmpfile .close ()
208208
209- mock_time = MockTime ()
210- with mock_time ():
209+ mock_time = MockTime ()
210+ with mock_time ():
211211
212- @file_reader (tmpfile .name )
213- def read_file ():
214- with open (tmpfile .name , "r" ) as f :
215- nonlocal invocations
216- invocations += 1
217- return f .read ()
212+ @file_reader (tmpfile .name )
213+ def read_file ():
214+ with open (tmpfile .name , "r" ) as f :
215+ nonlocal invocations
216+ invocations += 1
217+ return f .read ()
218218
219- with isolate ():
220- assert invocations == 0
221- await flush ()
219+ with isolate ():
220+ assert invocations == 0
221+ await flush ()
222222
223- assert read_file () == "hello\n "
224- assert invocations == 1
225- # Advancing time without a write does nothing
226- await mock_time .advance_time (1.01 )
227- assert read_file () == "hello\n "
228- assert invocations == 1
223+ assert read_file () == "hello\n "
224+ assert invocations == 1
225+ # Advancing time without a write does nothing
226+ await mock_time .advance_time (1.01 )
227+ assert read_file () == "hello\n "
228+ assert invocations == 1
229229
230- tmpfile .write ("goodbye\n " .encode ())
231- tmpfile .flush ()
232- # The file's been updated, but we haven't looked yet
233- assert read_file () == "hello\n "
230+ with open (tmpfile .name , "a" ) as f :
231+ f .write ("goodbye\n " )
234232
235- await mock_time .advance_time (1.01 )
233+ # The file's been updated, but we haven't looked yet
234+ assert read_file () == "hello\n "
235+
236+ await mock_time .advance_time (1.01 )
236237
237- assert read_file () == "hello\n goodbye\n "
238- assert invocations == 2
238+ assert read_file () == "hello\n goodbye\n "
239+ assert invocations == 2
240+ finally :
241+ os .unlink (tmpfile .name )
239242
240243
241244@pytest .mark .asyncio
@@ -253,6 +256,7 @@ def read_file():
253256 with isolate ():
254257 await flush ()
255258 assert read_file () is True
259+ tmpfile1 .close ()
256260
257261 os .unlink (tmpfile1 .name )
258262 await mock_time .advance_time (1.01 )
0 commit comments