1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +02:00

api/redis/subscriber.go: Adding context as first parameter to NewSubscriber()

This commit is contained in:
Henrik Hautakoski 2023-06-05 13:20:58 +02:00
parent c1efffd4ba
commit f9124c2864
4 changed files with 11 additions and 9 deletions

View file

@ -27,10 +27,10 @@ func WithTimeout(value time.Duration) SubscriberOption {
}
}
func NewSubscriber(client *redis.Client, ns Namespace, options ...SubscriberOption) *Subscriber {
func NewSubscriber(ctx context.Context, client *redis.Client, ns Namespace, options ...SubscriberOption) *Subscriber {
sub := &Subscriber{
ctx: client.Context(),
sub: client.PSubscribe(client.Context()),
ctx: ctx,
sub: client.PSubscribe(ctx),
channels: make(map[string]chan []byte),
timeout: time.Millisecond * 200,
ns: ns,

View file

@ -1,6 +1,7 @@
package redis
import (
"context"
"testing"
"time"
@ -16,14 +17,14 @@ func TestSubscriber_Construct(t *testing.T) {
client, _ := redismock.NewClientMock()
ns := Namespace{Prefix: "prefix", ChainID: "8f2f6ec19400d372c9b3340b1438e9c805cf9e69be962fa81d055bc037ceed8d"}
s := NewSubscriber(client, ns)
s := NewSubscriber(context.Background(), client, ns)
assert.Equal(t, s.ctx, client.Context())
assert.Equal(t, s.ctx, context.Background())
assert.NotNil(t, s.sub)
assert.Equal(t, s.ns, ns)
assert.Equal(t, s.timeout, 200*time.Millisecond)
s = NewSubscriber(client, ns, WithTimeout(4*time.Second))
s = NewSubscriber(context.Background(), client, ns, WithTimeout(4*time.Second))
assert.Equal(t, s.timeout, 4*time.Second)
}
@ -36,7 +37,7 @@ func TestSubscriber_Read(t *testing.T) {
Addr: server.Addr(),
})
s := NewSubscriber(client, Namespace{Prefix: "prefix", ChainID: "d41dbd2921d5a377325661427090c6c508904d60920d6b7ea771c58da5299754"})
s := NewSubscriber(context.Background(), client, Namespace{Prefix: "prefix", ChainID: "d41dbd2921d5a377325661427090c6c508904d60920d6b7ea771c58da5299754"})
go func() {
time.Sleep(time.Millisecond * 10)