<commit_before>package com.github.dexecutor.executor;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.github.dexecutor.executor.DependentTasksExecutor.ExecutionBehavior;
import com.github.dexecutor.executor.graph.Graph.Node;
import com.github.dexecutor.executor.support.ThreadPoolUtil;

import mockit.Deencapsulation;
import mockit.Mock;
import mockit.MockUp;
import mockit.integration.junit4.JMockit;

/**
 * 
 * @author <NAME>
 *
 */
@RunWith(JMockit.class)
public class DefaultDependentTasksExecutorTest {

	@Test
	public void testDependentTaskExecutionOrder() {

		new MockedCompletionService();

		DefaultDependentTasksExecutor<Integer> executor = newTaskExecutor(false);

		addDependencies(executor);

		executor.execute(ExecutionBehavior.RETRY_ONCE_TERMINATING);

		Collection<Node<Integer>> processedNodesOrder = Deencapsulation.getField(executor, "processedNodes");

		assertThat(processedNodesOrder, equalTo(executionOrderExpectedResult()));
	}

	
	@Test
	public void testDependentTaskExecutionOrderWithException() {

		new MockedCompletionService();

		DefaultDependentTasksExecutor<Integer> executor = newTaskExecutor(true);

		addDependencies(executor);

		executor.execute(ExecutionBehavior.RETRY_ONCE_TERMINATING);

		Collection<Node<Integer>> processedNodesOrder = Deencapsulation.getField(executor, "processedNodes");

		assertThat(processedNodesOrder, equalTo((Collection<Node<Integer>>)Arrays.asList(new Node<Integer>(1), new Node<Integer>(11), new Node<Integer>(12))));
	}


	private void addDependencies(DefaultDependentTasksExecutor<Integer> executor) {
		executor.addDependency(1, 2);
		executor.addDependency(1, 2);
		executor.addDependency(1, 3);
		executor.addDependency(3, 4);
		executor.addDependency(3, 5);
		executor.addDependency(3, 6);
		executor.addDependency(2, 7);
		executor.addDependency(2, 9);
		executor.addDependency(2, 8);
		executor.addDependency(9, 10);
		executor.addDependency(12, 13);
		executor.addDependency(13, 4);
		executor.addDependency(13, 14);
		executor.addIndependent(11);
	}

	private Collection<Node<Integer>> executionOrderExpectedResult() {
		List<Node<Integer>> result = new ArrayList<Node<Integer>>();
		result.add(new Node<Integer>(1));
		result.add(new Node<Integer>(11));
		result.add(new Node<Integer>(12));
		result.add(new Node<Integer>(2));
		result.add(new Node<Integer>(3));
		result.add(new Node<Integer>(13));
		result.add(new Node<Integer>(7));
		result.add(new Node<Integer>(9));
		result.add(new Node<Integer>(8));
		result.add(new Node<Integer>(5));
		result.add(new Node<Integer>(6));
		result.add(new Node<Integer>(4));
		result.add(new Node<Integer>(14));
		result.add(new Node<Integer>(10));<commit_msg>More tests<commit_after>package com.github.dexecutor.executor;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.github.dexecutor.executor.DependentTasksExecutor.ExecutionBehavior;
import com.github.dexecutor.executor.graph.Graph.Node;
import com.github.dexecutor.executor.support.ThreadPoolUtil;

import mockit.Deencapsulation;
import mockit.Mock;
import mockit.MockUp;
import mockit.integration.junit4.JMockit;

/**
 * 
 * @author <NAME>
 *
 */
@RunWith(JMockit.class)
public class DefaultDependentTasksExecutorTest {

	@Test
	public void testDependentTaskExecutionOrder() {

		new MockedCompletionService();

		DefaultDependentTasksExecutor<Integer> executor = newTaskExecutor(false);

		addDependencies(executor);

		executor.execute(ExecutionBehavior.RETRY_ONCE_TERMINATING);

		Collection<Node<Integer>> processedNodesOrder = Deencapsulation.getField(executor, "processedNodes");

		assertThat(processedNodesOrder, equalTo(executionOrderExpectedResult()));
	}

	
	@Test
	public void testDependentTaskExecutionOrderWithException() {

		new MockedCompletionService();

		DefaultDependentTasksExecutor<Integer> executor = newTaskExecutor(true);

		addDependencies(executor);

		executor.execute(ExecutionBehavior.RETRY_ONCE_TERMINATING);

		Collection<Node<Integer>> processedNodesOrder = Deencapsulation.getField(executor, "processedNodes");

		assertThat(processedNodesOrder, equalTo((Collection<Node<Integer>>)Arrays.asList(new Node<Integer>(1), new Node<Integer>(11), new Node<Integer>(12))));
	}


	private void addDependencies(DefaultDependentTasksExecutor<Integer> executor) {
		executor.addDependency(1, 2);
		executor.addDependency(1, 2);
		executor.addDependency(1, 3);
		executor.addDependency(3, 4);
		executor.addDependency(3, 5);
		executor.addDependency(3, 6);
		executor.addDependency(2, 7);
		executor.addDependency(2, 9);
		executor.addDependency(2, 8);
		executor.addDependency(9, 10);
		executor.addDependency(12, 13);
		executor.addDependency(13, 4);
		executor.addDependency(13, 14);
		executor.addIndependent(11);
		executor.addAsDependencyToAllLeafNodes(15);
	}

	private Collection<Node<Integer>> executionOrderExpectedResult() {
		List<Node<Integer>> result = new ArrayList<Node<Integer>>();
		result.add(new Node<Integer>(1));
		result.add(new Node<Integer>(11));
		result.add(new Node<Integer>(12));
		result.add(new Node<Integer>(2));
		result.add(new Node<Integer>(3));
		result.add(new Node<Integer>(13));
		result.add(new Node<Integer>(7));
		result.add(new Node<Integer>(9));
		result.add(new Node<Integer>(8));
		result.add(new Node<Integer>(5));
		result.add(new Node<Integer>(6));
		result.add(new Node<Integer>(4));
		result.add(new Node<Integer>(14));
		result.add(new Node<Integer>(10));
		result.add(new Node<Integer>(15));
{'fraction_non_alphanumeric': 0.12984670874661858, 'fraction_numerical': 0.01833483618875864, 'mean_word_length': 7.008423586040915, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 70, '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': '4445166', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2052, 'n_words': 329}