Skip to content

Commit

Permalink
refactor and improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
y-du committed Jul 19, 2024
1 parent 95e03ab commit 0b595e0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 73 deletions.
2 changes: 1 addition & 1 deletion handler/itf.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ type MessageRelayHandler interface {
Put(m Message) error
}

type MessageHandler func(m Message) error
type MessageHandler func(m Message)
35 changes: 21 additions & 14 deletions handler/message_hdl/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package message_hdl
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/SENERGY-Platform/mgw-device-manager/handler"
lib_model "github.com/SENERGY-Platform/mgw-device-manager/lib/model"
"github.com/SENERGY-Platform/mgw-device-manager/util"
"github.com/SENERGY-Platform/mgw-device-manager/util/topic"
)

const logPrefix = "[message-hdl]"

type Handler struct {
devicesHdl handler.DevicesHandler
}
Expand All @@ -18,18 +19,21 @@ func New(devicesHdl handler.DevicesHandler) *Handler {
return &Handler{devicesHdl: devicesHdl}
}

func (h *Handler) HandleMessage(m handler.Message) error {
func (h *Handler) HandleMessage(m handler.Message) {
var ref string
switch {
case parseTopic(topic.DevicesSub, m.Topic(), &ref):
var dm lib_model.DeviceMessage
if err := json.Unmarshal(m.Payload(), &dm); err != nil {
return err
util.Logger.Errorf("%s unmarshal message: %s", logPrefix, err)
return
}
switch dm.Method {
case lib_model.Set:
util.Logger.Infof("%s set device (%s)", logPrefix, dm.DeviceID)
if dm.Data == nil {
return errors.New("missing device data")
util.Logger.Errorf("%s set device (%s): missing data", logPrefix, dm.DeviceID)
return
}
err := h.devicesHdl.Put(context.Background(), lib_model.DeviceData{
ID: dm.DeviceID,
Expand All @@ -40,20 +44,23 @@ func (h *Handler) HandleMessage(m handler.Message) error {
Attributes: dm.Data.Attributes,
})
if err != nil {
var iie *lib_model.InvalidInputError
if errors.As(err, &iie) {
return err
}
util.Logger.Errorf("%s set device (%s): %s", logPrefix, dm.DeviceID, err)
}
case lib_model.Delete:
_ = h.devicesHdl.Delete(context.Background(), dm.DeviceID)
util.Logger.Infof("%s delete device (%s)", logPrefix, dm.DeviceID)
if err := h.devicesHdl.Delete(context.Background(), dm.DeviceID); err != nil {
util.Logger.Errorf("%s delete device (%s): %s", logPrefix, dm.DeviceID, err)
}
default:
return fmt.Errorf("unknown method '%s'", dm.Method)
util.Logger.Errorf("%s unknown method '%s'", logPrefix, dm.Method)
}
case parseTopic(topic.LastWillSub, m.Topic(), &ref):
_ = h.devicesHdl.SetStates(context.Background(), ref, lib_model.Offline)
util.Logger.Infof("%s set device states (%s)", logPrefix, ref)
if err := h.devicesHdl.SetStates(context.Background(), ref, lib_model.Offline); err != nil {
util.Logger.Errorf("%s set device states (%s): %s", logPrefix, ref, err)
}
default:
return fmt.Errorf("parsing topic '%s' failed", m.Topic())
util.Logger.Errorf("%s unknown topic '%s'", logPrefix, m.Topic())
}
return nil
return
}
55 changes: 32 additions & 23 deletions handler/message_hdl/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"context"
"encoding/json"
"errors"
sb_util "github.com/SENERGY-Platform/go-service-base/util"
lib_model "github.com/SENERGY-Platform/mgw-device-manager/lib/model"
"github.com/SENERGY-Platform/mgw-device-manager/util"
"reflect"
"testing"
)

func TestHandler_HandleMessage(t *testing.T) {
util.InitLogger(sb_util.LoggerConfig{Terminal: true, Level: 4})
t.Run("set device", func(t *testing.T) {
mockDHdl := &mockDeviceHdl{
Devices: make(map[string]lib_model.DeviceData),
Expand Down Expand Up @@ -46,13 +49,10 @@ func TestHandler_HandleMessage(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = h.HandleMessage(&mockMessage{
h.HandleMessage(&mockMessage{
topic: "device-manager/device/test",
payload: p,
})
if err != nil {
t.Error(err)
}
b, ok := mockDHdl.Devices["123"]
if !ok {
t.Error("not in map")
Expand All @@ -70,24 +70,21 @@ func TestHandler_HandleMessage(t *testing.T) {
Method: lib_model.Set,
DeviceID: "123",
})
err = h.HandleMessage(&mockMessage{
if err != nil {
t.Fatal(err)
}
h.HandleMessage(&mockMessage{
topic: "device-manager/device/test",
payload: p2,
})
if err == nil {
t.Error("expected error")
}
})
t.Run("error", func(t *testing.T) {
mockDHdl := &mockDeviceHdl{PutErr: lib_model.NewInvalidInputError(errors.New("test"))}
mockDHdl := &mockDeviceHdl{PutErr: errors.New("test")}
h := Handler{devicesHdl: mockDHdl}
err = h.HandleMessage(&mockMessage{
h.HandleMessage(&mockMessage{
topic: "device-manager/device/test",
payload: p,
})
if err == nil {
t.Error("expected error")
}
})
})
t.Run("delete device", func(t *testing.T) {
Expand All @@ -100,20 +97,28 @@ func TestHandler_HandleMessage(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_ = h.HandleMessage(&mockMessage{
h.HandleMessage(&mockMessage{
topic: "device-manager/device/test",
payload: p,
})
if mockDHdl.DeleteC != 1 {
t.Error("missing call")
}
t.Run("error", func(t *testing.T) {
mockDHdl := &mockDeviceHdl{DeleteErr: errors.New("test")}
h := Handler{devicesHdl: mockDHdl}
h.HandleMessage(&mockMessage{
topic: "device-manager/device/test",
payload: p,
})
})
})
t.Run("set states", func(t *testing.T) {
mockDHdl := &mockDeviceHdl{
States: make(map[string]lib_model.DeviceState),
}
h := Handler{devicesHdl: mockDHdl}
_ = h.HandleMessage(&mockMessage{
h.HandleMessage(&mockMessage{
topic: "device-manager/device/test/lw",
})
s, ok := mockDHdl.States["test"]
Expand All @@ -123,6 +128,13 @@ func TestHandler_HandleMessage(t *testing.T) {
if s != lib_model.Offline {
t.Error("got", s, "expected", lib_model.Offline)
}
t.Run("error", func(t *testing.T) {
mockDHdl := &mockDeviceHdl{SetStatesErr: errors.New("test")}
h := Handler{devicesHdl: mockDHdl}
h.HandleMessage(&mockMessage{
topic: "device-manager/device/test/lw",
})
})
})
t.Run("unknown method", func(t *testing.T) {
mockDHdl := &mockDeviceHdl{}
Expand All @@ -133,23 +145,17 @@ func TestHandler_HandleMessage(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = h.HandleMessage(&mockMessage{
h.HandleMessage(&mockMessage{
topic: "device-manager/device/test",
payload: p,
})
if err == nil {
t.Error("expected error")
}
})
t.Run("parse topic error", func(t *testing.T) {
mockDHdl := &mockDeviceHdl{}
h := Handler{devicesHdl: mockDHdl}
err := h.HandleMessage(&mockMessage{
h.HandleMessage(&mockMessage{
topic: "test",
})
if err == nil {
t.Error("expected error")
}
})
}

Expand Down Expand Up @@ -196,6 +202,9 @@ func (m *mockDeviceHdl) SetStates(ctx context.Context, ref string, state lib_mod

func (m *mockDeviceHdl) Delete(ctx context.Context, id string) error {
m.DeleteC++
if m.DeleteErr != nil {
return m.DeleteErr
}
return nil
}

Expand Down
8 changes: 1 addition & 7 deletions handler/msg_relay_hdl/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package msg_relay_hdl
import (
"errors"
"github.com/SENERGY-Platform/mgw-device-manager/handler"
"github.com/SENERGY-Platform/mgw-device-manager/util"
)

const logPrefix = "[relay-hdl]"

type Handler struct {
messages chan handler.Message
handleFunc handler.MessageHandler
Expand Down Expand Up @@ -42,10 +39,7 @@ func (h *Handler) Stop() {

func (h *Handler) run() {
for message := range h.messages {
err := h.handleFunc(message)
if err != nil {
util.Logger.Errorf("%s handle message: %s", logPrefix, err)
}
h.handleFunc(message)
}
h.dChan <- struct{}{}
}
30 changes: 2 additions & 28 deletions handler/msg_relay_hdl/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package msg_relay_hdl

import (
"errors"
sb_util "github.com/SENERGY-Platform/go-service-base/util"
"github.com/SENERGY-Platform/mgw-device-manager/handler"
"github.com/SENERGY-Platform/mgw-device-manager/util"
"reflect"
"testing"
"time"
Expand All @@ -25,17 +22,15 @@ func (m *mockMessage) Payload() []byte {
}

func TestHandler(t *testing.T) {
util.InitLogger(sb_util.LoggerConfig{Terminal: true, Level: 4})
msg := &mockMessage{
topic: "test",
payload: []byte("test"),
timestamp: time.Now(),
}
testMsgHdl := func(m handler.Message) error {
testMsgHdl := func(m handler.Message) {
if !reflect.DeepEqual(m, msg) {
t.Error("expected", msg, "got", m)
}
return nil
}
h := New(1, testMsgHdl)
err := h.Put(msg)
Expand All @@ -51,29 +46,8 @@ func TestHandler(t *testing.T) {
t.Error("message not consumed")
}
h.Stop()
t.Run("message handler error", func(t *testing.T) {
testMsgHdl = func(m handler.Message) error {
return errors.New("test error")
}
h = New(1, testMsgHdl)
err = h.Put(msg)
if err != nil {
t.Error(err)
}
if len(h.messages) != 1 {
t.Error("message not in channel")
}
h.Start()
time.Sleep(1 * time.Second)
if len(h.messages) > 0 {
t.Error("message not consumed")
}
h.Stop()
})
t.Run("buffer full", func(t *testing.T) {
testMsgHdl = func(m handler.Message) error {
return nil
}
testMsgHdl = func(m handler.Message) {}
h = New(1, testMsgHdl)
err = h.Put(msg)
if err != nil {
Expand Down

0 comments on commit 0b595e0

Please sign in to comment.