Skip to content

Commit 4a2a33e

Browse files
authored
Merge pull request #435 from evan-choi/fix/xadd-equals-trim-modifier
fix: accept = trim modifier in xadd
2 parents c1b59bf + f4d8aa3 commit 4a2a33e

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

cmd_stream.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func (m *Miniredis) cmdXadd(c *server.Peer, cmd string, args []string) {
4949
}
5050
if strings.ToLower(args[0]) == "maxlen" {
5151
args = args[1:]
52-
// we don't treat "~" special
53-
if args[0] == "~" {
52+
// ignore exact/approximate trimming modifiers
53+
if args[0] == "~" || args[0] == "=" {
5454
args = args[1:]
5555
}
5656
n, err := strconv.Atoi(args[0])
@@ -66,8 +66,8 @@ func (m *Miniredis) cmdXadd(c *server.Peer, cmd string, args []string) {
6666
args = args[1:]
6767
} else if strings.ToLower(args[0]) == "minid" {
6868
args = args[1:]
69-
// we don't treat "~" special
70-
if args[0] == "~" {
69+
// ignore exact/approximate trimming modifiers
70+
if args[0] == "~" || args[0] == "=" {
7171
args = args[1:]
7272
}
7373
minID = args[0]

cmd_stream_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ func TestStreamAdd(t *testing.T) {
150150
}
151151
nowz, _ := s.Stream("nowz")
152152
equals(t, 10, len(nowz))
153+
154+
for i := 0; i < 100; i++ {
155+
_, err := c.Do("XADD", "nowexact", "MAXLEN", "=", "10", "*", "one", "1")
156+
ok(t, err)
157+
nowexact, _ := s.Stream("nowexact")
158+
assert(t, len(nowexact) <= 10, "deleted entries")
159+
}
160+
nowexact, _ := s.Stream("nowexact")
161+
equals(t, 10, len(nowexact))
153162
})
154163

155164
t.Run("XADD MINID", func(t *testing.T) {
@@ -193,6 +202,20 @@ func TestStreamAdd(t *testing.T) {
193202
proto.Array(proto.String("1672545848004-0"), proto.Strings("five", "5")),
194203
),
195204
)
205+
206+
now = now.Add(time.Second)
207+
s.SetTime(now)
208+
minID = strconv.FormatInt(now.Add(-time.Second).UnixNano()/time.Millisecond.Nanoseconds(), 10)
209+
_, err = c.Do("XADD", "mid", "MINID", "=", minID, "*", "six", "6")
210+
ok(t, err)
211+
212+
mustDo(t, c,
213+
"XRANGE", "mid", "-", "+",
214+
proto.Array(
215+
proto.Array(proto.String("1672545848004-0"), proto.Strings("five", "5")),
216+
proto.Array(proto.String("1672545849004-0"), proto.Strings("six", "6")),
217+
),
218+
)
196219
})
197220

198221
t.Run("XADD NOMKSTREAM", func(t *testing.T) {

integration/stream_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func TestStream(t *testing.T) {
7979
c.Do("XADD", "planets", "MAXLEN", "4", "456-6", "name", "Mercury")
8080
c.Do("XLEN", "planets")
8181
c.Do("XADD", "planets", "MAXLEN", "~", "4", "456-7", "name", "Mercury")
82+
c.Do("XADD", "planets", "MAXLEN", "=", "4", "456-8", "name", "Mercury")
83+
c.Do("XRANGE", "planets", "-", "+")
8284

8385
c.Error("not an integer", "XADD", "planets", "MAXLEN", "!", "4", "*", "name", "Mercury")
8486
c.Error("not an integer", "XADD", "planets", "MAXLEN", " ~", "4", "*", "name", "Mercury")
@@ -89,7 +91,7 @@ func TestStream(t *testing.T) {
8991
c.Error("wrong number", "XADD", "planets", "MAXLEN", "~")
9092
c.Error("wrong number", "XADD", "planets", "MAXLEN")
9193

92-
c.Do("XADD", "planets", "MAXLEN", "0", "456-8", "name", "Mercury")
94+
c.Do("XADD", "planets", "MAXLEN", "0", "456-9", "name", "Mercury")
9395
c.Do("XLEN", "planets")
9496

9597
c.Do("SET", "str", "I am a string")
@@ -107,6 +109,8 @@ func TestStream(t *testing.T) {
107109
c.Do("XADD", "planets", "MINID", "450", "456-6", "name", "Mercury")
108110
c.Do("XADD", "planets", "MINID", "~", "450", "456-7", "name", "Mercury")
109111
c.Do("XLEN", "planets")
112+
c.Do("XADD", "planets", "MINID", "=", "456-8", "456-8", "name", "Mercury")
113+
c.Do("XRANGE", "planets", "-", "+")
110114

111115
c.Error("equal or smaller than the target", "XADD", "planets", "MINID", "450", "449-0", "name", "Earth")
112116
c.Error("equal or smaller than the target", "XADD", "planets", "MINID", "450", "450", "name", "Earth")

0 commit comments

Comments
 (0)