Java – why not use a pseudo-random number generator to generate test data?

Test the correctness from the Book Java concurrency in practice, chapter 12.1, especially 12.1 3 test security (the author wants to set up test cases to test the data security of bounded buffer classes)

I don't understand the author's objection to using random number generator to generate test input In particular, row random number generation can create coupling between classes, and I don't know timing artifacts

>What classes and time artifacts does he refer to? > What kind of coupling can RNG create?

Solution

This is all the more clear considering the following sentence:

Memory synchronization may change the time of the program If you look at random, you can see that it uses atomicinteger, so using it will cause read-write memory obstacles, because the generation of test data may change the way and time that other threads see data Overall application

Any class that uses threads and relies on memory synchronization may be affected Basically all the threads and classes they call

As lizard @ bill commented, this book says that by using RNG, the time of the program depends on or is affected by RNG synchronization

The real lesson here is that if possible, the test data you inject into the program should not change the time of the program It is usually difficult and may not be possible, but the goal is to simulate the application behavior (time, input, output...) as much as possible in the test

For the solution, you can use another simple random algorithm that is not synchronized You can also generate a class that pre stores 10000 random numbers (or more than you need) and then distributes them without synchronization But by using classes for memory synchronization in your tests, you are changing the timing of your program

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>