1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +02:00

Adding internal/redis/channel.go

This commit is contained in:
Henrik Hautakoski 2023-01-06 16:50:00 +01:00
parent 177b715ba4
commit e91951d7ed
2 changed files with 121 additions and 0 deletions

49
internal/redis/channel.go Normal file
View file

@ -0,0 +1,49 @@
package redis
import (
"fmt"
"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(fmt.Sprintf("contract:%s", ac.Contract))
}
if len(ac.Action) > 0 {
ch.Append(fmt.Sprintf("action:%s", ac.Action))
}
return ch.String()
}

View file

@ -0,0 +1,72 @@
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)
}
})
}
}