doanduyhai / Achilles

An advanced Java Object Mapper/Query DSL generator for Cassandra
http://achilles.archinnov.info
Apache License 2.0
241 stars 92 forks source link

Fix Flaky Test #387

Open Alisha-0321 opened 1 year ago

Alisha-0321 commented 1 year ago

What changes were proposed in this pull request?

I observe that a test (info.archinnov.achilles.it.TestALLEntityAsChild.should_insert) is a flaky test than can fail due to the slow execution of achilles-embedded/src/main/java/info/archinnov/achilles/embedded/ServerStarter#L159. When ServerStarter.java#159 is not executed quick enough, the method (initializeFromParameters) tries to connect the cluster given at Line 63, then fails. While it may be possible to fix this flaky test by adding a waiting time before the execution of this line, but I believe such a fix might be unstable in a CI environment or when run on different machines, given the dependency on some constant wait time. Hence, I suggest a new way to fix the test by adding some synchronization for the test execution only. I at first identify the code location that needs to be executed before making the connection with the cluster (achilles-embedded/src/main/java/info/archinnov/achilles/embedded/ServerStarter#L159), so I introduce one variable in this class that is only there to provide some synchronization. Basically, until these statements are executed, I force the thread that the test runs to wait before it proceeds to the assertions.

Why are the changes needed?

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 30.377 s <<< FAILURE! - in info.archinnov.achilles.it.TestALLEntityAsChild [ERROR] should_insert(info.archinnov.achilles.it.TestALLEntityAsChild) Time elapsed: 30.377 s <<< ERROR! com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9042] Cannot connect)) at info.archinnov.achilles.it.TestALLEntityAsChild.(TestALLEntityAsChild.java:49)

[INFO] [INFO] Results: [INFO] [ERROR] Errors: [ERROR] TestALLEntityAsChild.:49 » NoHostAvailable All host(s) tried for query f... [INFO] [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

How was this patch tested?

I run this test more than 1000 times and the test always passes.