|
5 | 5 | import org.junit.jupiter.api.Test; |
6 | 6 |
|
7 | 7 | import java.io.File; |
| 8 | +import java.io.FileOutputStream; |
8 | 9 | import java.io.IOException; |
9 | 10 | import java.net.URL; |
| 11 | +import java.util.concurrent.ExecutorService; |
| 12 | +import java.util.concurrent.atomic.AtomicInteger; |
10 | 13 |
|
| 14 | +import static io.microsphere.concurrent.CustomizedThreadFactory.newThreadFactory; |
11 | 15 | import static io.microsphere.io.FileUtils.cleanDirectory; |
12 | 16 | import static io.microsphere.io.FileUtils.deleteDirectory; |
13 | 17 | import static io.microsphere.io.FileUtils.forceDelete; |
|
21 | 25 | import static io.microsphere.util.StringUtils.EMPTY_STRING; |
22 | 26 | import static io.microsphere.util.SystemUtils.IS_OS_WINDOWS; |
23 | 27 | import static io.microsphere.util.SystemUtils.JAVA_IO_TMPDIR; |
| 28 | +import static java.util.concurrent.Executors.newFixedThreadPool; |
| 29 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
24 | 30 | import static org.junit.jupiter.api.Assertions.assertEquals; |
25 | 31 | import static org.junit.jupiter.api.Assertions.assertFalse; |
26 | 32 | import static org.junit.jupiter.api.Assertions.assertNull; |
@@ -147,7 +153,6 @@ public void testDeleteDirectoryOnIOException() throws Exception { |
147 | 153 | // while (true) { |
148 | 154 | // try { |
149 | 155 | // deleteDirectory(testDir); |
150 | | -// sleep(500); |
151 | 156 | // } catch (IOException e) { |
152 | 157 | // exception = e; |
153 | 158 | // running.set(false); |
@@ -222,79 +227,47 @@ public void testForceDeleteOnFileNotExists() { |
222 | 227 |
|
223 | 228 | @Test |
224 | 229 | public void testForceDeleteOnIOException() throws Exception { |
| 230 | + File testFile = createRandomTempFile(); |
| 231 | + |
| 232 | + ExecutorService executor = newFixedThreadPool(3, newThreadFactory("testForceDelete-", true)); |
| 233 | + |
| 234 | + // status : 0 -> init |
| 235 | + // status : 1 -> writing |
| 236 | + // status : 2 -> deleting |
| 237 | + AtomicInteger status = new AtomicInteger(0); |
| 238 | + |
| 239 | + executor.submit(() -> { |
| 240 | + synchronized (testFile) { |
| 241 | + try (FileOutputStream outputStream = new FileOutputStream(testFile, true)) { |
| 242 | + outputStream.write('a'); |
| 243 | + status.set(1); |
| 244 | + // wait for notification |
| 245 | + testFile.wait(); |
| 246 | + } |
| 247 | + } |
| 248 | + return null; |
| 249 | + }); |
225 | 250 |
|
226 | | -// if (IS_OS_WINDOWS) { |
227 | | -// File testFile = createRandomTempFile(); |
228 | | -// |
229 | | -// ExecutorService executor = newFixedThreadPool(3); |
230 | | -// |
231 | | -// // status : 0 -> init |
232 | | -// // status : 1 -> writing |
233 | | -// // status : 2 -> deleting |
234 | | -// AtomicInteger status = new AtomicInteger(0); |
235 | | -// |
236 | | -// executor.submit(() -> { |
237 | | -// synchronized (testFile) { |
238 | | -// try (FileOutputStream outputStream = new FileOutputStream(testFile, true)) { |
239 | | -// outputStream.write('a'); |
240 | | -// status.set(1); |
241 | | -// // wait for notification |
242 | | -// testFile.wait(); |
243 | | -// } |
244 | | -// } |
245 | | -// return null; |
246 | | -// }); |
247 | | -// |
248 | | -// executor.submit(() -> { |
249 | | -// while (status.get() != 1) { |
250 | | -// } |
251 | | -// assertThrows(IOException.class, () -> forceDelete(testFile)); |
252 | | -// status.set(2); |
253 | | -// return null; |
254 | | -// }); |
255 | | -// |
256 | | -// executor.submit(() -> { |
257 | | -// while (status.get() != 2) { |
258 | | -// } |
259 | | -// synchronized (testFile) { |
260 | | -// testFile.notify(); |
261 | | -// } |
262 | | -// return null; |
263 | | -// }); |
264 | | -// |
265 | | -// executor.awaitTermination(100, MILLISECONDS); |
266 | | -// |
267 | | -// executor.shutdown(); |
268 | | -// return; |
269 | | -// } |
270 | | -// |
271 | | -// File root = new File(USER_HOME); |
272 | | -// Path readOnlyFilePath = walkFileTree(root.toPath(), new SimpleFileVisitor<Path>() { |
273 | | -// @Override |
274 | | -// public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
275 | | -// if (!attrs.isRegularFile()) { |
276 | | -// return CONTINUE; |
277 | | -// } |
278 | | -// if (attrs instanceof DosFileAttributes) { |
279 | | -// DosFileAttributes dosFileAttributes = (DosFileAttributes) attrs; |
280 | | -// if (dosFileAttributes.isReadOnly()) { |
281 | | -// return TERMINATE; |
282 | | -// } |
283 | | -// } else if (attrs instanceof PosixFileAttributes) { |
284 | | -// PosixFileAttributes posixFileAttributes = (PosixFileAttributes) attrs; |
285 | | -// Set<PosixFilePermission> permissions = posixFileAttributes.permissions(); |
286 | | -// if (!permissions.contains(OWNER_WRITE)) { |
287 | | -// return TERMINATE; |
288 | | -// |
289 | | -// } |
290 | | -// } |
291 | | -// return super.visitFile(file, attrs); |
292 | | -// } |
293 | | -// }); |
294 | | -// |
295 | | -// if (exists(readOnlyFilePath)) { |
296 | | -// assertThrows(IOException.class, () -> forceDelete(readOnlyFilePath.toFile())); |
297 | | -// } |
| 251 | + executor.submit(() -> { |
| 252 | + while (status.get() != 1) { |
| 253 | + } |
| 254 | + assertThrows(IOException.class, () -> forceDelete(testFile)); |
| 255 | + status.set(2); |
| 256 | + return null; |
| 257 | + }); |
| 258 | + |
| 259 | + executor.submit(() -> { |
| 260 | + while (status.get() != 2) { |
| 261 | + } |
| 262 | + synchronized (testFile) { |
| 263 | + testFile.notify(); |
| 264 | + } |
| 265 | + return null; |
| 266 | + }); |
| 267 | + |
| 268 | + executor.awaitTermination(100, MILLISECONDS); |
| 269 | + |
| 270 | + executor.shutdown(); |
298 | 271 | } |
299 | 272 |
|
300 | 273 | @Test |
|
0 commit comments