1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-20 09:56:47 +02:00

move writer interface and redis publish from api module as these are for the server side.

This commit is contained in:
Henrik Hautakoski 2023-11-25 15:12:08 +01:00
parent a94fbe191a
commit 24bf44a175
5 changed files with 11 additions and 5 deletions

View file

@ -1,36 +0,0 @@
package redis
import (
"context"
"github.com/eosswedenorg/thalos/api"
"github.com/redis/go-redis/v9"
)
type Publisher struct {
pipeline redis.Pipeliner
ctx context.Context
ns Namespace
}
func NewPublisher(ctx context.Context, client *redis.Client, ns Namespace) *Publisher {
return &Publisher{
pipeline: client.Pipeline(),
ctx: ctx,
ns: ns,
}
}
func (r *Publisher) Write(channel api.Channel, payload []byte) error {
return r.pipeline.Publish(r.ctx, r.ns.NewKey(channel).String(), payload).Err()
}
func (r *Publisher) Flush() error {
_, err := r.pipeline.Exec(r.ctx)
return err
}
func (r *Publisher) Close() error {
return r.Flush()
}

View file

@ -1,27 +0,0 @@
package redis
import (
"context"
"testing"
"github.com/eosswedenorg/thalos/api"
"github.com/go-redis/redismock/v9"
"github.com/stretchr/testify/assert"
)
func TestPublisher_Write(t *testing.T) {
client, mock := redismock.NewClientMock()
pub := NewPublisher(context.Background(), client, Namespace{ChainID: "id"})
mock.MatchExpectationsInOrder(true)
mock.ExpectPublish("ship::id::test", []byte("some string")).SetVal(0)
mock.ExpectPublish("ship::id::test2", []byte("some other string")).SetVal(0)
assert.NoError(t, pub.Write(api.Channel{"test"}, []byte("some string")))
assert.NoError(t, pub.Write(api.Channel{"test2"}, []byte("some other string")))
assert.NoError(t, pub.Flush())
assert.NoError(t, mock.ExpectationsWereMet())
}