<commit_before>package brooklyn.util.task

import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore;

import junit.extensions.RepeatedTest;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import brooklyn.management.ExecutionManager;
import brooklyn.management.Task;

class SynchronizedExecutionTest {
    List data = new ArrayList()
    
    /** accepts flags last and size */
    protected void assertDataInOrder(Map flags=[:]) {
        Object last = flags["last"]
        Object size = flags["size"]?:null
        
        if (size!=null) Assert.assertTrue("expected size $size but it is ${data.size}; list is $data", data.size()==size)
        
        Object lo = null
        for (Object o in data) {
            if (lo!=null) Assert.assertTrue("expected $o greater than $lo; list is $data", lo.compareTo(o) < 0)
            lo = o
        }
        if (last!=null) Assert.assertTrue("expected last element $last but it is $lo; list is $data", lo==last)
    }
    
    @Test
    public void runTwoSynchedTasks() {
        data.clear()
        BasicTask t1 = [ { data << 1;  } ]
        BasicTask t2 = [ { data << 2; } ]
        data << 0
        
        BasicExecutionManager em = []
        em.setTaskPreprocessorForTag("category1", SingleThreadedExecution.class);
        
        em.submit tag:"category1", t1
        em.submit tag:"category1", t2

        t2.get()
        assertDataInOrder(size:3, last:2)
    }

    @Test
    public void runTwoSynchedTasks100Times() {
        100.times { runTwoSynchedTasks() }
    }
}<commit_msg>extra debug as test seems to hang in jenkins (fine locally)<commit_after>package brooklyn.util.task

import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore;

import junit.extensions.RepeatedTest;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import brooklyn.management.ExecutionManager;
import brooklyn.management.Task;

class SynchronizedExecutionTest {
    List data = new ArrayList()
    
    /** accepts flags last and size */
    protected void assertDataInOrder(Map flags=[:]) {
        Object last = flags["last"]
        Object size = flags["size"]?:null
        
        if (size!=null) Assert.assertTrue("expected size $size but it is ${data.size}; list is $data", data.size()==size)
        
        Object lo = null
        for (Object o in data) {
            if (lo!=null) Assert.assertTrue("expected $o greater than $lo; list is $data", lo.compareTo(o) < 0)
            lo = o
        }
        if (last!=null) Assert.assertTrue("expected last element $last but it is $lo; list is $data", lo==last)
    }
    
    @Test
    public void runTwoSynchedTasks() {
        data.clear()
        BasicTask t1 = [ { data << 1;  } ]
        BasicTask t2 = [ { data << 2; } ]
        data << 0
        
        BasicExecutionManager em = []
        
        //for debug
        Thread mon = new Thread({ try { Thread.sleep(1000); println "tasks for $em"; em.allTasks.each { println "  "+it.getStatusDetail(true); } } catch (Throwable t) {} })
        mon.start();
        
        em.setTaskPreprocessorForTag("category1", SingleThreadedExecution.class);
        
        em.submit tag:"category1", t1
        em.submit tag:"category1", t2

        t2.get()
        assertDataInOrder(size:3, last:2)
        
        mon.interrupt()
    }

    @Test
    public void runTwoSynchedTasks100Times() {
        100.times { runTwoSynchedTasks() }
    }
}
{'fraction_non_alphanumeric': 0.11878685762426285, 'fraction_numerical': 0.012356079752878404, 'mean_word_length': 2.48873653281097, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 3, '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': '2277781', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1059, 'n_words': 278}