Skip to content

Commit d2f2315

Browse files
committed
test: Add tests for cancelled context
These tests are failing and are disabled as a result.
1 parent 345ad75 commit d2f2315

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

commands_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4843,6 +4843,22 @@ var _ = Describe("Commands", func() {
48434843
Expect(err).To(Equal(redis.Nil))
48444844
})
48454845

4846+
// https://github.com/redis/go-redis/issues/2276
4847+
XDescribe("canceled context", func() {
4848+
It("should unblock XREAD", func() {
4849+
ctx2, cancel := context.WithCancel(ctx)
4850+
errChan := make(chan error, 1)
4851+
go func() {
4852+
errChan <- client.XRead(ctx2, &redis.XReadArgs{
4853+
Streams: []string{"stream", "$"},
4854+
}).Err()
4855+
}()
4856+
Consistently(errChan).ShouldNot(Receive())
4857+
cancel()
4858+
Eventually(errChan).Should(Receive(MatchError(context.Canceled)))
4859+
})
4860+
})
4861+
48464862
Describe("group", func() {
48474863
BeforeEach(func() {
48484864
err := client.XGroupCreate(ctx, "stream", "group", "0").Err()
@@ -5023,6 +5039,24 @@ var _ = Describe("Commands", func() {
50235039
Expect(err).NotTo(HaveOccurred())
50245040
Expect(n).To(Equal(int64(2)))
50255041
})
5042+
5043+
// https://github.com/redis/go-redis/issues/2276
5044+
XDescribe("canceled context", func() {
5045+
It("should unblock XReadGroup", func() {
5046+
ctx2, cancel := context.WithCancel(ctx)
5047+
errChan := make(chan error, 1)
5048+
go func() {
5049+
errChan <- client.XReadGroup(ctx2, &redis.XReadGroupArgs{
5050+
Group: "group",
5051+
Consumer: "consumer",
5052+
Streams: []string{"stream", ">"},
5053+
}).Err()
5054+
}()
5055+
Consistently(errChan).ShouldNot(Receive())
5056+
cancel()
5057+
Eventually(errChan).Should(Receive(MatchError(context.Canceled)))
5058+
})
5059+
})
50265060
})
50275061

50285062
Describe("xinfo", func() {

pubsub_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis_test
22

33
import (
4+
"context"
45
"io"
56
"net"
67
"sync"
@@ -567,4 +568,31 @@ var _ = Describe("PubSub", func() {
567568
Expect(msg.Channel).To(Equal("mychannel"))
568569
Expect(msg.Payload).To(Equal(text))
569570
})
571+
572+
// https://github.com/redis/go-redis/issues/2276
573+
XDescribeTable("canceled context", func(receiveFn func(ctx context.Context, pubsub *redis.PubSub) error) {
574+
pubsub := client.Subscribe(ctx, "mychannel")
575+
defer pubsub.Close()
576+
ctx2, cancel := context.WithCancel(ctx)
577+
errChan := make(chan error, 1)
578+
go func() {
579+
errChan <- receiveFn(ctx2, pubsub)
580+
}()
581+
Consistently(errChan).ShouldNot(Receive())
582+
cancel()
583+
Eventually(errChan).Should(Receive(MatchError(context.Canceled)))
584+
},
585+
Entry("Receive", func(ctx context.Context, pubsub *redis.PubSub) error {
586+
_, err := pubsub.Receive(ctx)
587+
return err
588+
}),
589+
Entry("ReceiveMessage", func(ctx context.Context, pubsub *redis.PubSub) error {
590+
_, err := pubsub.ReceiveMessage(ctx)
591+
return err
592+
}),
593+
Entry("ReceiveTimeout", func(ctx context.Context, pubsub *redis.PubSub) error {
594+
_, err := pubsub.ReceiveTimeout(ctx, time.Hour)
595+
return err
596+
}),
597+
)
570598
})

0 commit comments

Comments
 (0)