package com.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;

import com.fircosoft.cdb.shared.utils.ds.gen.DSAuditLog;
import com.isomorphic.datasource.DSRequest;
import com.isomorphic.datasource.DataSource;

public class DSTestCase
{
	@Before
	public void setUp()
	{
		new DSRequest(DSAuditLog.DS_NAME, DataSource.OP_ADD);
	}
	
	@Test(timeout = 100000)
	public void test() throws Exception
	{
		Thread.sleep(1000); //wait for init to complete
		ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(10, true));
		
		for (Runnable runnable : createJobs())
			executor.execute(runnable);
		
		Thread.sleep(10000);
	}
	
	private java.util.List<Runnable> createJobs()
	{
		List<Runnable> jobs = new ArrayList<Runnable>();
		for (int i = 0 ; i < 10 ; i++)
			jobs.add(new Runnable()
			{
				@Override
				public void run()
				{
					executeDSRequest();
				}
			});
		return jobs;
	}
	
	private void executeDSRequest()
	{
		System.out.println("executeDSRequest: " + Thread.currentThread().getName());
		final DSRequest auditRequest = new DSRequest(DSAuditLog.DS_NAME, DataSource.OP_ADD);
		auditRequest.setFieldValue(DSAuditLog.CREATED_BY, 302);
		auditRequest.setFieldValue(DSAuditLog.EVENT_ID, 2101);
		auditRequest.setFieldValue(DSAuditLog.RELOBJ_ID, 1);
		auditRequest.setFieldValue(DSAuditLog.RELOBJ_TYPE, "");
		auditRequest.setFieldValue(DSAuditLog.RELOBJ_LABEL, "");
		auditRequest.setFieldValue(DSAuditLog.DATA, "");
		try
		{
			auditRequest.execute();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
