Skip to content

Commit

Permalink
Fixing randomly failing test (#3437)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
IlianIliev and vladvildanov authored Dec 5, 2024
1 parent 5043b69 commit 9d5751d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
13 changes: 7 additions & 6 deletions tests/test_asyncio/test_hash.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import math
from datetime import datetime, timedelta

from tests.conftest import skip_if_server_version_lt
Expand Down Expand Up @@ -128,9 +129,9 @@ async def test_hpexpire_multiple_fields(r):
async def test_hexpireat_basic(r):
await r.delete("test:hash")
await r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
assert await r.hexpireat("test:hash", exp_time, "field1") == [1]
await asyncio.sleep(1.1)
await asyncio.sleep(2.1)
assert await r.hexists("test:hash", "field1") is False
assert await r.hexists("test:hash", "field2") is True

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

Expand Down Expand Up @@ -175,9 +176,9 @@ async def test_hexpireat_multiple_fields(r):
"test:hash",
mapping={"field1": "value1", "field2": "value2", "field3": "value3"},
)
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
assert await r.hexpireat("test:hash", exp_time, "field1", "field2") == [1, 1]
await asyncio.sleep(1.5)
await asyncio.sleep(2.1)
assert await r.hexists("test:hash", "field1") is False
assert await r.hexists("test:hash", "field2") is False
assert await r.hexists("test:hash", "field3") is True
Expand Down
13 changes: 7 additions & 6 deletions tests/test_hash.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import time
from datetime import datetime, timedelta

Expand Down Expand Up @@ -147,9 +148,9 @@ def test_hpexpire_multiple_condition_flags_error(r):
def test_hexpireat_basic(r):
r.delete("test:hash")
r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
assert r.hexpireat("test:hash", exp_time, "field1") == [1]
time.sleep(1.1)
time.sleep(2.1)
assert r.hexists("test:hash", "field1") is False
assert r.hexists("test:hash", "field2") is True

Expand All @@ -158,9 +159,9 @@ def test_hexpireat_basic(r):
def test_hexpireat_with_datetime(r):
r.delete("test:hash")
r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
exp_time = datetime.now() + timedelta(seconds=1)
exp_time = (datetime.now() + timedelta(seconds=2)).replace(microsecond=0)
assert r.hexpireat("test:hash", exp_time, "field1") == [1]
time.sleep(1.1)
time.sleep(2.1)
assert r.hexists("test:hash", "field1") is False
assert r.hexists("test:hash", "field2") is True

Expand Down Expand Up @@ -194,9 +195,9 @@ def test_hexpireat_multiple_fields(r):
"test:hash",
mapping={"field1": "value1", "field2": "value2", "field3": "value3"},
)
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
assert r.hexpireat("test:hash", exp_time, "field1", "field2") == [1, 1]
time.sleep(1.1)
time.sleep(2.1)
assert r.hexists("test:hash", "field1") is False
assert r.hexists("test:hash", "field2") is False
assert r.hexists("test:hash", "field3") is True
Expand Down

0 comments on commit 9d5751d

Please sign in to comment.