package com.tvd12.ezyfox.hazelcast.testing.service;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import org.testng.annotations.Test;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.tvd12.ezyfox.hazelcast.service.EzyEntryProcessorMaxIdService;
import com.tvd12.ezyfox.hazelcast.testing.HazelcastBaseTest;

public class EzyEntryProcessorMaxIdServiceTest extends HazelcastBaseTest {

	@Test
	public void test() throws Exception {
		final EzyEntryProcessorMaxIdService service = new EzyEntryProcessorMaxIdService(HZ_INSTANCE);
		service.setMapTransactionFactory(MAP_TRANSACTION_FACTORY);
		
		List<Long> nums = new ArrayList<>();
		Thread[] threads = new Thread[1000];
		for(int i = 0 ; i < threads.length ; ++i) {
			threads[i] = new Thread(() -> {
				nums.add(service.incrementAndGet("something"));
			});
		}
		for(int i = 0 ; i < threads.length ; ++i) {
			threads[i].start();
//			threads[i].join();
		}
		
		Thread.sleep(2000L);
		
		System.out.println(nums);
		for(int i = 0 ; i < nums.size() - 1 ; ++i) {
			if(nums.get(i + 1) != nums.get(i) + 1) {
				System.err.println("entryprocessor xxx: error in " + i);
			}
		}
		
	}
	
	@Test
	public void test2() throws Exception {
		final EzyEntryProcessorMaxIdService service = new EzyEntryProcessorMaxIdService();
		service.setHazelcastInstance(HZ_INSTANCE);
		service.setMapTransactionFactory(MAP_TRANSACTION_FACTORY);
		
		List<Long> nums = new ArrayList<>();
		Thread[] threads = new Thread[1000];
		for(int i = 0 ; i < threads.length ; ++i) {
			threads[i] = new Thread(() -> {
				nums.add(service.incrementAndGet("somethingy", 2));
			});
		}
		for(int i = 0 ; i < threads.length ; ++i) {
			threads[i].start();
//			threads[i].join();
		}
		
		Thread.sleep(3000L);
		
		System.out.println(nums);
		for(int i = 0 ; i < nums.size() - 1 ; ++i) {
			if(nums.get(i + 1) != nums.get(i) + 2) {
				System.err.println("entryprocessor yyy: error in " + i);
			}
		}
		
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Test
	public void test3() {
		IMap map = mock(IMap.class);
		HazelcastInstance hzInstance = mock(HazelcastInstance.class);
		when(hzInstance.getMap(anyString())).thenReturn(map);
		EzyEntryProcessorMaxIdService service = new EzyEntryProcessorMaxIdService(hzInstance);
		service.setMapTransactionFactory(MAP_TRANSACTION_FACTORY);
		service.loadAll();
	}
	
}

@@ -1914,32 +1914,41 @@
 .println(nums);%0A
+%09%09try %7B%0A%09
 %09%09for(int i = 0 
@@ -1973,32 +1973,33 @@
 - 1 ; ++i) %7B%0A%09%09%09
+%09
 if(nums.get(i + 
@@ -2019,24 +2019,25 @@
 t(i) + 2) %7B%0A
+%09
 %09%09%09%09System.e
@@ -2080,33 +2080,91 @@
 r in %22 + i);%0A%09%09%09
-%7D
+%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09catch (Exception e) %7B%0A%09%09%09e.printStackTrace();
 %0A%09%09%7D%0A%09%09%0A%09%7D%0A%09%0A%09@S

update unitest