Open
test(discovery): 同步端点参数并隔离发现测试状态 #9
liuxingsenAdd on 23 days ago
test(discovery): 同步端点参数并隔离发现测试状态 #9
Open
Total: 1 files changed+17-16
| @@ -36,7 +36,7 @@ end | |||
| 36 | TestMctpDiscovery = {} | 36 | TestMctpDiscovery = {} |
| 37 | 37 | ||
| 38 | function TestMctpDiscovery:setupClass() | 38 | function TestMctpDiscovery:setupClass() |
| 39 | - -- discovery的init会启动轮询任务,测试环境loop同步死循环,仅在setupClass期间替换get_instance, | 39 | + -- discovery的init会启动轮询任务,测试环境loop同步死循环,在本测试类运行期间替换get_instance, |
| 40 | -- teardownClass恢复,避免污染依赖真实c_tasks的其它测试类(如TestMctpEngine的hook_tasks) | 40 | -- teardownClass恢复,避免污染依赖真实c_tasks的其它测试类(如TestMctpEngine的hook_tasks) |
| 41 | self._origin_get_instance = c_tasks.get_instance | 41 | self._origin_get_instance = c_tasks.get_instance |
| 42 | c_tasks.get_instance = function() | 42 | c_tasks.get_instance = function() |
| @@ -46,9 +46,19 @@ function TestMctpDiscovery:setupClass() | |||
| 46 | end, | 46 | end, |
| 47 | } | 47 | } |
| 48 | end | 48 | end |
| 49 | +end | ||
| 50 | + | ||
| 51 | +function TestMctpDiscovery:setup() | ||
| 49 | self.system_id = 1 | 52 | self.system_id = 1 |
| 53 | + self.target_eid = 9 | ||
| 50 | self.mctp_engine = c_mctp_engine.new(nil, self.system_id) | 54 | self.mctp_engine = c_mctp_engine.new(nil, self.system_id) |
| 51 | self.mctp_discovery = c_mctp_discovery.new(self.system_id, self.mctp_engine) | 55 | self.mctp_discovery = c_mctp_discovery.new(self.system_id, self.mctp_engine) |
| 56 | + self._origin_create_endpoint = self.mctp_engine.endpoints.create_endpoint | ||
| 57 | +end | ||
| 58 | + | ||
| 59 | +function TestMctpDiscovery:teardown() | ||
| 60 | + self.mctp_engine.endpoints.create_endpoint = self._origin_create_endpoint | ||
O | |||
| 61 | + self.mctp_discovery:smbus_set_eid_remove(self.target_eid) | ||
| 52 | end | 62 | end |
| 53 | 63 | ||
| 54 | function TestMctpDiscovery:teardownClass() | 64 | function TestMctpDiscovery:teardownClass() |
| @@ -77,7 +87,6 @@ end | |||
| 77 | 87 | ||
| 78 | local function stub_endpoints(mctp_engine, fake_ep, path, medium_type) | 88 | local function stub_endpoints(mctp_engine, fake_ep, path, medium_type) |
| 79 | local endpoints = mctp_engine.endpoints | 89 | local endpoints = mctp_engine.endpoints |
| 80 | - local tmp_create_endpoint = endpoints.create_endpoint | ||
| 81 | endpoints.create_endpoint = function(_, phy_addr, eid, msg_type, actual_medium_type, ep_status, ref_chip) | 90 | endpoints.create_endpoint = function(_, phy_addr, eid, msg_type, actual_medium_type, ep_status, ref_chip) |
| 82 | lu.assertEquals(phy_addr, 0) | 91 | lu.assertEquals(phy_addr, 0) |
| 83 | lu.assertEquals(eid, 0) | 92 | lu.assertEquals(eid, 0) |
| @@ -87,13 +96,10 @@ local function stub_endpoints(mctp_engine, fake_ep, path, medium_type) | |||
| 87 | lu.assertEquals(ref_chip.path, path) | 96 | lu.assertEquals(ref_chip.path, path) |
| 88 | return fake_ep | 97 | return fake_ep |
| 89 | end | 98 | end |
| 90 | - return function() | ||
| 91 | - endpoints.create_endpoint = tmp_create_endpoint | ||
| 92 | - end | ||
| 93 | end | 99 | end |
| 94 | 100 | ||
| 95 | function TestMctpDiscovery:test_mctp_discovery_success() | 101 | function TestMctpDiscovery:test_mctp_discovery_success() |
| 96 | - local target_eid = 9 | 102 | + local target_eid = self.target_eid |
| 97 | local path = '/path/to/chip' | 103 | local path = '/path/to/chip' |
| 98 | local target_phy_addr = 0x1d | 104 | local target_phy_addr = 0x1d |
| 99 | local bmc_eid = 8 | 105 | local bmc_eid = 8 |
| @@ -103,7 +109,7 @@ function TestMctpDiscovery:test_mctp_discovery_success() | |||
| 103 | 109 | ||
| 104 | local fake_ep = make_fake_ep() | 110 | local fake_ep = make_fake_ep() |
| 105 | fake_ep.rsp = { completion_code = MCTP_CTRL_CC_SUCCESS } | 111 | fake_ep.rsp = { completion_code = MCTP_CTRL_CC_SUCCESS } |
| 106 | - local restore = stub_endpoints(self.mctp_engine, fake_ep, path, medium_type) | 112 | + stub_endpoints(self.mctp_engine, fake_ep, path, medium_type) |
| 107 | local ref_chip = { path = path } | 113 | local ref_chip = { path = path } |
| 108 | 114 | ||
| 109 | local status_changed = false | 115 | local status_changed = false |
| @@ -114,6 +120,7 @@ function TestMctpDiscovery:test_mctp_discovery_success() | |||
| 114 | end) | 120 | end) |
| 115 | 121 | ||
| 116 | self.mctp_discovery:smbus_set_eid_to_endpoint( | 122 | self.mctp_discovery:smbus_set_eid_to_endpoint( |
| 123 | + 'TestEndpoint', | ||
| 117 | target_eid, | 124 | target_eid, |
| 118 | ref_chip, | 125 | ref_chip, |
| 119 | medium_type, | 126 | medium_type, |
| @@ -140,12 +147,10 @@ function TestMctpDiscovery:test_mctp_discovery_success() | |||
| 140 | lu.assertEquals(sig_eid, target_eid) | 147 | lu.assertEquals(sig_eid, target_eid) |
| 141 | -- 校验请求只发送一次,未触发重试 | 148 | -- 校验请求只发送一次,未触发重试 |
| 142 | lu.assertEquals(fake_ep.get_req_count(), 1) | 149 | lu.assertEquals(fake_ep.get_req_count(), 1) |
| 143 | - | ||
| 144 | - restore() | ||
| 145 | end | 150 | end |
| 146 | 151 | ||
| 147 | function TestMctpDiscovery:test_mctp_discovery_failed() | 152 | function TestMctpDiscovery:test_mctp_discovery_failed() |
| 148 | - local target_eid = 9 | 153 | + local target_eid = self.target_eid |
| 149 | local path = '/path/to/chip' | 154 | local path = '/path/to/chip' |
| 150 | local target_phy_addr = 0x1d | 155 | local target_phy_addr = 0x1d |
| 151 | local bmc_eid = 8 | 156 | local bmc_eid = 8 |
| @@ -154,10 +159,11 @@ function TestMctpDiscovery:test_mctp_discovery_failed() | |||
| 154 | 159 | ||
| 155 | local fake_ep = make_fake_ep() | 160 | local fake_ep = make_fake_ep() |
| 156 | fake_ep.rsp = { completion_code = 0x01 } | 161 | fake_ep.rsp = { completion_code = 0x01 } |
| 157 | - local restore = stub_endpoints(self.mctp_engine, fake_ep, path, medium_type) | 162 | + stub_endpoints(self.mctp_engine, fake_ep, path, medium_type) |
| 158 | local ref_chip = { path = path } | 163 | local ref_chip = { path = path } |
| 159 | 164 | ||
| 160 | self.mctp_discovery:smbus_set_eid_to_endpoint( | 165 | self.mctp_discovery:smbus_set_eid_to_endpoint( |
| 166 | + 'TestEndpoint', | ||
| 161 | target_eid, | 167 | target_eid, |
| 162 | ref_chip, | 168 | ref_chip, |
| 163 | medium_type, | 169 | medium_type, |
| @@ -177,11 +183,6 @@ function TestMctpDiscovery:test_mctp_discovery_failed() | |||
| 177 | -- 校验轮询任务重发失败列表中的set eid报文 | 183 | -- 校验轮询任务重发失败列表中的set eid报文 |
| 178 | self.mctp_discovery:smbus_eid_poll_once() | 184 | self.mctp_discovery:smbus_eid_poll_once() |
| 179 | lu.assertEquals(fake_ep.get_req_count(), 2) | 185 | lu.assertEquals(fake_ep.get_req_count(), 2) |
| 180 | - | ||
| 181 | - -- 清理失败列表,避免影响其它用例 | ||
| 182 | - self.mctp_discovery:smbus_set_eid_remove(target_eid) | ||
| 183 | - | ||
| 184 | - restore() | ||
| 185 | end | 186 | end |
| 186 | 187 | ||
| 187 | function TestMctpDiscovery:test_smbus_ctrl_ep_created_in_init() | 188 | function TestMctpDiscovery:test_smbus_ctrl_ep_created_in_init() |
💡 建议:新增 teardown 后,用例中的手动 restore() 可简化
本次新增的
teardown()(本行)已经负责把self.mctp_engine.endpoints.create_endpoint恢复为_origin_create_endpoint,且setup()/teardown()会为每个用例自动执行。因此test_mctp_discovery_success末尾的restore()(stub_endpoints返回的恢复函数)与 teardown 存在双重恢复。虽然这不会造成功能问题(恢复是幂等的),但会引入冗余。修复建议:保留 teardown 的恢复作为「断言失败时也能保证清理」的兜底,同时可移除用例末尾的手动
restore()调用,让清理逻辑集中到 fixture 中,代码更简洁。若希望在用例内保持防御性恢复,也建议加注释说明与 teardown 的职责分工。