Skip to content

Commit

Permalink
🐛 修复测试错误
Browse files Browse the repository at this point in the history
  • Loading branch information
XYCode-Kerman committed Jun 15, 2024
1 parent f786bd9 commit 4fbb397
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
4 changes: 3 additions & 1 deletion mirai_onebot/adapters/reverse_websocket_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def stop(self):
if self.server is None:
return

tasks: List[asyncio.Task] = []
for ws in self.ws_connections:
asyncio.get_event_loop().run_until_complete(ws.close())
tasks.append(asyncio.create_task(ws.close()))
asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks))

async def handler(self, websocket: websockets.WebSocketServerProtocol, path: str):
# 检测OneBot标准版本
Expand Down
10 changes: 6 additions & 4 deletions mirai_onebot/event/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ def decorator(func: Callable) -> Callable:

return decorator

async def emit(self, event: Union[Type[EventBase], str], *args, **kwargs) -> None:
async def emit(self, event: Union[Type[EventBase], str], background: bool = True, *args, **kwargs) -> None:
"""触发事件
Args:
event (str | Type[EventBase]): 事件
background (bool, optional): 是否在后台触发事件,设置为False会等待事件完成. Defaults to True.
args/kwargs: 传递给事件处理器的参数
"""
if event in self._subscribers.keys():
[asyncio.create_task(subscriber(*args, **kwargs))
for subscriber in self._subscribers[event]]
# await asyncio.wait(tasks)
tasks = [asyncio.create_task(subscriber(*args, **kwargs))
for subscriber in self._subscribers[event]]
if not background:
await asyncio.wait(tasks)
6 changes: 3 additions & 3 deletions test/test_event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ async def handle_message_group_event():

bus.subscribe('print_message', handle_print_message)

await bus.emit(MessageGroupEvent)
await bus.emit('print_message', 'hello')
await bus.emit(MessageGroupEvent, background=False)
await bus.emit(event='print_message', background=False, message='hello')

assert run1 is True
assert run2 is True
Expand Down Expand Up @@ -60,7 +60,7 @@ async def test31():
global run2
run2 = True

await bus.emit('test3')
await bus.emit('test3', background=False)

assert run1 is True
assert run2 is True
Expand Down
14 changes: 7 additions & 7 deletions test/test_rwebsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,21 @@ async def subscribe(data: dict):
assert data['id'] == id

# 错误事件
await ws_client.send('hello?')
await ws_client.send('{"good": "nice"}')

# 调用api
asyncio.create_task(adapter.call_api('hello', test='test'))
asyncio.create_task(adapter._call_api('hello', {'test': 'test'}))

# 发送响应
echo = json.loads(await ws_client.recv())['echo']
await ws_client.send(json.dumps({'echo': echo, 'resp': 'test'}))

# 调用api超时
await adapter.call_api('hello', test='test')
await adapter._call_api('hello', {'test': 'test'})
await asyncio.sleep(1.5)

# 关闭连接
await ws_client.close()

# 再次调用handler函数,引发错误
await adapter.handler(adapter.ws_connections[0], '/')
def test_new_adapter():
adapter = ReverseWebsocketAdapter('hello', '0.0.0.0', 4561, 1)
adapter.start()
adapter.stop()

0 comments on commit 4fbb397

Please sign in to comment.