1
0
Fork 0
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:
Henrik Hautakoski 2023-01-13 13:30:50 +01:00
parent 6b6a375228
commit 45bf043d8a
4 changed files with 70 additions and 2 deletions

View 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
}

View 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())
}