-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
RolloverFileOutputStreamTest.java
387 lines (328 loc) · 14.1 KB
/
RolloverFileOutputStreamTest.java
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
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.Arrays;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
@ExtendWith(WorkDirExtension.class)
public class RolloverFileOutputStreamTest
{
public WorkDir testingDir;
private static ZoneId toZoneId(String timezoneId)
{
ZoneId zone = TimeZone.getTimeZone(timezoneId).toZoneId();
// System.out.printf(".toZoneId(\"%s\") = [id=%s,normalized=%s]%n", timezoneId, zone.getId(), zone.normalized());
return zone;
}
private static ZonedDateTime toDateTime(String timendate, ZoneId zone)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd-hh:mm:ss.S a z")
.withZone(zone);
return ZonedDateTime.parse(timendate, formatter);
}
private static String toString(TemporalAccessor date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd-hh:mm:ss.S a z");
return formatter.format(date);
}
private void assertSequence(ZonedDateTime midnight, Object[][] expected)
{
ZonedDateTime nextEvent = midnight;
for (int i = 0; i < expected.length; i++)
{
long currentMillis = nextEvent.toInstant().toEpochMilli();
nextEvent = nextEvent.toLocalDate().plus(1, ChronoUnit.DAYS).atStartOfDay(nextEvent.getZone());
assertThat("Next Event", toString(nextEvent), is(expected[i][0]));
long duration = (nextEvent.toInstant().toEpochMilli() - currentMillis);
assertThat("Duration to next event", duration, is((long)expected[i][1]));
}
}
/**
* <a href="Issue #1507">https://github.com/eclipse/jetty.project/issues/1507</a>
*/
@Test
public void testMidnightRolloverCalcPDTIssue1507()
{
ZoneId zone = toZoneId("PST");
ZonedDateTime initialDate = toDateTime("2017.04.26-08:00:00.0 PM PDT", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2017.04.27-12:00:00.0 AM PDT"));
Object[][] expected = {
{"2017.04.27-12:00:00.0 AM PDT", 14_400_000L},
{"2017.04.28-12:00:00.0 AM PDT", 86_400_000L},
{"2017.04.29-12:00:00.0 AM PDT", 86_400_000L},
{"2017.04.30-12:00:00.0 AM PDT", 86_400_000L},
{"2017.05.01-12:00:00.0 AM PDT", 86_400_000L},
{"2017.05.02-12:00:00.0 AM PDT", 86_400_000L},
};
assertSequence(initialDate, expected);
}
@Test
public void testMidnightRolloverCalcPSTDSTStart()
{
ZoneId zone = toZoneId("PST");
ZonedDateTime initialDate = toDateTime("2016.03.10-01:23:45.0 PM PST", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.03.11-12:00:00.0 AM PST"));
Object[][] expected = {
{"2016.03.12-12:00:00.0 AM PST", 86_400_000L},
{"2016.03.13-12:00:00.0 AM PST", 86_400_000L},
{"2016.03.14-12:00:00.0 AM PDT", 82_800_000L}, // the short day
{"2016.03.15-12:00:00.0 AM PDT", 86_400_000L},
{"2016.03.16-12:00:00.0 AM PDT", 86_400_000L},
};
assertSequence(midnight, expected);
}
@Test
public void testMidnightRolloverCalcPSTDSTEnd()
{
ZoneId zone = toZoneId("PST");
ZonedDateTime initialDate = toDateTime("2016.11.03-11:22:33.0 AM PDT", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.11.04-12:00:00.0 AM PDT"));
Object[][] expected = {
{"2016.11.05-12:00:00.0 AM PDT", 86_400_000L},
{"2016.11.06-12:00:00.0 AM PDT", 86_400_000L},
{"2016.11.07-12:00:00.0 AM PST", 90_000_000L}, // the long day
{"2016.11.08-12:00:00.0 AM PST", 86_400_000L},
{"2016.11.09-12:00:00.0 AM PST", 86_400_000L},
};
assertSequence(midnight, expected);
}
@Test
public void testMidnightRolloverCalcSydneyDSTStart()
{
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime initialDate = toDateTime("2016.09.31-01:23:45.0 PM AEST", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.10.01-12:00:00.0 AM AEST"));
Object[][] expected = {
{"2016.10.02-12:00:00.0 AM AEST", 86_400_000L},
{"2016.10.03-12:00:00.0 AM AEDT", 82_800_000L}, // the short day
{"2016.10.04-12:00:00.0 AM AEDT", 86_400_000L},
{"2016.10.05-12:00:00.0 AM AEDT", 86_400_000L},
{"2016.10.06-12:00:00.0 AM AEDT", 86_400_000L},
};
assertSequence(midnight, expected);
}
@Test
public void testMidnightRolloverCalcSydneyDSTEnd()
{
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime initialDate = toDateTime("2016.04.01-11:22:33.0 AM AEDT", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.04.02-12:00:00.0 AM AEDT"));
Object[][] expected = {
{"2016.04.03-12:00:00.0 AM AEDT", 86_400_000L},
{"2016.04.04-12:00:00.0 AM AEST", 90_000_000L}, // The long day
{"2016.04.05-12:00:00.0 AM AEST", 86_400_000L},
{"2016.04.06-12:00:00.0 AM AEST", 86_400_000L},
{"2016.04.07-12:00:00.0 AM AEST", 86_400_000L},
};
assertSequence(midnight, expected);
}
@Test
public void testFileHandling() throws Exception
{
Path testPath = testingDir.getEmptyPathDir();
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime now = toDateTime("2016.04.10-08:30:12.3 AM AEDT", zone);
Path template = testPath.resolve("test-rofos-yyyy_mm_dd.log");
String templateString = template.toAbsolutePath().toString();
try (RolloverFileOutputStream rofos =
new RolloverFileOutputStream(templateString, false, 3, TimeZone.getTimeZone(zone), null, null, now))
{
rofos.write("TICK".getBytes());
rofos.flush();
}
now = now.plus(5, ChronoUnit.MINUTES);
try (RolloverFileOutputStream rofos =
new RolloverFileOutputStream(templateString, false, 3, TimeZone.getTimeZone(zone), null, null, now))
{
rofos.write("TOCK".getBytes());
rofos.flush();
String[] ls = ls(testPath);
assertThat(ls.length, is(2));
String backup = null;
for (String n : ls)
{
if (!"test-rofos-2016_04_10.log".equals(n))
backup = n;
}
assertThat(Arrays.asList(ls), Matchers.containsInAnyOrder(backup, "test-rofos-2016_04_10.log"));
Files.setLastModifiedTime(testPath.resolve(backup), FileTime.from(now.toInstant()));
Files.setLastModifiedTime(testPath.resolve("test-rofos-2016_04_10.log"), FileTime.from(now.toInstant()));
ZonedDateTime time = now.minus(1, ChronoUnit.DAYS);
for (int i = 10; i-- > 5; )
{
String file = "test-rofos-2016_04_0" + i + ".log";
Path path = testPath.resolve(file);
FS.touch(path);
Files.setLastModifiedTime(path, FileTime.from(time.toInstant()));
if (i % 2 == 0)
{
file = "test-rofos-2016_04_0" + i + ".log.083512300";
path = testPath.resolve(file);
FS.touch(path);
Files.setLastModifiedTime(path, FileTime.from(time.toInstant()));
time = time.minus(1, ChronoUnit.DAYS);
}
file = "unrelated-" + i;
path = testPath.resolve(file);
FS.touch(path);
Files.setLastModifiedTime(path, FileTime.from(time.toInstant()));
time = time.minus(1, ChronoUnit.DAYS);
}
ls = ls(testPath);
assertThat(ls.length, is(14));
assertThat(Arrays.asList(ls), Matchers.containsInAnyOrder(
"test-rofos-2016_04_05.log",
"test-rofos-2016_04_06.log",
"test-rofos-2016_04_07.log",
"test-rofos-2016_04_08.log",
"test-rofos-2016_04_09.log",
"test-rofos-2016_04_10.log",
"test-rofos-2016_04_06.log.083512300",
"test-rofos-2016_04_08.log.083512300",
"test-rofos-2016_04_10.log.083512300",
"unrelated-9",
"unrelated-8",
"unrelated-7",
"unrelated-6",
"unrelated-5"
));
rofos.removeOldFiles(now);
ls = ls(testPath);
assertThat(ls.length, is(10));
assertThat(Arrays.asList(ls), Matchers.containsInAnyOrder(
"test-rofos-2016_04_08.log",
"test-rofos-2016_04_09.log",
"test-rofos-2016_04_10.log",
"test-rofos-2016_04_08.log.083512300",
"test-rofos-2016_04_10.log.083512300",
"unrelated-9",
"unrelated-8",
"unrelated-7",
"unrelated-6",
"unrelated-5"));
assertThat(readPath(testPath.resolve(backup)), is("TICK"));
assertThat(readPath(testPath.resolve("test-rofos-2016_04_10.log")), is("TOCK"));
}
}
@Test
public void testRollover() throws Exception
{
Path testPath = testingDir.getEmptyPathDir();
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime now = toDateTime("2016.04.10-11:59:55.0 PM AEDT", zone);
Path template = testPath.resolve("test-rofos-yyyy_mm_dd.log");
String templateString = template.toAbsolutePath().toString();
try (RolloverFileOutputStream rofos =
new RolloverFileOutputStream(templateString, false, 0, TimeZone.getTimeZone(zone), null, null, now))
{
rofos.write("BEFORE".getBytes());
rofos.flush();
String[] ls = ls(testPath);
assertThat(ls.length, is(1));
assertThat(ls[0], is("test-rofos-2016_04_10.log"));
TimeUnit.SECONDS.sleep(10);
rofos.write("AFTER".getBytes());
ls = ls(testPath);
assertThat(ls.length, is(2));
for (String n : ls)
{
String content = readPath(testPath.resolve(n));
if ("test-rofos-2016_04_10.log".equals(n))
{
assertThat(content, is("BEFORE"));
}
else
{
assertThat(content, is("AFTER"));
}
}
}
}
@Test
public void testRolloverBackup() throws Exception
{
Path testPath = testingDir.getEmptyPathDir();
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime now = toDateTime("2016.04.10-11:59:55.0 PM AEDT", zone);
Path template = testPath.resolve("test-rofosyyyy_mm_dd.log");
String templateString = template.toAbsolutePath().toString();
try (RolloverFileOutputStream rofos =
new RolloverFileOutputStream(templateString, false, 0, TimeZone.getTimeZone(zone), "", null, now))
{
rofos.write("BEFORE".getBytes());
rofos.flush();
String[] ls = ls(testPath);
assertThat("File List.length", ls.length, is(1));
assertThat(ls[0], is("test-rofos.log"));
TimeUnit.SECONDS.sleep(10);
rofos.write("AFTER".getBytes());
ls = ls(testPath);
assertThat("File List.length", ls.length, is(2));
for (String n : ls)
{
String content = readPath(testPath.resolve(n));
if ("test-rofos.log".equals(n))
{
assertThat(content, is("AFTER"));
}
else
{
assertThat(content, is("BEFORE"));
}
}
}
}
private String readPath(Path path) throws IOException
{
try (BufferedReader reader = Files.newBufferedReader(path))
{
return IO.toString(reader);
}
}
private String[] ls(Path path) throws IOException
{
try (Stream<Path> s = Files.list(path))
{
return s.map(p -> p.getFileName().toString()).toArray(String[]::new);
}
}
}