1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
START_MARKER 
 
test_do([[ 
  class TestMonitor 
  { 
    inherit Filesystem.Monitor.symlinks; 
 
    enum Event { 
      EVT_DATA_CHANGED = 1, 
      EVT_ATTR_CHANGED, 
      EVT_FILE_EXISTS, 
      EVT_FILE_CREATED, 
      EVT_FILE_DELETED, 
      EVT_STABLE_DATA_CHANGE 
    } 
 
    protected array(mapping(Event:string)) evts = ({}); 
 
    void clear_events() 
    { 
      evts = ({}); 
    } 
 
    int(0..1) wait_for_stable_event (string path) 
    { 
#if constant(System.FSEvents.EventStream) || constant(System.Inotify) 
      int timeout = gethrtime() + 5000000; dnl 5 secs 
#else 
      int timeout = gethrtime() + 10000000; dnl 10 secs. 
#endif 
      int got_path; 
 
      path = canonic_path(path); 
      while (gethrtime() < timeout) { 
        Pike.DefaultBackend(0.005); 
        if (has_value (stable_data_change_evts(), path)) { 
          got_path = 1; 
          break; 
        } 
      } 
 
      return got_path; 
    } 
 
    int(0..1) wait_for_delete_event(string path) 
    { 
#if constant(System.FSEvents.EventStream) || constant(System.Inotify) 
      int timeout = gethrtime() + 5000000; dnl 5 secs 
#else 
      int timeout = gethrtime() + 10000000; dnl 10 secs. 
#endif 
      int got_path; 
 
      path = canonic_path(path); 
      while (gethrtime() < timeout) { 
        Pike.DefaultBackend(0.005); 
        if (has_value(delete_evts(), path)) { 
          got_path = 1; 
          break; 
        } 
      } 
 
      if (!got_path) { 
        werror("delete events: %O\n", delete_evts()); 
      } 
 
      return got_path; 
    } 
 
    array(string) stable_data_change_evts() 
    { 
      return map (evts, 
                  lambda(mapping(Event:string) evt) 
                  { 
                    return evt[EVT_STABLE_DATA_CHANGE]; 
                  }) - ({ 0 }); 
    } 
 
    array(string) delete_evts() 
    { 
      return map (evts, 
                  lambda(mapping(Event:string) evt) 
                  { 
                    return evt[EVT_FILE_DELETED]; 
                  }) - ({ 0 }); 
    } 
 
    void data_changed (string path) 
    { 
      evts += ({ ([ EVT_DATA_CHANGED: path ]) }); 
    } 
 
    void attr_changed (string path, Stdio.Stat st) 
    { 
      evts += ({ ([ EVT_ATTR_CHANGED: path ]) }); 
    } 
 
    void file_exists (string path, Stdio.Stat st) 
    { 
      evts += ({ ([ EVT_FILE_EXISTS: path ]) }); 
    } 
 
    void file_created (string path, Stdio.Stat st) 
    { 
      evts += ({ ([ EVT_FILE_CREATED: path ]) }); 
    } 
 
    void file_deleted (string path) 
    { 
      evts += ({ ([ EVT_FILE_DELETED: path ]) }); 
    } 
 
    void stable_data_change (string path, Stdio.Stat st) 
    { 
      evts += ({ ([ EVT_STABLE_DATA_CHANGE: path ]) }); 
    } 
  }; 
 
  string basedir = combine_path (getcwd(), "testsuite.dir/"); 
  add_constant("test_basedir", basedir); 
  Stdio.mkdirhier(basedir); 
  Stdio.mkdirhier(basedir + "preexisting/foo/bar/"); 
  Stdio.write_file(basedir + "preexisting/test", "1234"); 
 
  sleep (2); 
 
  TestMonitor mon = TestMonitor(); 
 
  mon->monitor(basedir, 3, 
#if constant(System.FSEvents.EventStream) || constant(System.Inotify) 
               3600, dnl High interval to make sure acceleration is enabled. 
#else 
               1, 
#endif 
               1, 
               1); 
 
  mon->set_nonblocking (0); 
 
  add_constant("test_mon", mon); 
]]) 
 
test_true([[ 
            lambda() { 
              test_mon->clear_events(); 
              Stdio.write_file (test_basedir + "preexisting/foo/bar/test", 
                                "12345"); 
              return test_mon->wait_for_stable_event (test_basedir + 
                                                      "preexisting/foo/bar/test"); 
            }(); 
            ]]) 
 
test_true([[ 
            lambda() { 
              test_mon->clear_events(); 
              Stdio.write_file (test_basedir + "preexisting/test", "12345"); 
              return test_mon->wait_for_stable_event (test_basedir + 
                                                      "preexisting/test"); 
            }(); 
            ]]) 
 
test_true([[ 
            lambda() { 
              test_mon->clear_events(); 
              Stdio.write_file (test_basedir + "foo", "12345"); 
              return test_mon->wait_for_stable_event (test_basedir + "foo"); 
            }(); 
]]) 
 
test_true([[ 
            lambda() { 
              test_mon->clear_events(); 
              Stdio.mkdirhier (test_basedir + "bar/fie"); 
              Stdio.write_file (test_basedir + "bar/fie/gaz", "12345"); 
              return test_mon->wait_for_stable_event (test_basedir + "bar/fie/gaz"); 
            }(); 
            ]]) 
 
test_true([[ 
            lambda() { 
              test_mon->clear_events(); 
              Stdio.write_file (test_basedir + "bar/fie/gaz", "12347"); 
              return test_mon->wait_for_stable_event (test_basedir + "bar/fie/gaz"); 
            }(); 
            ]]) 
 
test_true([[ 
            lambda() { 
              test_mon->clear_events(); 
              Stdio.recursive_rm(test_basedir); 
              return test_mon->wait_for_delete_event(test_basedir); 
            }(); 
            ]]) 
 
test_do([[ 
  add_constant("test_mon"); 
  add_constant("test_basedir"); 
]]) 
 
END_MARKER