-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (81 loc) · 3.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from tester_files.server.server import start_http_server
from tester_files.client.client import start_http_clients
from tester_files.server.server_timer import handle_server_timer
from db_tables.http_tests import HttpTest
from db_tables.db_base import session
import threading
import time
import logging
import os
from logging import config
ERROR_FORMAT = "%(levelname)s at %(asctime)s in function '%(funcName)s' in file \"%(pathname)s\" at line %(lineno)d: %(message)s"
DEBUG_FORMAT = "%(levelname)s at %(asctime)s in function '%(funcName)s' in file \"%(pathname)s\" at line %(lineno)d: %(message)s"
LOG_CONFIG = {'version':1,
'formatters':{'error':{'format':ERROR_FORMAT},
'debug':{'format':DEBUG_FORMAT}},
'handlers':{'console':{'class':'logging.StreamHandler',
'formatter':'debug',
'level':logging.DEBUG},
'file':{'class':'logging.FileHandler',
'filename':'http_test.log',
'formatter':'error',
'level':logging.ERROR}},
'root':{'handlers':['console'], 'level':'DEBUG'}}
logging.config.dictConfig(LOG_CONFIG)
logger = logging.getLogger(__name__)
MAX_CONCURRENT_TESTS = 3
class HTTPClient(threading.Thread):
"""
Description : HTTP Client Thread to send the request
"""
def __init__(self, test_id):
threading.Thread.__init__(self)
self.test_id = test_id
def run(self):
"""
Description: Runs the Thread HTTP Client thread
input_param:
input_type:
out_param:
out_type:
sample output:
"""
logger.debug("Starting the clients Thread")
start_http_clients(self.test_id)
class HTTPServer(threading.Thread):
"""
Description : HTTP Server Thread to send the response
"""
def __init__(self):
threading.Thread.__init__(self)
def run(self):
"""
Description: Runs the Thread HTTP Server thread
input_param:
input_type:
out_param:
out_type:
sample output:
"""
logger.debug("Starting the server Thread")
start_http_server()
if __name__ == "__main__":
http_server = HTTPServer()
http_server.start()
time.sleep(2)
while True:
try:
tot_running_tests = session.query(HttpTest).filter(HttpTest.running==True).count()
if tot_running_tests < MAX_CONCURRENT_TESTS and not os.path.isfile("dont_run"):
new_test = session.query(HttpTest).filter(HttpTest.running==False)\
.filter(HttpTest.completed==False).first()
if new_test:
http_clients = HTTPClient(new_test.id)
http_clients.start()
new_test.running=True
#http_clients.join()
time.sleep(2)
time.sleep(10)
except Exception as e:
logger.exception("Exception in Main thread " + str(e) )
http_server.join()