]> gitweb.factorcode.org Git - factor.git/blob - basis/io/monitors/monitors-tests.factor
io.files: exists? -> file-exists? and rename primitive.
[factor.git] / basis / io / monitors / monitors-tests.factor
1 USING: accessors calendar concurrency.count-downs
2 concurrency.promises continuations destructors io io.directories
3 io.files io.monitors io.pathnames io.timeouts kernel namespaces
4 sequences threads tools.test ;
5
6 [
7     [
8         { } [ "." t <monitor> "m" set ] unit-test
9
10         { } [ "a1" make-directory ] unit-test
11         { } [ "a2" make-directory ] unit-test
12         { } [ "a1" "a2" move-file-into ] unit-test
13
14         { t } [ "a2/a1" file-exists? ] unit-test
15
16         { } [ "a2/a1/a3.txt" touch-file ] unit-test
17
18         { t } [ "a2/a1/a3.txt" file-exists? ] unit-test
19
20         { } [ "a2/a1/a4.txt" touch-file ] unit-test
21         { } [ "a2/a1/a5.txt" touch-file ] unit-test
22         { } [ "a2/a1/a4.txt" delete-file ] unit-test
23         { } [ "a2/a1/a5.txt" "a2/a1/a4.txt" move-file ] unit-test
24
25         { t } [ "a2/a1/a4.txt" file-exists? ] unit-test
26
27         { } [ "m" get dispose ] unit-test
28     ] with-monitors
29 ] with-test-directory
30
31 [
32     [
33         { } [ "xyz" make-directory ] unit-test
34         { } [ "." t <monitor> "m" set ] unit-test
35
36         { } [ 1 <count-down> "b" set ] unit-test
37         { } [ 1 <count-down> "c1" set ] unit-test
38         { } [ 1 <count-down> "c2" set ] unit-test
39
40         [
41             "b" get count-down
42
43             [
44                 "m" get next-change path>>
45                 dup print flush
46                 dup parent-directory
47                 [ trim-tail-separators "xyz" tail? ] either? not
48             ] loop
49
50             "c1" get count-down
51             [
52                 "m" get next-change path>>
53                 dup print flush
54                 dup parent-directory
55                 [ trim-tail-separators "yxy" tail? ] either? not
56             ] loop
57
58             "c2" get count-down
59         ] "Monitor test thread" spawn drop
60
61         { } [ "b" get await ] unit-test
62         { } [ "xyz/test.txt" touch-file ] unit-test
63         { } [ "c1" get 1 minutes await-timeout ] unit-test
64         { } [ "subdir/blah/yxy" make-directories ] unit-test
65         { } [ "subdir/blah/yxy/test.txt" touch-file ] unit-test
66         { } [ "c2" get 1 minutes await-timeout ] unit-test
67
68         ! Dispose twice
69         { } [ "m" get dispose ] unit-test
70         { } [ "m" get dispose ] unit-test
71     ] with-monitors
72 ] with-test-directory
73
74 ! Out-of-scope disposal should not fail
75 { } [ [ "resource:" f <monitor> ] with-monitors dispose ] unit-test
76 { } [ [ "resource:" t <monitor> ] with-monitors dispose ] unit-test
77
78 ! Timeouts
79 [
80     [
81         ! Non-recursive
82         { } [
83             "." f <monitor> "m" set
84             100 milliseconds "m" get set-timeout
85             [ [ t ] [ "m" get next-change drop ] while ] must-fail
86             "m" get dispose
87         ] unit-test
88
89         ! Recursive
90         { } [
91             "." t <monitor> "m" set
92             100 milliseconds "m" get set-timeout
93             [ [ t ] [ "m" get next-change drop ] while ] must-fail
94             "m" get dispose
95         ] unit-test
96     ] with-monitors
97 ] with-test-directory
98
99 ! Disposing a monitor should throw an error in any threads
100 ! waiting on notifications
101 [
102     [
103         { } [
104             <promise> "p" set
105             "." t <monitor> "m" set
106             10 seconds "m" get set-timeout
107         ] unit-test
108
109         [
110             [ "m" get next-change ] [ ] recover
111             "p" get fulfill
112         ] in-thread
113
114         { } [
115             100 milliseconds sleep
116             "m" get dispose
117         ] unit-test
118
119         { t } [
120             "p" get 10 seconds ?promise-timeout
121             already-disposed?
122         ] unit-test
123     ] with-monitors
124 ] with-test-directory