|
15 | 15 | from rq.serializers import JSONSerializer |
16 | 16 | from rq.utils import current_timestamp, utcformat |
17 | 17 | from rq.worker import Worker |
18 | | -from tests import RQTestCase, find_empty_redis_database, ssl_test |
| 18 | +from tests import RQTestCase, find_empty_redis_database, ssl_test, system_timezone |
19 | 19 |
|
20 | 20 | from .fixtures import kill_worker, say_hello |
21 | 21 |
|
@@ -82,54 +82,26 @@ def test_schedule(self): |
82 | 82 | job.save() |
83 | 83 | registry = ScheduledJobRegistry(queue=queue) |
84 | 84 |
|
85 | | - from datetime import timezone |
86 | | - |
87 | | - # If we pass in a datetime with no timezone, `schedule()` |
88 | | - # assumes local timezone so depending on your local timezone, |
89 | | - # the timestamp maybe different |
90 | | - # |
91 | | - # we need to account for the difference between a timezone |
92 | | - # with DST active and without DST active. The time.timezone |
93 | | - # property isn't accurate when time.daylight is non-zero, |
94 | | - # we'll test both. |
95 | | - # |
96 | | - # first, time.daylight == 0 (not in DST). |
97 | | - # mock the sitatuoin for American/New_York not in DST (UTC - 5) |
98 | | - # time.timezone = 18000 |
99 | | - # time.daylight = 0 |
100 | | - # time.altzone = 14400 |
101 | | - |
102 | | - mock_day = mock.patch('time.daylight', 0) |
103 | | - mock_tz = mock.patch('time.timezone', 18000) |
104 | | - mock_atz = mock.patch('time.altzone', 14400) |
105 | | - with mock_tz, mock_day, mock_atz: |
106 | | - registry.schedule(job, datetime(2019, 1, 1)) |
107 | | - self.assertEqual( |
108 | | - self.connection.zscore(registry.key, job.id), 1546300800 + 18000 |
109 | | - ) # 2019-01-01 UTC in Unix timestamp |
110 | | - |
111 | | - # second, time.daylight != 0 (in DST) |
112 | | - # mock the sitatuoin for American/New_York not in DST (UTC - 4) |
113 | | - # time.timezone = 18000 |
114 | | - # time.daylight = 1 |
115 | | - # time.altzone = 14400 |
116 | | - mock_day = mock.patch('time.daylight', 1) |
117 | | - mock_tz = mock.patch('time.timezone', 18000) |
118 | | - mock_atz = mock.patch('time.altzone', 14400) |
119 | | - with mock_tz, mock_day, mock_atz: |
120 | | - registry.schedule(job, datetime(2019, 1, 1)) |
121 | | - self.assertEqual( |
122 | | - self.connection.zscore(registry.key, job.id), 1546300800 + 14400 |
123 | | - ) # 2019-01-01 UTC in Unix timestamp |
124 | | - |
125 | | - # Score is always stored in UTC even if datetime is in a different tz |
126 | | - tz = timezone(timedelta(hours=7)) |
127 | | - job = Job.create('myfunc', connection=self.connection) |
128 | | - job.save() |
129 | | - registry.schedule(job, datetime(2019, 1, 1, 7, tzinfo=tz)) |
130 | | - self.assertEqual( |
131 | | - self.connection.zscore(registry.key, job.id), 1546300800 |
132 | | - ) # 2019-01-01 UTC in Unix timestamp |
| 85 | + # A naive datetime is local time, at the UTC offset in effect on that date: |
| 86 | + # New York is UTC-5 in January and UTC-4 in July. Unique scheduling converts it the same way. |
| 87 | + with system_timezone('America/New_York'): |
| 88 | + for scheduled_time, timestamp in [ |
| 89 | + (datetime(2019, 1, 1), 1546300800 + 5 * 3600), |
| 90 | + (datetime(2019, 7, 1), 1561939200 + 4 * 3600), |
| 91 | + ]: |
| 92 | + registry.schedule(job, scheduled_time) |
| 93 | + self.assertEqual(self.connection.zscore(registry.key, job.id), timestamp) |
| 94 | + |
| 95 | + unique_job = Job.create('myfunc', id=f'unique-{scheduled_time.month}', connection=self.connection) |
| 96 | + queue.schedule_job(unique_job, scheduled_time, unique=True) |
| 97 | + self.assertEqual(self.connection.zscore(registry.key, unique_job.id), timestamp) |
| 98 | + |
| 99 | + # Score is always stored in UTC even if datetime is in a different tz |
| 100 | + tz = timezone(timedelta(hours=7)) |
| 101 | + job = Job.create('myfunc', connection=self.connection) |
| 102 | + job.save() |
| 103 | + registry.schedule(job, datetime(2019, 1, 1, 7, tzinfo=tz)) |
| 104 | + self.assertEqual(self.connection.zscore(registry.key, job.id), 1546300800) # 2019-01-01 UTC in Unix timestamp |
133 | 105 |
|
134 | 106 | def test_remove_jobs(self): |
135 | 107 | """Removing job ids from ScheduledJobRegistry. Will be deprecated in the future.""" |
|
0 commit comments