MCPcopy Create free account
hub / github.com/celery/celery / test_remaining

Function test_remaining

t/unit/utils/test_time.py:144–232  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

142
143
144def test_remaining():
145 # Relative
146 remaining(datetime.now(_timezone.utc), timedelta(hours=1), relative=True)
147
148 """
149 The upcoming cases check whether the next run is calculated correctly
150 """
151 eastern_tz = ZoneInfo("US/Eastern")
152 tokyo_tz = ZoneInfo("Asia/Tokyo")
153
154 # Case 1: `start` in UTC and `now` in other timezone
155 start = datetime.now(ZoneInfo("UTC"))
156 now = datetime.now(eastern_tz)
157 delta = timedelta(hours=1)
158 assert str(start.tzinfo) == str(ZoneInfo("UTC"))
159 assert str(now.tzinfo) == str(eastern_tz)
160 rem_secs = remaining(start, delta, now).total_seconds()
161 # assert remaining time is approximately equal to delta
162 assert rem_secs == pytest.approx(delta.total_seconds(), abs=1)
163
164 # Case 2: `start` and `now` in different timezones (other than UTC)
165 start = datetime.now(eastern_tz)
166 now = datetime.now(tokyo_tz)
167 delta = timedelta(hours=1)
168 assert str(start.tzinfo) == str(eastern_tz)
169 assert str(now.tzinfo) == str(tokyo_tz)
170 rem_secs = remaining(start, delta, now).total_seconds()
171 assert rem_secs == pytest.approx(delta.total_seconds(), abs=1)
172
173 """
174 Case 3: DST check
175 Suppose start (which is last_run_time) is in EST while next_run is in EDT,
176 then check whether the `next_run` is actually the time specified in the
177 start (i.e. there is not an hour diff due to DST).
178 In 2019, DST starts on March 10
179 """
180 start = datetime(
181 month=3, day=9, year=2019, hour=10,
182 minute=0, tzinfo=eastern_tz) # EST
183
184 now = datetime(
185 day=11, month=3, year=2019, hour=1,
186 minute=0, tzinfo=eastern_tz) # EDT
187 delta = ffwd(hour=10, year=2019, microsecond=0, minute=0,
188 second=0, day=11, weeks=0, month=3)
189 # `next_actual_time` is the next time to run (derived from delta)
190 next_actual_time = datetime(
191 day=11, month=3, year=2019, hour=10, minute=0, tzinfo=eastern_tz) # EDT
192 assert start.tzname() == "EST"
193 assert now.tzname() == "EDT"
194 assert next_actual_time.tzname() == "EDT"
195 rem_time = remaining(start, delta, now)
196 next_run = now + rem_time
197 assert next_run == next_actual_time
198
199 """
200 Case 4: DST check between now and next_run
201 Suppose start (which is last_run_time) and now are in EST while next_run

Callers

nothing calls this directly

Calls 4

remainingFunction · 0.90
ffwdClass · 0.90
tznameMethod · 0.80
nowMethod · 0.45

Tested by

no test coverage detected