Skip to content

Commit 9d5751d

Browse files
Fixing randomly failing test (#3437)
* Fixing randomly failing test * Always rounding up to avoid randomly failing tests * Always rounding up to avoid randomly failing tests --------- Co-authored-by: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com>
1 parent 5043b69 commit 9d5751d

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

tests/test_asyncio/test_hash.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import math
23
from datetime import datetime, timedelta
34

45
from tests.conftest import skip_if_server_version_lt
@@ -128,9 +129,9 @@ async def test_hpexpire_multiple_fields(r):
128129
async def test_hexpireat_basic(r):
129130
await r.delete("test:hash")
130131
await r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
131-
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
132+
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
132133
assert await r.hexpireat("test:hash", exp_time, "field1") == [1]
133-
await asyncio.sleep(1.1)
134+
await asyncio.sleep(2.1)
134135
assert await r.hexists("test:hash", "field1") is False
135136
assert await r.hexists("test:hash", "field2") is True
136137

@@ -139,9 +140,9 @@ async def test_hexpireat_basic(r):
139140
async def test_hexpireat_with_datetime(r):
140141
await r.delete("test:hash")
141142
await r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
142-
exp_time = datetime.now() + timedelta(seconds=1)
143+
exp_time = (datetime.now() + timedelta(seconds=2)).replace(microsecond=0)
143144
assert await r.hexpireat("test:hash", exp_time, "field1") == [1]
144-
await asyncio.sleep(1.1)
145+
await asyncio.sleep(2.1)
145146
assert await r.hexists("test:hash", "field1") is False
146147
assert await r.hexists("test:hash", "field2") is True
147148

@@ -175,9 +176,9 @@ async def test_hexpireat_multiple_fields(r):
175176
"test:hash",
176177
mapping={"field1": "value1", "field2": "value2", "field3": "value3"},
177178
)
178-
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
179+
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
179180
assert await r.hexpireat("test:hash", exp_time, "field1", "field2") == [1, 1]
180-
await asyncio.sleep(1.5)
181+
await asyncio.sleep(2.1)
181182
assert await r.hexists("test:hash", "field1") is False
182183
assert await r.hexists("test:hash", "field2") is False
183184
assert await r.hexists("test:hash", "field3") is True

tests/test_hash.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
import time
23
from datetime import datetime, timedelta
34

@@ -147,9 +148,9 @@ def test_hpexpire_multiple_condition_flags_error(r):
147148
def test_hexpireat_basic(r):
148149
r.delete("test:hash")
149150
r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
150-
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
151+
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
151152
assert r.hexpireat("test:hash", exp_time, "field1") == [1]
152-
time.sleep(1.1)
153+
time.sleep(2.1)
153154
assert r.hexists("test:hash", "field1") is False
154155
assert r.hexists("test:hash", "field2") is True
155156

@@ -158,9 +159,9 @@ def test_hexpireat_basic(r):
158159
def test_hexpireat_with_datetime(r):
159160
r.delete("test:hash")
160161
r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
161-
exp_time = datetime.now() + timedelta(seconds=1)
162+
exp_time = (datetime.now() + timedelta(seconds=2)).replace(microsecond=0)
162163
assert r.hexpireat("test:hash", exp_time, "field1") == [1]
163-
time.sleep(1.1)
164+
time.sleep(2.1)
164165
assert r.hexists("test:hash", "field1") is False
165166
assert r.hexists("test:hash", "field2") is True
166167

@@ -194,9 +195,9 @@ def test_hexpireat_multiple_fields(r):
194195
"test:hash",
195196
mapping={"field1": "value1", "field2": "value2", "field3": "value3"},
196197
)
197-
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
198+
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
198199
assert r.hexpireat("test:hash", exp_time, "field1", "field2") == [1, 1]
199-
time.sleep(1.1)
200+
time.sleep(2.1)
200201
assert r.hexists("test:hash", "field1") is False
201202
assert r.hexists("test:hash", "field2") is False
202203
assert r.hexists("test:hash", "field3") is True

0 commit comments

Comments
 (0)