Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Stream Lag and Add EntriesAdded/EntriesRead #377

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cmd_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,11 @@ func (m *Miniredis) cmdXinfoStream(c *server.Peer, args []string) {
return
}

c.WriteMapLen(1)
c.WriteMapLen(2)
c.WriteBulk("length")
c.WriteInt(len(s.entries))
c.WriteBulk("entries-added")
c.WriteInt(s.entriesAdded)
})
}

Expand Down Expand Up @@ -610,9 +612,14 @@ func (m *Miniredis) cmdXinfoGroups(c *server.Peer, args []string) {
c.WriteBulk("last-delivered-id")
c.WriteBulk(g.lastID)
c.WriteBulk("entries-read")
c.WriteNull()
c.WriteInt(g.entriesRead)
c.WriteBulk("lag")
c.WriteInt(len(g.stream.entries))
lag := g.lag()
if lag == -1 {
c.WriteNull()
} else {
c.WriteInt(lag)
}
}
})
}
Expand Down
197 changes: 179 additions & 18 deletions cmd_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestStream(t *testing.T) {

mustDo(t, c,
"XINFO", "STREAM", "s",
proto.Array(proto.String("length"), proto.Int(1)),
proto.Array(proto.String("length"), proto.Int(1), proto.String("entries-added"), proto.Int(1)),
)

now := time.Date(2001, 1, 1, 4, 4, 5, 4000000, time.UTC)
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestStream(t *testing.T) {
t.Run("resp3", func(t *testing.T) {
mustDo(t, c,
"XINFO", "STREAM", "s",
proto.Map(proto.String("length"), proto.Int(1)),
proto.Map(proto.String("length"), proto.Int(1), proto.String("entries-added"), proto.Int(1)),
)
})
}
Expand Down Expand Up @@ -546,7 +546,7 @@ func TestStreamInfo(t *testing.T) {

mustDo(t, c,
"XINFO", "STREAM", "planets",
proto.Array(proto.String("length"), proto.Int(1)),
proto.Array(proto.String("length"), proto.Int(1), proto.String("entries-added"), proto.Int(1)),
)

mustDo(t, c,
Expand Down Expand Up @@ -605,7 +605,7 @@ func TestStreamGroup(t *testing.T) {
proto.String("consumers"), proto.Int(0),
proto.String("pending"), proto.Int(0),
proto.String("last-delivered-id"), proto.String("0-0"),
proto.String("entries-read"), proto.Nil,
proto.String("entries-read"), proto.Int(0),
proto.String("lag"), proto.Int(0),
),
),
Expand All @@ -627,7 +627,7 @@ func TestStreamGroup(t *testing.T) {
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(0),
proto.String("last-delivered-id"), proto.String("0-0"),
proto.String("entries-read"), proto.Nil,
proto.String("entries-read"), proto.Int(0),
proto.String("lag"), proto.Int(0),
),
),
Expand Down Expand Up @@ -660,7 +660,7 @@ func TestStreamGroup(t *testing.T) {
proto.String("consumers"), proto.Int(0),
proto.String("pending"), proto.Int(0),
proto.String("last-delivered-id"), proto.String("0-0"),
proto.String("entries-read"), proto.Nil,
proto.String("entries-read"), proto.Int(0),
proto.String("lag"), proto.Int(0),
),
),
Expand Down Expand Up @@ -727,7 +727,7 @@ func TestStreamReadGroup(t *testing.T) {
proto.String("consumers"), proto.Int(0),
proto.String("pending"), proto.Int(0),
proto.String("last-delivered-id"), proto.String("0-0"),
proto.String("entries-read"), proto.Nil,
proto.String("entries-read"), proto.Int(0),
proto.String("lag"), proto.Int(0),
),
),
Expand All @@ -747,6 +747,20 @@ func TestStreamReadGroup(t *testing.T) {
"XLEN", "planets",
)

mustDo(t, c,
"XINFO", "GROUPS", "planets",
proto.Array(
proto.Array(
proto.String("name"), proto.String("processing"),
proto.String("consumers"), proto.Int(0),
proto.String("pending"), proto.Int(0),
proto.String("last-delivered-id"), proto.String("0-0"),
proto.String("entries-read"), proto.Int(0),
proto.String("lag"), proto.Int(1),
),
),
)

