From 9d5751d2409c1ca42f74c8c2186ec1e3d9d3b780 Mon Sep 17 00:00:00 2001 From: Ilian Iliev Date: Thu, 5 Dec 2024 16:32:37 +0200 Subject: [PATCH] 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> --- tests/test_asyncio/test_hash.py | 13 +++++++------ tests/test_hash.py | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/tests/test_asyncio/test_hash.py b/tests/test_asyncio/test_hash.py index 8d94799fbb..15e426673b 100644 --- a/tests/test_asyncio/test_hash.py +++ b/tests/test_asyncio/test_hash.py @@ -1,4 +1,5 @@ import asyncio +import math from datetime import datetime, timedelta from tests.conftest import skip_if_server_version_lt @@ -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 @@ -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 @@ -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 diff --git a/tests/test_hash.py b/tests/test_hash.py index 9ed5e98132..0422185865 100644 --- a/tests/test_hash.py +++ b/tests/test_hash.py @@ -1,3 +1,4 @@ +import math import time from datetime import datetime, timedelta @@ -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 @@ -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 @@ -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