11from unittest .mock import MagicMock , patch
22
33import pytest
4-
54from src .jobs .strikes_handler import StrikesHandler
65
76
@@ -78,7 +77,7 @@ def test_log_change_logs_expected_strike_changes():
7877 }
7978
8079 recovered = ["hash_old" ]
81- affected = [ "hash_gone" ]
80+ affected = { "hash_gone" : { "title" : "Gone" }}
8281
8382 with patch ("src.jobs.strikes_handler.logger" ) as mock_logger :
8483 handler .log_change (recovered , affected )
@@ -96,3 +95,53 @@ def test_log_change_logs_expected_strike_changes():
9695 assert "hash_inc" in str (args )
9796 assert "hash_old" in str (args )
9897 assert "hash_gone" in str (args )
98+
99+
100+ @pytest .mark .parametrize (
101+ "max_strikes, initial_strikes, expected_removed_after_two_runs" ,
102+ [
103+ # max_strikes = 3
104+ (3 , 0 , False ), # 0 → 1 → 2
105+ (3 , 1 , False ), # 1 → 2 → 3
106+ (3 , 2 , True ), # 2 → 3 → 4
107+ (3 , 3 , True ), # 3 → 4 → 5
108+
109+ # max_strikes = 2
110+ (2 , 0 , False ), # 0 → 1 → 2
111+ (2 , 1 , True ), # 1 → 2 → 3
112+ (2 , 2 , True ), # 2 → 3 → 4
113+ (2 , 3 , True ), # 3 → 4 → 5
114+ ]
115+ )
116+ def test_strikes_handler_overall (max_strikes , initial_strikes , expected_removed_after_two_runs ):
117+ """
118+ Verify that incrementing of strikes works and that
119+ based on its initial strikes and the max_strikes limit
120+ removal happens
121+ Note: The logging output does not show the strike where the removal will be triggered (ie., 4/3 if max strikes = 3)
122+ Reason: This is on verbose-level, as instead the removal handler then shows another info-level log
123+ """
124+ job_name = "remove_stalled"
125+ d_id = "some_hash"
126+
127+ tracker_mock = MagicMock ()
128+ tracker_mock .defective = {
129+ job_name : {
130+ d_id : {"title" : "Some Title" , "strikes" : initial_strikes }
131+ }
132+ }
133+
134+ arr = MagicMock ()
135+ arr .tracker = tracker_mock
136+
137+ affected_downloads = {d_id : {"title" : "Some Title" }}
138+
139+ handler = StrikesHandler (job_name = job_name , arr = arr , max_strikes = max_strikes )
140+ handler .check_permitted_strikes (affected_downloads .copy ())
141+ handler = StrikesHandler (job_name = job_name , arr = arr , max_strikes = max_strikes )
142+ result = handler .check_permitted_strikes (affected_downloads .copy ())
143+
144+ assert (d_id in result ) == expected_removed_after_two_runs , (
145+ f"Expected removed={ expected_removed_after_two_runs } for "
146+ f"initial_strikes={ initial_strikes } , max_strikes={ max_strikes } , but got { d_id in result } "
147+ )
0 commit comments