mustDo(t, c,
"XREADGROUP", "GROUP", "processing", "alice", "STREAMS", "planets", ">",
proto.Array(
Expand All @@ -762,8 +776,8 @@ func TestStreamReadGroup(t *testing.T) {
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(1),
proto.String("last-delivered-id"), proto.String("0-1"),
proto.String("entries-read"), proto.Nil,
proto.String("lag"), proto.Int(1),
proto.String("entries-read"), proto.Int(1),
proto.String("lag"), proto.Int(0),
),
),
)
Expand Down Expand Up @@ -806,6 +820,11 @@ func TestStreamDelete(t *testing.T) {
proto.String("0-1"),
)

mustDo(t, c,
"XADD", "planets", "0-2", "name", "Venus",
proto.String("0-2"),
)

mustDo(t, c,
"XREADGROUP", "GROUP", "processing", "alice", "STREAMS", "planets", ">",
proto.Array(
Expand All @@ -816,24 +835,107 @@ func TestStreamDelete(t *testing.T) {
proto.String("0-1"),
proto.Strings("name", "Mercury"),
),
proto.Array(
proto.String("0-2"),
proto.Strings("name", "Venus"),
),
),
),
),
)

mustDo(t, c,
"XADD", "planets", "0-2", "name", "Mercury",
proto.String("0-2"),
"XINFO", "GROUPS", "planets",
proto.Array(
proto.Array(
proto.String("name"), proto.String("processing"),
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(2),
proto.String("last-delivered-id"), proto.String("0-2"),
proto.String("entries-read"), proto.Int(2),
proto.String("lag"), proto.Int(0),
),
),
)

mustDo(t, c,
"XADD", "planets", "0-3", "name", "Earth",
proto.String("0-3"),
)

mustDo(t, c,
"XADD", "planets", "0-4", "name", "Jupiter",
proto.String("0-4"),
)

mustDo(t, c,
"XINFO", "GROUPS", "planets",
proto.Array(
proto.Array(
proto.String("name"), proto.String("processing"),
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(2),
proto.String("last-delivered-id"), proto.String("0-2"),
proto.String("entries-read"), proto.Int(2),
proto.String("lag"), proto.Int(2),
),
),
)

must1(t, c,
"XDEL", "planets", "0-1",
)

mustDo(t, c,
"XINFO", "GROUPS", "planets",
proto.Array(
proto.Array(
proto.String("name"), proto.String("processing"),
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(1),
proto.String("last-delivered-id"), proto.String("0-2"),
proto.String("entries-read"), proto.Int(2),
proto.String("lag"), proto.Int(2),
),
),
)

must1(t, c,
"XDEL", "planets", "0-2",
)

mustDo(t, c,
"XINFO", "GROUPS", "planets",
proto.Array(
proto.Array(
proto.String("name"), proto.String("processing"),
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(0),
proto.String("last-delivered-id"), proto.String("0-2"),
proto.String("entries-read"), proto.Int(2),
proto.String("lag"), proto.Int(2),
),
),
)

must1(t, c,
"XDEL", "planets", "0-3",
)

mustDo(t, c,
"XINFO", "GROUPS", "planets",
proto.Array(
proto.Array(
proto.String("name"), proto.String("processing"),
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(0),
proto.String("last-delivered-id"), proto.String("0-2"),
proto.String("entries-read"), proto.Int(2),
proto.String("lag"), proto.Nil,
),
),
)

mustDo(t, c,
"XREADGROUP", "GROUP", "processing", "alice", "STREAMS", "planets", "0-0",
proto.Array(
Expand All @@ -843,6 +945,36 @@ func TestStreamDelete(t *testing.T) {
),
),
)

mustDo(t, c,
"XREADGROUP", "GROUP", "processing", "alice", "STREAMS", "planets", ">",
proto.Array(
proto.Array(
proto.String("planets"),
proto.Array(
proto.Array(
proto.String("0-4"),
proto.Strings("name", "Jupiter"),
),
),
),
),
)

mustDo(t, c,
"XINFO", "GROUPS", "planets",
proto.Array(
proto.Array(
proto.String("name"), proto.String("processing"),
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(1),
proto.String("last-delivered-id"), proto.String("0-4"),
proto.String("entries-read"), proto.Int(4),
proto.String("lag"), proto.Int(0),
),
),
)

}

// Test XACK
Expand All @@ -865,9 +997,38 @@ func TestStreamAck(t *testing.T) {
),
)

mustDo(t, c,
"XINFO", "GROUPS", "planets",
proto.Array(
proto.Array(
proto.String("name"), proto.String("processing"),
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(1),
proto.String("last-delivered-id"), proto.String("0-1"),
proto.String("entries-read"), proto.Int(1),
proto.String("lag"), proto.Int(0),
),
),
)

