/hazelcast-client/src/test/java/com/hazelcast/client/ClientRunnableTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 85 lines · 64 code · 6 blank · 15 comment · 0 complexity · 2b1483c47177911d4572d597807d0cae MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.client;
  17. import org.junit.Test;
  18. import org.junit.runner.RunWith;
  19. import java.util.concurrent.CountDownLatch;
  20. import java.util.concurrent.TimeUnit;
  21. import static org.junit.Assert.assertFalse;
  22. import static org.junit.Assert.assertTrue;
  23. @RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
  24. public class ClientRunnableTest {
  25. @Test
  26. public void testRun() throws Exception {
  27. final CountDownLatch waitLatch = new CountDownLatch(1);
  28. final ClientRunnable clientRunnable = new ClientRunnable() {
  29. @Override
  30. protected void customRun() throws InterruptedException {
  31. waitLatch.countDown();
  32. }
  33. };
  34. final CountDownLatch latch = new CountDownLatch(1);
  35. new Thread(new Runnable() {
  36. public void run() {
  37. try {
  38. waitLatch.await(10, TimeUnit.SECONDS);
  39. synchronized (clientRunnable.monitor) {
  40. clientRunnable.running = false;
  41. clientRunnable.monitor.wait();
  42. }
  43. latch.countDown();
  44. } catch (InterruptedException e) {
  45. }
  46. }
  47. }).start();
  48. clientRunnable.run();
  49. assertTrue("Not notified", latch.await(10, TimeUnit.SECONDS));
  50. }
  51. @Test
  52. public void testShutdown() throws Exception {
  53. final ClientRunnable clientRunnable = new ClientRunnable() {
  54. @Override
  55. protected void customRun() throws InterruptedException {
  56. }
  57. };
  58. final CountDownLatch latch1 = new CountDownLatch(1);
  59. final CountDownLatch latch2 = new CountDownLatch(1);
  60. final CountDownLatch latchShutDown = new CountDownLatch(1);
  61. new Thread(new Runnable() {
  62. public void run() {
  63. try {
  64. latch1.await();
  65. latch2.countDown();
  66. clientRunnable.shutdown();
  67. latchShutDown.countDown();
  68. } catch (InterruptedException e) {
  69. }
  70. }
  71. }).start();
  72. latch1.countDown();
  73. latch2.await();
  74. Thread.sleep(10);
  75. clientRunnable.terminate();
  76. assertTrue(latchShutDown.await(5, TimeUnit.SECONDS));
  77. assertFalse(clientRunnable.running);
  78. }
  79. }