PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

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