@Test
  public void testConfigLifecycleListener() throws InterruptedException {
    ClientConfig config = new ClientConfig();
    final BlockingQueue<LifecycleEvent> q = new LinkedBlockingQueue<LifecycleEvent>();
    config.addListener(
        new LifecycleListener() {
          public void stateChanged(final LifecycleEvent event) {
            q.offer(event);
            System.out.println(event);
          }
        });
    Hazelcast.getDefaultInstance();
    HazelcastClient client = HazelcastClient.newHazelcastClient(config);

    Assert.assertEquals(new LifecycleEvent(LifecycleState.STARTING), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(
        new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_OPENING), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(
        new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_OPENED), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(new LifecycleEvent(LifecycleState.STARTED), q.poll(3, TimeUnit.SECONDS));
    client.shutdown();
    //        Assert.assertEquals(new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_LOST),
    // q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(
        new LifecycleEvent(LifecycleState.SHUTTING_DOWN), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(new LifecycleEvent(LifecycleState.SHUTDOWN), q.poll(3, TimeUnit.SECONDS));
  }
  /* github issue #183 */
  @Test
  public void testKeyBasedListeners() throws InterruptedException {
    try {
      Config config = new Config();
      HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
      IMap<String, String> map = instance.getMap("map");
      map.put("key1", "value1");
      map.put("key2", "value2");
      map.put("key3", "value3");

      ClientConfig clientConfig = new ClientConfig();
      HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

      final AtomicInteger count = new AtomicInteger(0);
      IMap<String, String> clientMap = client.getMap("map");

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryRemoved(EntryEvent<String, String> entryEvent) {}

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key1",
          true);

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryRemoved(EntryEvent<String, String> entryEvent) {}

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              System.out.println("event map");
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key2",
          true);

      map.put("key1", "new-value1");
      Thread.sleep(100);
      Assert.assertEquals(count.get(), 1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }