mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-18 04:40:03 +02:00
move channel stuffs from internal/redis to transport.
This commit is contained in:
parent
d0782c3c7c
commit
b22118898d
8 changed files with 18 additions and 18 deletions
|
|
@ -1,48 +0,0 @@
|
|||
package redis
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Generic interface for all channel types.
|
||||
type ChannelInterface interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
// Standard channel. Just a wrapper around string slice
|
||||
type Channel []string
|
||||
|
||||
func (c *Channel) Append(name ...string) {
|
||||
*c = append(*c, name...)
|
||||
}
|
||||
|
||||
func (c Channel) String() string {
|
||||
return strings.Join(c, "/")
|
||||
}
|
||||
|
||||
// Define channels without any variables.
|
||||
var (
|
||||
TransactionChannel = Channel{"transaction"}
|
||||
HeartbeatChannel = Channel{"heartbeat"}
|
||||
)
|
||||
|
||||
// Action channel.
|
||||
|
||||
type ActionChannel struct {
|
||||
Contract string
|
||||
Action string
|
||||
}
|
||||
|
||||
func (ac ActionChannel) String() string {
|
||||
ch := Channel{"actions"}
|
||||
|
||||
if len(ac.Contract) > 0 {
|
||||
ch.Append("contract", ac.Contract)
|
||||
}
|
||||
|
||||
if len(ac.Action) > 0 {
|
||||
ch.Append("action", ac.Action)
|
||||
}
|
||||
|
||||
return ch.String()
|
||||
}
|
||||
|
|
@ -1,72 +0,0 @@
|
|||
package redis
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChannel_Append(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
arg string
|
||||
obj Channel
|
||||
expected Channel
|
||||
}{
|
||||
{"One", "one", Channel{}, Channel{"one"}},
|
||||
{"More", "more", Channel{"one", "two"}, Channel{"one", "two", "more"}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.obj.Append(tt.arg)
|
||||
if reflect.DeepEqual(tt.obj, tt.expected) == false {
|
||||
t.Errorf("Channel.Append() expected %v, got %v", tt.expected, tt.obj)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannel_String(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
c Channel
|
||||
want string
|
||||
}{
|
||||
{"Empty", Channel{}, ""},
|
||||
{"Alot", Channel{"one", "two", "three"}, "one/two/three"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.c.String(); got != tt.want {
|
||||
t.Errorf("Channel.String() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestActionChannel_String(t *testing.T) {
|
||||
type fields struct {
|
||||
Contract string
|
||||
Action string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want string
|
||||
}{
|
||||
{"Empty", fields{}, "actions"},
|
||||
{"Contract", fields{Contract: "mycontract"}, "actions/contract/mycontract"},
|
||||
{"Action", fields{Action: "myaction"}, "actions/action/myaction"},
|
||||
{"ContractAction", fields{Contract: "mycontract", Action: "myaction"}, "actions/contract/mycontract/action/myaction"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ac := ActionChannel{
|
||||
Contract: tt.fields.Contract,
|
||||
Action: tt.fields.Action,
|
||||
}
|
||||
if got := ac.String(); got != tt.want {
|
||||
t.Errorf("ActionChannel.String() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package redis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Key consists of a namespace and a channel.
|
||||
// And is encoded to a string in this format: `<namespace>::<channel>`
|
||||
|
||||
type Key struct {
|
||||
NS Namespace
|
||||
Channel ChannelInterface
|
||||
}
|
||||
|
||||
func (k Key) String() string {
|
||||
return fmt.Sprintf("%s::%s", k.NS, k.Channel)
|
||||
}
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
package redis
|
||||
|
||||
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,45 +0,0 @@
|
|||
package redis
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// Default prefix to use when none is set.
|
||||
defaultPrefix = "ship"
|
||||
|
||||
// We need to have some chain_id, so if no one is specified.
|
||||
// we use a "null" id that is all zeros.
|
||||
nullChain = "0000000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
|
||||
// Namespace type.
|
||||
//
|
||||
// Contains a prefix and chain_id to guard keys against collision.
|
||||
// Prefix should be sufficient to not collide with other application using the same redis database.
|
||||
// chain_id should be ok to not let multiple reader with different chains to write to the same channels.
|
||||
|
||||
type Namespace struct {
|
||||
Prefix string
|
||||
ChainID string
|
||||
}
|
||||
|
||||
// Create a new key with this namespace.
|
||||
func (ns Namespace) NewKey(ch ChannelInterface) Key {
|
||||
return Key{NS: ns, Channel: ch}
|
||||
}
|
||||
|
||||
func (ns Namespace) String() string {
|
||||
// No Chain id, set to "nullChain"
|
||||
if len(ns.ChainID) < 1 {
|
||||
ns.ChainID = nullChain
|
||||
}
|
||||
|
||||
// Set default prefix if empty.
|
||||
if len(ns.Prefix) < 1 {
|
||||
ns.Prefix = defaultPrefix
|
||||
}
|
||||
|
||||
// Otherwise. return both.
|
||||
return strings.Join([]string{ns.Prefix, ns.ChainID}, "::")
|
||||
}
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
package redis
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNamespace_String(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ns Namespace
|
||||
want string
|
||||
}{
|
||||
{"Empty", Namespace{}, "ship::0000000000000000000000000000000000000000000000000000000000000000"},
|
||||
{"Prefix Only", Namespace{Prefix: "some.prefix"}, "some.prefix::0000000000000000000000000000000000000000000000000000000000000000"},
|
||||
{"ChainID Only", Namespace{ChainID: "1234"}, "ship::1234"},
|
||||
{"Both", Namespace{Prefix: "my.prefix", ChainID: "1234"}, "my.prefix::1234"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.ns.String(); got != tt.want {
|
||||
t.Errorf("Namespace.String() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue