java icon indicating copy to clipboard operation
java copied to clipboard

[Umbrella Issue] Many tests use Thread.sleep instead of synchronization primitives

Open brendandburns opened this issue 5 years ago • 14 comments
trafficstars

Many of our tests simply have a Thread.sleep(duration) when testing multi-threaded code. This is inherently flaky.

Instead we should use synchronization classes (generally Semaphore) to synchronized the tests which is both faster in the general case and not flaky in the extreme case.

See for instance #1218 and #1219

brendandburns avatar Sep 03 '20 04:09 brendandburns

  • [ ] util/src/test/java/io/kubernetes/client/informer/cache/ReflectorRunnableTest.java
  • [ ] extended/src/test/java/io/kubernetes/client/extended/workqueue/DefaultDelayingQueueTest.java
  • [ ] util/src/test/java/io/kubernetes/client/informer/cache/ProcessorListenerTest.java
  • [ ] util/src/test/java/io/kubernetes/client/informer/cache/SharedProcessorTest.java
  • [x] util/src/test/java/io/kubernetes/client/CopyTest.java
  • [ ] util/src/test/java/io/kubernetes/client/informer/cache/ControllerTest.java
  • [ ] extended/src/test/java/io/kubernetes/client/extended/workqueue/DefaultWorkQueueTest.java
  • [ ] extended/src/test/java/io/kubernetes/client/extended/leaderelection/LeaderElectionTest.java
  • [ ] util/src/test/java/io/kubernetes/client/informer/impl/DefaultSharedIndexInformerTest.java
  • [ ] extended/src/test/java/io/kubernetes/client/extended/controller/LeaderElectingControllerTest.java
  • [ ] util/src/test/java/io/kubernetes/client/PortForwardTest.java
  • [ ] util/src/test/java/io/kubernetes/client/AttachTest.java
  • [ ] util/src/test/java/io/kubernetes/client/ExecTest.java
  • [ ] extended/src/test/java/io/kubernetes/client/extended/workqueue/ratelimiter/BucketRateLimiterTest.java
  • [ ] spring/src/test/java/io/kubernetes/client/spring/extended/controller/KubernetesInformerCreatorTest.java
  • [ ] spring/src/test/java/io/kubernetes/client/spring/extended/controller/KubernetesReconcilerCreatorTest.java
  • [ ] extended/src/test/java/io/kubernetes/client/extended/event/EventCorrelatorTest.java

brendandburns avatar Sep 03 '20 05:09 brendandburns

@brendandburns thanks for sorting out these potentially flaking tests.. will fix them later!

/assign

yue9944882 avatar Sep 03 '20 07:09 yue9944882

I would like to help :-)

sarveshkaushal avatar Sep 03 '20 07:09 sarveshkaushal

@sarveshkaushal we'd welcome the help. In most cases the Thread.sleep(foo) is to wait for some other thread to complete it's work. Instead, we should create a Semaphore, then acquire() one or more permits for the executions we're waiting for, and then release() them in the Thread, then attempt to acquire() in the test thread to implement the wait.

See an example PR #1219

The reason that Thread.sleep() is flaky is because in a CI/CD system like GitHub actions the machines can be massively overloaded, which leads to much longer delays than you might otherwise see when running tests, so the sleep(..) ends up not being long enough. You can always make the sleep longer, but that makes the tests run longer (and it's still flaky in extreme cases) so using synchronization primitives is (almost) always the right answer.

brendandburns avatar Sep 03 '20 15:09 brendandburns

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle stale

fejta-bot avatar Dec 03 '20 17:12 fejta-bot

Stale issues rot after 30d of inactivity. Mark the issue as fresh with /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle rotten

fejta-bot avatar Jan 02 '21 17:01 fejta-bot

/lifecycle frozen

brendandburns avatar Jan 09 '21 20:01 brendandburns

If still available I would like to contribute

Priya103 avatar Jan 20 '21 17:01 Priya103

well i am new to java and i would like to get in touch with you guys and try to do my best in solving all the issues hope you all will understand my curiosity and get in touch with me and help me to grow and learn.........

Vishal-thakur-01 avatar Aug 14 '22 06:08 Vishal-thakur-01

@brendandburns I have one doubt, I was working on util/src/test/java/io/kubernetes/client/informer/cache/SharedProcessorTest.java, particularly on: https://github.com/kubernetes-client/java/blob/536f72556899a5a951a9d1d000f4b5073429e017/util/src/test/java/io/kubernetes/client/informer/cache/SharedProcessorTest.java#L27-L60

I can create a semaphore and acquire it, blocking the main thread, but how will I release so that blocked main thread will continue the execution, I looked at many tests, facing similar problem in many of them.

Edit- I cannot access ExecutorService instance variable which would have been useful over here, please suggest.

Vyom-Yadav avatar Aug 18 '22 10:08 Vyom-Yadav

what are the things I need to learn in order to start contributiing to this project?

vinayak912002 avatar Aug 22 '22 15:08 vinayak912002

what are the things I need to learn to start contributing to this project?

DSA, API, and Kubernetes or just contribute by documentation correction or making better changes

Mohdcode avatar Aug 27 '22 05:08 Mohdcode

@brendandburns ping

Vyom-Yadav avatar Aug 27 '22 12:08 Vyom-Yadav

/assign

tmrpavan avatar Apr 10 '23 06:04 tmrpavan