mirror of
https://github.com/redis/go-redis.git
synced 2025-07-29 17:41:15 +03:00
Add basic redis streams support
This commit is contained in:
committed by
Vladimir Mihailenco
parent
4b568cdf1a
commit
39bdfc3fa8
193
commands_test.go
193
commands_test.go
@ -3018,6 +3018,199 @@ var _ = Describe("Commands", func() {
|
||||
|
||||
})
|
||||
|
||||
Describe("streams", func() {
|
||||
createStream := func() {
|
||||
id, err := client.XAdd("stream", "1-0", map[string]interface{}{
|
||||
"uno": "un",
|
||||
}).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(id).To(Equal("1-0"))
|
||||
|
||||
id, err = client.XAdd("stream", "2-0", map[string]interface{}{
|
||||
"dos": "deux",
|
||||
}).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(id).To(Equal("2-0"))
|
||||
|
||||
id, err = client.XAdd("stream", "3-0", map[string]interface{}{
|
||||
"tres": "troix",
|
||||
}).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(id).To(Equal("3-0"))
|
||||
}
|
||||
|
||||
It("should XAdd", func() {
|
||||
createStream()
|
||||
|
||||
id, err := client.XAdd("stream", "*", map[string]interface{}{
|
||||
"quatro": "quatre",
|
||||
}).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
vals, err := client.XRange("stream", "-", "+").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(vals).To(Equal([]*redis.XMessage{
|
||||
{ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
|
||||
{ID: id, Values: map[string]interface{}{"quatro": "quatre"}},
|
||||
}))
|
||||
})
|
||||
|
||||
It("should XAddExt", func() {
|
||||
createStream()
|
||||
|
||||
id, err := client.XAddExt(&redis.XAddExt{
|
||||
Stream: "stream",
|
||||
MaxLen: 1,
|
||||
Values: map[string]interface{}{"quatro": "quatre"},
|
||||
}).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
vals, err := client.XRange("stream", "-", "+").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(vals).To(Equal([]*redis.XMessage{
|
||||
{ID: id, Values: map[string]interface{}{"quatro": "quatre"}},
|
||||
}))
|
||||
})
|
||||
|
||||
It("should XLen", func() {
|
||||
createStream()
|
||||
|
||||
n, err := client.XLen("stream").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(n).To(Equal(int64(3)))
|
||||
})
|
||||
|
||||
It("should XRange", func() {
|
||||
createStream()
|
||||
|
||||
msgs, err := client.XRange("stream", "-", "+").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(msgs).To(Equal([]*redis.XMessage{
|
||||
{ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
|
||||
}))
|
||||
|
||||
msgs, err = client.XRange("stream", "2", "+").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(msgs).To(Equal([]*redis.XMessage{
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
|
||||
}))
|
||||
|
||||
msgs, err = client.XRange("stream", "-", "2").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(msgs).To(Equal([]*redis.XMessage{
|
||||
{ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
}))
|
||||
})
|
||||
|
||||
It("should XRangeN", func() {
|
||||
createStream()
|
||||
|
||||
msgs, err := client.XRangeN("stream", "-", "+", 2).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(msgs).To(Equal([]*redis.XMessage{
|
||||
{ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
}))
|
||||
|
||||
msgs, err = client.XRangeN("stream", "2", "+", 1).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(msgs).To(Equal([]*redis.XMessage{
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
}))
|
||||
|
||||
msgs, err = client.XRangeN("stream", "-", "2", 1).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(msgs).To(Equal([]*redis.XMessage{
|
||||
{ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
|
||||
}))
|
||||
})
|
||||
|
||||
It("should XRevRange", func() {
|
||||
createStream()
|
||||
|
||||
msgs, err := client.XRevRange("stream", "+", "-").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(msgs).To(Equal([]*redis.XMessage{
|
||||
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
{ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
|
||||
}))
|
||||
|
||||
msgs, err = client.XRevRange("stream", "+", "2").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(msgs).To(Equal([]*redis.XMessage{
|
||||
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
}))
|
||||
})
|
||||
|
||||
It("should XRevRangeN", func() {
|
||||
createStream()
|
||||
|
||||
msgs, err := client.XRevRangeN("stream", "+", "-", 2).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(msgs).To(Equal([]*redis.XMessage{
|
||||
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
}))
|
||||
|
||||
msgs, err = client.XRevRangeN("stream", "+", "2", 1).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(msgs).To(Equal([]*redis.XMessage{
|
||||
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
|
||||
}))
|
||||
})
|
||||
|
||||
It("should XRead", func() {
|
||||
createStream()
|
||||
|
||||
res, err := client.XRead("stream", "0").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res).To(Equal([]*redis.XStream{{
|
||||
Stream: "stream",
|
||||
Messages: []*redis.XMessage{
|
||||
{ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
|
||||
}},
|
||||
}))
|
||||
|
||||
_, err = client.XRead("stream", "3").Result()
|
||||
Expect(err).To(Equal(redis.Nil))
|
||||
})
|
||||
|
||||
It("should XReadExt", func() {
|
||||
createStream()
|
||||
|
||||
res, err := client.XReadExt(&redis.XReadExt{
|
||||
Streams: []string{"stream", "0"},
|
||||
Count: 2,
|
||||
Block: 100 * time.Millisecond,
|
||||
}).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res).To(Equal([]*redis.XStream{{
|
||||
Stream: "stream",
|
||||
Messages: []*redis.XMessage{
|
||||
{ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
|
||||
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
|
||||
}},
|
||||
}))
|
||||
|
||||
_, err = client.XReadExt(&redis.XReadExt{
|
||||
Streams: []string{"stream", "3"},
|
||||
Count: 1,
|
||||
Block: 100 * time.Millisecond,
|
||||
}).Result()
|
||||
Expect(err).To(Equal(redis.Nil))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Geo add and radius search", func() {
|
||||
BeforeEach(func() {
|
||||
geoAdd := client.GeoAdd(
|
||||
|
Reference in New Issue
Block a user