/*
 * Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.map.impl.mapstore.writebehind;

import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MapStoreConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.EntryStore;
import com.hazelcast.map.IMap;
import com.hazelcast.map.impl.mapstore.TestEntryStore;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestHazelcastInstanceFactory;
import com.hazelcast.test.annotation.ParallelJVMTest;
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.TimeUnit;

import static org.junit.Assert.assertNull;

@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelJVMTest.class})
public class WriteBehindEntryStoreQueueReplicationTest extends HazelcastTestSupport {

    @Test
    public void queued_entries_with_expiration_times_are_not_lost_when_cluster_scaled_down() {
        final int entryCount = 1000;
        final int ttlSec = 10;
        final int writeDelaySec = 5;
        final int backupCount = 1;
        final String mapName = randomMapName();

        TestEntryStore<Integer, Integer> testEntryStore = new TestEntryStore<>();
        Config config = getConfigWithEntryStore(testEntryStore, writeDelaySec, backupCount);

        TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
        HazelcastInstance[] instances = factory.newInstances(config);

        IMap<Integer, Integer> map = instances[0].getMap(mapName);
        long expectedExpirationTime = System.currentTimeMillis() + ttlSec * 1000;
        for (int i = 0; i < 1000; i++) {
            map.put(i, i, ttlSec, TimeUnit.SECONDS);
        }

        // scale down
        instances[0].shutdown();
        instances[1].shutdown();

        IMap<Integer, Integer> mapFromSurvivingInstance = instances[2].getMap(mapName);

        // assert that entries are still stored in
        // the entry store after original nodes crash
        assertTrueEventually(() -> {
            for (int i = 0; i < entryCount; i++) {
                testEntryStore.assertRecordStored(i, i, expectedExpirationTime, 2000);
            }
        });

        // assert that entries still had
        // expiration times and expired accordingly
        assertTrueEventually(() -> {
            for (int i = 0; i < entryCount; i++) {
                assertNull(mapFromSurvivingInstance.get(i));
            }
        });
    }

    @Test
    public void queued_entries_with_expirationTimes_are_replicated_when_cluster_scaled_up() {
        final int entryCount = 1000;
        final int ttlSec = 10;
        final int writeDelaySec = 5;
        final int backupCount = 1;
        final String mapName = randomMapName();

        TestEntryStore<Integer, Integer> testEntryStore = new TestEntryStore<>();
        Config config = getConfigWithEntryStore(testEntryStore, writeDelaySec, backupCount);

        TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
        HazelcastInstance node1 = factory.newHazelcastInstance(config);

        IMap<Integer, Integer> map = node1.getMap(mapName);
        long expectedExpirationTime = System.currentTimeMillis() + ttlSec * 1000;
        for (int i = 0; i < 1000; i++) {
            map.put(i, i, ttlSec, TimeUnit.SECONDS);
        }

        // scale up
        HazelcastInstance node2 = factory.newHazelcastInstance(config);
        HazelcastInstance node3 = factory.newHazelcastInstance(config);

        IMap<Integer, Integer> mapFromNewInstance = node3.getMap(mapName);

        // assert that entries are still stored in
        // the entry store after original nodes crash
        assertTrueEventually(() -> {
            for (int i = 0; i < entryCount; i++) {
                testEntryStore.assertRecordStored(i, i, expectedExpirationTime, 2000);
            }
        });

        // assert that entries still had
        // expiration times and expired accordingly
        assertTrueEventually(() -> {
            for (int i = 0; i < entryCount; i++) {
                assertNull(mapFromNewInstance.get(i));
            }
        });
    }

    private Config getConfigWithEntryStore(EntryStore entryStore,
                                           int writeDelaySeconds, int backupCount) {
        Config config = getConfig();
        MapStoreConfig mapStoreConfig = new MapStoreConfig();
        mapStoreConfig
                .setEnabled(true)
                .setWriteDelaySeconds(writeDelaySeconds)
                .setImplementation(entryStore);
        MapConfig mapConfig = config.getMapConfig("default");
        mapConfig.setBackupCount(backupCount);
        mapConfig.setMapStoreConfig(mapStoreConfig);
        return config;
    }
}

@@ -1331,16 +1331,64 @@
 nWith;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A
 import j
@@ -2261,32 +2261,98 @@
 ances(config);%0A%0A
+        Map%3CInteger, Long%3E expectedExpiryTimes = new HashMap%3C%3E();%0A
         IMap%3CInt
@@ -2406,90 +2406,8 @@
 e);%0A
-        long expectedExpirationTime = System.currentTimeMillis() + ttlSec * 1000;%0A
     
@@ -2488,32 +2488,113 @@
 eUnit.SECONDS);%0A
+            expectedExpiryTimes.put(i, map.getEntryView(i).getExpirationTime());%0A
         %7D%0A%0A     
@@ -3024,33 +3024,37 @@
 xpectedExpir
-ationTime
+yTimes.get(i)
 , 2000);%0A   
@@ -3975,33 +3975,32 @@
 nfig);%0A%0A        
-I
 Map%3CInteger, Int
@@ -4000,126 +4000,111 @@
 er, 
-Integer%3E map = node1.getMap(mapName);%0A        long expectedExpirationTime = System.currentTimeMillis() + ttlSec * 1000
+Long%3E expectedExpiryTimes = new HashMap%3C%3E();%0A        IMap%3CInteger, Integer%3E map = node1.getMap(mapName)
 ;%0A  
@@ -4187,32 +4187,113 @@
 eUnit.SECONDS);%0A
+            expectedExpiryTimes.put(i, map.getEntryView(i).getExpirationTime());%0A
         %7D%0A%0A     
@@ -4790,25 +4790,29 @@
 tedExpir
-ationTime
+yTimes.get(i)
 , 2000);

Use per entry expiration times (#15881)