File: CachedFileStatTest.cpp

package info (click to toggle)
ruby-passenger 4.0.53-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,668 kB
  • ctags: 70,512
  • sloc: cpp: 264,280; ruby: 25,606; sh: 22,815; ansic: 18,277; python: 767; makefile: 99; perl: 20
file content (402 lines) | stat: -rw-r--r-- 10,203 bytes parent folder | download
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "TestSupport.h"
#include "Utils/CachedFileStat.hpp"
#include "Utils/SystemTime.h"
#include <sys/types.h>
#include <utime.h>

using namespace std;
using namespace Passenger;

namespace tut {
	struct CachedFileStatTest {
		struct stat buf;
		
		~CachedFileStatTest() {
			SystemTime::release();
			unlink("test.txt");
			unlink("test2.txt");
			unlink("test3.txt");
			unlink("test4.txt");
		}
	};
	
	DEFINE_TEST_GROUP(CachedFileStatTest);
	
	static void touch(const char *filename, time_t timestamp = 0) {
		FILE *f = fopen(filename, "w");
		fprintf(f, "hi");
		fclose(f);
		if (timestamp != 0) {
			struct utimbuf buf;
			buf.actime = timestamp;
			buf.modtime = timestamp;
			utime(filename, &buf);
		}
	}
	
	/************ Tests involving a single file ************/
	
	TEST_METHOD(1) {
		// Statting a new file works.
		touch("test.txt");
		CachedFileStat stat(1);
		ensure_equals(stat.stat("test.txt", &buf, 1), 0);
		ensure_equals((long) buf.st_size, (long) 2);
	}
	
