package com.apm4all.tracy;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.junit.Test;

public class TracyTestFuture {
	private static final int NTHREDS = 10;
	static final String TASK_ID = "TID-ab1234-x";
	static final String PARENT_OPT_ID = "AAAA";
	static final String L1_LABEL_NAME = "L1 Operation";
	static final String L11_LABEL_NAME = "L11 Operation";
	private	ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);

	//TODO: Create ctx = Tracy.createFutureTheadContext();
	//TODO: Create TracyableFuture interface providing getData() and getTracyThreadContext()
	//TODO: Create Tracy.mergeFutureThreadContext(ctx);
	
	private Future<Long> retrieve()	{
		Callable<Long> worker = null;
		System.out.println("Executing future");
		worker = new Callable<Long>() {
			public Long call() throws Exception {
				System.out.println("Executing future (call)");
				Long someInteger = System.currentTimeMillis();
				return someInteger;
			}
		};
		return executor.submit(worker);
	}
	
	@Test
	public void testFutureTrace() {
		//FIXME: Future test blocking
		final int NUM_FUTURES = 2;
		ArrayList<Future<Long>> futuresList = new ArrayList<Future<Long>>();
		int i;
		try {
			for (i=0; i<NUM_FUTURES ; i++)	{
				System.out.println("Calling future");
				futuresList.add(retrieve());
			}
			Thread.sleep(1000);
			
			for (Future<Long> future : futuresList)	{
				System.out.println("Polling future");
				Long l =  future.get();
				System.out.println("Got " + l);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
}

@@ -743,64 +743,8 @@
 );%0A%0A
-%09//TODO: Create ctx = Tracy.createFutureTheadContext();%0A
 %09//T
@@ -834,93 +834,53 @@
 ()%0A%09
-//TODO: Create Tracy.mergeFutureThreadContext(ctx);%0A%09%0A%09private Future%3CLong%3E retrieve(
+%0A%09private Future%3CString%3E futureIt(final int i
 )%09%7B%0A
@@ -890,18 +890,20 @@
 allable%3C
-Lo
+Stri
 ng%3E work
@@ -955,16 +955,73 @@
 ture%22);%0A
+%09%09//TODO: Create ctx = Tracy.createFutureTheadContext();%0A
 %09%09worker
@@ -1036,18 +1036,20 @@
 allable%3C
-Lo
+Stri
 ng%3E() %7B%0A
@@ -1058,18 +1058,20 @@
 %09public 
-Lo
+Stri
 ng call(
@@ -1142,60 +1142,93 @@
 all)
-%22
+ %22 + i
 );%0A%09%09%09%09
-Long someInteger = System.currentTimeMillis(
+String out = Thread.currentThread().getName() + %22 %22 + Integer.toString(i
 );%0A%09
@@ -1241,19 +1241,11 @@
 urn 
-someInteger
+out
 ;%0A%09%09
@@ -1407,26 +1407,28 @@
 List%3CFuture%3C
-Lo
+Stri
 ng%3E%3E futures
@@ -1455,18 +1455,20 @@
 %3CFuture%3C
-Lo
+Stri
 ng%3E%3E();%0A
@@ -1554,25 +1554,29 @@
 lling future
-%22
+ %22 +i
 );%0A%09%09%09%09futur
@@ -1590,17 +1590,18 @@
 add(
-retrieve(
+futureIt(i
 ));%0A
@@ -1605,16 +1605,18 @@
 );%0A%09%09%09%7D%0A
+//
 %09%09%09Threa
@@ -1649,18 +1649,20 @@
 (Future%3C
-Lo
+Stri
 ng%3E futu
@@ -1731,14 +1731,18 @@
 %09%09%09%09
-Long l
+String out
  =  
@@ -1787,13 +1787,90 @@
 Got 
-%22 + l
+future %22 + out);%0A                //TODO: Create Tracy.mergeFutureThreadContext(ctx
 );%0A%09

rework of futures test