Skip to content

Commit

Permalink
Fix xadd timestamp
Browse files Browse the repository at this point in the history
* Fixed xadd timestamp
* Add test for redis7+
Fix #151
---------

Co-authored-by: Daniel M <[email protected]>
  • Loading branch information
alexporter8013 and cunla authored May 19, 2023
1 parent 053ae9e commit cc8709f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion fakeredis/_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def add(self, fields: List, id_str: str = '*') -> Union[None, bytes]:
id_str = id_str.decode()

if id_str is None or id_str == '*':
ts, seq = int(time.time() + 1), 0
ts, seq = int(1000 * time.time()), 0
if (len(self._values) > 0
and self._values[-1][0][0] == ts
and self._values[-1][0][1] >= seq):
Expand Down
9 changes: 8 additions & 1 deletion test/test_mixins/test_streams_commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import redis
import time

from fakeredis._stream import XStream
from test import testtools
Expand Down Expand Up @@ -44,11 +45,14 @@ def test_xstream(r):
@pytest.mark.max_server('6.3')
def test_xadd_redis6(r: redis.Redis):
stream = "stream"
before = time.time()
m1 = r.xadd(stream, {"some": "other"})
after = time.time()
ts1, seq1 = m1.decode().split('-')
seq1 = int(seq1)
m2 = r.xadd(stream, {'add': 'more'}, id=f'{ts1}-{seq1 + 1}')
ts2, seq2 = m2.decode().split('-')
assert int(1000 * before) <= int(ts1) <= int(1000 * after)
assert ts1 == ts2
assert int(seq2) == int(seq1) + 1

Expand All @@ -65,10 +69,13 @@ def test_xadd_redis6(r: redis.Redis):
@pytest.mark.min_server('7')
def test_xadd_redis7(r: redis.Redis):
stream = "stream"
before = time.time()
m1 = r.xadd(stream, {"some": "other"})
ts1, seq1 = m1.decode().split('-')
after = time.time()
m2 = r.xadd(stream, {'add': 'more'}, id=f'{ts1}-*')
ts1, seq1 = m1.decode().split('-')
ts2, seq2 = m2.decode().split('-')
assert int(1000 * before) <= int(ts1) <= int(1000 * after)
assert ts1 == ts2
assert int(seq2) == int(seq1) + 1

Expand Down

0 comments on commit cc8709f

Please sign in to comment.