1818import java .util .UUID ;
1919import java .util .concurrent .Phaser ;
2020import java .util .concurrent .TimeUnit ;
21+ import java .util .concurrent .atomic .AtomicBoolean ;
2122import java .util .concurrent .locks .LockSupport ;
2223import java .util .stream .Stream ;
2324import org .junit .jupiter .api .Test ;
@@ -173,7 +174,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
173174 }
174175 };
175176
176- TempLocationManager mgr = instance (baseDir , blocker );
177+ TempLocationManager mgr = instance (baseDir , true , blocker );
177178
178179 // wait for the cleanup start
179180 phaser .arriveAndAwaitAdvance ();
@@ -187,8 +188,9 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
187188
188189 @ ParameterizedTest
189190 @ MethodSource ("timeoutTestArguments" )
190- void testCleanupWithTimeout (boolean selfCleanup , String section ) throws Exception {
191- long timeoutMs = 500 ;
191+ void testCleanupWithTimeout (boolean selfCleanup , boolean shouldSucceed , String section )
192+ throws Exception {
193+ long timeoutMs = 10 ;
192194 TempLocationManager .CleanupHook delayer =
193195 new TempLocationManager .CleanupHook () {
194196 @ Override
@@ -229,30 +231,63 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
229231 Files .createTempDirectory (
230232 "ddprof-test-" ,
231233 PosixFilePermissions .asFileAttribute (PosixFilePermissions .fromString ("rwx------" )));
232- TempLocationManager instance = instance (baseDir , delayer );
233- boolean rslt = instance .cleanup (selfCleanup , timeoutMs , TimeUnit .MILLISECONDS );
234- assertTrue (rslt );
234+ TempLocationManager instance = instance (baseDir , false , delayer );
235+ Path mytempdir = instance .getTempDir ();
236+ Path otherTempdir = mytempdir .getParent ().resolve ("pid_0000" );
237+ Files .createDirectories (otherTempdir );
238+ Files .createFile (mytempdir .resolve ("dummy" ));
239+ Files .createFile (otherTempdir .resolve ("dummy" ));
240+ boolean rslt =
241+ instance .cleanup (
242+ selfCleanup , (long ) (timeoutMs * (shouldSucceed ? 10 : 0.5d )), TimeUnit .MILLISECONDS );
243+ assertEquals (shouldSucceed , rslt );
244+ }
245+
246+ @ Test
247+ void testShortCircuit () throws Exception {
248+ Path baseDir =
249+ Files .createTempDirectory (
250+ "ddprof-test-" ,
251+ PosixFilePermissions .asFileAttribute (PosixFilePermissions .fromString ("rwx------" )));
252+ AtomicBoolean executed = new AtomicBoolean ();
253+ TempLocationManager .CleanupHook hook =
254+ new TempLocationManager .CleanupHook () {
255+ @ Override
256+ public void onCleanupStart (boolean selfCleanup , long timeout , TimeUnit unit ) {
257+ executed .set (true );
258+ }
259+ };
260+ TempLocationManager instance = instance (baseDir , false , hook );
261+
262+ instance .createDirStructure ();
263+
264+ boolean ret = instance .cleanup (false );
265+ assertTrue (ret );
266+ assertFalse (executed .get ());
235267 }
236268
237269 private static Stream <Arguments > timeoutTestArguments () {
238270 List <Arguments > argumentsList = new ArrayList <>();
239271 for (boolean selfCleanup : new boolean [] {true , false }) {
240272 for (String intercepted :
241273 new String [] {"preVisitDirectory" , "visitFile" , "postVisitDirectory" }) {
242- argumentsList .add (Arguments .of (selfCleanup , intercepted ));
274+ argumentsList .add (Arguments .of (selfCleanup , true , intercepted ));
275+ argumentsList .add (Arguments .of (selfCleanup , false , intercepted ));
243276 }
244277 }
245278 return argumentsList .stream ();
246279 }
247280
248- private TempLocationManager instance (Path baseDir , TempLocationManager .CleanupHook cleanupHook )
281+ private TempLocationManager instance (
282+ Path baseDir , boolean withStartupCleanup , TempLocationManager .CleanupHook cleanupHook )
249283 throws IOException {
250284 Properties props = new Properties ();
251285 props .put (ProfilingConfig .PROFILING_TEMP_DIR , baseDir .toString ());
252286 props .put (
253287 ProfilingConfig .PROFILING_UPLOAD_PERIOD ,
254288 "0" ); // to force immediate cleanup; must be a string value!
255289
256- return new TempLocationManager (ConfigProvider .withPropertiesOverride (props ), cleanupHook );
290+ return new TempLocationManager (
291+ ConfigProvider .withPropertiesOverride (props ), withStartupCleanup , cleanupHook );
257292 }
258293}
0 commit comments