My program has two threads:
- Thread A scans the filesystem for files matching certain criteria, and adds them to an internal catalog.
- Thread B scans the internal catalog for entries matching certain other criteria, and deletes them from both the catalog and the filesystem.
I've got locking that should ensure the program does the correct thing if both threads decide to operate on the same item at the same time, rather than, for example, creating a "ghost" catalog entry that refers to a deleted file. How can I verify this in an automated way?
The "correct thing" here is either "thread A notices the file, notices it's in the catalog, and skips it, then thread B notices the catalog entry, deletes the file and catalog entry, and continues" or "thread A notices the file, then thread B notices the catalog entry and deletes the file and catalog entry, then thread A notices the file has been deleted and skips it". Ideally a test would verify that both correct interleavings of operations are possible, and that no incorrect interleavings are possible.