package io.enmasse.systemtest.marathon;

import io.enmasse.systemtest.Logging;
import io.enmasse.systemtest.TestBase;
import org.junit.Rule;
import org.junit.rules.ErrorCollector;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class MarathonTestBase extends TestBase {

    @Rule
    public ErrorCollector collector = new ErrorCollector();

    protected void runTestInLoop(int durationMinutes, TestLoop test) {
        Logging.log.info(String.format("Starting test running for %d minutes at %s",
                durationMinutes, new Date().toString()));
        for(long stop = System.nanoTime() + TimeUnit.MINUTES.toNanos(durationMinutes); stop > System.nanoTime();) {
            try {
                test.run();
            }catch (Exception ex){
                collector.addError(ex);
            }
        }
    }

    protected interface TestLoop{
        void run() throws Exception;
    }
}

@@ -111,16 +111,90 @@
 stBase;%0A
+import org.apache.http.util.ExceptionUtils;%0Aimport org.junit.BeforeClass;%0A
 import o
@@ -271,16 +271,65 @@
 l.Date;%0A
+import java.util.Iterator;%0Aimport java.util.Map;%0A
 import j
@@ -409,16 +409,1081 @@
 Base %7B%0A%0A
+    @BeforeClass%0A    public void initThreadDump() %7B%0A        Logging.log.info(%22Setting default exception handler for threads%22);%0A        Thread.setDefaultUncaughtExceptionHandler((thread, e) -%3E %7B%0A            if (e instanceof OutOfMemoryError) %7B%0A                Logging.log.error(%22Got OOM, dumping thread info%22);%0A                printThreadDump();%0A            %7D else %7B%0A                Logging.log.error(%22Caught exception %7B%7D%22, e);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public static void printThreadDump() %7B%0A        Map%3CThread, StackTraceElement%5B%5D%3E allThreads = Thread.getAllStackTraces();%0A        Iterator%3CThread%3E iterator = allThreads.keySet().iterator();%0A        while (iterator.hasNext()) %7B%0A            StringBuilder sb = new StringBuilder();%0A            Thread key = iterator.next();%0A            StackTraceElement%5B%5D trace = allThreads.get(key);%0A            sb.append(key + %22%5Cr%5Cn%22);%0A            for (int i = 0; i %3C trace.length; i++) %7B%0A                sb.append(%22 %22 + trace%5Bi%5D + %22%5Cr%5Cn%22);%0A            %7D%0A            Logging.log.error(sb.toString());%0A        %7D%0A    %7D%0A%0A%0A
     @Rul

Set default thread exception handler before starting tests