/*
 * arcus-java-client : Arcus Java client
 * Copyright 2010-2014 NAVER Corp.
 *
 * 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 net.spy.memcached.bulkoperation;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import junit.framework.Assert;
import net.spy.memcached.collection.BaseIntegrationTest;
import net.spy.memcached.collection.CollectionAttributes;
import net.spy.memcached.ops.CollectionOperationStatus;

public class SopInsertBulkTest extends BaseIntegrationTest {

	private String KEY = SopInsertBulkTest.class.getSimpleName();

	public void testInsertAndGet() {
		String value = "MyValue";
		int keySize = 500;
		String[] keys = new String[keySize];
		for (int i = 0; i < keys.length; i++) {
			keys[i] = KEY + i;
		}

		try {
			// REMOVE
			for (String key : keys) {
				mc.asyncSopDelete(key, value, true).get();
			}

			// SET
			Future<Map<String, CollectionOperationStatus>> future = mc
					.asyncSopInsertBulk(Arrays.asList(keys), value,
							new CollectionAttributes());
			try {
                /* ENABLE_REPLICATION start */
                Map<String, CollectionOperationStatus> errorList = future.get(
                        1000L, TimeUnit.MILLISECONDS);
                /* ENABLE_REPLICATION else */
                /*
				Map<String, CollectionOperationStatus> errorList = future.get(
						100L, TimeUnit.MILLISECONDS);
                 */
                /* ENABLE_REPLICATION end */
				Assert.assertTrue("Error list is not empty.",
						errorList.isEmpty());
			} catch (TimeoutException e) {
				future.cancel(true);
				e.printStackTrace();
			}

			// GET
			int errorCount = 0;
			for (String key : keys) {
				Future<Set<Object>> f = mc.asyncSopGet(key, 1, false, false);
				Set<Object> cachedList = null;
				try {
					cachedList = f.get();
				} catch (Exception e) {
					f.cancel(true);
					e.printStackTrace();
				}

				Assert.assertTrue("Cached list is empty.",
						!cachedList.isEmpty());

				for (Object o : cachedList) {
					if (!value.equals(o)) {
						errorCount++;
					}
				}
			}
			Assert.assertEquals("Error count is greater than 0.", 0, errorCount);

			// REMOVE
			for (String key : keys) {
				mc.asyncSopDelete(key, value, true).get();
			}
		} catch (Exception e) {
			e.printStackTrace();
			Assert.fail();
		}
	}

	public void testTimeout() {
		String value = "MyValue";
		int keySize = 100000;
		String[] keys = new String[keySize];
		for (int i = 0; i < keys.length; i++) {
			keys[i] = KEY + i;
		}

		try {
			long s = 0;
			// SET
			s = System.currentTimeMillis();
			Future<Map<String, CollectionOperationStatus>> future = mc
					.asyncSopInsertBulk(Arrays.asList(keys), value,
							new CollectionAttributes());

			try {
				future.get(1L, TimeUnit.MILLISECONDS);

				// System.out.println(System.currentTimeMillis() - s);

				Assert.fail("There is no timeout.");
			} catch (TimeoutException e) {
				future.cancel(true);
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Assert.fail("ERROR");
		}
	}

	public void testErrorCount() {
		String value = "MyValue";
		int keySize = 1200;

		String[] keys = new String[keySize];

		try {
			for (int i = 0; i < keys.length; i++) {
				keys[i] = KEY + i;
				mc.delete(keys[i]).get();
			}

			// SET
			Future<Map<String, CollectionOperationStatus>> future = mc
					.asyncSopInsertBulk(Arrays.asList(keys), value, null);

			Map<String, CollectionOperationStatus> map = future.get(1000L,
					TimeUnit.MILLISECONDS);
			assertEquals(keySize, map.size());

		} catch (Exception e) {
			e.printStackTrace();
			Assert.fail("ERROR");
		}
	}
}

@@ -1659,254 +1659,8 @@
 y %7B%0A
-                /* ENABLE_REPLICATION start */%0A                Map%3CString, CollectionOperationStatus%3E errorList = future.get(%0A                        1000L, TimeUnit.MILLISECONDS);%0A                /* ENABLE_REPLICATION else */%0A                /*%0A
 %09%09%09%09
@@ -1762,73 +1762,8 @@
 S);%0A
-                 */%0A                /* ENABLE_REPLICATION end */%0A
 %09%09%09%09

Revert operation timeout value in SopInsertBulkTest.