Skip to content

Commit c0d82d3

Browse files
committed
improve tests
* add checks that token is removed from map * remove watchKey where possible * shutdown executorService after each test
1 parent 8499d3e commit c0d82d3

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

src/test/java/org/cryptomator/cryptofs/inuse/RealUseTokenTest.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.Properties;
2727
import java.util.concurrent.ConcurrentHashMap;
2828
import java.util.concurrent.ConcurrentMap;
29-
import java.util.concurrent.Executor;
3029
import java.util.concurrent.ExecutorService;
3130
import java.util.concurrent.Executors;
3231
import java.util.concurrent.TimeUnit;
@@ -66,10 +65,15 @@ public void beforeEach() throws IOException {
6665
public void afterEach() {
6766
try {
6867
watchService.close();
69-
tokenPersistor.shutdown();
70-
tokenPersistor.awaitTermination(3000, TimeUnit.MILLISECONDS);
7168
} catch (IOException _) {
7269
//no-op
70+
}
71+
72+
try {
73+
tokenPersistor.shutdown();
74+
if(!tokenPersistor.awaitTermination(3000, TimeUnit.MILLISECONDS)) {
75+
tokenPersistor.shutdownNow();
76+
}
7377
} catch (InterruptedException e) {
7478
Thread.currentThread().interrupt();
7579
}
@@ -84,7 +88,7 @@ private static void assertInUseFile(String expectedOwner, Path filePath) throws
8488
}
8589

8690
@RepeatedTest(5)
87-
@DisplayName("Creating a token creates valid inUse file and on close is deleted")
91+
@DisplayName("Creating a token creates valid inUse file, deletes file on close and removes itself from token map")
8892
public void testValidFileContent() throws IOException {
8993
var filePath = tmpDir.resolve("inUse.file");
9094
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, CREATION_DELAY_MILLIS, StandardOpenOption.CREATE_NEW, encWrapper)) {
@@ -98,21 +102,14 @@ public void testValidFileContent() throws IOException {
98102
}
99103

100104
@Test
101-
@DisplayName("After X seconds of token creation, a file is updated")
105+
@DisplayName("After X seconds of token creation, a file is updated/stolen")
102106
public void testFileSteal() throws IOException {
103107
var filePath = tmpDir.resolve("inUse.file");
104108
Files.createFile(filePath);
105-
var watchKey = tmpDir.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
106-
var fileTime = Files.getLastModifiedTime(filePath);
107109

108110
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, CREATION_DELAY_MILLIS, StandardOpenOption.TRUNCATE_EXISTING, encWrapper)) {
109-
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX).until(() -> fileTime.compareTo(Files.getLastModifiedTime(filePath)) < 0);
110-
var events = watchKey.pollEvents();
111-
var createEvent = events.stream().filter(e -> e.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)).findAny();
112-
Assertions.assertTrue(createEvent.isPresent());
113-
createEvent.ifPresent(e -> {
114-
Assertions.assertTrue(filePath.endsWith((Path) e.context()));
115-
});
111+
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX) //
112+
.untilAsserted(() -> assertInUseFile("test3000", filePath));
116113
}
117114
Assertions.assertTrue(Files.notExists(filePath));
118115
}
@@ -124,9 +121,9 @@ public void testFileStealFails() throws IOException {
124121
var watchKey = tmpDir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
125122

126123
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, CREATION_DELAY_MILLIS, StandardOpenOption.TRUNCATE_EXISTING, encWrapper)) {
127-
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX).until(token::isClosed);
124+
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX) //
125+
.until(token::isClosed);
128126
Assertions.assertTrue(Files.notExists(filePath));
129-
Assertions.assertTrue(token.isClosed());
130127
}
131128
MatcherAssert.assertThat(watchKey.pollEvents(), Matchers.empty());
132129
}
@@ -156,8 +153,11 @@ public void testMoveToBefore() throws IOException {
156153
var watchKey = tmpDir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
157154

158155
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, CREATION_DELAY_MILLIS, StandardOpenOption.CREATE_NEW, encWrapper)) {
156+
useTokens.put(filePath, token);
159157
token.moveToInternal(targetPath);
160158

159+
Assertions.assertNull(useTokens.get(filePath));
160+
Assertions.assertNotNull(useTokens.get(targetPath));
161161
//no file operation after move
162162
MatcherAssert.assertThat(watchKey.pollEvents(), Matchers.empty());
163163
// target file exists
@@ -185,6 +185,7 @@ public void testMoveToAfter() {
185185
var targetPath = tmpDir.resolve("inUseMove2.file");
186186

187187
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, CREATION_DELAY_MILLIS, StandardOpenOption.CREATE_NEW, encWrapper)) {
188+
useTokens.put(filePath, token);
188189
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX) //
189190
.untilAsserted(() -> assertInUseFile("test3000", filePath));
190191

@@ -194,6 +195,7 @@ public void testMoveToAfter() {
194195
// orginal filePath does not exist, target exists
195196
Assertions.assertTrue(Files.notExists(filePath), "inUse.file still exists after move");
196197
Assertions.assertTrue(Files.exists(targetPath), "inUse2.file does not exist after move");
198+
Assertions.assertNull(useTokens.get(filePath));
197199
Assertions.assertNotNull(useTokens.get(targetPath));
198200
}
199201
Assertions.assertNull(useTokens.get(targetPath));

0 commit comments

Comments
 (0)