1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-17 04:30:03 +02:00

transport: move key and namespace into redis_pubsub as it is redis specific.

This commit is contained in:
Henrik Hautakoski 2023-01-19 19:07:35 +01:00
parent e0bf53496a
commit af0a4d2714
8 changed files with 55 additions and 43 deletions

View file

@ -12,7 +12,6 @@ import (
"eosio-ship-trace-reader/app"
"eosio-ship-trace-reader/config"
"eosio-ship-trace-reader/transport"
"eosio-ship-trace-reader/transport/redis_pubsub"
"github.com/nikoksr/notify"
@ -241,7 +240,10 @@ func main() {
app.SpawnProccessor(
shClient,
redis_pubsub.New(rdb),
redis_pubsub.New(rdb, redis_pubsub.Namespace{
Prefix: conf.Redis.Prefix,
ChainID: chainInfo.ChainID.String(),
}),
abi.NewAbiManager(rdb, eosClient, conf.Redis.CacheID),
)

View file

@ -1,31 +0,0 @@
package transport
import "testing"
func TestKey_String(t *testing.T) {
type fields struct {
NS Namespace
Channel ChannelInterface
}
tests := []struct {
name string
fields fields
want string
}{
{"Empty", fields{NS: Namespace{}, Channel: Channel{}}, "ship::0000000000000000000000000000000000000000000000000000000000000000::"},
{"Transactions", fields{NS: Namespace{ChainID: "id"}, Channel: Channel{"transactions"}}, "ship::id::transactions"},
{"Nested", fields{NS: Namespace{ChainID: "id"}, Channel: Channel{"one.two"}}, "ship::id::one.two"},
{"Action", fields{NS: Namespace{ChainID: "id"}, Channel: ActionChannel{Contract: "mycontract"}}, "ship::id::actions/contract/mycontract"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := Key{
NS: tt.fields.NS,
Channel: tt.fields.Channel,
}
if got := k.String(); got != tt.want {
t.Errorf("Key.String() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -1,7 +1,9 @@
package transport
package redis_pubsub
import (
"fmt"
"eosio-ship-trace-reader/transport"
)
// Key consists of a namespace and a channel.
@ -9,7 +11,7 @@ import (
type Key struct {
NS Namespace
Channel ChannelInterface
Channel transport.ChannelInterface
}
func (k Key) String() string {

View file

@ -0,0 +1,35 @@
package redis_pubsub
import (
"testing"
"eosio-ship-trace-reader/transport"
)
func TestKey_String(t *testing.T) {
type fields struct {
NS Namespace
Channel transport.ChannelInterface
}
tests := []struct {
name string
fields fields
want string
}{
{"Empty", fields{NS: Namespace{}, Channel: transport.Channel{}}, "ship::0000000000000000000000000000000000000000000000000000000000000000::"},
{"Transactions", fields{NS: Namespace{ChainID: "id"}, Channel: transport.Channel{"transactions"}}, "ship::id::transactions"},
{"Nested", fields{NS: Namespace{ChainID: "id"}, Channel: transport.Channel{"one.two"}}, "ship::id::one.two"},
{"Action", fields{NS: Namespace{ChainID: "id"}, Channel: transport.ActionChannel{Contract: "mycontract"}}, "ship::id::actions/contract/mycontract"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := Key{
NS: tt.fields.NS,
Channel: tt.fields.Channel,
}
if got := k.String(); got != tt.want {
t.Errorf("Key.String() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -1,7 +1,9 @@
package transport
package redis_pubsub
import (
"strings"
"eosio-ship-trace-reader/transport"
)
const (
@ -25,7 +27,7 @@ type Namespace struct {
}
// Create a new key with this namespace.
func (ns Namespace) NewKey(ch ChannelInterface) Key {
func (ns Namespace) NewKey(ch transport.ChannelInterface) Key {
return Key{NS: ns, Channel: ch}
}

View file

@ -1,4 +1,4 @@
package transport
package redis_pubsub
import "testing"

View file

@ -11,17 +11,19 @@ import (
type RedisPubsub struct {
pipeline redis.Pipeliner
ctx context.Context
ns Namespace
}
func New(client *redis.Client) *RedisPubsub {
func New(client *redis.Client, ns Namespace) *RedisPubsub {
return &RedisPubsub{
pipeline: client.Pipeline(),
ctx: client.Context(),
ns: ns,
}
}
func (r *RedisPubsub) Publish(channel transport.ChannelInterface, payload []byte) error {
return r.pipeline.Publish(r.ctx, channel.String(), payload).Err()
return r.pipeline.Publish(r.ctx, r.ns.NewKey(channel).String(), payload).Err()
}
func (r *RedisPubsub) Flush() error {

View file

@ -12,11 +12,11 @@ import (
func TestRedisPubsub(t *testing.T) {
client, mock := redismock.NewClientMock()
pubsub := New(client)
pubsub := New(client, Namespace{ChainID: "id"})
mock.MatchExpectationsInOrder(true)
mock.ExpectPublish("test", []byte("some string")).SetVal(0)
mock.ExpectPublish("test2", []byte("some other string")).SetVal(0)
mock.ExpectPublish("ship::id::test", []byte("some string")).SetVal(0)
mock.ExpectPublish("ship::id::test2", []byte("some other string")).SetVal(0)
assert.NoError(t, pubsub.Publish(transport.Channel{"test"}, []byte("some string")))
assert.NoError(t, pubsub.Publish(transport.Channel{"test2"}, []byte("some other string")))