package com.hazelcast.map;

import com.hazelcast.core.EntryAdapter;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestHazelcastInstanceFactory;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

import static org.junit.Assert.assertEquals;

@RunWith(HazelcastParallelClassRunner.class)
@Category(QuickTest.class)
public class MapEvictAllTest extends HazelcastTestSupport {

    @Test
    public void testEvictAll_firesEvent() throws Exception {
        final CountDownLatch countDownLatch = new CountDownLatch(1000);
        final HazelcastInstance node = createHazelcastInstance();
        final IMap map = node.getMap(randomMapName());
        map.addLocalEntryListener(new EntryAdapter() {
            @Override
            public void onEvictAll(MapEvent event) {
                final int numberOfEntries = event.getNumberOfEntriesAffected();
                for (int i = 0; i < numberOfEntries; i++) {
                    countDownLatch.countDown();
                }
            }
        });
        for (int i = 0; i < 1000; i++) {
            map.put(i, i);
        }
        map.evictAll();
        assertOpenEventually(countDownLatch);
    }

    @Test
    public void testEvictAll_preserveLockedKeys() throws Exception {
        final int numberOfEntries = 1000;
        final int numberOfLockedKeys = 123;
        final int expectedNumberOfEvictedKeys = numberOfEntries - numberOfLockedKeys;
        final CountDownLatch countDownLatch = new CountDownLatch(expectedNumberOfEvictedKeys);
        final HazelcastInstance node = createHazelcastInstance();
        final IMap map = node.getMap(randomMapName());
        map.addLocalEntryListener(new EntryAdapter() {
            @Override
            public void onEvictAll(MapEvent event) {
                final int numberOfEntries = event.getNumberOfEntriesAffected();
                for (int i = 0; i < numberOfEntries; i++) {
                    countDownLatch.countDown();
                }
            }
        });
        for (int i = 0; i < numberOfEntries; i++) {
            map.put(i, i);
        }
        for (int i = 0; i < numberOfLockedKeys; i++) {
            map.lock(i);
        }
        map.evictAll();

        assertOpenEventually(countDownLatch);
        assertEquals(0, countDownLatch.getCount());
    }


    @Test
    public void testEvictAll_onBackup() throws Exception {
        final String mapName = randomMapName();
        final CountDownLatch countDownLatch = new CountDownLatch(10000);
        final TestHazelcastInstanceFactory instanceFactory = new TestHazelcastInstanceFactory(5);
        final HazelcastInstance node1 = instanceFactory.newHazelcastInstance();
        final HazelcastInstance node2 = instanceFactory.newHazelcastInstance();
        final IMap map = node1.getMap(mapName);
        map.addEntryListener(new EntryAdapter() {
            @Override
            public void onEvictAll(MapEvent event) {
                System.out.println("event = " + event);
                final int numberOfEntries = event.getNumberOfEntriesAffected();
                for (int i = 0; i < numberOfEntries; i++) {
                    countDownLatch.countDown();
                }
            }
        }, false);
        for (int i = 0; i < 10000; i++) {
            map.put(i, i);
        }
        map.evictAll();
        assertOpenEventually(countDownLatch);
        assertEquals(0, countDownLatch.getCount());


    }

}
@@ -3064,24 +3064,25 @@
 nal IMap map
+1
  = node1.get
@@ -3099,27 +3099,77 @@
 e);%0A        
-map
+final IMap map2 = node2.getMap(mapName);%0A        map1
 .addEntryLis
@@ -3274,64 +3274,8 @@
 ) %7B%0A
-                System.out.println(%22event = %22 + event);%0A
     
@@ -3558,32 +3558,33 @@
 %0A            map
+1
 .put(i, i);%0A    
@@ -3592,32 +3592,33 @@
    %7D%0A        map
+1
 .evictAll();%0A   
@@ -3712,16 +3712,144 @@
 unt());%0A
+        assertEquals(0, map1.getLocalMapStats().getHeapCost());%0A        assertEquals(0, map2.getLocalMapStats().getHeapCost());%0A
 %0A%0A    %7D%0A

test improvements