must1(t, c,
"XACK", "planets", "processing", "0-1",
)

mustDo(t, c,
"XINFO", "GROUPS", "planets",
proto.Array(
proto.Array(
proto.String("name"), proto.String("processing"),
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(0),
proto.String("last-delivered-id"), proto.String("0-1"),
proto.String("entries-read"), proto.Int(1),
proto.String("lag"), proto.Int(0),
),
),
)

mustDo(t, c,
"XREADGROUP", "GROUP", "processing", "alice", "STREAMS", "planets", "0-0",
proto.Array(
Expand All @@ -885,8 +1046,8 @@ func TestStreamAck(t *testing.T) {
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(0),
proto.String("last-delivered-id"), proto.String("0-1"),
proto.String("entries-read"), proto.Nil,
proto.String("lag"), proto.Int(1),
proto.String("entries-read"), proto.Int(1),
proto.String("lag"), proto.Int(0),
),
),
)
Expand Down Expand Up @@ -1353,8 +1514,8 @@ func TestStreamAutoClaim(t *testing.T) {
proto.String("consumers"), proto.Int(0),
proto.String("pending"), proto.Int(0),
proto.String("last-delivered-id"), proto.String("0-2"),
proto.String("entries-read"), proto.Nil,
proto.String("lag"), proto.Int(2),
proto.String("entries-read"), proto.Int(2),
proto.String("lag"), proto.Int(0),
),
),
)
Expand Down Expand Up @@ -1428,7 +1589,7 @@ func TestStreamClaim(t *testing.T) {
proto.String("consumers"), proto.Int(1),
proto.String("pending"), proto.Int(2),
proto.String("last-delivered-id"), proto.String("0-0"),
proto.String("entries-read"), proto.Nil,
proto.String("entries-read"), proto.Int(0),
proto.String("lag"), proto.Int(2),
),
),
Expand Down Expand Up @@ -1479,8 +1640,8 @@ func TestStreamClaim(t *testing.T) {
proto.String("consumers"), proto.Int(2),
proto.String("pending"), proto.Int(1),
proto.String("last-delivered-id"), proto.String("0-0"),
proto.String("entries-read"), proto.Nil,
proto.String("lag"), proto.Int(1),
proto.String("entries-read"), proto.Int(0),
proto.String("lag"), proto.Nil,
),
),
)
Expand Down
24 changes: 24 additions & 0 deletions integration/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,30 @@ func TestStreamGroup(t *testing.T) {
})
})

t.Run("XREADGROUP XDEL XINFO lag", func(t *testing.T) {
testRaw(t, func(c *client) {
// TODO ALL INFO needs to be uncommented, even DoLoosely is not enough
// for some of them becase Nil is returned instead of Int
c.Do("XGROUP", "CREATE", "planets", "processing", "$", "MKSTREAM")
c.Do("XADD", "planets", "0-1", "name", "Mercury")
c.Do("XADD", "planets", "0-2", "name", "Venus")
c.Do("XREADGROUP", "GROUP", "processing", "alice", "STREAMS", "planets", ">")
//c.Do("XINFO", "GROUPS", "planets") //SPEC entries-added 2, entries-read = 2, lag = 0
c.Do("XADD", "planets", "0-3", "name", "Earth")
c.Do("XADD", "planets", "0-4", "name", "Jupiter")
//c.Do("XINFO", "GROUPS", "planets") //SPEC entries-added 4, entries-read = 2, lag = 2

c.Do("XDEL", "planets", "0-1")
//c.Do("XINFO", "GROUPS", "planets") // SPEC entries-added 4, entries-read = 2, lag = 2
c.Do("XDEL", "planets", "0-2")
//c.Do("XINFO", "GROUPS", "planets") // SPEC entries-added 4, entries-read = 2, lag = 2
c.Do("XDEL", "planets", "0-3")
//c.Do("XINFO", "GROUPS", "planets") // SPEC entries-added 4, entries-read = 2, lag = nil
c.Do("XREADGROUP", "GROUP", "processing", "alice", "STREAMS", "planets", ">")
//c.Do("XINFO", "GROUPS", "planets") // SPEC entries-added 4, entries-read = 4, lag = 0
})
})

t.Run("XACK", func(t *testing.T) {
testRaw(t, func(c *client) {
c.Do("XGROUP", "CREATE", "planets", "processing", "$", "MKSTREAM")
Expand Down
Loading
Loading