mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-16 04:24:56 +02:00
move writer interface and redis publish from api module as these are for the server side.
This commit is contained in:
parent
a94fbe191a
commit
24bf44a175
5 changed files with 11 additions and 5 deletions
|
|
@ -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()
|
||||
}
|
||||
|
|
@ -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())
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
package api
|
||||
|
||||
// Writer interface defines the required methods
|
||||
// to send messages over an channel.
|
||||
//
|
||||
// This is a low-level interface typically implemented by backend drivers
|
||||
type Writer interface {
|
||||
// Write writes a message over a channel.
|
||||
// The message may or may not be buffered depending on the implementation.
|
||||
Write(channel Channel, payload []byte) error
|
||||
|
||||
// Flush writes any buffered messages to the channel.
|
||||
// If the implementation does not support buffering. this is a noop.
|
||||
Flush() error
|
||||
|
||||
// Close closes the writer
|
||||
// Any blocked Flush or Write operations will be unblocked.
|
||||
Close() error
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue