<commit_before>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.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.junit.Test;

public class TracyTestFuture {
    private static final int NTHREDS = 10;
    private	ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);

    //TODO: Create TracyableFuture interface providing getData() and getTracyThreadContext()

    private Future<TracyableFutureResponse> futureIt(final TracyableFutureRequest request)	{
        Callable<TracyableFutureResponse> worker = null;
//        System.out.println("Executing future");
        // Creates Tracy worker thread context to be bound to the worker thread
        final TracyThreadContext ctx = Tracy.createWorkerTheadContext();
        worker = new Callable<TracyableFutureResponse>() {
            public TracyableFutureResponse call() throws Exception {
                TracyableFutureResponse td = new TracyableFutureResponse();
                // Binds context to worker thread so static Tracy calls can be made (e.g. before() after())
                if (request.isTraced())	{
                    Tracy.setWorkerContext(ctx);
                }
                else	{
                	Tracy.clearWorkerContext();
                }
//                System.out.println("Executing future (call) ");
                Thread.sleep(((Integer)(request.getData()))*100);
                Tracy.before("Worker-" + request.getData().toString());
                String out = request.getData().toString();
                Tracy.after("Worker-" + request.getData().toString());
                td.setData(out);
                td.setTracyThreadContext(Tracy.getWorkerContext());
                return td;
            }
        };
        return executor.submit(worker);
    }

    @Test
    public void testFutureTrace() throws InterruptedException {
        final int NUM_FUTURES = 2;
        ArrayList<Future<TracyableFutureResponse>> futuresList = new ArrayList<Future<TracyableFutureResponse>>();
        int i;
        
        Tracy.setContext();
        Tracy.before("Requestor");
        try {
            for (i=0; i<NUM_FUTURES ; i++)	{
//                System.out.println("Calling future " +i);
                futuresList.add(futureIt(new TracyableFutureRequest(true, new Integer(i))));
            }

            i=0;
            for (Future<TracyableFutureResponse> future : futuresList)	{
//                System.out.println("Polling future");
                TracyableFutureResponse out =  future.get();
                String str = (String) out.getData();
                assertEquals(Integer.toString(i), str);
//                System.out.println("Got future " + str);
                // Merge worker trace into the task handler thread Tracy context
                Tracy.mergeWorkerContext(out.getTracyThreadContext());
                i++;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Tracy.after("Requestor");
        List<TracyEvent> events = Tracy.getEvents();
        assertEquals(3, events.size());
        assertEquals("Worker-0", events.get(0).getLabel());
        assertEquals("Worker-1", events.get(1).getLabel());
        assertEquals("Requestor", events.get(2).getLabel());
    }
    
    @Test
    public void testFutureTrace_disabled() throws InterruptedException {
        final int NUM_FUTURES = 2;
        ArrayList<Future<TracyableFutureResponse>> futuresList = new ArrayList<Future<TracyableFutureResponse>>();
        int i;
       
        Tracy.clearContext();
        Tracy.before("Requestor");
        try {
            for (i=0; i<NUM_FUTURES ; i++)	{
//                System.out.println("Calling future " +i);
                futuresList.add(futureIt(new TracyableFutureRequest(false, new Integer(i))));
            }

            i=0;
            for (Future<TracyableFutureResponse> future : futuresList)	{
//                System.out.println("Polling future");
                TracyableFutureResponse out =  future.get();
                String str = (String) out.getData();
                assertEquals(Integer.toString(i), str);
//                System.out.println("Got future " + str);
                // Merge worker trace into the task handler thread Tracy context
                Tracy.mergeWorkerContext(out.getTracyThreadContext());
                i++;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Tracy.after("Requestor");
        assertEquals(null, Tracy.getEvents());
    }
}
<commit_msg>Added comment<commit_after>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.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.junit.Test;

public class TracyTestFuture {
    private static final int NTHREDS = 10;
    private	ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);

    //TODO: Create TracyableFuture interface providing getData() and getTracyThreadContext()

    private Future<TracyableFutureResponse> futureIt(final TracyableFutureRequest request)	{
        Callable<TracyableFutureResponse> worker = null;
//        System.out.println("Executing future");
        // Creates Tracy worker thread context to be bound to the worker thread
        final TracyThreadContext ctx = Tracy.createWorkerTheadContext();
        worker = new Callable<TracyableFutureResponse>() {
            public TracyableFutureResponse call() throws Exception {
                TracyableFutureResponse td = new TracyableFutureResponse();
                // Binds context to worker thread so static Tracy calls can be made (e.g. before() after())
                if (request.isTraced())	{
                    Tracy.setWorkerContext(ctx);
                }
                else	{
                	Tracy.clearWorkerContext();
                }
//                System.out.println("Executing future (call) ");
                Thread.sleep(((Integer)(request.getData()))*100);
                // request.getData() retrieves input data the worker required
                Tracy.before("Worker-" + request.getData().toString());
                String out = request.getData().toString();
                Tracy.after("Worker-" + request.getData().toString());
                td.setData(out);
                td.setTracyThreadContext(Tracy.getWorkerContext());
                return td;
            }
        };
        return executor.submit(worker);
    }

    @Test
    public void testFutureTrace() throws InterruptedException {
        final int NUM_FUTURES = 2;
        ArrayList<Future<TracyableFutureResponse>> futuresList = new ArrayList<Future<TracyableFutureResponse>>();
        int i;
        
        Tracy.setContext();
        Tracy.before("Requestor");
        try {
            for (i=0; i<NUM_FUTURES ; i++)	{
//                System.out.println("Calling future " +i);
                futuresList.add(futureIt(new TracyableFutureRequest(true, new Integer(i))));
            }

            i=0;
            for (Future<TracyableFutureResponse> future : futuresList)	{
//                System.out.println("Polling future");
                TracyableFutureResponse out =  future.get();
                String str = (String) out.getData();
                assertEquals(Integer.toString(i), str);
//                System.out.println("Got future " + str);
                // Merge worker trace into the task handler thread Tracy context
                Tracy.mergeWorkerContext(out.getTracyThreadContext());
                i++;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Tracy.after("Requestor");
        List<TracyEvent> events = Tracy.getEvents();
        assertEquals(3, events.size());
        assertEquals("Worker-0", events.get(0).getLabel());
        assertEquals("Worker-1", events.get(1).getLabel());
        assertEquals("Requestor", events.get(2).getLabel());
    }
    
    @Test
    public void testFutureTrace_disabled() throws InterruptedException {
        final int NUM_FUTURES = 2;
        ArrayList<Future<TracyableFutureResponse>> futuresList = new ArrayList<Future<TracyableFutureResponse>>();
        int i;
       
        Tracy.clearContext();
        Tracy.before("Requestor");
        try {
            for (i=0; i<NUM_FUTURES ; i++)	{
//                System.out.println("Calling future " +i);
                futuresList.add(futureIt(new TracyableFutureRequest(false, new Integer(i))));
            }

            i=0;
            for (Future<TracyableFutureResponse> future : futuresList)	{
//                System.out.println("Polling future");
                TracyableFutureResponse out =  future.get();
                String str = (String) out.getData();
                assertEquals(Integer.toString(i), str);
//                System.out.println("Got future " + str);
                // Merge worker trace into the task handler thread Tracy context
                Tracy.mergeWorkerContext(out.getTracyThreadContext());
                i++;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Tracy.after("Requestor");
        assertEquals(null, Tracy.getEvents());
    }
}

{'fraction_non_alphanumeric': 0.11047360734404339, 'fraction_numerical': 0.003755476736907991, 'mean_word_length': 2.123818833496253, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7482507', 'n_tokens_mistral': 2567, 'n_tokens_neox': 2428, 'n_words': 536}