Unit testing for secure publishing

How would you not allow the guarantee of secure publishing in Java?

To be specific:

I have a Cache interface that has a getOrLoad method (K key, ObjectLoader loader). The fact is that if the cache cannot find an object for this key, it must load it from an ObjectLoader instance. However, Cache is required to ensure that the action of loading an object from the loader and putting it in the cache is a safe publication.

I am now in the middle of writing a general junit test for this Cache interface, and I wonder how I will verify that cache implementations adhere to this secure publishing guarantee.

Any ideas? The actual code is in the test-systest module of the code repository module in case you want to pop real files.

+4
source share
3 answers

I found a presentation by Java Pune Bill Pew, Brian Goetz, and Cliff. Click on the topic of testing parallel code. They suggested this approach, because I think this is the best I've heard:

Several manufacturers create stateful and thread-safe objects with a state-dependent hashCode implementation. Since objects are transmitted through the intended synchronization point, the hash codes are summed (stream-locally). Similarly, consumers on the other side of the gate summarize the hash codes.

At the end of the test, we summarize all the results for manufacturers and consumers, respectively. If the two amounts are equal, the test passes.

We could also use XOR as an alternative to the sum. In fact, any commutative operation will be performed. Just keep in mind that the test wiring itself should not introduce any additional synchronization.

+1
source

Perhaps you can use ConTest to at least give you a little more confidence in the correctness of your code.

You will need to run a couple of tests that run multiple threads at the same time. ConTest will then increase the likelihood that a concurrency error will actually be detected by using byte code (adding heuristically controlled conditional dreams and exit instructions).

+1
source

Actually getting an error due to unsafe publishing is very difficult (if anyone knows how, let me know). Static analysis is your best choice for an automated solution. I would stick to a code review and not worry about it unnecessarily.

0
source

Source: https://habr.com/ru/post/1277728/


All Articles