Skip to content

Commit 175e610

Browse files
author
Jan Thunqvist
committed
Split stop transaction test in separate tests.
1 parent 4ec2f33 commit 175e610

File tree

1 file changed

+105
-25
lines changed

1 file changed

+105
-25
lines changed

tests/test_charge_point_v16.py

Lines changed: 105 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -380,26 +380,27 @@ async def test_cms_responses_normal_v16(
380380
) as ws:
381381
# use a different id for debugging
382382
cp = ChargePoint(f"{cp_id}_client", ws)
383-
with contextlib.suppress(asyncio.TimeoutError):
384-
await asyncio.wait_for(
385-
asyncio.gather(
386-
cp.start(),
387-
cp.send_boot_notification(),
388-
cp.send_authorize(),
389-
cp.send_heartbeat(),
390-
cp.send_status_notification(),
391-
cp.send_security_event(),
392-
cp.send_firmware_status(),
393-
cp.send_data_transfer(),
394-
cp.send_start_transaction(12345),
395-
cp.send_meter_err_phases(),
396-
cp.send_meter_line_voltage(),
397-
cp.send_meter_periodic_data(),
398-
# add delay to allow meter data to be processed
399-
cp.send_stop_transaction(1),
400-
),
401-
timeout=8,
402-
)
383+
cp_task = asyncio.create_task(cp.start())
384+
385+
await cp.send_boot_notification()
386+
await wait_ready(cs.charge_points[cp_id])
387+
await cp.send_boot_notification()
388+
await cp.send_authorize()
389+
await cp.send_heartbeat()
390+
await cp.send_status_notification()
391+
await cp.send_security_event()
392+
await cp.send_firmware_status()
393+
await cp.send_data_transfer()
394+
await cp.send_start_transaction(12345)
395+
await cp.send_meter_err_phases()
396+
await cp.send_meter_line_voltage()
397+
await cp.send_meter_periodic_data()
398+
# add delay to allow meter data to be processed
399+
await cp.send_stop_transaction(1)
400+
401+
cp_task.cancel()
402+
with contextlib.suppress(asyncio.CancelledError):
403+
await cp_task
403404
await ws.close()
404405

405406
cpid = cs.charge_points[cp_id].settings.cpid
@@ -485,6 +486,8 @@ async def test_cms_responses_actions_v16(
485486
timeout=10,
486487
)
487488
cp_task.cancel()
489+
with contextlib.suppress(asyncio.CancelledError):
490+
await cp_task
488491
await ws.close()
489492

490493
# cpid set in cs after websocket connection
@@ -638,6 +641,8 @@ async def test_cms_responses_errors_v16(
638641
)
639642
await cs.charge_points[cp_id].stop()
640643
cp_task.cancel()
644+
with contextlib.suppress(asyncio.CancelledError):
645+
await cp_task
641646
await ws.close()
642647

