/*
 * Copyright (c) 2008-2013, 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;

import com.hazelcast.config.Config;
import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.DataSerializable;
import com.hazelcast.test.AssertTask;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestHazelcastInstanceFactory;
import com.hazelcast.test.annotation.NightlyTest;
import com.hazelcast.test.annotation.ProblematicTest;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.locks.LockSupport;

import static org.junit.Assert.assertTrue;


@RunWith(HazelcastSerialClassRunner.class)
@Category(NightlyTest.class)
public class EntryProcessorStressTest extends HazelcastTestSupport {

    @Test
    @Category(ProblematicTest.class)
    public void dropedEntryProcessorTest_withKeyOwningNodeTermination() throws ExecutionException, InterruptedException {
        String mapName = randomString();
        Config cfg = new Config();
        cfg.getMapConfig(mapName).setInMemoryFormat(InMemoryFormat.OBJECT);

        final int maxIterations = 50;

        TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(maxIterations+1);
        HazelcastInstance instance1 = factory.newHazelcastInstance(cfg);

        for (int iteration = 0; iteration < maxIterations; iteration++) {
            HazelcastInstance instance2 = factory.newHazelcastInstance(cfg);

            final int maxTasks = 20;
            final Object key = generateKeyOwnedBy(instance2);

            final IMap<Object, List<Integer>> processorMap = instance1.getMap(mapName);
            processorMap.put(key, new ArrayList<Integer>());

            for (int i = 0; i < maxTasks; i++) {
                processorMap.submitToKey(key, new SimpleEntryProcessor(i));

                if (i == maxTasks / 2) {
                    instance2.getLifecycleService().terminate();
                }
            }

            assertTrueEventually(new AssertTask() {
                public void run() throws Exception {
                    List<Integer> actualOrder = processorMap.get(key);
                    //using >= for the test, as it can be the case that an entry processor could be executed more the once
                    //when the owning node is terminated after running the entry processor (and the backup) but before the response is sent
                    assertTrue("failed to execute all entry processor tasks at iteration",
                            actualOrder.size() >= maxTasks);
                }
            }, 30);
        }
    }

    private static class SimpleEntryProcessor implements DataSerializable, EntryProcessor<Object, List<Integer>>,
            EntryBackupProcessor<Object, List<Integer>> {

        private int id;

        public SimpleEntryProcessor() {}

        public SimpleEntryProcessor(Integer id) {
            this.id = id;
        }

        @Override
        public Object process(Map.Entry<Object, List<Integer>> entry) {
            List<Integer> list = entry.getValue();
            list.add(id);
            // add a random artificial latency
            LockSupport.parkNanos((long) (Math.random() * 10000));
            entry.setValue(list);
            return id;
        }

        @Override
        public void processBackup(Map.Entry<Object, List<Integer>> entry) {
            process(entry);
        }

        @Override
        public void writeData(ObjectDataOutput out) throws IOException {
            out.writeInt(id);
        }

        @Override
        public void readData(ObjectDataInput in) throws IOException {
            id = in.readInt();
        }

        @Override
        public EntryBackupProcessor<Object, List<Integer>> getBackupProcessor() {
            return this;
        }
    }
}

@@ -1197,62 +1197,8 @@
 st;%0A
-import com.hazelcast.test.annotation.ProblematicTest;%0A
 impo
@@ -1542,17 +1542,16 @@
 tTrue;%0A%0A
-%0A
 @RunWith
@@ -1698,45 +1698,8 @@
 est%0A
-    @Category(ProblematicTest.class)%0A
     
@@ -2799,25 +2799,24 @@
 rvice().
-terminate
+shutdown
 ();%0A    

Terminate does not guarantee data safety. Test should use graceful shutdown.