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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 124 lines · 87 code · 17 blank · 20 comment · 0 complexity · 9a1bc0a1c770b6509b46d35f93fa33bd 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 com.hazelcast.config.Config;
  18. import com.hazelcast.core.*;
  19. import com.hazelcast.core.LifecycleEvent.LifecycleState;
  20. import org.junit.After;
  21. import org.junit.Assert;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.junit.runner.RunWith;
  25. import java.util.concurrent.BlockingQueue;
  26. import java.util.concurrent.LinkedBlockingQueue;
  27. import java.util.concurrent.TimeUnit;
  28. import java.util.concurrent.atomic.AtomicInteger;
  29. /**
  30. * @mdogan 4/24/12
  31. */
  32. @RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
  33. public class ListenerTest {
  34. @After
  35. @Before
  36. public void cleanup() throws Exception {
  37. Hazelcast.shutdownAll();
  38. HazelcastClient.shutdownAll();
  39. }
  40. /* github issue #183 */
  41. @Test
  42. public void testKeyBasedListeners() throws InterruptedException {
  43. try {
  44. Config config = new Config();
  45. HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
  46. IMap<String, String> map = instance.getMap("map");
  47. map.put("key1", "value1");
  48. map.put("key2", "value2");
  49. map.put("key3", "value3");
  50. ClientConfig clientConfig = new ClientConfig();
  51. HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
  52. final AtomicInteger count = new AtomicInteger(0);
  53. IMap<String, String> clientMap = client.getMap("map");
  54. clientMap.addEntryListener(new EntryListener<String, String>() {
  55. public void entryAdded(EntryEvent<String, String> entryEvent) {
  56. count.incrementAndGet();
  57. }
  58. public void entryRemoved(EntryEvent<String, String> entryEvent) {
  59. }
  60. public void entryUpdated(EntryEvent<String, String> entryEvent) {
  61. count.incrementAndGet();
  62. }
  63. public void entryEvicted(EntryEvent<String, String> entryEvent) {
  64. }
  65. },"key1" , true);
  66. clientMap.addEntryListener(new EntryListener<String, String>() {
  67. public void entryAdded(EntryEvent<String, String> entryEvent) {
  68. count.incrementAndGet();
  69. }
  70. public void entryRemoved(EntryEvent<String, String> entryEvent) {
  71. }
  72. public void entryUpdated(EntryEvent<String, String> entryEvent) {
  73. System.out.println("event map");
  74. count.incrementAndGet();
  75. }
  76. public void entryEvicted(EntryEvent<String, String> entryEvent) {
  77. }
  78. },"key2" , true);
  79. map.put("key1", "new-value1");
  80. Thread.sleep(100);
  81. Assert.assertEquals(count.get(), 1);
  82. } catch (InterruptedException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. @Test
  87. public void testConfigLifecycleListener() throws InterruptedException {
  88. ClientConfig config = new ClientConfig();
  89. final BlockingQueue<LifecycleEvent> q = new LinkedBlockingQueue<LifecycleEvent>();
  90. config.addListener(new LifecycleListener() {
  91. public void stateChanged(final LifecycleEvent event) {
  92. q.offer(event);
  93. System.out.println(event);
  94. }
  95. });
  96. Hazelcast.getDefaultInstance();
  97. HazelcastClient client = HazelcastClient.newHazelcastClient(config);
  98. Assert.assertEquals(new LifecycleEvent(LifecycleState.STARTING), q.poll(3, TimeUnit.SECONDS));
  99. Assert.assertEquals(new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_OPENING), q.poll(3, TimeUnit.SECONDS));
  100. Assert.assertEquals(new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_OPENED), q.poll(3, TimeUnit.SECONDS));
  101. Assert.assertEquals(new LifecycleEvent(LifecycleState.STARTED), q.poll(3, TimeUnit.SECONDS));
  102. client.shutdown();
  103. // Assert.assertEquals(new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_LOST), q.poll(3, TimeUnit.SECONDS));
  104. Assert.assertEquals(new LifecycleEvent(LifecycleState.SHUTTING_DOWN), q.poll(3, TimeUnit.SECONDS));
  105. Assert.assertEquals(new LifecycleEvent(LifecycleState.SHUTDOWN), q.poll(3, TimeUnit.SECONDS));
  106. }
  107. }