This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_sample.py
151 lines (119 loc) · 3.87 KB
/
test_sample.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/python3
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 您可以在下面的链接找到该许可证.
# https://github.com/weilinfox/py-hakuBot/blob/main/LICENSE
"""
自定义测试模板
"""
import threading
import requests
import time
import logging
import main as hakubot
import test.cqhttp as cqhttp
SENDHOST = '127.0.0.1'
SENDPORT = hakubot.PORT
sendurl = f'http://{SENDHOST}:{SENDPORT}'
LISTENURL = hakubot.POSTURL.split(':', 1)
LISTENHOST = LISTENURL[0]
LISTENPORT = int(LISTENURL[1])
listenurl = f'http://{LISTENHOST}:{LISTENPORT}'
cqhttp.cqhttp_init(LISTENHOST, LISTENPORT)
testthreads = list()
testfunctions = list()
groupmsgdict = {'message': '这是一个测试',
'message_type': 'private',
'post_type': 'message',
'raw_message': '这是一个测试',
'self_id': 2000000000,
'time': time.time(),
'user_id': 2000000002}
usermsgdict = {'message': '这是一个测试',
'message_type': 'group',
'post_type': 'message',
'raw_message': '这是一个测试',
'group_id': 2000000001,
'self_id': 2000000000,
'time': time.time(),
'user_id': 2000000002}
mylogger = logging.getLogger('hakuBot')
def test_server_start():
"""
启动所有测试服务器
:return: 成功返回 True 失败返回 False
"""
bot = threading.Thread(target=hakubot.haku_start, args=[])
cq = threading.Thread(target=cqhttp.cqhttp_start, args=[])
bot.start()
cq.start()
time.sleep(0.5)
return bot.is_alive() and cq.is_alive()
def test_server_stop():
"""
关闭所有测试服务器
:return: 无返回值
"""
# requests.get(url=f'{listenurl}/STOP')
requests.get(url=f'{sendurl}/STOP')
def test_server_version():
"""
获取所有测试服务器的版本号
:return: 格式化后的版本号
"""
return f'''hakubot version: {requests.get(url=f'{listenurl}/VERSION').text}
test cqhttp version: {requests.get(url=f'{sendurl}/VERSION').text}'''
def test_bot_busy():
"""
获取 hakubot 当前执行线程数量
:return: 是否 > 0
"""
res = requests.get(url=f'{sendurl}/THREADS').text.split()
n = int(res[1])
return n > 0
if test_server_start():
mylogger.info('测试服务器启动成功')
else:
mylogger.info('测试服务器启动失败!')
test_server_stop()
exit(-1)
print(test_server_version())
def test_send(msgdict):
res = requests.post(url=sendurl, json=msgdict)
print(f'请求返回 {res.status_code} {res.text}')
# 消息发送测试
def test_message_group():
test_send(groupmsgdict)
def test_message_private():
test_send(usermsgdict)
# ping 测试
def test_message_group_ping():
global groupmsgdict
groupmsgdict['message'] = groupmsgdict['raw_message'] = '.ping'
test_send(groupmsgdict)
def test_message_private_ping():
global usermsgdict
usermsgdict['message'] = usermsgdict['raw_message'] = '.ping'
test_send(usermsgdict)
testfunctions = [test_message_group,
test_message_private,
test_message_group_ping,
test_message_private_ping
]
for f in testfunctions:
testthreads.append(threading.Thread(target=f, args=[], daemon=True))
print('Starting tests...')
time.sleep(1)
testcount = 0
for t in testthreads:
testcount += 1
print('--------------------------------------------------------------------------------')
try:
t.start()
t.join()
while test_bot_busy():
time.sleep(0.5)
except Exception as e:
mylogger.exception(e)
print(f'Test {testcount} finished.')
print('--------------------------------------------------------------------------------')
print('Test finished.')
test_server_stop()