This PR provides a regression test for 
[JDK-8325179](https://bugs.openjdk.org/browse/JDK-8325179): _Race in 
BasicDirectoryModel.validateFileCache_ reviewed in #18111.

The test is inspired and based on `ConcurrentModification` that I wrote for 
[JDK-8327137](https://bugs.openjdk.org/browse/JDK-8327137) / 
[JDK-8323670](https://bugs.openjdk.org/browse/JDK-8323670).

**How the test works**

The test creates a temporary directory in the current directory and creates a 
number of files in it. (The number of files is controlled by 
`NUMBER_OF_THREADS` constant). Then the test creates `JFileChooser` in the 
temporary directory.

The test starts several scanner threads, the number is controlled by 
`NUMBER_OF_THREADS`, which repeatedly call 
`fileChooser.rescanCurrentDirectory()`. This results in calling 
`BasicDirectoryModel.validateFileCache` which starts a background thread — 
"Basic L&F File Loading Thread" — to enumerate the files.

The test runner thread and scanner threads are synchronised with 
`CyclicBarrier`, this ensures all the scanner threads start at the same time. 
After a short delay, the runner thread takes a snapshot of live threads. After 
a longer delay, the operation is repeated. See the `getThreadSnapshot` method. 
(The number of snapshots is controlled by `SNAPSHOTS` constant.)

The number of File Loading Threads in each snapshot is counted. There should be 
no more than two threads. It is possible that thread two such threads after 
JDK-8325179 is fixed: the existing thread is interrupted but hasn't exited yet, 
and a new thread is already created.

The test fails consistently without the fix for JDK-8325179. On Windows, the 
output looks like this:


Number of snapshots: 20
Number of snapshots where number of loader threads:
  = 1: 0
  = 2: 0
  > 2: 20
java.lang.RuntimeException: Detected 20 snapshots with several loading threads
        at LoaderThreadCount.runTest(LoaderThreadCount.java:132)
        at LoaderThreadCount.wrapper(LoaderThreadCount.java:72)
        at java.base/java.lang.Thread.run(Thread.java:1583)


On Linux and macOS, there's more variation, for example I got the following 
output on one of Linux systems:


Number of snapshots: 15
Number of snapshots where number of loader threads:
  = 1: 7
  = 2: 2
  > 2: 6


The test passes on the builds where JDK-8325179 is present.

-------------

Commit messages:
 - Add copyright header; ensure headless mode (for macOS)
 - Merge master
 - Remove printing number of loader threads in each snapshot
 - Add comments to clarify how thread snapshots are captured
 - Use > 2 condition for error case
 - Add a test for JDK-8325179: count FilesLoader threads

Changes: https://git.openjdk.org/jdk/pull/18957/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=18957&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8331142
  Stats: 263 lines in 1 file changed: 263 ins; 0 del; 0 mod
  Patch: https://git.openjdk.org/jdk/pull/18957.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/18957/head:pull/18957

PR: https://git.openjdk.org/jdk/pull/18957

Reply via email to