Files
Convention-Python/[Test]/test.py

97 lines
2.6 KiB
Python
Raw Normal View History

2025-07-09 17:39:50 +08:00
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
2025-07-24 11:45:44 +08:00
from Convention.Runtime.Asynchrony import *
2025-07-10 15:08:20 +08:00
2025-07-24 11:45:44 +08:00
class Test(Asynchronous):
a:int
b:int
c:int
2025-07-21 14:19:27 +08:00
def __init__(self):
2025-07-24 11:45:44 +08:00
super().__init__(c={"timeout":2},b={"timeout":10})
self.a = 1
2025-07-09 17:39:50 +08:00
2025-07-24 11:45:44 +08:00
2025-07-25 10:48:07 +08:00
async def seta(obj:Test, value:int, delay:float = 1) -> None:
await asyncio.sleep(delay)
obj.a = value
2025-07-24 11:45:44 +08:00
async def setb(obj:Test, value:int, delay:float = 1) -> None:
await asyncio.sleep(delay)
obj.b = value
2025-07-25 10:48:07 +08:00
async def setc(obj:Test, value:int, delay:float = 1) -> None:
await asyncio.sleep(delay)
obj.c = value
2025-07-24 11:45:44 +08:00
async def run():
print("=== 测试优化后的异步字段系统 ===")
test = Test()
# 测试字段状态检查
2025-07-25 10:48:07 +08:00
assert test.is_field_initialized('a')
assert not test.is_field_initialized('b')
assert not test.is_field_initialized('c')
2025-07-24 11:45:44 +08:00
print("\n=== 测试1未设置值的情况应该超时===")
try:
2025-07-25 10:48:07 +08:00
print(f"失败: {test.a,test.b,test.c}")
raise RuntimeError("测试1应该超时")
2025-07-24 11:45:44 +08:00
except Exception as e:
2025-07-25 10:48:07 +08:00
print(f"成功: {e}")
2025-07-24 11:45:44 +08:00
print("\n=== 测试2在超时前设置字段b的值 ===")
# 创建新的测试实例
test2 = Test()
2025-07-25 10:48:07 +08:00
assert not test2.is_field_initialized('b')
2025-07-24 11:45:44 +08:00
2025-07-25 10:48:07 +08:00
# 启动并发任务设置b的值和获取b的值
# 并发执行设置b值延迟0.5秒和获取b值
await asyncio.gather(
setb(test2, 42, delay=0.5), # 0.5秒后设置b=42
return_exceptions=True
)
assert test2.b == 42
assert test2.is_field_initialized('b')
assert test2.b == 42
2025-07-24 11:45:44 +08:00
test3 = Test()
test3.b = 100
2025-07-25 10:48:07 +08:00
assert test3.is_field_initialized('b')
assert test3.b == 100
2025-07-24 11:45:44 +08:00
2025-07-25 10:48:07 +08:00
print("\n=== 测试3测试字段c短超时应该仍然超时===")
2025-07-24 11:45:44 +08:00
try:
2025-07-25 10:48:07 +08:00
print(f"失败: {test.c}")
2025-07-24 11:45:44 +08:00
except TimeoutError as timeout_e:
2025-07-25 10:48:07 +08:00
print(f"成功: {timeout_e}")
2025-07-24 11:45:44 +08:00
def test_sync_access():
"""测试同步访问(在非异步上下文中)"""
print("\n=== 测试同步访问 ===")
test = Test()
# 测试已初始化字段的同步访问
try:
2025-07-25 10:48:07 +08:00
print(f"成功: a = {test.a}")
2025-07-24 11:45:44 +08:00
except Exception as e:
2025-07-25 10:48:07 +08:00
raise
2025-07-24 11:45:44 +08:00
# 测试未初始化字段的同步访问(应该有更友好的错误提示)
try:
2025-07-25 10:48:07 +08:00
print(f"失败: c = {test.c}")
raise RuntimeError("字段c此时不应该能够被访问")
2025-07-24 11:45:44 +08:00
except Exception as e:
2025-07-25 10:48:07 +08:00
print(f"成功: {e}")
2025-07-09 17:39:50 +08:00
if __name__ == "__main__":
2025-07-24 11:45:44 +08:00
# 测试同步访问
test_sync_access()
# 测试异步访问
print("\n=== 开始异步测试 ===")
run_until_complete(run())