PageRenderTime 113ms CodeModel.GetById 20ms app.highlight 79ms RepoModel.GetById 1ms app.codeStats 1ms

/hazelcast/src/test/java/com/hazelcast/impl/ClusterTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 2656 lines | 2397 code | 165 blank | 94 comment | 122 complexity | 3ae228378b54e5f9585ad4e09ed7fb9a MD5 | raw file

Large files files are truncated, but you can click here to view the full 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.impl;
  18
  19import com.hazelcast.cluster.AddOrRemoveConnection;
  20import com.hazelcast.config.*;
  21import com.hazelcast.core.*;
  22import com.hazelcast.examples.TestApp;
  23import com.hazelcast.monitor.DistributedMapStatsCallable;
  24import com.hazelcast.nio.Address;
  25import com.hazelcast.partition.MigrationEvent;
  26import com.hazelcast.partition.MigrationListener;
  27import com.hazelcast.partition.Partition;
  28import com.hazelcast.util.Clock;
  29import com.hazelcast.util.ConcurrentHashSet;
  30import org.junit.*;
  31import org.junit.runner.RunWith;
  32
  33import java.io.Serializable;
  34import java.util.*;
  35import java.util.concurrent.*;
  36import java.util.concurrent.atomic.AtomicBoolean;
  37import java.util.concurrent.atomic.AtomicInteger;
  38import java.util.concurrent.atomic.AtomicLong;
  39
  40import static com.hazelcast.impl.TestUtil.*;
  41import static java.lang.Thread.sleep;
  42import static org.junit.Assert.*;
  43
  44/**
  45 * Run these tests with
  46 * -Xms512m -Xmx512m
  47 */
  48@RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
  49public class ClusterTest {
  50
  51    @BeforeClass
  52    public static void init() throws Exception {
  53        System.setProperty(GroupProperties.PROP_WAIT_SECONDS_BEFORE_JOIN, "1");
  54        System.setProperty(GroupProperties.PROP_VERSION_CHECK_ENABLED, "false");
  55        Hazelcast.shutdownAll();
  56    }
  57
  58    @After
  59    public void cleanup() throws Exception {
  60        Hazelcast.shutdownAll();
  61    }
  62
  63    @Test
  64    public void testIdle() throws Exception {
  65        Config config = new Config();
  66        MapConfig mapConfig = config.getMapConfig("default");
  67        mapConfig.setMaxIdleSeconds(3);
  68        mapConfig.setEvictionDelaySeconds(10);
  69        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
  70        Map map = h1.getMap("default");
  71        map.put(1, 1);
  72        assertEquals(1, map.get(1));
  73        sleep(2000);
  74        assertEquals(1, map.get(1));
  75        sleep(2000);
  76        assertEquals(1, map.get(1));
  77        sleep(4000);
  78        assertNull(map.get(1));
  79        assertEquals(0, map.size());
  80    }
  81
  82    @Test
  83    public void testPartitions() throws Exception {
  84        final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
  85        assertEquals(271, getLocalPartitions(h1).size());
  86        final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
  87        assertEquals(271, getLocalPartitions(h1).size() + getLocalPartitions(h2).size());
  88    }
  89
  90    @Test
  91    public void testAtomicNumber() throws Exception {
  92        final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
  93        final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
  94        AtomicNumber a1 = h1.getAtomicNumber("default");
  95        AtomicNumber a2 = h2.getAtomicNumber("default");
  96        assertEquals(1, a1.incrementAndGet());
  97        assertEquals(1, a1.get());
  98        assertEquals(1, a2.get());
  99        assertEquals(5, a2.addAndGet(4));
 100        assertEquals(5, a1.getAndSet(13));
 101        assertEquals(13, a1.get());
 102        assertEquals(13, a2.get());
 103        h1.getLifecycleService().shutdown();
 104        assertEquals(13, a2.getAndSet(21));
 105        assertEquals(21, a2.get());
 106        final HazelcastInstance h3 = Hazelcast.newHazelcastInstance(new Config());
 107        AtomicNumber a3 = h3.getAtomicNumber("default");
 108        assertEquals(20, a3.decrementAndGet());
 109        assertEquals(20, a2.getAndAdd(-20));
 110        assertFalse(a2.compareAndSet(1, 6));
 111        assertFalse(a3.compareAndSet(1, 6));
 112        assertTrue(a2.compareAndSet(0, 6));
 113        assertTrue(a3.compareAndSet(6, 0));
 114        assertEquals(0, a3.get());
 115        assertEquals(0, a2.get());
 116        h2.getLifecycleService().shutdown();
 117        assertEquals(0, a3.get());
 118    }
 119
 120    @Test
 121    public void testFirstNodeWait() throws Exception {
 122        final Config config = new Config();
 123        final BlockingQueue<Integer> counts = new ArrayBlockingQueue<Integer>(2);
 124        final HazelcastInstance[] instances = new HazelcastInstance[2];
 125        for (int i = 0; i < 2; i++) {
 126            instances[i] = Hazelcast.newHazelcastInstance(config);
 127        }
 128        for (int j = 0; j < 2; j++) {
 129            final int instanceIndex = j;
 130            new Thread(new Runnable() {
 131                public void run() {
 132                    final HazelcastInstance h = instances[instanceIndex];
 133                    for (int i = 0; i < 3000; i++) {
 134                        h.getMap("default").put(i, "value");
 135                    }
 136                    counts.offer(getLocalPartitions(h).size());
 137                }
 138            }).start();
 139        }
 140        int first = counts.take();
 141        int second = counts.take();
 142        assertTrue("Found " + first, first > 134);
 143        assertTrue("Found " + second, second > 134);
 144        assertEquals(271, second + first);
 145    }
 146
 147    private Set<Partition> getLocalPartitions(HazelcastInstance h) {
 148        Set<Partition> partitions = h.getPartitionService().getPartitions();
 149        Set<Partition> localPartitions = new HashSet<Partition>();
 150        for (Partition partition : partitions) {
 151            if (h.getCluster().getLocalMember().equals(partition.getOwner())) {
 152                localPartitions.add(partition);
 153            }
 154        }
 155        return localPartitions;
 156    }
 157
 158    @Test(timeout = 120000, expected = RuntimeException.class)
 159    public void testPutAfterShutdown() throws InterruptedException {
 160        final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 161        Map map = h1.getMap("default");
 162        h1.shutdown();
 163        map.put("1", "value");
 164    }
 165
 166    @Test(timeout = 120000)
 167    public void testSuperClientPartitionOwnership() throws Exception {
 168        Config configSuperClient = new Config();
 169        configSuperClient.setLiteMember(true);
 170        HazelcastInstance hNormal = Hazelcast.newHazelcastInstance(new Config());
 171        final HazelcastInstance hSuper = Hazelcast.newHazelcastInstance(configSuperClient);
 172        IMap mapSuper = hSuper.getMap("default");
 173        IMap mapNormal = hNormal.getMap("default");
 174        for (int i = 0; i < 1000; i++) {
 175            mapNormal.put("item" + i, "value" + i);
 176        }
 177        for (int i = 1000; i < 2000; i++) {
 178            mapSuper.put("item" + i, "value" + i);
 179        }
 180        Set<Partition> partitions2 = hSuper.getPartitionService().getPartitions();
 181        for (Partition partition : partitions2) {
 182            assertEquals(partition.getOwner(), hNormal.getCluster().getLocalMember());
 183        }
 184        assertEquals(2000, mapNormal.size());
 185        assertEquals(2000, mapSuper.size());
 186        assertEquals(0, mapSuper.getLocalMapStats().getOwnedEntryCount());
 187        assertEquals(0, mapSuper.getLocalMapStats().getBackupEntryCount());
 188        assertEquals(2000, mapNormal.getLocalMapStats().getOwnedEntryCount());
 189        assertEquals(0, mapNormal.getLocalMapStats().getBackupEntryCount());
 190        hNormal.shutdown();
 191        Thread.sleep(3000);
 192        Set<Partition> partitions = hSuper.getPartitionService().getPartitions();
 193        for (Partition partition : partitions) {
 194            assertNull(partition.getOwner());
 195        }
 196        hNormal = Hazelcast.newHazelcastInstance(new Config());
 197        partitions = hSuper.getPartitionService().getPartitions();
 198        for (Partition partition : partitions) {
 199            assertEquals(hNormal.getCluster().getLocalMember(), partition.getOwner());
 200        }
 201        assertNull(mapSuper.put("1", "value"));
 202        hSuper.shutdown();
 203        partitions = hNormal.getPartitionService().getPartitions();
 204        for (Partition partition : partitions) {
 205            assertEquals(hNormal.getCluster().getLocalMember(), partition.getOwner());
 206        }
 207        assertEquals("value", hNormal.getMap("default").get("1"));
 208    }
 209
 210    @Test(timeout = 50000)
 211    public void testPutAfterRestart() {
 212        final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 213        Map map = h1.getMap("default");
 214        h1.getLifecycleService().restart();
 215        map.put("1", "value");
 216    }
 217
 218    @Test
 219    public void testSuperBeingMaster() throws Exception {
 220        Config config = new Config();
 221        config.setLiteMember(true);
 222        final HazelcastInstance hSuper = Hazelcast.newHazelcastInstance(config);
 223        final HazelcastInstance hSuper2 = Hazelcast.newHazelcastInstance(config);
 224        sleep(11000);
 225        HazelcastInstance hNormal = Hazelcast.newHazelcastInstance(new Config());
 226        Map map = hSuper.getMap("default");
 227        map.put("1", "value");
 228        assertEquals("value", hNormal.getMap("default").get("1"));
 229        sleep(10000);
 230        assertEquals("value", hNormal.getMap("default").get("1"));
 231        assertEquals("value", map.get("1"));
 232    }
 233
 234    @Test(timeout = 120000)
 235    public void testSuperClientPutAfterBeforeNormalMember() throws Exception {
 236        final CountDownLatch latch = new CountDownLatch(1);
 237        final CountDownLatch latchSuperPut = new CountDownLatch(1);
 238        new Thread(new Runnable() {
 239            public void run() {
 240                Config config = new Config();
 241                config.setLiteMember(true);
 242                final HazelcastInstance hSuper = Hazelcast.newHazelcastInstance(config);
 243                latch.countDown();
 244                Map map = hSuper.getMap("default");
 245                map.put("1", "value");
 246                latchSuperPut.countDown();
 247            }
 248        }).start();
 249        assertTrue(latch.await(10, TimeUnit.SECONDS));
 250        HazelcastInstance hNormal = Hazelcast.newHazelcastInstance(new Config());
 251        assertTrue(latchSuperPut.await(10, TimeUnit.SECONDS));
 252        assertEquals("value", hNormal.getMap("default").get("1"));
 253    }
 254
 255    @Test(timeout = 120000)
 256    public void testRestart() throws Exception {
 257        final HazelcastInstance h = Hazelcast.newHazelcastInstance(new Config());
 258        IMap map = h.getMap("default");
 259        map.put("1", "value");
 260        final CountDownLatch latch = new CountDownLatch(1);
 261        Thread interrupter = new Thread(new Runnable() {
 262            public void run() {
 263                try {
 264                    sleep(1000);
 265                    h.getLifecycleService().restart();
 266                    latch.countDown();
 267                } catch (Throwable e) {
 268                    e.printStackTrace();
 269                }
 270            }
 271        });
 272        interrupter.start();
 273        map.put("1", "value2");
 274        assertTrue(latch.await(10, TimeUnit.SECONDS));
 275    }
 276
 277    @Test(timeout = 120000)
 278    public void testRestart2() throws Exception {
 279        HazelcastInstance h = Hazelcast.newHazelcastInstance(new Config());
 280        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 281        IMap map = h2.getMap("default");
 282        map.put("1", "value1");
 283        assertEquals(2, h.getCluster().getMembers().size());
 284        h2.getLifecycleService().restart();
 285        sleep(400);
 286        assertEquals("value1", map.get("1"));
 287        assertEquals("value1", map.put("1", "value2"));
 288        assertEquals("value2", map.get("1"));
 289        assertEquals("value2", h.getMap("default").get("1"));
 290    }
 291
 292    @Test
 293    public void issue397MapReplaceLeadsToMemoryLeak() {
 294        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 295        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 296        IMap map1 = h1.getMap("def");
 297        Object old = map1.replace(1, "v");
 298        assertNull(old);
 299        old = map1.replace(200, "v");
 300        assertNull(old);
 301        old = map1.replace(140, "v");
 302        assertNull(old);
 303        old = map1.replace(55, "v");
 304        assertNull(old);
 305        assertFalse(map1.containsKey(1));
 306        assertFalse(map1.containsKey(200));
 307        assertFalse(map1.containsKey(55));
 308        assertEquals(0, map1.getLocalMapStats().getBackupEntryCount());
 309        IMap map2 = h2.getMap("def");
 310        assertEquals(0, map2.getLocalMapStats().getBackupEntryCount());
 311    }
 312
 313    @Test
 314    public void issue452SetMigration() throws InterruptedException {
 315        Config config = new Config();
 316        final CountDownLatch latch = new CountDownLatch(1);
 317        config.addListenerConfig(new ListenerConfig(new MigrationListener() {
 318            public void migrationStarted(MigrationEvent migrationEvent) {
 319            }
 320
 321            public void migrationCompleted(MigrationEvent migrationEvent) {
 322                latch.countDown();
 323            }
 324
 325            public void migrationFailed(final MigrationEvent migrationEvent) {
 326            }
 327        }));
 328        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
 329        ISet set1 = h1.getSet("mySet");
 330        for (int i = 0; i < 1000; i++) {
 331            set1.add(i);
 332        }
 333        assertEquals(1000, set1.size());
 334        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 335        HazelcastInstance h3 = Hazelcast.newHazelcastInstance(new Config());
 336        ISet set2 = h2.getSet("mySet");
 337        ISet set3 = h3.getSet("mySet");
 338        assertTrue(latch.await(30, TimeUnit.SECONDS));
 339        assertEquals(1000, set1.size());
 340        assertEquals(1000, set2.size());
 341        assertEquals(1000, set3.size());
 342        h2.getLifecycleService().shutdown();
 343        assertEquals(1000, set1.size());
 344        assertEquals(1000, set3.size());
 345    }
 346
 347    @Test(timeout = 60000)
 348    public void testMapReplaceIfSame() throws Exception {
 349        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 350        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 351        IMap map1 = h1.getMap("default");
 352        IMap map2 = h2.getMap("default");
 353        map1.put("1", "value1");
 354        assertEquals(2, h1.getCluster().getMembers().size());
 355        assertEquals("value1", map1.get("1"));
 356        assertEquals("value1", map2.get("1"));
 357        assertTrue(map2.replace("1", "value1", "value2"));
 358        assertTrue(map1.replace("1", "value2", "value3"));
 359    }
 360
 361    @Test
 362    public void testMapReplaceIfSame2() throws Exception {
 363        HazelcastInstance hz = Hazelcast.newHazelcastInstance(new Config());
 364        final IMap<String, String> map = hz.getMap("default");
 365        final int loop = 50000;
 366        final String key = "key";
 367        final String value = "value";
 368        map.put(key, value);
 369
 370        final int threads = 10;
 371        final CountDownLatch latch = new CountDownLatch(loop);
 372        ExecutorService ex = Executors.newFixedThreadPool(threads);
 373        try {
 374            for (int i = 0; i < loop; i++) {
 375                ex.execute(new Runnable() {
 376                    public void run() {
 377                        if (map.replace(key, value, value)) {
 378                            latch.countDown();
 379                        }
 380                    }
 381                });
 382            }
 383        } finally {
 384            ex.shutdown();
 385            ex.awaitTermination(5, TimeUnit.SECONDS);
 386        }
 387        assertTrue(latch.await(30, TimeUnit.SECONDS));
 388    }
 389
 390    @Test
 391    public void testMapReplaceIfSame3() throws Exception {
 392        HazelcastInstance hz = Hazelcast.newHazelcastInstance(new Config());
 393        final IMap<String, Counter> map = hz.getMap("default");
 394        final int loop = 10000;
 395        final String id = "key";
 396        map.put(id, new Counter(id, 0L));
 397
 398        Thread[] threads = new Thread[2];
 399        for (int k = 0; k < threads.length; k++) {
 400            threads[k] = new Thread(new Runnable() {
 401                public void run() {
 402                    for (int i = 0; i < loop; i++) {
 403                        increment();
 404                    }
 405                }
 406                void increment() {
 407                    for (; ; ) {
 408                        Counter oldCounter = map.get(id);
 409                        if (oldCounter == null) throw new IllegalArgumentException();
 410                        Counter newCounter = new Counter(id, oldCounter.value);
 411                        newCounter.inc();
 412                        if (map.replace(id, oldCounter, newCounter)) {
 413                            return;
 414                        }
 415                    }
 416                }
 417            });
 418        }
 419
 420        for (Thread thread : threads) thread.start();
 421
 422        for (Thread thread : threads) thread.join();
 423
 424        long actualCount = map.get(id).value;
 425        long expectedCount = loop * threads.length;
 426        assertEquals(expectedCount, actualCount);
 427    }
 428
 429    @Test
 430    public void testMapRemoveIfSame() throws Exception {
 431        HazelcastInstance hz = Hazelcast.newHazelcastInstance(new Config());
 432        final IMap<String, Counter> map = hz.getMap("default");
 433        final int loop = 10000;
 434        final String key = "key";
 435
 436        final int t = 3;
 437        final ExecutorService ex = Executors.newFixedThreadPool(t);
 438        try {
 439            final AtomicInteger k = new AtomicInteger();
 440            for (int i = 0; i < loop; i++) {
 441                final Counter c = new Counter(key, i);
 442                map.put(key, c) ;
 443                final CountDownLatch latch = new CountDownLatch(t);
 444                for (int j = 0; j < t; j++) {
 445                    ex.submit(new Runnable() {
 446                        public void run() {
 447                            if (map.remove(key, c)) {
 448                                k.incrementAndGet();
 449                            }
 450                            latch.countDown();
 451                        }
 452                    });
 453                }
 454                assertTrue(latch.await(3, TimeUnit.SECONDS));
 455                assertEquals("Remove if same should be successful only once! ["
 456                         + i + "]", 1, k.getAndSet(0));
 457            }
 458        } finally {
 459            ex.shutdownNow();
 460        }
 461    }
 462
 463    static class Counter implements Serializable {
 464
 465        private String id;
 466        private long value;
 467
 468        public Counter() {
 469        }
 470
 471        public Counter(String id, long value) {
 472            this.value = value;
 473            this.id = id;
 474        }
 475
 476        public void inc() {
 477            value++;
 478        }
 479
 480        public boolean equals(Object thatObject) {
 481            if (thatObject == this) return true;
 482            if (!(thatObject instanceof Counter)) return false;
 483            Counter that = (Counter) thatObject;
 484            if (!(that.id.equals(this.id))) return false;
 485            if (that.value != this.value) return false;
 486            return true;
 487        }
 488
 489        public int hashCode() {
 490            int hash = id.hashCode();
 491            hash = 31 * hash + ((int) (value ^ (value >>> 32)));
 492            return hash;
 493        }
 494    }
 495
 496
 497    @Test
 498    public void testLockInstance() {
 499        ILock lock = Hazelcast.getLock("testLock");
 500        lock.lock();
 501        Collection<Instance> instances = Hazelcast.getInstances();
 502        boolean found = false;
 503        for (Instance instance : instances) {
 504            if (instance.getInstanceType().isLock()) {
 505                ILock lockInstance = (ILock) instance;
 506                if (lockInstance.getLockObject().equals("testLock")) {
 507                    found = true;
 508                }
 509            }
 510        }
 511        Assert.assertTrue(found);
 512        instances = Hazelcast.getInstances();
 513        found = false;
 514        for (Instance instance : instances) {
 515            if (instance.getInstanceType().isLock()) {
 516                ILock lockInstance = (ILock) instance;
 517                if (lockInstance.getLockObject().equals("testLock2")) {
 518                    found = true;
 519                }
 520            }
 521        }
 522        assertFalse(found);
 523        Hazelcast.getLock("testLock2");
 524        instances = Hazelcast.getInstances();
 525        found = false;
 526        for (Instance instance : instances) {
 527            if (instance.getInstanceType().isLock()) {
 528                ILock lockInstance = (ILock) instance;
 529                if (lockInstance.getLockObject().equals("testLock2")) {
 530                    found = true;
 531                }
 532            }
 533        }
 534        Assert.assertTrue(found);
 535    }
 536
 537    @Test
 538    public void testPutIfAbsentWhenThereIsTTL() throws InterruptedException {
 539        String mapName = "testTTL";
 540        int ttl = 1;
 541        Config myConfig = configTTLForMap(mapName, ttl);
 542        HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(myConfig);
 543        IMap<String, String> myMap = hazelcast.getMap(mapName);
 544        String key = "1";
 545        String value = "value1";
 546        myMap.put(key, value);
 547        assertEquals(value, myMap.get(key));
 548        assertTrue(myMap.containsKey(key));
 549        sleep((ttl + 1) * 1000);
 550        assertFalse(myMap.containsKey(key));
 551        assertNull(myMap.get(key));
 552        assertNull(myMap.putIfAbsent(key, "value2"));
 553    }
 554
 555    @Test
 556    public void testPutIfAbsentWhenThereIsTTLAndRemovedBeforeTTL() throws InterruptedException {
 557        String mapName = "testTTL";
 558        int ttl = 1;
 559        Config myConfig = configTTLForMap(mapName, ttl);
 560        HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(myConfig);
 561        IMap<String, String> myMap = hazelcast.getMap(mapName);
 562        String key = "1";
 563        String value = "value1";
 564        myMap.put(key, value);
 565        assertEquals(value, myMap.get(key));
 566        assertTrue(myMap.containsKey(key));
 567        assertEquals(value, myMap.remove(key));
 568        sleep((ttl + 1) * 1000);
 569        assertFalse(myMap.containsKey(key));
 570        assertNull(myMap.get(key));
 571        assertNull(myMap.putIfAbsent(key, "value2"));
 572    }
 573
 574    private Config configTTLForMap(String mapName, int ttl) {
 575        Config myConfig = new Config();
 576        Map<String, MapConfig> myHazelcastMapConfigs = myConfig.getMapConfigs();
 577        MapConfig myMapConfig = myHazelcastMapConfigs.get(mapName);
 578        if (myMapConfig == null) {
 579            myMapConfig = new MapConfig();
 580            myMapConfig.setName(mapName);
 581            myMapConfig.setTimeToLiveSeconds(ttl);
 582            myConfig.addMapConfig(myMapConfig);
 583        } else {
 584            myMapConfig.setTimeToLiveSeconds(ttl);
 585        }
 586        return myConfig;
 587    }
 588
 589    @Test(timeout = 120000)
 590    public void testDifferentGroups() {
 591        Config c1 = new Config();
 592        c1.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
 593        c1.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
 594        c1.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1");
 595        c1.getNetworkConfig().getInterfaces().clear();
 596        c1.getNetworkConfig().getInterfaces().addInterface("127.0.0.1");
 597        c1.getNetworkConfig().getInterfaces().setEnabled(true);
 598        Config c2 = new Config();
 599        c2.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
 600        c2.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
 601        c2.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1");
 602        c2.getNetworkConfig().getInterfaces().clear();
 603        c2.getNetworkConfig().getInterfaces().addInterface("127.0.0.1");
 604        c2.getNetworkConfig().getInterfaces().setEnabled(true);
 605        c1.getGroupConfig().setName("sameGroup");
 606        c2.getGroupConfig().setName("sameGroup");
 607        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(c1);
 608        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(c2);
 609        assertEquals(2, h1.getCluster().getMembers().size());
 610        assertEquals(2, h2.getCluster().getMembers().size());
 611        Hazelcast.shutdownAll();
 612        c2.getGroupConfig().setName("differentGroup");
 613        h1 = Hazelcast.newHazelcastInstance(c1);
 614        h2 = Hazelcast.newHazelcastInstance(c2);
 615        assertEquals(1, h1.getCluster().getMembers().size());
 616        assertEquals(1, h2.getCluster().getMembers().size());
 617    }
 618
 619    @Test(timeout = 60000)
 620    public void shutdownSuperClient() {
 621        Config c1 = new Config();
 622        Config c2 = new Config();
 623        c2.setLiteMember(true);
 624        HazelcastInstance hNormal = Hazelcast.newHazelcastInstance(c1);
 625        HazelcastInstance hSuper = Hazelcast.newHazelcastInstance(c2);
 626        hNormal.getMap("default").put("1", "first");
 627        assert hSuper.getMap("default").
 628                get("1").equals("first");
 629        hNormal.shutdown();
 630        hSuper.shutdown();
 631    }
 632
 633    @Test(timeout = 60000)
 634    public void testSuperClientRestart() throws Exception {
 635        Config configNormal = new Config();
 636        configNormal.setProperty(GroupProperties.PROP_CONNECTION_MONITOR_INTERVAL, "1");
 637        configNormal.setProperty(GroupProperties.PROP_CONNECTION_MONITOR_MAX_FAULTS, "1");
 638        Config configSuper = new Config();
 639        configSuper.setProperty(GroupProperties.PROP_CONNECTION_MONITOR_INTERVAL, "1");
 640        configSuper.setProperty(GroupProperties.PROP_CONNECTION_MONITOR_MAX_FAULTS, "1");
 641        configSuper.setLiteMember(true);
 642        HazelcastInstance h = Hazelcast.newHazelcastInstance(configNormal);
 643        HazelcastInstance s = Hazelcast.newHazelcastInstance(configSuper);
 644        assertEquals(2, h.getCluster().getMembers().size());
 645        assertEquals(2, s.getCluster().getMembers().size());
 646        assertFalse(h.getCluster().getLocalMember().isLiteMember());
 647        assertTrue(s.getCluster().getLocalMember().isLiteMember());
 648        IMap map = h.getMap("default");
 649        final IMap maps = s.getMap("default");
 650        assertNull(map.put("1", "value1"));
 651        assertEquals("value1", map.get("1"));
 652        assertEquals("value1", maps.get("1"));
 653        assertEquals(1, map.size());
 654        assertEquals(1, maps.size());
 655        h.getLifecycleService().shutdown();
 656        sleep(500);
 657        assertEquals(1, s.getCluster().getMembers().size());
 658        final CountDownLatch latch = new CountDownLatch(1);
 659        new Thread(new Runnable() {
 660            public void run() {
 661                maps.size();
 662                assertNull(maps.get("1"));
 663                maps.put("1", "value3");
 664                latch.countDown();
 665            }
 666        }).start();
 667        h = Hazelcast.newHazelcastInstance(configNormal);
 668        assertTrue(latch.await(20, TimeUnit.SECONDS));
 669        assertEquals(2, h.getCluster().getMembers().size());
 670        assertEquals(2, s.getCluster().getMembers().size());
 671        assertFalse(h.getCluster().getLocalMember().isLiteMember());
 672        assertTrue(s.getCluster().getLocalMember().isLiteMember());
 673        map = h.getMap("default");
 674        assertEquals("value3", map.put("1", "value2"));
 675        assertEquals("value2", map.get("1"));
 676        assertEquals(1, map.size());
 677        assertEquals(1, maps.size());
 678    }
 679
 680    /**
 681     * 3 node cluster: normal member(h1), super client (hSuper) and another normal member (h2)
 682     * if h1 goes down, hSuper becomes the oldest member
 683     * If hSuper fails to update the partition ownerships,
 684     * h2.getMap("default").get(key) gets into infinite Re-Do.
 685     *
 686     * @throws Exception
 687     */
 688    @Test
 689    public void testSuperClientBeingOldestMember() throws Exception {
 690        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 691        Config superConfig = new Config();
 692        superConfig.setLiteMember(true);
 693        HazelcastInstance hSuper = Hazelcast.newHazelcastInstance(superConfig);
 694        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 695        final IMap map = h2.getMap("default");
 696        h1.getLifecycleService().shutdown();
 697        final CountDownLatch latch = new CountDownLatch(1);
 698        new Thread(new Runnable() {
 699            public void run() {
 700                assertTrue(map.get("1") == null);
 701                latch.countDown();
 702            }
 703        }).start();
 704        assertTrue(latch.await(10, TimeUnit.SECONDS));
 705    }
 706
 707    @Test(timeout = 120000)
 708    public void testTcpIpWithMembers() throws Exception {
 709        Config c = new Config();
 710        c.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
 711        c.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
 712        c.getNetworkConfig().getInterfaces().setEnabled(true);
 713        c.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1");
 714        c.getNetworkConfig().getInterfaces().addInterface("127.0.0.1");
 715        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(c);
 716        assertEquals(1, h1.getCluster().getMembers().size());
 717        h1.getMap("default").put("1", "value1");
 718        assertEquals("value1", h1.getMap("default").put("1", "value2"));
 719        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(c);
 720        testTwoNodes(h1, h2);
 721        h1.getLifecycleService().shutdown();
 722        h1 = Hazelcast.newHazelcastInstance(c);
 723        testTwoNodes(h2, h1);
 724    }
 725
 726    @Test(timeout = 120000)
 727    public void testTcpIp() throws Exception {
 728        Config c = new Config();
 729        c.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
 730        c.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
 731        c.getNetworkConfig().getInterfaces().setEnabled(true);
 732        c.getNetworkConfig().getJoin().getTcpIpConfig()
 733                .addAddress(new Address("127.0.0.1", 5701))
 734                .addAddress(new Address("127.0.0.1", 5702));
 735        c.getNetworkConfig().getInterfaces().addInterface("127.0.0.1");
 736        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(c);
 737        assertEquals(1, h1.getCluster().getMembers().size());
 738        h1.getMap("default").put("1", "value1");
 739        assertEquals("value1", h1.getMap("default").put("1", "value2"));
 740        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(c);
 741        testTwoNodes(h1, h2);
 742        h1.getLifecycleService().shutdown();
 743        h1 = Hazelcast.newHazelcastInstance(c);
 744        testTwoNodes(h2, h1);
 745    }
 746
 747    @Test(timeout = 120000)
 748    public void testTcpIpWithoutInterfaces() throws Exception {
 749        Config c = new Config();
 750        c.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
 751        c.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
 752        c.getNetworkConfig().getInterfaces().setEnabled(true);
 753        c.getNetworkConfig().getJoin().getTcpIpConfig()
 754                .addAddress(new Address("127.0.0.1", 5701))
 755                .addAddress(new Address("127.0.0.1", 5702));
 756        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(c);
 757        assertEquals(1, h1.getCluster().getMembers().size());
 758        h1.getMap("default").put("1", "value1");
 759        assertEquals("value1", h1.getMap("default").put("1", "value2"));
 760        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(c);
 761        testTwoNodes(h1, h2);
 762        h1.getLifecycleService().shutdown();
 763        h1 = Hazelcast.newHazelcastInstance(c);
 764        testTwoNodes(h2, h1);
 765    }
 766
 767    @Test(timeout = 120000)
 768    public void testMulticast() throws Exception {
 769        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 770        assertEquals(1, h1.getCluster().getMembers().size());
 771        h1.getMap("default").put("1", "value1");
 772        assertEquals("value1", h1.getMap("default").put("1", "value2"));
 773        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 774        testTwoNodes(h1, h2);
 775    }
 776
 777    private void testTwoNodes(HazelcastInstance h1, HazelcastInstance h2) throws Exception {
 778        h1.getMultiMap("default").clear();
 779        IMap map1 = h1.getMap("default");
 780        IMap map2 = h2.getMap("default");
 781        assertEquals(2, h1.getCluster().getMembers().size());
 782        assertEquals(2, h2.getCluster().getMembers().size());
 783        assertEquals("value2", h2.getMap("default").get("1"));
 784        assertEquals("value2", h1.getMap("default").get("1"));
 785        assertEquals(1, h1.getMap("default").size());
 786        assertEquals(1, h2.getMap("default").size());
 787        assertFalse(map1.containsKey("2"));
 788        assertFalse(map2.containsKey("2"));
 789        assertFalse(map1.containsValue("value1"));
 790        assertFalse(map2.containsValue("value1"));
 791        assertTrue(map1.containsKey("1"));
 792        assertTrue(map2.containsKey("1"));
 793        assertTrue(map1.containsValue("value2"));
 794        assertTrue(map2.containsValue("value2"));
 795        map1.lock("1");
 796        assertFalse(map2.tryLock("1"));
 797        map1.unlock("1");
 798        assertTrue(map2.tryLock("1"));
 799        map2.unlock("1");
 800        assertEquals("value2", map1.putIfAbsent("1", "value1"));
 801        assertEquals("value2", map2.putIfAbsent("1", "value1"));
 802        assertEquals("value2", map1.get("1"));
 803        assertEquals("value2", map2.get("1"));
 804        assertNull(map1.putIfAbsent("3", "value3"));
 805        assertEquals("value3", map1.get("3"));
 806        assertEquals("value3", map2.get("3"));
 807        assertEquals("value3", map2.remove("3"));
 808        assertNull(map1.get("3"));
 809        assertNull(map2.get("3"));
 810        assertNull(map2.putIfAbsent("3", "value3"));
 811        assertEquals("value3", map1.get("3"));
 812        assertEquals("value3", map2.get("3"));
 813        assertEquals("value3", map1.remove("3"));
 814        assertNull(map1.get("3"));
 815        assertNull(map2.get("3"));
 816        assertEquals(1, map1.keySet().size());
 817        assertEquals(1, map1.values().size());
 818        assertEquals(1, map1.entrySet().size());
 819        assertEquals(1, map2.keySet().size());
 820        assertEquals(1, map2.values().size());
 821        assertEquals(1, map2.entrySet().size());
 822        Set<Map.Entry> entries = map1.entrySet();
 823        for (Map.Entry entry : entries) {
 824            assertEquals("1", entry.getKey());
 825            assertEquals("value2", entry.getValue());
 826        }
 827        entries = map2.entrySet();
 828        for (Map.Entry entry : entries) {
 829            assertEquals("1", entry.getKey());
 830            assertEquals("value2", entry.getValue());
 831        }
 832        allMapListenerTest(map2, "5", map1);
 833        MultiMap<String, String> mm1 = h1.getMultiMap("default");
 834        MultiMap<String, String> mm2 = h2.getMultiMap("default");
 835        mm1.put("Hello", "World");
 836        Collection<String> values = mm2.get("Hello");
 837        assertEquals("World", values.iterator().next());
 838        mm2.put("Hello", "Europe");
 839        mm1.put("Hello", "America");
 840        mm1.put("Hello", "Asia");
 841        mm1.put("Hello", "Africa");
 842        mm1.put("Hello", "Antartica");
 843        mm1.put("Hello", "Australia");
 844        values = mm2.get("Hello");
 845        assertEquals(7, values.size());
 846        junit.framework.Assert.assertFalse(mm2.remove("Hello", "Unknown"));
 847        assertEquals(7, mm1.get("Hello").size());
 848        assertTrue(mm1.remove("Hello", "Antartica"));
 849        assertEquals(6, mm1.get("Hello").size());
 850    }
 851
 852    @Test(timeout = 120000)
 853    public void testListeners2() throws Exception {
 854        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 855        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 856        final Member member1 = h1.getCluster().getLocalMember();
 857        final Member member2 = h2.getCluster().getLocalMember();
 858        final CountDownLatch latchAdded = new CountDownLatch(4);
 859        final CountDownLatch latchUpdated = new CountDownLatch(2);
 860        final CountDownLatch latchRemoved = new CountDownLatch(2);
 861        final CountDownLatch latchEvicted = new CountDownLatch(2);
 862        EntryListener listener = new EntryListener() {
 863            public synchronized void entryAdded(EntryEvent entryEvent) {
 864                if (latchAdded.getCount() > 2) {
 865                    assertEquals(member1, entryEvent.getMember());
 866                } else {
 867                    assertEquals(member2, entryEvent.getMember());
 868                }
 869                latchAdded.countDown();
 870            }
 871
 872            public void entryRemoved(EntryEvent entryEvent) {
 873                assertEquals(member1, entryEvent.getMember());
 874                latchRemoved.countDown();
 875            }
 876
 877            public void entryUpdated(EntryEvent entryEvent) {
 878                assertEquals(member2, entryEvent.getMember());
 879                latchUpdated.countDown();
 880            }
 881
 882            public void entryEvicted(EntryEvent entryEvent) {
 883                assertEquals(member2, entryEvent.getMember());
 884                latchEvicted.countDown();
 885            }
 886        };
 887        IMap map2 = h2.getMap("default");
 888        IMap map1 = h1.getMap("default");
 889        Object key = "2133aa";
 890        map1.addEntryListener(listener, key, true);
 891        map2.addEntryListener(listener, key, true);
 892        assertNull(map1.put(key, "value5"));
 893        assertEquals("value5", map2.put(key, "value55"));
 894        assertTrue(map2.evict(key));
 895        assertNull(map2.put(key, "value5"));
 896        assertEquals("value5", map1.remove(key));
 897        int waitSeconds = 20;
 898        assertTrue(latchRemoved.await(waitSeconds, TimeUnit.SECONDS));
 899        map1.removeEntryListener(listener, key);
 900        assertFalse(map2.evict(key));
 901        map2.removeEntryListener(listener, key);
 902        assertTrue(latchAdded.await(waitSeconds, TimeUnit.SECONDS));
 903        assertTrue(latchUpdated.await(waitSeconds, TimeUnit.SECONDS));
 904        assertTrue(latchRemoved.await(waitSeconds, TimeUnit.SECONDS));
 905        assertTrue(latchEvicted.await(waitSeconds, TimeUnit.SECONDS));
 906    }
 907
 908    @Test(timeout = 120000)
 909    public void testListeners() throws Exception {
 910        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 911        assertEquals(1, h1.getCluster().getMembers().size());
 912        h1.getMap("default").put("1", "value1");
 913        assertEquals("value1", h1.getMap("default").put("1", "value2"));
 914        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 915        allMapListenerTest(h2.getMap("default"), "5", h1.getMap("default"));
 916    }
 917
 918    private void allMapListenerTest(IMap map, Object keyToUpdate, IMap mapSource) throws Exception {
 919        final CountDownLatch latchAdded = new CountDownLatch(2);
 920        final CountDownLatch latchUpdated = new CountDownLatch(1);
 921        final CountDownLatch latchRemoved = new CountDownLatch(1);
 922        final CountDownLatch latchEvicted = new CountDownLatch(1);
 923        EntryListener listener = new EntryListener() {
 924            public void entryAdded(EntryEvent entryEvent) {
 925                latchAdded.countDown();
 926            }
 927
 928            public void entryRemoved(EntryEvent entryEvent) {
 929                latchRemoved.countDown();
 930            }
 931
 932            public void entryUpdated(EntryEvent entryEvent) {
 933                latchUpdated.countDown();
 934            }
 935
 936            public void entryEvicted(EntryEvent entryEvent) {
 937                latchEvicted.countDown();
 938            }
 939        };
 940        map.addEntryListener(listener, true);
 941        assertNull(mapSource.put(keyToUpdate, "value5"));
 942        assertEquals("value5", mapSource.put(keyToUpdate, "value55"));
 943        assertTrue(mapSource.evict(keyToUpdate));
 944        assertNull(mapSource.put(keyToUpdate, "value5"));
 945        assertEquals("value5", mapSource.remove(keyToUpdate));
 946        int waitSeconds = 20;
 947        assertTrue(latchAdded.await(waitSeconds, TimeUnit.SECONDS));
 948        assertTrue(latchUpdated.await(waitSeconds, TimeUnit.SECONDS));
 949        assertTrue(latchRemoved.await(waitSeconds, TimeUnit.SECONDS));
 950        assertTrue(latchEvicted.await(waitSeconds, TimeUnit.SECONDS));
 951        map.removeEntryListener(listener);
 952    }
 953
 954    @Test(timeout = 120000)
 955    public void testTcpIpWithDifferentBuildNumber() throws Exception {
 956        System.setProperty("hazelcast.build", "1");
 957        Config c = new Config();
 958        c.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
 959        c.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
 960        c.getNetworkConfig().getInterfaces().setEnabled(true);
 961        c.getNetworkConfig().getJoin().getTcpIpConfig().addAddress(new Address("127.0.0.1", 5701));
 962        c.getNetworkConfig().getInterfaces().addInterface("127.0.0.1");
 963        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(c);
 964        assertEquals(1, h1.getCluster().getMembers().size());
 965        h1.getMap("default").put("1", "value1");
 966        assertEquals("value1", h1.getMap("default").put("1", "value2"));
 967        System.setProperty("hazelcast.build", "2");
 968        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(c);
 969        assertEquals(2, h1.getCluster().getMembers().size());
 970        assertEquals(2, h2.getCluster().getMembers().size());
 971        System.clearProperty("hazelcast.build");
 972    }
 973
 974    @Test(timeout = 120000)
 975    public void testMulticastWithDifferentBuildNumber() throws Exception {
 976        System.setProperty("hazelcast.build", "1");
 977        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
 978        assertEquals(1, h1.getCluster().getMembers().size());
 979        h1.getMap("default").put("1", "value1");
 980        assertEquals("value1", h1.getMap("default").put("1", "value2"));
 981        System.setProperty("hazelcast.build", "2");
 982        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
 983        assertEquals(2, h1.getCluster().getMembers().size());
 984        assertEquals(2, h2.getCluster().getMembers().size());
 985        System.setProperty("hazelcast.build", "t");
 986    }
 987
 988    @Test(timeout = 120000)
 989    public void testMapMaxSize() throws Exception {
 990        int maxSize = 40;
 991        Config c = new Config();
 992        MapConfig mapConfig = c.getMapConfig("default");
 993        mapConfig.setEvictionPolicy("LRU");
 994        mapConfig.setMaxSizeConfig(new MaxSizeConfig()
 995                                           .setMaxSizePolicy(MaxSizeConfig.POLICY_CLUSTER_WIDE_MAP_SIZE)
 996                                           .setSize(maxSize));
 997        mapConfig.setEvictionPercentage(25);
 998        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(c);
 999        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(c);
1000        IMap map1 = h1.getMap("default");
1001        for (int i = 0; i < 100; i++) {
1002            map1.put(String.valueOf(i), String.valueOf(i));
1003            int mapSize = map1.size();
1004            assertTrue("CurrentMapSize : " + mapSize, mapSize <= maxSize);
1005        }
1006        IMap map2 = h2.getMap("default");
1007        for (int i = 0; i < 100; i++) {
1008            map2.put(String.valueOf(i), String.valueOf(i));
1009            int mapSize = map2.size();
1010            assertTrue("CurrentMapSize : " + mapSize, mapSize <= maxSize);
1011        }
1012    }
1013
1014    @Test(timeout = 120000)
1015    @Ignore
1016    public void testMapMaxHeap() throws Exception {
1017        int maxSize = 1; // MB
1018        Config c = new Config();
1019        c.setProperty(GroupProperties.PROP_CLEANUP_DELAY_SECONDS, "1");
1020        MapConfig mapConfig = c.getMapConfig("default");
1021        mapConfig.setMaxSizeConfig(new MaxSizeConfig()
1022                                           .setMaxSizePolicy(MaxSizeConfig.POLICY_USED_HEAP_SIZE)
1023                                           .setSize(maxSize));
1024        final byte[] data = new byte[700]; // cost = value + overhead(312) = 1012
1025        HazelcastInstance hz = Hazelcast.newHazelcastInstance(c);
1026        IMap map = hz.getMap("default");
1027        for (int i = 0; i < 1024; i++) {  // cost = key(+overhead)(12) + value(700) + overhead(312) = 1024
1028            map.put(i, data);
1029        }
1030        Thread.sleep(1500); // wait for cleanup
1031        assertFalse(map.tryPut(1024, data, 0, TimeUnit.SECONDS));
1032        map.remove(0);
1033        Thread.sleep(1500); // wait for cleanup
1034        assertTrue(map.tryPut(1024, data, 0, TimeUnit.SECONDS));
1035    }
1036
1037    /**
1038     * Test for issue #204:
1039     * http://code.google.com/p/hazelcast/issues/detail?id=204
1040     * <p/>
1041     * Summary:
1042     * Eviction events are not fired
1043     */
1044    @Test
1045    public void testEvictionOfEntriesWithTTL() throws Exception {
1046        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
1047        IMap map1 = h1.getMap("default");
1048        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
1049        IMap map2 = h2.getMap("default");
1050        TestEntryListener cl1 = new TestEntryListener(100, 0, 0, 100);
1051        TestEntryListener cl2 = new TestEntryListener(100, 0, 0, 100);
1052        map1.addEntryListener(cl1, true);
1053        map2.addEntryListener(cl2, true);
1054        for (int i = 0; i < 50; i++) {
1055            map1.put(Integer.valueOf(i), i, 5, TimeUnit.SECONDS);
1056            map1.put(String.valueOf(i), i, 5, TimeUnit.SECONDS);
1057        }
1058        assertTrue(cl1.await(30));
1059        assertTrue(cl2.await(30));
1060    }
1061
1062    @Test(timeout = 180000)
1063    public void testLosingEntries() throws Exception {
1064        final CountDownLatch latch = new CountDownLatch(2);
1065        final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
1066        final AtomicBoolean failed = new AtomicBoolean(false);
1067        new Thread(new Runnable() {
1068            public void run() {
1069                try {
1070                    callSize(h1, 110000);
1071                } catch (Exception e) {
1072                    failed.set(true);
1073                    fail(e.getMessage());
1074                } finally {
1075                    latch.countDown();
1076                }
1077            }
1078        }).start();
1079        sleep(4000);
1080        final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
1081        new Thread(new Runnable() {
1082            public void run() {
1083                try {
1084                    callSize(h2, 2000);
1085                } catch (Exception e) {
1086                    failed.set(true);
1087                    fail(e.getMessage());
1088                } finally {
1089                    latch.countDown();
1090                }
1091            }
1092        }).start();
1093        assertTrue(latch.await(20, TimeUnit.SECONDS));
1094        assertFalse(failed.get());
1095    }
1096
1097    private void callSize(HazelcastInstance h, int numberOfIterations) throws Exception {
1098        Random r = new Random();
1099        Map<Integer, Integer> map = h.getMap("testMap");
1100        try {
1101            sleep(5000);
1102        } catch (InterruptedException ignored) {
1103        }
1104        int size = 0;
1105        for (int i = 0; i < numberOfIterations; i++) {
1106            if (i % 1000 == 0) {
1107                int sizeNow = map.size();
1108                if (sizeNow < size)
1109                    throw new RuntimeException("CurrentSize cannot be smaller. " + sizeNow + ", was " + size);
1110                size = sizeNow;
1111            }
1112            map.put(r.nextInt(200000), i);
1113        }
1114        h.getLifecycleService().shutdown();
1115    }
1116
1117    @Test(timeout = 60000)
1118    public void testMapRecovery() throws Exception {
1119        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
1120        IMap map1 = h1.getMap("default");
1121        map1.put("1", "value");
1122        assertEquals(1, map1.size());
1123        assertEquals(1, map1.keySet().size());
1124        CMap cmap1 = getCMap(h1, "default");
1125        assertEquals(1, cmap1.mapRecords.size());
1126        assertEquals(1, cmap1.getMapIndexService().getOwnedRecords().size());
1127        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
1128        MigrationCompletionLatch l = new MigrationCompletionLatch("1", h1, h2);
1129        l.await(3, TimeUnit.SECONDS);
1130        IMap map2 = h2.getMap("default");
1131        CMap cmap2 = getCMap(h2, "default");
1132        assertEquals(1, cmap1.mapRecords.size());
1133        assertEquals(1, cmap2.mapRecords.size());
1134        assertEquals(1, cmap1.getMapIndexService().getOwnedRecords().size()
1135                + cmap2.getMapIndexService().getOwnedRecords().size());
1136        assertEquals(1, map1.size());
1137        assertEquals(1, map1.keySet().size());
1138        assertEquals(1, map2.size());
1139        assertEquals(1, map2.keySet().size());
1140        h1.getLifecycleService().shutdown();
1141        assertEquals(1, map2.size());
1142        assertEquals(1, map2.keySet().size());
1143        assertEquals(1, cmap2.mapRecords.size());
1144        assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size());
1145    }
1146
1147    @Test(timeout = 60000)
1148    public void testMultiMapRecovery() throws Exception {
1149        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
1150        MultiMap mm = h1.getMultiMap("default");
1151        CMap cmap1 = getCMapForMultiMap(h1, "default");
1152        Collection<String> expectedValues = new HashSet<String>();
1153        expectedValues.add("value1");
1154        expectedValues.add("value2");
1155        mm.put("1", "value1");
1156        mm.put("1", "value2");
1157        assertEquals(2, mm.size());
1158        assertEquals(1, mm.keySet().size());
1159        Collection values = mm.get("1");
1160        for (Object value : values) {
1161            assertTrue(expectedValues.contains(value));
1162        }
1163        assertEquals(1, cmap1.getMapIndexService().getOwnedRecords().size());
1164        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(new Config());
1165        MultiMap mm2 = h2.getMultiMap("default");
1166        CMap cmap2 = getCMapForMultiMap(h2, "default");
1167        MigrationCompletionLatch l = new MigrationCompletionLatch("1", h1, h2);
1168        l.await(3, TimeUnit.SECONDS);
1169        assertEquals(1, cmap1.getMapIndexService().getOwnedRecords().size()
1170                + cmap2.getMapIndexService().getOwnedRecords().size());
1171        cmap1.startCleanup(true);
1172        cmap2.startCleanup(true);
1173        assertEquals(2, mm.size());
1174        assertEquals(1, mm.keySet().size());
1175        values = mm.get("1");
1176        for (Object value : values) {
1177            assertTrue(expectedValues.contains(value));
1178        }
1179        assertEquals(2, mm2.size());
1180        assertEquals(1, mm2.keySet().size());
1181        values = mm2.get("1");
1182        for (Object value : values) {
1183            assertTrue(expectedValues.contains(value));
1184        

Large files files are truncated, but you can click here to view the full file