mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-20 09:56:47 +02:00
Adding transport/redis_pubsub publisher
This commit is contained in:
parent
6b6a375228
commit
45bf043d8a
4 changed files with 70 additions and 2 deletions
28
transport/redis_pubsub/redis.go
Normal file
28
transport/redis_pubsub/redis.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package redis_pubsub
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
redis "github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type RedisPubsub struct {
|
||||
pipeline redis.Pipeliner
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func New(client *redis.Client) *RedisPubsub {
|
||||
return &RedisPubsub{
|
||||
pipeline: client.Pipeline(),
|
||||
ctx: client.Context(),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RedisPubsub) Publish(channel string, payload []byte) error {
|
||||
return r.pipeline.Publish(r.ctx, channel, payload).Err()
|
||||
}
|
||||
|
||||
func (r *RedisPubsub) Flush() error {
|
||||
_, err := r.pipeline.Exec(r.ctx)
|
||||
return err
|
||||
}
|
||||
24
transport/redis_pubsub/redis_test.go
Normal file
24
transport/redis_pubsub/redis_test.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package redis_pubsub
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/go-redis/redismock/v8"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRedisPubsub(t *testing.T) {
|
||||
client, mock := redismock.NewClientMock()
|
||||
|
||||
pubsub := New(client)
|
||||
|
||||
mock.MatchExpectationsInOrder(true)
|
||||
mock.ExpectPublish("test", []byte("some string")).SetVal(0)
|
||||
mock.ExpectPublish("test2", []byte("some other string")).SetVal(0)
|
||||
|
||||
assert.NoError(t, pubsub.Publish("test", []byte("some string")))
|
||||
assert.NoError(t, pubsub.Publish("test2", []byte("some other string")))
|
||||
assert.NoError(t, pubsub.Flush())
|
||||
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue