mirror of
https://github.com/eosswedenorg/thalos
synced 2026-07-04 12:03:41 +02:00
transport: move key and namespace into redis_pubsub as it is redis specific.
This commit is contained in:
parent
e0bf53496a
commit
af0a4d2714
8 changed files with 55 additions and 43 deletions
6
main.go
6
main.go
|
|
@ -12,7 +12,6 @@ import (
|
||||||
|
|
||||||
"eosio-ship-trace-reader/app"
|
"eosio-ship-trace-reader/app"
|
||||||
"eosio-ship-trace-reader/config"
|
"eosio-ship-trace-reader/config"
|
||||||
"eosio-ship-trace-reader/transport"
|
|
||||||
"eosio-ship-trace-reader/transport/redis_pubsub"
|
"eosio-ship-trace-reader/transport/redis_pubsub"
|
||||||
|
|
||||||
"github.com/nikoksr/notify"
|
"github.com/nikoksr/notify"
|
||||||
|
|
@ -241,7 +240,10 @@ func main() {
|
||||||
|
|
||||||
app.SpawnProccessor(
|
app.SpawnProccessor(
|
||||||
shClient,
|
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),
|
abi.NewAbiManager(rdb, eosClient, conf.Redis.CacheID),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
package transport
|
package redis_pubsub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"eosio-ship-trace-reader/transport"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Key consists of a namespace and a channel.
|
// Key consists of a namespace and a channel.
|
||||||
|
|
@ -9,7 +11,7 @@ import (
|
||||||
|
|
||||||
type Key struct {
|
type Key struct {
|
||||||
NS Namespace
|
NS Namespace
|
||||||
Channel ChannelInterface
|
Channel transport.ChannelInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k Key) String() string {
|
func (k Key) String() string {
|
||||||
35
transport/redis_pubsub/key_test.go
Normal file
35
transport/redis_pubsub/key_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
package transport
|
package redis_pubsub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"eosio-ship-trace-reader/transport"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -25,7 +27,7 @@ type Namespace struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new key with this namespace.
|
// 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}
|
return Key{NS: ns, Channel: ch}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package transport
|
package redis_pubsub
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
|
|
@ -11,17 +11,19 @@ import (
|
||||||
type RedisPubsub struct {
|
type RedisPubsub struct {
|
||||||
pipeline redis.Pipeliner
|
pipeline redis.Pipeliner
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
ns Namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(client *redis.Client) *RedisPubsub {
|
func New(client *redis.Client, ns Namespace) *RedisPubsub {
|
||||||
return &RedisPubsub{
|
return &RedisPubsub{
|
||||||
pipeline: client.Pipeline(),
|
pipeline: client.Pipeline(),
|
||||||
ctx: client.Context(),
|
ctx: client.Context(),
|
||||||
|
ns: ns,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RedisPubsub) Publish(channel transport.ChannelInterface, payload []byte) error {
|
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 {
|
func (r *RedisPubsub) Flush() error {
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,11 @@ import (
|
||||||
func TestRedisPubsub(t *testing.T) {
|
func TestRedisPubsub(t *testing.T) {
|
||||||
client, mock := redismock.NewClientMock()
|
client, mock := redismock.NewClientMock()
|
||||||
|
|
||||||
pubsub := New(client)
|
pubsub := New(client, Namespace{ChainID: "id"})
|
||||||
|
|
||||||
mock.MatchExpectationsInOrder(true)
|
mock.MatchExpectationsInOrder(true)
|
||||||
mock.ExpectPublish("test", []byte("some string")).SetVal(0)
|
mock.ExpectPublish("ship::id::test", []byte("some string")).SetVal(0)
|
||||||
mock.ExpectPublish("test2", []byte("some other 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{"test"}, []byte("some string")))
|
||||||
assert.NoError(t, pubsub.Publish(transport.Channel{"test2"}, []byte("some other string")))
|
assert.NoError(t, pubsub.Publish(transport.Channel{"test2"}, []byte("some other string")))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue