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

api/client.go: implement Client.Subscribe correctly by returning a error on unsupported channel type.

This commit is contained in:
Henrik Hautakoski 2023-05-26 14:09:47 +02:00
parent 9e586d879d
commit 8154bee1c2
2 changed files with 35 additions and 3 deletions

View file

@ -1,6 +1,7 @@
package api
import (
"fmt"
"sync"
"github.com/eosswedenorg/thalos/api/message"
@ -63,13 +64,16 @@ func (c *Client) hbHandler(payload []byte) {
c.OnHeartbeat(hb)
}
func (c *Client) Subscribe(channel Channel) {
func (c *Client) Subscribe(channel Channel) error {
var handler handler
if HeartbeatChannel.Is(channel) {
switch t := channel.Type(); t {
case HeartbeatChannel.Type():
handler = c.hbHandler
} else {
case ActionChannel{}.Channel().Type():
handler = c.actHandler
default:
return fmt.Errorf("invalid channel type. %s", t)
}
// Start a worker for this channel.
@ -78,6 +82,8 @@ func (c *Client) Subscribe(channel Channel) {
defer c.wg.Done()
c.worker(channel, handler)
}()
return nil
}
func (c *Client) Run() {

26
api/client_test.go Normal file
View file

@ -0,0 +1,26 @@
package api
import (
"testing"
)
func TestClient_Subscribe(t *testing.T) {
tests := []struct {
name string
channel Channel
wantErr bool
}{
{"Channel", Channel{}, true},
{"ActionChannel", ActionChannel{}.Channel(), false},
{"HeartbeatChannel", HeartbeatChannel, false},
{"TransactionChannel", TransactionChannel, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := Client{}
if err := c.Subscribe(tt.channel); (err != nil) != tt.wantErr {
t.Errorf("Client.Subscribe() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}