643648
# test services when charger is unavailable
@@ -650,7 +655,7 @@ async def test_cms_responses_errors_v16(
650655
@pytest.mark.timeout(20) # Set timeout for this test
651656
@pytest.mark.parametrize(
652657
"setup_config_entry",
653-
[{"port": 9007, "cp_id": "CP_1_norm_mc", "cms": "cms_norm", "num_connectors": 2}],
658+
[{"port": 9007, "cp_id": "CP_1_norm_mc", "cms": "cms_norm"}],
654659
indirect=True,
655660
)
656661
@pytest.mark.parametrize("cp_id", ["CP_1_norm_mc"])
@@ -692,6 +697,8 @@ async def test_cms_responses_normal_multiple_connectors_v16(
692697
await cp.send_stop_transaction(1)
693698

694699
cp_task.cancel()
700+
with contextlib.suppress(asyncio.CancelledError):
701+
await cp_task
695702
await ws.close()
696703

697704
cpid = cs.charge_points[cp_id].settings.cpid
@@ -770,6 +777,8 @@ async def test_clear_profile_v16(hass, socket_enabled, cp_id, port, setup_config
770777
assert isinstance(cp.last_clear_profile_kwargs, dict)
771778

772779
cp_task.cancel()
780+
with contextlib.suppress(asyncio.CancelledError):
781+
await cp_task
773782
await ws.close()
774783

775784

@@ -792,7 +801,7 @@ async def set_report_session_energyreport(
792801
)
793802
@pytest.mark.parametrize("cp_id", ["CP_1_stop_paths"])
794803
@pytest.mark.parametrize("port", [9010])
795-
async def test_stop_transaction_paths_v16(
804+
async def test_stop_transaction_paths_v16_a(
796805
hass, socket_enabled, cp_id, port, setup_config_entry
797806
):
798807
"""Exercise all branches of ocppv16.on_stop_transaction."""
@@ -832,8 +841,26 @@ async def test_stop_transaction_paths_v16(
832841
assert cs.get_unit(cpid, "Energy.Session", connector_id=1) == "kWh"
833842

834843
cp_task.cancel()
844+
with contextlib.suppress(asyncio.CancelledError):
845+
await cp_task
835846
await ws.close()
836847

848+
849+
# @pytest.mark.skip(reason="skip")
850+
@pytest.mark.timeout(20)
851+
@pytest.mark.parametrize(
852+
"setup_config_entry",
853+
[{"port": 9021, "cp_id": "CP_1_stop_paths_a1", "cms": "cms_stop_paths_a1"}],
854+
indirect=True,
855+
)
856+
@pytest.mark.parametrize("cp_id", ["CP_1_stop_paths_a1"])
857+
@pytest.mark.parametrize("port", [9021])
858+
async def test_stop_transaction_paths_v16_a1(
859+
hass, socket_enabled, cp_id, port, setup_config_entry
860+
):
861+
"""Exercise all branches of ocppv16.on_stop_transaction."""
862+
cs: CentralSystem = setup_config_entry
863+
837864
#
838865
# SCENARIO A (variant): charger reports session energy AND last EAIR already kWh.
839866
#
@@ -862,8 +889,26 @@ async def test_stop_transaction_paths_v16(
862889
assert cs.get_unit(cpid, "Energy.Session", connector_id=1) == "kWh"
863890

864891
cp_task.cancel()
892+
with contextlib.suppress(asyncio.CancelledError):
893+
await cp_task
865894
await ws.close()
866895

896+
897+
# @pytest.mark.skip(reason="skip")
898+
@pytest.mark.timeout(20)
899+
@pytest.mark.parametrize(
900+
"setup_config_entry",
901+
[{"port": 9022, "cp_id": "CP_1_stop_paths_b", "cms": "cms_stop_paths_b"}],
902+
indirect=True,
903+
)
904+
@pytest.mark.parametrize("cp_id", ["CP_1_stop_paths_b"])
905+
@pytest.mark.parametrize("port", [9022])
906+
async def test_stop_transaction_paths_v16_b(
907+
hass, socket_enabled, cp_id, port, setup_config_entry
908+
):
909+
"""Exercise all branches of ocppv16.on_stop_transaction."""
910+
cs: CentralSystem = setup_config_entry
911+
867912
#
868913
# SCENARIO B: charger reports session energy BUT SessionEnergy already set → do not overwrite.
869914
#
@@ -895,8 +940,26 @@ async def test_stop_transaction_paths_v16(
895940
assert round(sess, 3) == 7.777 # unchanged
896941

897942
cp_task.cancel()
943+
with contextlib.suppress(asyncio.CancelledError):
944+
await cp_task
898945
await ws.close()
899946

947+
948+
# @pytest.mark.skip(reason="skip")
949+
@pytest.mark.timeout(20)
950+
@pytest.mark.parametrize(
951+
"setup_config_entry",
952+
[{"port": 9023, "cp_id": "CP_1_stop_paths_c", "cms": "cms_stop_paths_c"}],
953+
indirect=True,
954+
)
955+
@pytest.mark.parametrize("cp_id", ["CP_1_stop_paths_c"])
956+
@pytest.mark.parametrize("port", [9023])
957+
async def test_stop_transaction_paths_v16_c(
958+
hass, socket_enabled, cp_id, port, setup_config_entry
959+
):
960+
"""Exercise all branches of ocppv16.on_stop_transaction."""
961+
cs: CentralSystem = setup_config_entry
962+
900963
#
901964
# SCENARIO C: _charger_reports_session_energy = False -> compute from meter_stop - meter_start
902965
#
@@ -1048,6 +1111,8 @@ async def test_on_meter_values_paths_v16(
10481111

10491112
finally:
10501113
cp_task.cancel()
1114+
with contextlib.suppress(asyncio.CancelledError):
1115+
await cp_task
10511116
await ws.close()
10521117

10531118

@@ -1163,6 +1228,8 @@ def fake_get_ha_metric(name: str, connector_id: int | None = None):
11631228
assert cs.get_unit(cpid, "Energy.Session", connector_id=1) == "kWh"
11641229

11651230
cp_task.cancel()
1231+
with contextlib.suppress(asyncio.CancelledError):
1232+
await cp_task
11661233
await ws.close()
11671234

11681235

@@ -1225,6 +1292,8 @@ async def test_api_get_extra_attr_paths(
12251292

12261293
# Clean up
12271294
cp_task.cancel()
1295+
with contextlib.suppress(asyncio.CancelledError):
1296+
await cp_task
12281297
await ws.close()
12291298

12301299

@@ -1273,6 +1342,8 @@ async def test_update_firmware_supported_valid_url_v16(
12731342
)
12741343

12751344
cp_task.cancel()
1345+
with contextlib.suppress(asyncio.CancelledError):
1346+
await cp_task
12761347
await ws.close()
12771348

12781349

@@ -1315,6 +1386,8 @@ async def test_update_firmware_supported_invalid_url_v16(
13151386
assert getattr(cp, "last_update_firmware", None) is None
13161387

13171388
cp_task.cancel()
1389+
with contextlib.suppress(asyncio.CancelledError):
1390+
await cp_task
13181391
await ws.close()
13191392

13201393

@@ -1357,6 +1430,8 @@ async def test_update_firmware_not_supported_v16(
13571430
assert getattr(cp, "last_update_firmware", None) is None
13581431

13591432
cp_task.cancel()
1433+
with contextlib.suppress(asyncio.CancelledError):
1434+
await cp_task
13601435
await ws.close()
13611436

13621437

@@ -1402,17 +1477,19 @@ async def boom(_req):
14021477
assert getattr(cp, "last_update_firmware", None) is None
14031478

14041479
cp_task.cancel()
1480+
with contextlib.suppress(asyncio.CancelledError):
1481+
await cp_task
14051482
await ws.close()
14061483

14071484

14081485
@pytest.mark.timeout(20)
14091486
@pytest.mark.parametrize(
14101487
"setup_config_entry",
1411-
[{"port": 9018, "cp_id": "CP_1_unit_fallback", "cms": "cms_unit_fallback"}],
1488+
[{"port": 9020, "cp_id": "CP_1_unit_fallback", "cms": "cms_unit_fallback"}],
14121489
indirect=True,
14131490
)
14141491
@pytest.mark.parametrize("cp_id", ["CP_1_unit_fallback"])
1415-
@pytest.mark.parametrize("port", [9018])
1492+
@pytest.mark.parametrize("port", [9020])
14161493
async def test_api_get_unit_fallback_to_later_connectors(
14171494
hass, socket_enabled, cp_id, port, setup_config_entry
14181495
):
@@ -1453,6 +1530,8 @@ async def test_api_get_unit_fallback_to_later_connectors(
14531530

14541531
# Cleanup
14551532
cp_task.cancel()
1533+
with contextlib.suppress(asyncio.CancelledError):
1534+
await cp_task
14561535
await ws.close()
14571536

14581537

@@ -1523,6 +1602,8 @@ async def test_api_get_extra_attr_fallback_to_later_connectors(
15231602

15241603
# Cleanup
15251604
cp_task.cancel()
1605+
with contextlib.suppress(asyncio.CancelledError):
1606+
await cp_task
15261607
await ws.close()
15271608

15281609

@@ -1651,7 +1732,6 @@ async def test_get_diagnostics_and_data_transfer_v16(
16511732
cp_task.cancel()
16521733
with contextlib.suppress(asyncio.CancelledError):
16531734
await cp_task
1654-
16551735
await ws.close()
16561736

16571737

0 commit comments

Comments
 (0)