import pprint
from testtools import ConcurrentStreamTestSuite, StreamResult, TextTestResult, StreamFailFast, TestResult, StreamToDict, \
    StreamToExtendedDecorator, StreamSummary, CopyStreamResult
import time
import bitcodin

__author__ = 'Dominic Miglar <dominic.miglar@bitmovin.net>'

import sys
from unittest import TestSuite, TextTestRunner
from . import core
from . import http
from . import encodingprofile
from . import input
from . import output
from . import job
from . import statistics

VERBOSITY_LEVEL = 2


def _collect_test_suites():
    test_suites = list()
    test_suites.append(core.get_test_suite())
    test_suites.append(http.get_test_suite())
    test_suites.append(encodingprofile.get_test_suite())
    test_suites.append(input.get_test_suite())
    test_suites.append(output.get_test_suite())
    test_suites.append(job.get_test_suite())
    test_suites.append(statistics.get_test_suite())
    return test_suites


def _run_tests(test_suite):
    return TextTestRunner(verbosity=VERBOSITY_LEVEL).run(test_suite)

def callback(arg):
    if "traceback" in arg["details"]:
        traceback = arg["details"]['traceback'].as_text()
        global FAILED
        FAILED = True
    else:
        traceback = ""

    print(arg["status"] + " " + traceback)



def main():

    test_suites = _collect_test_suites()
    main_test_suite = TestSuite()

    for test_suite in test_suites:
        main_test_suite.addTests(test_suite)

    concurrent_suite = ConcurrentStreamTestSuite(lambda: ((case, None) for case in main_test_suite))

    result = StreamToDict(callback)
    result.startTestRun()
    try:
        concurrent_suite.run(result)
    finally:
        result.stopTestRun()

    if 'FAILED' in globals() and FAILED:
        sys.exit(1)


if __name__ == '__main__':
    main()

@@ -1284,17 +1284,16 @@
 main():%0A
-%0A
     test
@@ -1441,16 +1441,320 @@
 suite)%0A%0A
+    test_suite_result = _run_tests(main_test_suite)%0A    if not test_suite_result.wasSuccessful():%0A        sys.exit(1)%0A%0A# def main():%0A#%0A#     test_suites = _collect_test_suites()%0A#     main_test_suite = TestSuite()%0A#%0A#     for test_suite in test_suites:%0A#         main_test_suite.addTests(test_suite)%0A#%0A# 
     conc
@@ -1846,17 +1846,20 @@
 suite))%0A
-%0A
+#%0A# 
     resu
@@ -1886,20 +1886,22 @@
 llback)%0A
+#
     
+ 
 result.s
@@ -1914,16 +1914,18 @@
 stRun()%0A
+# 
     try:
@@ -1925,24 +1925,25 @@
    try:%0A
+#
         
 concurre
@@ -1930,24 +1930,25 @@
 y:%0A#        
+ 
 concurrent_s
@@ -1964,16 +1964,18 @@
 result)%0A
+# 
     fina
@@ -1979,16 +1979,18 @@
 inally:%0A
+# 
         
@@ -2010,17 +2010,20 @@
 stRun()%0A
-%0A
+#%0A# 
     if '
@@ -2055,16 +2055,18 @@
 FAILED:%0A
+# 
         

Disable parallel test execution.