	TEST_METHOD(2) {
		// It does not re-stat an existing file until the cache has expired.
		CachedFileStat stat(1);
		
		SystemTime::force(5);
		touch("test.txt", 1);
		ensure_equals("1st stat succceeded",
			stat.stat("test.txt", &buf, 1),
			0);
		
		touch("test.txt", 1000);
		ensure_equals("2nd stat succceeded",
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals("Cached value was used",
			buf.st_mtime,
			(time_t) 1);
		
		SystemTime::force(6);
		ensure_equals("3rd stat succceeded",
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals("Cache has been invalidated",
			buf.st_mtime,
			(time_t) 1000);
	}
	
	TEST_METHOD(3) {
		// Statting a nonexistant file returns an error.
		CachedFileStat stat(1);
		ensure_equals(stat.stat("test.txt", &buf, 1), -1);
		ensure_equals("It sets errno appropriately", errno, ENOENT);
	}
	
	TEST_METHOD(4) {
		// It does not re-stat a previously nonexistant file until
		// the cache has expired.
		SystemTime::force(5);
		CachedFileStat stat(1);
		ensure_equals("1st stat failed",
			stat.stat("test.txt", &buf, 1),
			-1);
		ensure_equals("It sets errno appropriately", errno, ENOENT);
		
		errno = EEXIST;
		touch("test.txt", 1000);
		ensure_equals("2nd stat failed",
			stat.stat("test.txt", &buf, 1),
			-1);
		ensure_equals("It sets errno appropriately", errno, ENOENT);
		ensure_equals("Cached value was used",
			buf.st_mtime,
			(time_t) 0);
		
		SystemTime::force(6);
		ensure_equals("3rd stat succeeded",
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals("Cache has been invalidated",
			buf.st_mtime,
			(time_t) 1000);
		
		unlink("test.txt");
		ensure_equals("4th stat succeeded even though file was unlinked",
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals("Cached value was used",
			buf.st_mtime,
			(time_t) 1000);
	}
	
	TEST_METHOD(5) {
		// If the throttling rate is 0 then the cache will be bypassed.
		SystemTime::force(5);
		CachedFileStat stat(2);
		ensure_equals("1st stat returns -1",
			stat.stat("test.txt", &buf, 0),
			-1);
		touch("test.txt");
		ensure_equals("2nd stat did not go through the cache",
			stat.stat("test.txt", &buf, 0),
			0);
	}
	
	
	/************ Tests involving multiple files ************/
	
	TEST_METHOD(10) {
		// Throttling in combination with multiple files works.
		CachedFileStat stat(2);
		SystemTime::force(5);
		
		// Touch and stat test.txt. The next stat should return
		// the old info.
		
		touch("test.txt", 10);
		ensure_equals(
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 10);
		
		touch("test.txt", 20);
		ensure_equals(
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 10);
		
		// Touch and stat test2.txt. The next stat should return
		// the old info.
		
		touch("test2.txt", 30);
		ensure_equals(
			stat.stat("test2.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 30);
		
		touch("test2.txt", 40);
		ensure_equals(
			stat.stat("test2.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 30);
		
		// Forward timer, then stat both files again. The most recent
		// information should be returned.
		
		SystemTime::force(6);
		ensure_equals(
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 20);
		ensure_equals(
			stat.stat("test2.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 40);
	}
	
	TEST_METHOD(11) {
		// Cache limiting works.
		CachedFileStat stat(3);
		SystemTime::force(5);
		
		// Create and stat test.txt, test2.txt and test3.txt.
		
		touch("test.txt", 1000);
		ensure_equals(
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 1000);
		
		touch("test2.txt", 1001);
		ensure_equals(
			stat.stat("test2.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 1001);
		
		touch("test3.txt", 1003);
		ensure_equals(
			stat.stat("test3.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 1003);
		
		// Stat test2.txt, then create and stat test4.txt, then touch test.txt.
		// test.txt should have been removed from the cache, and thus
		// upon statting it again its new timestamp should be returned.
		
		ensure_equals(
			stat.stat("test2.txt", &buf, 1),
			0);
		
		touch("test4.txt", 1004);
		ensure_equals(
			stat.stat("test4.txt", &buf, 1),
			0);
		
		touch("test.txt", 3000);
		ensure_equals(
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 3000);
	}
	
	TEST_METHOD(12) {
		// Increasing the cache size dynamically works.
		SystemTime::force(5);
		CachedFileStat stat(2);
		touch("test.txt", 1);
		touch("test2.txt", 2);
		touch("test3.txt", 3);
		
		ensure_equals("1st stat succeeded",
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals("2nd stat succeeded",
			stat.stat("test2.txt", &buf, 1),
			0);
		ensure_equals("3rd stat succeeded",
			stat.stat("test3.txt", &buf, 1),
			0);
		
		// test.txt should now be removed from the cache.
		
		touch("test.txt", 10);
		ensure_equals("4th stat succeeded",
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 10);
		
		// test2.txt should now be removed from the cache.
		// If we stat test2.txt now, test3.txt would normally
		// be removed from the cache. But if we increase the
		// cache size here then that won't happen:
		stat.setMaxSize(3);
		touch("test2.txt", 11);
		touch("test3.txt", 12);
		
		ensure_equals("5th stat succeeded",
			stat.stat("test2.txt", &buf, 1),
			0);
		ensure_equals(buf.st_mtime, (time_t) 11);
		
		ensure_equals("6th stat succeeded",
			stat.stat("test3.txt", &buf, 1),
			0);
		ensure_equals("test3.txt is still cached",
			buf.st_mtime,
			(time_t) 3);
		
		ensure_equals("7th stat succeeded",
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals("test.txt is still cached",
			buf.st_mtime,
			(time_t) 10);
	}
	
	TEST_METHOD(13) {
		// If we decrease the cache size dynamically, then
		// the oldest entries will be removed.
		SystemTime::force(5);
		CachedFileStat stat(3);
		touch("test.txt", 1);
		touch("test2.txt", 2);
		touch("test3.txt", 3);
		
		ensure_equals("1st stat succeeded",
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals("2nd stat succeeded",
			stat.stat("test2.txt", &buf, 1),
			0);
		ensure_equals("3rd stat succeeded",
			stat.stat("test3.txt", &buf, 1),
			0);
		
		// The following should remove test.txt and test2.txt from the cache.
		stat.setMaxSize(1);
		
		touch("test.txt", 10);
		touch("test2.txt", 11);
		touch("test3.txt", 12);
		
		ensure_equals("6th stat succeeded",
			stat.stat("test3.txt", &buf, 1),
			0);
		ensure_equals("test3.txt is still in the cache",
			buf.st_mtime,
			(time_t) 3);
		
		ensure_equals("4th stat succeeded",
			stat.stat("test.txt", &buf, 1),
			0);
		ensure_equals("test.txt is removed from the cache",
			buf.st_mtime,
			(time_t) 10);
		
		ensure_equals("5th stat succeeded",
			stat.stat("test2.txt", &buf, 1),
			0);
		ensure_equals("test2.txt is removed from the cache",
			buf.st_mtime,
			(time_t) 11);
	}
	
	TEST_METHOD(14) {
		// An initial cache size of 0 means that the cache size is unlimited.
		SystemTime::force(1);
		CachedFileStat stat(0);
		
		touch("test.txt", 1);
		touch("test2.txt", 2);
		touch("test3.txt", 3);
		stat.stat("test.txt", &buf, 1);
		stat.stat("test2.txt", &buf, 1);
		stat.stat("test3.txt", &buf, 1);
		
		touch("test.txt", 11);
		touch("test2.txt", 12);
		touch("test3.txt", 13);
		stat.stat("test.txt", &buf, 1);
		ensure_equals(buf.st_mtime, (time_t) 1);
		stat.stat("test2.txt", &buf, 1);
		ensure_equals(buf.st_mtime, (time_t) 2);
		stat.stat("test3.txt", &buf, 1);
		ensure_equals(buf.st_mtime, (time_t) 3);
	}
	
	TEST_METHOD(15) {
		// Setting the cache size dynamically to 0 makes the cache size unlimited.
		SystemTime::force(1);
		CachedFileStat stat(2);
		
		touch("test.txt", 1);
		touch("test2.txt", 2);
		touch("test3.txt", 3);
		stat.stat("test.txt", &buf, 1);
		stat.stat("test2.txt", &buf, 1);
		stat.stat("test3.txt", &buf, 1);
		
		// test.txt is now no longer in the cache.
		
		stat.setMaxSize(0);
		touch("test.txt", 11);
		touch("test2.txt", 12);
		touch("test3.txt", 13);
		stat.stat("test.txt", &buf, 1);
		stat.stat("test2.txt", &buf, 1);
		stat.stat("test3.txt", &buf, 1);
		
		// test.txt should now have been re-statted while test2.txt
		// and test3.txt are still cached.
		
		stat.stat("test.txt", &buf, 1);
		ensure_equals("test.txt is re-statted", buf.st_mtime, (time_t) 11);
		stat.stat("test2.txt", &buf, 1);
		ensure_equals("test2.txt is still cached", buf.st_mtime, (time_t) 2);
		stat.stat("test3.txt", &buf, 1);
		ensure_equals("test3.txt is still cached", buf.st_mtime, (time_t) 3);
	}
	
	TEST_METHOD(16) {
		// Changing the cache size dynamically from 0 to non-0 works;
		// it removes the oldest entries, if necessary.
		CachedFileStat stat(0);
		stat.stat("test.txt", &buf, 1);
		stat.stat("test2.txt", &buf, 1);
		stat.stat("test3.txt", &buf, 1);
		stat.stat("test4.txt", &buf, 1);
		stat.stat("test5.txt", &buf, 1);
		stat.setMaxSize(2);
		ensure("(1)", !stat.knows("test.txt"));
		ensure("(2)", !stat.knows("test2.txt"));
		ensure("(3)", !stat.knows("test3.txt"));
		ensure("(4)", stat.knows("test4.txt"));
		ensure("(5)", stat.knows("test5.txt"));
	}
}