Fiddling with threads
This commit is contained in:
parent
7134d9737c
commit
abb45db79d
|
|
@ -1,3 +1,4 @@
|
|||
import asyncio
|
||||
import os
|
||||
|
||||
|
||||
|
|
@ -17,6 +18,7 @@ class SpideyTest:
|
|||
self.os_name = agnpath.os
|
||||
self.main_path = agnpath.main_path
|
||||
self.test_data = test_data
|
||||
self.alive = True
|
||||
|
||||
def run_test(self):
|
||||
"""
|
||||
|
|
@ -25,6 +27,9 @@ class SpideyTest:
|
|||
:return: Whether any of the index.js is more than one line
|
||||
:rtype: bool
|
||||
"""
|
||||
import random, time
|
||||
# Wait a random amount of time to make sure the files are loaded
|
||||
time.sleep(random.randint(1, 5))
|
||||
paths = self.get_path()
|
||||
self.is_infected = False
|
||||
lines = 0
|
||||
|
|
@ -75,4 +80,16 @@ class SpideyTest:
|
|||
:rtype: (int, str)
|
||||
"""
|
||||
# TODO: Make use of a global_dict
|
||||
return (self.get_status_code(), "")
|
||||
return self.get_status_code(), ""
|
||||
|
||||
|
||||
async def main(args, agnpath, test_data):
|
||||
"""
|
||||
The main function of the test.
|
||||
:return: The status of the test
|
||||
:rtype: (int, str)
|
||||
"""
|
||||
spidey_test = SpideyTest(args, agnpath, test_data)
|
||||
await asyncio.sleep(5)
|
||||
spidey_test.run_test()
|
||||
return spidey_test.get_status()
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import threading
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
|
@ -35,6 +36,7 @@ class TestRunner:
|
|||
|
||||
self.loaded_test_modules = {}
|
||||
self.loaded_test_classes = {}
|
||||
self.list_of_tests = []
|
||||
self.update_module_list()
|
||||
self.exec_all_tests()
|
||||
|
||||
|
|
@ -58,18 +60,66 @@ class TestRunner:
|
|||
self.loaded_test_modules.update(modules)
|
||||
self.loaded_test_classes.update(classes)
|
||||
|
||||
def test_controller(self, rate=1):
|
||||
"""
|
||||
Controller for the tests.
|
||||
:param rate: The rate at which the controller will check the tests
|
||||
:type rate: int
|
||||
:return: 1 if all test ran, 0 if one or more tests failed, 2 if the user stopped the tests
|
||||
"""
|
||||
l_test = len(self.list_of_tests)
|
||||
while True:
|
||||
still_running = 0
|
||||
for t in self.list_of_tests:
|
||||
if t.is_alive():
|
||||
still_running += 1
|
||||
if still_running == 0:
|
||||
return 1
|
||||
|
||||
def test_killer(self):
|
||||
"""
|
||||
Kills the tests if the user press enter
|
||||
"""
|
||||
while True:
|
||||
if input() == "":
|
||||
for t in self.list_of_tests:
|
||||
t.terminate()
|
||||
return
|
||||
|
||||
def exec_all_tests(self):
|
||||
"""
|
||||
Executes all the tests (later in parallel), get their result as they finishes
|
||||
:return: The number of test ran, success, failures, skipped (with ran = success+failures+skip)
|
||||
:rtype: (int,int,int,int) # TODO: Change this to a nametuple
|
||||
"""
|
||||
self.list_of_tests = []
|
||||
|
||||
for module_name, test_class in self.loaded_test_classes.items():
|
||||
tc = test_class(self.args, self.agnostic_path, None)
|
||||
tc.run_test()
|
||||
tc.daemon = True
|
||||
tc = threading.Thread(target=tc.run_test)
|
||||
self.list_of_tests.append(tc)
|
||||
tc.start()
|
||||
|
||||
# Launch a controller to allow the user to stop the tests
|
||||
# controller = threading.Thread(target=self.test_launcher)
|
||||
|
||||
# Allow the user to stop the tests
|
||||
tk = threading.Thread(target=self.test_killer)
|
||||
tk.start()
|
||||
|
||||
for t in self.list_of_tests:
|
||||
t.join()
|
||||
|
||||
if tk.is_alive():
|
||||
tk.terminate()
|
||||
|
||||
# await asyncio.gather(*list_of_tests)
|
||||
# tc.run_test()
|
||||
|
||||
|
||||
def run_check(args, agnpath): # Todo, skip the is_infected check and defer everything to the exec_all_tests
|
||||
test_selected = True
|
||||
spd = TestRunner(args, agnpath)
|
||||
if not spd.is_infected:
|
||||
return 0
|
||||
|
|
|
|||
Reference in New Issue