From af0a4d2714f3479232610c818db528031f63418e Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 19 Jan 2023 19:07:35 +0100 Subject: [PATCH] transport: move key and namespace into redis_pubsub as it is redis specific. --- main.go | 6 ++-- transport/key_test.go | 31 ---------------- transport/{ => redis_pubsub}/key.go | 6 ++-- transport/redis_pubsub/key_test.go | 35 +++++++++++++++++++ transport/{ => redis_pubsub}/namespace.go | 6 ++-- .../{ => redis_pubsub}/namespace_test.go | 2 +- transport/redis_pubsub/redis.go | 6 ++-- transport/redis_pubsub/redis_test.go | 6 ++-- 8 files changed, 55 insertions(+), 43 deletions(-) delete mode 100644 transport/key_test.go rename transport/{ => redis_pubsub}/key.go (73%) create mode 100644 transport/redis_pubsub/key_test.go rename transport/{ => redis_pubsub}/namespace.go (89%) rename transport/{ => redis_pubsub}/namespace_test.go (97%) diff --git a/main.go b/main.go index f49d4a4..a94ecd7 100644 --- a/main.go +++ b/main.go @@ -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), ) diff --git a/transport/key_test.go b/transport/key_test.go deleted file mode 100644 index 5d295a5..0000000 --- a/transport/key_test.go +++ /dev/null @@ -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) - } - }) - } -} diff --git a/transport/key.go b/transport/redis_pubsub/key.go similarity index 73% rename from transport/key.go rename to transport/redis_pubsub/key.go index 7c952b9..bd01cd4 100644 --- a/transport/key.go +++ b/transport/redis_pubsub/key.go @@ -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 { diff --git a/transport/redis_pubsub/key_test.go b/transport/redis_pubsub/key_test.go new file mode 100644 index 0000000..81b7001 --- /dev/null +++ b/transport/redis_pubsub/key_test.go @@ -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) + } + }) + } +} diff --git a/transport/namespace.go b/transport/redis_pubsub/namespace.go similarity index 89% rename from transport/namespace.go rename to transport/redis_pubsub/namespace.go index 7352604..505e577 100644 --- a/transport/namespace.go +++ b/transport/redis_pubsub/namespace.go @@ -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} } diff --git a/transport/namespace_test.go b/transport/redis_pubsub/namespace_test.go similarity index 97% rename from transport/namespace_test.go rename to transport/redis_pubsub/namespace_test.go index 9658c23..7c9eef9 100644 --- a/transport/namespace_test.go +++ b/transport/redis_pubsub/namespace_test.go @@ -1,4 +1,4 @@ -package transport +package redis_pubsub import "testing" diff --git a/transport/redis_pubsub/redis.go b/transport/redis_pubsub/redis.go index ef587d6..b5988e9 100644 --- a/transport/redis_pubsub/redis.go +++ b/transport/redis_pubsub/redis.go @@ -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 { diff --git a/transport/redis_pubsub/redis_test.go b/transport/redis_pubsub/redis_test.go index 95bd8c9..674f491 100644 --- a/transport/redis_pubsub/redis_test.go +++ b/transport/redis_pubsub/redis_test.go @@ -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")))