@@ -15,14 +15,16 @@ defmodule Diff.TmpDirTest do
1515 test "cleanup on normal process exit" do
1616 test_pid = self ( )
1717
18- Task . start ( fn ->
19- file = Diff.TmpDir . tmp_file ( "test" )
20- dir = Diff.TmpDir . tmp_dir ( "test" )
21- send ( test_pid , { :paths , file , dir } )
22- end )
23-
18+ { :ok , task_pid } =
19+ Task . start ( fn ->
20+ file = Diff.TmpDir . tmp_file ( "test" )
21+ dir = Diff.TmpDir . tmp_dir ( "test" )
22+ send ( test_pid , { :paths , file , dir } )
23+ end )
24+
25+ ref = Process . monitor ( task_pid )
2426 assert_receive { :paths , file , dir }
25- Process . sleep ( 100 )
27+ wait_for_cleanup ( task_pid , ref )
2628
2729 refute File . exists? ( file )
2830 refute File . exists? ( dir )
@@ -32,15 +34,17 @@ defmodule Diff.TmpDirTest do
3234 test "cleanup on process crash" do
3335 test_pid = self ( )
3436
35- Task . start ( fn ->
36- file = Diff.TmpDir . tmp_file ( "test" )
37- dir = Diff.TmpDir . tmp_dir ( "test" )
38- send ( test_pid , { :paths , file , dir } )
39- raise "crash"
40- end )
37+ { :ok , task_pid } =
38+ Task . start ( fn ->
39+ file = Diff.TmpDir . tmp_file ( "test" )
40+ dir = Diff.TmpDir . tmp_dir ( "test" )
41+ send ( test_pid , { :paths , file , dir } )
42+ raise "crash"
43+ end )
4144
45+ ref = Process . monitor ( task_pid )
4246 assert_receive { :paths , file , dir }
43- Process . sleep ( 100 )
47+ wait_for_cleanup ( task_pid , ref )
4448
4549 refute File . exists? ( file )
4650 refute File . exists? ( dir )
@@ -49,19 +53,21 @@ defmodule Diff.TmpDirTest do
4953 test "multiple paths for one process" do
5054 test_pid = self ( )
5155
52- Task . start ( fn ->
53- paths =
54- for i <- 1 .. 5 do
55- file = Diff.TmpDir . tmp_file ( "test-#{ i } " )
56- dir = Diff.TmpDir . tmp_dir ( "test-#{ i } " )
57- { file , dir }
58- end
56+ { :ok , task_pid } =
57+ Task . start ( fn ->
58+ paths =
59+ for i <- 1 .. 5 do
60+ file = Diff.TmpDir . tmp_file ( "test-#{ i } " )
61+ dir = Diff.TmpDir . tmp_dir ( "test-#{ i } " )
62+ { file , dir }
63+ end
5964
60- send ( test_pid , { :paths , paths } )
61- end )
65+ send ( test_pid , { :paths , paths } )
66+ end )
6267
68+ ref = Process . monitor ( task_pid )
6369 assert_receive { :paths , paths }
64- Process . sleep ( 100 )
70+ wait_for_cleanup ( task_pid , ref )
6571
6672 for { file , dir } <- paths do
6773 refute File . exists? ( file )
@@ -76,4 +82,10 @@ defmodule Diff.TmpDirTest do
7682 assert File . exists? ( file )
7783 assert File . dir? ( dir )
7884 end
85+
86+ defp wait_for_cleanup ( task_pid , ref ) do
87+ assert_receive { :DOWN , ^ ref , :process , ^ task_pid , _ } , 5000
88+ # Sync with the GenServer to ensure the :DOWN cleanup has been processed
89+ :sys . get_state ( Diff.TmpDir )
90+ end
7991end
0 commit comments