pull all non-request scoped values out of context

This commit is contained in:
Joel Wetzell
2026-05-06 21:33:16 -05:00
parent eb25c73f3a
commit 833bd529d6
98 changed files with 328 additions and 584 deletions
-8
View File
@@ -1,8 +0,0 @@
package common
type contextKey string
const RouterContextKey contextKey = contextKey("router")
const SourceContextKey contextKey = contextKey("source")
const ModulesContextKey contextKey = contextKey("modules")
const SenderContextKey contextKey = contextKey("sender")
+1 -1
View File
@@ -8,7 +8,7 @@ import (
type Module interface { type Module interface {
Id() string Id() string
Type() string Type() string
Start(context.Context) error Start(context.Context, RouteIO) error
Stop() Stop()
} }
+1 -32
View File
@@ -1,40 +1,9 @@
package common package common
import (
"context"
)
type WrappedPayload struct { type WrappedPayload struct {
Payload any Payload any
Router RouteIO
Modules map[string]Module Modules map[string]Module
Sender any
Source string Source string
End bool End bool
} }
func GetWrappedPayload(ctx context.Context, payload any) WrappedPayload {
wrappedPayload := WrappedPayload{
Payload: payload,
End: false,
}
modules := ctx.Value(ModulesContextKey)
if modules != nil {
moduleMap, ok := modules.(map[string]Module)
if ok {
wrappedPayload.Modules = moduleMap
} else {
wrappedPayload.Modules = make(map[string]Module)
}
}
sender := ctx.Value(SenderContextKey)
if sender != nil {
wrappedPayload.Sender = sender
}
source := ctx.Value(SourceContextKey)
if source != nil {
wrappedPayload.Source = source.(string)
}
return wrappedPayload
}
-82
View File
@@ -1,82 +0,0 @@
package common_test
import (
"context"
"reflect"
"testing"
"github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/test"
)
func TestGoodGetWrappedPayload(t *testing.T) {
testCases := []struct {
name string
ctx context.Context
payload any
expected common.WrappedPayload
}{
{
name: "basic",
ctx: t.Context(),
payload: "test",
expected: common.WrappedPayload{
Payload: "test",
},
},
{
name: "with modules in context",
ctx: test.GetContextWithModules(t.Context(), map[string]common.Module{}),
payload: "test",
expected: common.WrappedPayload{
Payload: "test",
Modules: map[string]common.Module{},
},
},
{
name: "with sender in context",
ctx: test.GetContextWithSender(t.Context(), "sender"),
payload: "test",
expected: common.WrappedPayload{
Payload: "test",
Sender: "sender",
},
},
{
name: "with source in context",
ctx: test.GetContextWithSource(t.Context(), "source"),
payload: "test",
expected: common.WrappedPayload{
Payload: "test",
Source: "source",
},
},
{
name: "with all fields in context",
ctx: test.GetContextWithSource(
test.GetContextWithSender(
test.GetContextWithModules(t.Context(), map[string]common.Module{}),
"sender",
),
"source",
),
payload: "test",
expected: common.WrappedPayload{
Payload: "test",
Modules: map[string]common.Module{},
Sender: "sender",
Source: "source",
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
wrappedPayload := common.GetWrappedPayload(testCase.ctx, testCase.payload)
if !reflect.DeepEqual(wrappedPayload, testCase.expected) {
t.Fatalf("GetWrappedPayload expected got %+v, expected %+v", wrappedPayload, testCase.expected)
}
})
}
}
+1 -7
View File
@@ -3,7 +3,6 @@ package module
import ( import (
"context" "context"
"database/sql" "database/sql"
"errors"
"fmt" "fmt"
"log/slog" "log/slog"
@@ -59,13 +58,8 @@ func (t *DbSqlite) Type() string {
return t.config.Type return t.config.Type
} }
func (t *DbSqlite) Start(ctx context.Context) error { func (t *DbSqlite) Start(ctx context.Context, router common.RouteIO) error {
t.logger.Debug("running") t.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("db.sqlite unable to get router from context")
}
t.router = router t.router = router
t.ctx = ctx t.ctx = ctx
+1 -11
View File
@@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"log/slog" "log/slog"
"net"
"net/http" "net/http"
"github.com/google/jsonschema-go/jsonschema" "github.com/google/jsonschema-go/jsonschema"
@@ -99,10 +98,6 @@ func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
if hs.router != nil { if hs.router != nil {
inputContext := context.WithValue(hs.ctx, httpServerContextKey("responseWriter"), &responseWriter) inputContext := context.WithValue(hs.ctx, httpServerContextKey("responseWriter"), &responseWriter)
senderAddr, err := net.ResolveTCPAddr("tcp", r.RemoteAddr)
if err == nil {
inputContext = context.WithValue(inputContext, common.SenderContextKey, senderAddr)
}
aRouteFound, routingErrors := hs.router.HandleInput(inputContext, hs.Id(), r) aRouteFound, routingErrors := hs.router.HandleInput(inputContext, hs.Id(), r)
if !responseWriter.done { if !responseWriter.done {
if aRouteFound { if aRouteFound {
@@ -160,13 +155,8 @@ func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
func (hs *HTTPServer) Start(ctx context.Context) error { func (hs *HTTPServer) Start(ctx context.Context, router common.RouteIO) error {
hs.logger.Debug("running") hs.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("http.server unable to get router from context")
}
hs.router = router hs.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
hs.ctx = moduleContext hs.ctx = moduleContext
+1 -7
View File
@@ -4,7 +4,6 @@ package module
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"log/slog" "log/slog"
@@ -60,14 +59,9 @@ func (mi *MIDIInput) Type() string {
return mi.config.Type return mi.config.Type
} }
func (mi *MIDIInput) Start(ctx context.Context) error { func (mi *MIDIInput) Start(ctx context.Context, router common.RouteIO) error {
mi.logger.Debug("running") mi.logger.Debug("running")
defer midi.CloseDriver() defer midi.CloseDriver()
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("midi.input unable to get router from context")
}
mi.router = router mi.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
mi.ctx = moduleContext mi.ctx = moduleContext
+1 -6
View File
@@ -61,14 +61,9 @@ func (mo *MIDIOutput) Type() string {
return mo.config.Type return mo.config.Type
} }
func (mo *MIDIOutput) Start(ctx context.Context) error { func (mo *MIDIOutput) Start(ctx context.Context, router common.RouteIO) error {
mo.logger.Debug("running") mo.logger.Debug("running")
defer midi.CloseDriver() defer midi.CloseDriver()
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("midi.output unable to get router from context")
}
mo.router = router mo.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
mo.ctx = moduleContext mo.ctx = moduleContext
+1 -6
View File
@@ -80,13 +80,8 @@ func (mc *MQTTClient) Type() string {
return mc.config.Type return mc.config.Type
} }
func (mc *MQTTClient) Start(ctx context.Context) error { func (mc *MQTTClient) Start(ctx context.Context, router common.RouteIO) error {
mc.logger.Debug("running") mc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("mqtt.client unable to get router from context")
}
mc.router = router mc.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
mc.ctx = moduleContext mc.ctx = moduleContext
+1 -7
View File
@@ -68,14 +68,8 @@ func (nc *NATSClient) Type() string {
return nc.config.Type return nc.config.Type
} }
func (nc *NATSClient) Start(ctx context.Context) error { func (nc *NATSClient) Start(ctx context.Context, router common.RouteIO) error {
nc.logger.Debug("running") nc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("nats.client unable to get router from context")
}
nc.router = router nc.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
nc.ctx = moduleContext nc.ctx = moduleContext
+1 -7
View File
@@ -86,14 +86,8 @@ func (ns *NATSServer) Type() string {
return ns.config.Type return ns.config.Type
} }
func (ns *NATSServer) Start(ctx context.Context) error { func (ns *NATSServer) Start(ctx context.Context, router common.RouteIO) error {
ns.logger.Debug("running") ns.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("nats.server unable to get router from context")
}
ns.router = router ns.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
ns.ctx = moduleContext ns.ctx = moduleContext
+1 -7
View File
@@ -2,7 +2,6 @@ package module
import ( import (
"context" "context"
"errors"
"log/slog" "log/slog"
"net" "net"
"time" "time"
@@ -40,13 +39,8 @@ func (pc *PSNClient) Type() string {
return pc.config.Type return pc.config.Type
} }
func (pc *PSNClient) Start(ctx context.Context) error { func (pc *PSNClient) Start(ctx context.Context, router common.RouteIO) error {
pc.logger.Debug("running") pc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("psn.client unable to get router from context")
}
pc.router = router pc.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
pc.ctx = moduleContext pc.ctx = moduleContext
+1 -7
View File
@@ -73,15 +73,9 @@ func (rc *RedisClient) Printf(ctx context.Context, format string, v ...interface
rc.logger.Debug(msg) rc.logger.Debug(msg)
} }
func (rc *RedisClient) Start(ctx context.Context) error { func (rc *RedisClient) Start(ctx context.Context, router common.RouteIO) error {
redis.SetLogger(rc) redis.SetLogger(rc)
rc.logger.Debug("running") rc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("redis.client unable to get router from context")
}
rc.router = router rc.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
rc.ctx = moduleContext rc.ctx = moduleContext
+1 -7
View File
@@ -99,14 +99,8 @@ func (sc *SerialClient) SetupPort() error {
return nil return nil
} }
func (sc *SerialClient) Start(ctx context.Context) error { func (sc *SerialClient) Start(ctx context.Context, router common.RouteIO) error {
sc.logger.Debug("running") sc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("serial.client unable to get router from context")
}
sc.router = router sc.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
sc.ctx = moduleContext sc.ctx = moduleContext
+1 -6
View File
@@ -131,13 +131,8 @@ func (scs *SIPCallServer) Type() string {
return scs.config.Type return scs.config.Type
} }
func (scs *SIPCallServer) Start(ctx context.Context) error { func (scs *SIPCallServer) Start(ctx context.Context, router common.RouteIO) error {
scs.logger.Debug("running") scs.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("sip.call.server unable to get router from context")
}
scs.router = router scs.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
scs.ctx = moduleContext scs.ctx = moduleContext
+1 -6
View File
@@ -150,13 +150,8 @@ func (sds *SIPDTMFServer) Type() string {
return sds.config.Type return sds.config.Type
} }
func (sds *SIPDTMFServer) Start(ctx context.Context) error { func (sds *SIPDTMFServer) Start(ctx context.Context, router common.RouteIO) error {
sds.logger.Debug("running") sds.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("sip.dtmf.server unable to get router from context")
}
sds.router = router sds.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
sds.ctx = moduleContext sds.ctx = moduleContext
+1 -6
View File
@@ -91,13 +91,8 @@ func (tc *TCPClient) Type() string {
return tc.config.Type return tc.config.Type
} }
func (tc *TCPClient) Start(ctx context.Context) error { func (tc *TCPClient) Start(ctx context.Context, router common.RouteIO) error {
tc.logger.Debug("running") tc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.tcp.client unable to get router from context")
}
tc.router = router tc.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
tc.ctx = moduleContext tc.ctx = moduleContext
+3 -9
View File
@@ -166,10 +166,9 @@ ClientRead:
messages := ts.Framer.Decode(buffer[0:byteCount]) messages := ts.Framer.Decode(buffer[0:byteCount])
for _, message := range messages { for _, message := range messages {
if ts.router != nil { if ts.router != nil {
senderAddr, ok := client.RemoteAddr().(*net.TCPAddr) _, ok := client.RemoteAddr().(*net.TCPAddr)
if ok { if ok {
senderCtx := context.WithValue(ts.ctx, common.SenderContextKey, senderAddr) ts.router.HandleInput(ts.ctx, ts.Id(), message)
ts.router.HandleInput(senderCtx, ts.Id(), message)
} else { } else {
ts.router.HandleInput(ts.ctx, ts.Id(), message) ts.router.HandleInput(ts.ctx, ts.Id(), message)
} }
@@ -183,13 +182,8 @@ ClientRead:
} }
} }
func (ts *TCPServer) Start(ctx context.Context) error { func (ts *TCPServer) Start(ctx context.Context, router common.RouteIO) error {
ts.logger.Debug("running") ts.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.tcp.server unable to get router from context")
}
ts.router = router ts.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
ts.ctx = moduleContext ts.ctx = moduleContext
+1 -1
View File
@@ -73,7 +73,7 @@ func TestBadDbSqlite(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("db.sqlite expected to fail") t.Fatalf("db.sqlite expected to fail")
+1 -1
View File
@@ -73,7 +73,7 @@ func TestBadHTTPServer(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("http.server expected to fail") t.Fatalf("http.server expected to fail")
+1 -1
View File
@@ -73,7 +73,7 @@ func TestBadMIDIInput(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("midi.input expected to fail") t.Fatalf("midi.input expected to fail")
+1 -1
View File
@@ -73,7 +73,7 @@ func TestBadMIDIOutput(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("midi.output expected to fail") t.Fatalf("midi.output expected to fail")
+1 -1
View File
@@ -116,7 +116,7 @@ func TestBadMQTTClient(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("mqtt.client expected to fail") t.Fatalf("mqtt.client expected to fail")
+1 -1
View File
@@ -94,7 +94,7 @@ func TestBadNATSClient(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("nats.client expected to fail") t.Fatalf("nats.client expected to fail")
+1 -1
View File
@@ -71,7 +71,7 @@ func TestBadNATSServer(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("nats.server expected to fail") t.Fatalf("nats.server expected to fail")
+1 -1
View File
@@ -59,7 +59,7 @@ func TestBadPSNClient(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("psn.client expected to fail") t.Fatalf("psn.client expected to fail")
+1 -1
View File
@@ -94,7 +94,7 @@ func TestBadRedisClient(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("redis.client expected to fail") t.Fatalf("redis.client expected to fail")
+1 -1
View File
@@ -103,7 +103,7 @@ func TestBadSerialClient(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("serial.client expected to fail") t.Fatalf("serial.client expected to fail")
+1 -1
View File
@@ -88,7 +88,7 @@ func TestBadSIPCallServer(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("sip.call.server expected to fail") t.Fatalf("sip.call.server expected to fail")
+1 -1
View File
@@ -107,7 +107,7 @@ func TestBadSIPDTMFServer(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("sip.dtmf.server expected to fail") t.Fatalf("sip.dtmf.server expected to fail")
+1 -1
View File
@@ -95,7 +95,7 @@ func TestBadTCPClient(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("net.tcp.client expected to fail") t.Fatalf("net.tcp.client expected to fail")
+1 -1
View File
@@ -120,7 +120,7 @@ func TestBadTCPServer(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("net.tcp.server expected to fail") t.Fatalf("net.tcp.server expected to fail")
+1 -1
View File
@@ -75,7 +75,7 @@ func TestBadTimeInterval(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("time.interval expected to fail") t.Fatalf("time.interval expected to fail")
+1 -1
View File
@@ -75,7 +75,7 @@ func TestBadTimeTimer(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("time.timer expected to fail") t.Fatalf("time.timer expected to fail")
+1 -1
View File
@@ -95,7 +95,7 @@ func TestBadUDPClient(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("net.udp.client expected to fail") t.Fatalf("net.udp.client expected to fail")
+1 -1
View File
@@ -102,7 +102,7 @@ func TestBadUDPMulticast(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("net.udp.multicast expected to fail") t.Fatalf("net.udp.multicast expected to fail")
+1 -1
View File
@@ -99,7 +99,7 @@ func TestBadUDPServer(t *testing.T) {
return return
} }
err = moduleInstance.Start(t.Context()) err = moduleInstance.Start(t.Context(), nil)
if err == nil { if err == nil {
t.Fatalf("net.udp.server expected to fail") t.Fatalf("net.udp.server expected to fail")
+1 -7
View File
@@ -2,7 +2,6 @@ package module
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"log/slog" "log/slog"
"time" "time"
@@ -58,13 +57,8 @@ func (i *TimeInterval) Type() string {
return i.config.Type return i.config.Type
} }
func (i *TimeInterval) Start(ctx context.Context) error { func (i *TimeInterval) Start(ctx context.Context, router common.RouteIO) error {
i.logger.Debug("running") i.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("time.interval unable to get router from context")
}
i.router = router i.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
i.ctx = moduleContext i.ctx = moduleContext
+1 -7
View File
@@ -2,7 +2,6 @@ package module
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"log/slog" "log/slog"
"time" "time"
@@ -59,13 +58,8 @@ func (t *TimeTimer) Type() string {
return t.config.Type return t.config.Type
} }
func (t *TimeTimer) Start(ctx context.Context) error { func (t *TimeTimer) Start(ctx context.Context, router common.RouteIO) error {
t.logger.Debug("running") t.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.tcp.client unable to get router from context")
}
t.router = router t.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
t.ctx = moduleContext t.ctx = moduleContext
+1 -6
View File
@@ -79,13 +79,8 @@ func (uc *UDPClient) SetupConn() error {
return err return err
} }
func (uc *UDPClient) Start(ctx context.Context) error { func (uc *UDPClient) Start(ctx context.Context, router common.RouteIO) error {
uc.logger.Debug("running") uc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.udp.client unable to get router from context")
}
uc.router = router uc.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
uc.ctx = moduleContext uc.ctx = moduleContext
+1 -6
View File
@@ -73,13 +73,8 @@ func (um *UDPMulticast) Type() string {
return um.config.Type return um.config.Type
} }
func (um *UDPMulticast) Start(ctx context.Context) error { func (um *UDPMulticast) Start(ctx context.Context, router common.RouteIO) error {
um.logger.Debug("running") um.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.udp.multicast unable to get router from context")
}
um.router = router um.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
um.ctx = moduleContext um.ctx = moduleContext
+3 -9
View File
@@ -95,13 +95,8 @@ func (us *UDPServer) Type() string {
return us.config.Type return us.config.Type
} }
func (us *UDPServer) Start(ctx context.Context) error { func (us *UDPServer) Start(ctx context.Context, router common.RouteIO) error {
us.logger.Debug("running") us.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.udp.server unable to get router from context")
}
us.router = router us.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
us.ctx = moduleContext us.ctx = moduleContext
@@ -124,7 +119,7 @@ func (us *UDPServer) Start(ctx context.Context) error {
default: default:
listener.SetDeadline(time.Now().Add(time.Millisecond * 200)) listener.SetDeadline(time.Now().Add(time.Millisecond * 200))
numBytes, senderAddr, err := listener.ReadFromUDP(buffer) numBytes, _, err := listener.ReadFromUDP(buffer)
if err != nil { if err != nil {
//NOTE(jwetzell) we hit deadline //NOTE(jwetzell) we hit deadline
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() { if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
@@ -134,8 +129,7 @@ func (us *UDPServer) Start(ctx context.Context) error {
} }
message := buffer[:numBytes] message := buffer[:numBytes]
if us.router != nil { if us.router != nil {
senderCtx := context.WithValue(us.ctx, common.SenderContextKey, senderAddr) us.router.HandleInput(us.ctx, us.Id(), message)
us.router.HandleInput(senderCtx, us.Id(), message)
} else { } else {
us.logger.Error("input received but no router is configured") us.logger.Error("input received but no router is configured")
} }
+2 -4
View File
@@ -20,14 +20,12 @@ type RouterInput struct {
func (ro *RouterInput) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { func (ro *RouterInput) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
payload := wrappedPayload.Payload payload := wrappedPayload.Payload
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO) if wrappedPayload.Router == nil {
if !ok {
wrappedPayload.End = true wrappedPayload.End = true
return wrappedPayload, errors.New("router.input no router found") return wrappedPayload, errors.New("router.input no router found")
} }
_, err := router.HandleInput(ctx, ro.SourceId, payload) _, err := wrappedPayload.Router.HandleInput(ctx, ro.SourceId, payload)
if err != nil { if err != nil {
wrappedPayload.End = true wrappedPayload.End = true
+2 -3
View File
@@ -19,13 +19,12 @@ type RouterOutput struct {
func (ro *RouterOutput) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { func (ro *RouterOutput) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO) if wrappedPayload.Router == nil {
if !ok {
wrappedPayload.End = true wrappedPayload.End = true
return wrappedPayload, errors.New("router.output no router found") return wrappedPayload, errors.New("router.output no router found")
} }
err := router.HandleOutput(ctx, ro.ModuleId, wrappedPayload.Payload) err := wrappedPayload.Router.HandleOutput(ctx, ro.ModuleId, wrappedPayload.Payload)
if err != nil { if err != nil {
wrappedPayload.End = true wrappedPayload.End = true
-2
View File
@@ -34,8 +34,6 @@ func (sj *ScriptJS) Process(ctx context.Context, wrappedPayload common.WrappedPa
sj.vm.SetProperty(sj.vm.GlobalObject(), sj.payloadAtom, wrappedPayload.Payload) sj.vm.SetProperty(sj.vm.GlobalObject(), sj.payloadAtom, wrappedPayload.Payload)
sj.vm.SetProperty(sj.vm.GlobalObject(), sj.senderAtom, wrappedPayload.Sender)
_, err := sj.vm.Eval(sj.Program, quickjs.EvalGlobal) _, err := sj.vm.Eval(sj.Program, quickjs.EvalGlobal)
if err != nil { if err != nil {
@@ -58,7 +58,7 @@ func TestGoodArtnetPacketDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := packetDecoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := packetDecoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("artnet.packet.decode processing failed: %s", err) t.Fatalf("artnet.packet.decode processing failed: %s", err)
@@ -94,7 +94,7 @@ func TestBadArtnetPacketDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := packetDecoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := packetDecoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("artnet.packet.decode expected to fail but succeeded, got: %v", got) t.Fatalf("artnet.packet.decode expected to fail but succeeded, got: %v", got)
@@ -58,7 +58,7 @@ func TestGoodArtnetPacketEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := packetEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("artnet.packet.encode processing failed: %s", err) t.Fatalf("artnet.packet.encode processing failed: %s", err)
@@ -89,7 +89,7 @@ func TestBadArtnetPacketEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := packetEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("artnet.packet.encode expected to fail but succeeded, got: %v", got) t.Fatalf("artnet.packet.encode expected to fail but succeeded, got: %v", got)
+39 -37
View File
@@ -1,7 +1,6 @@
package processor_test package processor_test
import ( import (
"context"
"reflect" "reflect"
"testing" "testing"
@@ -36,12 +35,12 @@ func TestDbQueryFromRegistry(t *testing.T) {
payload := "hello" payload := "hello"
expected := map[string]any{"sqlite_version()": "3.53.0"} expected := map[string]any{"sqlite_version()": "3.53.0"}
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
t.Context(), Payload: payload,
map[string]common.Module{ Modules: map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}, },
), payload)) })
if err != nil { if err != nil {
t.Fatalf("db.query processing failed: %s", err) t.Fatalf("db.query processing failed: %s", err)
} }
@@ -115,12 +114,12 @@ func TestGoodDbQuery(t *testing.T) {
t.Fatalf("db.query failed to create processor: %s", err) t.Fatalf("db.query failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
t.Context(), Modules: map[string]common.Module{
map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}, },
), testCase.payload)) Payload: testCase.payload,
})
if err != nil { if err != nil {
t.Fatalf("db.query processing failed: %s", err) t.Fatalf("db.query processing failed: %s", err)
@@ -135,11 +134,11 @@ func TestGoodDbQuery(t *testing.T) {
func TestBadDbQuery(t *testing.T) { func TestBadDbQuery(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
wrappedPayloadCtx context.Context wrappedPayloadModules map[string]common.Module
errorString string errorString string
}{ }{
{ {
name: "no module param", name: "no module param",
@@ -147,9 +146,9 @@ func TestBadDbQuery(t *testing.T) {
params: map[string]any{ params: map[string]any{
"query": "SELECT sqlite_version();", "query": "SELECT sqlite_version();",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), },
errorString: "db.query module error: not found", errorString: "db.query module error: not found",
}, },
{ {
@@ -159,9 +158,9 @@ func TestBadDbQuery(t *testing.T) {
"module": 1, "module": 1,
"query": "SELECT sqlite_version();", "query": "SELECT sqlite_version();",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), },
errorString: "db.query module error: not a string", errorString: "db.query module error: not a string",
}, },
{ {
@@ -170,9 +169,9 @@ func TestBadDbQuery(t *testing.T) {
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), },
errorString: "db.query query error: not found", errorString: "db.query query error: not found",
}, },
{ {
@@ -182,9 +181,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": 1, "query": 1,
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), },
errorString: "db.query query error: not a string", errorString: "db.query query error: not a string",
}, },
{ {
@@ -194,9 +193,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": "select * from {{", "query": "select * from {{",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), },
errorString: "template: query:1: unclosed action", errorString: "template: query:1: unclosed action",
}, },
{ {
@@ -206,9 +205,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": "select * from {{.Data}}", "query": "select * from {{.Data}}",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), },
errorString: "template: query:1:16: executing \"query\" at <.Data>: can't evaluate field Data in type common.WrappedPayload", errorString: "template: query:1:16: executing \"query\" at <.Data>: can't evaluate field Data in type common.WrappedPayload",
}, },
{ {
@@ -218,9 +217,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": "select * from asdf;", "query": "select * from asdf;",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), },
errorString: "db.query error executing query: SQL logic error: no such table: asdf (1)", errorString: "db.query error executing query: SQL logic error: no such table: asdf (1)",
}, },
{ {
@@ -230,8 +229,8 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": "select * from test;", "query": "select * from test;",
}, },
wrappedPayloadCtx: t.Context(), wrappedPayloadModules: nil,
errorString: "db.query wrapped payload has no modules", errorString: "db.query wrapped payload has no modules",
}, },
{ {
name: "module not found in context", name: "module not found in context",
@@ -240,8 +239,8 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": "select * from test;", "query": "select * from test;",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{}), wrappedPayloadModules: map[string]common.Module{},
errorString: "db.query unable to find module with id: test", errorString: "db.query unable to find module with id: test",
}, },
{ {
name: "module not found in context", name: "module not found in context",
@@ -250,8 +249,8 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": "select * from test;", "query": "select * from test;",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{}), wrappedPayloadModules: map[string]common.Module{},
errorString: "db.query unable to find module with id: test", errorString: "db.query unable to find module with id: test",
}, },
{ {
name: "module not a DatabseModule", name: "module not a DatabseModule",
@@ -260,9 +259,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": "select * from test;", "query": "select * from test;",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}), },
errorString: "db.query module with id test is not a DatabaseModule", errorString: "db.query module with id test is not a DatabaseModule",
}, },
} }
@@ -287,7 +286,10 @@ func TestBadDbQuery(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.wrappedPayloadCtx, test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
Payload: test.payload,
Modules: test.wrappedPayloadModules,
})
if err == nil { if err == nil {
t.Fatalf("db.query expected to fail but got payload: %+v", got) t.Fatalf("db.query expected to fail but got payload: %+v", got)
+3 -3
View File
@@ -30,7 +30,7 @@ func TestDebugLogFromRegistry(t *testing.T) {
payload := "test" payload := "test"
expected := "test" expected := "test"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("debug.log processing failed: %s", err) t.Fatalf("debug.log processing failed: %s", err)
} }
@@ -66,7 +66,7 @@ func TestGoodDebugLog(t *testing.T) {
t.Fatalf("debug.log failed to create processor: %s", err) t.Fatalf("debug.log failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("debug.log processing failed: %s", err) t.Fatalf("debug.log processing failed: %s", err)
} }
@@ -106,7 +106,7 @@ func TestBadDebugLog(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("debug.log expected to fail but succeeded, got: %v", got) t.Fatalf("debug.log expected to fail but succeeded, got: %v", got)
@@ -29,7 +29,9 @@ func TestFilterChangeFromRegistry(t *testing.T) {
payload := "hello" payload := "hello"
expected := "hello" expected := "hello"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
Payload: payload,
})
if err != nil { if err != nil {
t.Fatalf("filter.change processing failed: %s", err) t.Fatalf("filter.change processing failed: %s", err)
} }
@@ -70,7 +72,7 @@ func TestGoodFilterChange(t *testing.T) {
t.Fatalf("filter.change failed to create processor: %s", err) t.Fatalf("filter.change failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("filter.change processing failed: %s", err) t.Fatalf("filter.change processing failed: %s", err)
@@ -110,7 +112,7 @@ func TestBadFilterChange(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("filter.change expected to fail but got payload: %+v", got) t.Fatalf("filter.change expected to fail but got payload: %+v", got)
+2 -2
View File
@@ -85,7 +85,7 @@ func TestGoodFilterExpr(t *testing.T) {
t.Fatalf("filter.expr failed to create processor: %s", err) t.Fatalf("filter.expr failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), testCase.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: testCase.payload})
if err != nil { if err != nil {
t.Fatalf("filter.expr processing failed: %s", err) t.Fatalf("filter.expr processing failed: %s", err)
@@ -159,7 +159,7 @@ func TestBadFilterExpr(t *testing.T) {
} }
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("filter.expr expected to fail but succeeded, got: %v", got) t.Fatalf("filter.expr expected to fail but succeeded, got: %v", got)
+3 -3
View File
@@ -31,7 +31,7 @@ func TestFilterRegexFromRegistry(t *testing.T) {
payload := "hello" payload := "hello"
expected := "hello" expected := "hello"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("filter.regex processing failed: %s", err) t.Fatalf("filter.regex processing failed: %s", err)
} }
@@ -90,7 +90,7 @@ func TestGoodFilterRegex(t *testing.T) {
t.Fatalf("filter.regex failed to create processor: %s", err) t.Fatalf("filter.regex failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("filter.regex processing failed: %s", err) t.Fatalf("filter.regex processing failed: %s", err)
@@ -161,7 +161,7 @@ func TestBadFilterRegex(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("filter.regex expected to fail but got payload: %+v", got) t.Fatalf("filter.regex expected to fail but got payload: %+v", got)
+2 -2
View File
@@ -76,7 +76,7 @@ func TestGoodFloatParse(t *testing.T) {
t.Fatalf("float.parse failed to create processor: %s", err) t.Fatalf("float.parse failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("float.parse processing failed: %s", err) t.Fatalf("float.parse processing failed: %s", err)
} }
@@ -152,7 +152,7 @@ func TestBadFloatParse(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("float.parse expected to fail but succeeded, got: %v", got) t.Fatalf("float.parse expected to fail but succeeded, got: %v", got)
+2 -2
View File
@@ -71,7 +71,7 @@ func TestGoodFloatRandom(t *testing.T) {
t.Fatalf("float.random failed to create processor: %s", err) t.Fatalf("float.random failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("float.random processing failed: %s", err) t.Fatalf("float.random processing failed: %s", err)
} }
@@ -172,7 +172,7 @@ func TestBadFloatRandom(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("float.random expected to fail but got payload: %+v", got) t.Fatalf("float.random expected to fail but got payload: %+v", got)
@@ -103,7 +103,7 @@ func TestGoodFreeDCreate(t *testing.T) {
t.Fatalf("freed.create failed to create processor: %s", err) t.Fatalf("freed.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("freed.create processing failed: %s", err) t.Fatalf("freed.create processing failed: %s", err)
} }
@@ -865,7 +865,7 @@ func TestBadFreeDCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("freed.create expected to fail but succeeded, got: %v", got) t.Fatalf("freed.create expected to fail but succeeded, got: %v", got)
@@ -59,7 +59,7 @@ func TestGoodFreeDDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := packetEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("freed.decode processing failed: %s", err) t.Fatalf("freed.decode processing failed: %s", err)
@@ -90,7 +90,7 @@ func TestBadFreeDDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := packetEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("freed.decode expected to fail but succeeded, got: %v", got) t.Fatalf("freed.decode expected to fail but succeeded, got: %v", got)
@@ -59,7 +59,7 @@ func TestGoodFreeDEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := packetEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("freed.encode processing failed: %s", err) t.Fatalf("freed.encode processing failed: %s", err)
@@ -90,7 +90,7 @@ func TestBadFreeDEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := packetEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("freed.encode expected to fail but succeeded, got: %v", got) t.Fatalf("freed.encode expected to fail but succeeded, got: %v", got)
@@ -58,7 +58,7 @@ func TestGoodHTTPRequestDo(t *testing.T) {
t.Fatalf("http.request.do failed to create processor: %s", err) t.Fatalf("http.request.do failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("http.request.do processing failed: %s", err) t.Fatalf("http.request.do processing failed: %s", err)
} }
@@ -149,7 +149,7 @@ func TestBadHTTPRequestDo(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("http.request.do expected to fail but succeeded, got: %v", got) t.Fatalf("http.request.do expected to fail but succeeded, got: %v", got)
@@ -68,7 +68,7 @@ func TestGoodHTTPResponseCreate(t *testing.T) {
t.Fatalf("http.response.create failed to create processor: %s", err) t.Fatalf("http.response.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("http.response.create processing failed: %s", err) t.Fatalf("http.response.create processing failed: %s", err)
} }
@@ -145,7 +145,7 @@ func TestBadHTTPResponseCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("http.response.create expected to fail but succeeded, got: %v", got) t.Fatalf("http.response.create expected to fail but succeeded, got: %v", got)
+2 -2
View File
@@ -97,7 +97,7 @@ func TestGoodIntParse(t *testing.T) {
t.Fatalf("int.parse failed to create processor: %s", err) t.Fatalf("int.parse failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("int.parse processing failed: %s", err) t.Fatalf("int.parse processing failed: %s", err)
} }
@@ -186,7 +186,7 @@ func TestBadIntParse(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("int.parse expected to fail but succeeded, got: %v", got) t.Fatalf("int.parse expected to fail but succeeded, got: %v", got)
+3 -3
View File
@@ -51,7 +51,7 @@ func TestIntRandomGoodConfig(t *testing.T) {
payload := "12345" payload := "12345"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("int.random processing failed: %s", err) t.Fatalf("int.random processing failed: %s", err)
} }
@@ -98,7 +98,7 @@ func TestGoodIntRandom(t *testing.T) {
t.Fatalf("int.random failed to create processor: %s", err) t.Fatalf("int.random failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("int.random processing failed: %s", err) t.Fatalf("int.random processing failed: %s", err)
} }
@@ -183,7 +183,7 @@ func TestBadIntRandom(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("int.random expected to fail but got payload: %+v", got) t.Fatalf("int.random expected to fail but got payload: %+v", got)
+2 -2
View File
@@ -69,7 +69,7 @@ func TestGoodIntScale(t *testing.T) {
t.Fatalf("int.scale failed to create processor: %s", err) t.Fatalf("int.scale failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("int.scale processing failed: %s", err) t.Fatalf("int.scale processing failed: %s", err)
} }
@@ -157,7 +157,7 @@ func TestBadIntScale(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("int.scale expected to fail but got payload: %+v", got) t.Fatalf("int.scale expected to fail but got payload: %+v", got)
+3 -3
View File
@@ -32,7 +32,7 @@ func TestJsonDecodeFromRegistry(t *testing.T) {
"property": "hello", "property": "hello",
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("json.decode processing failed: %s", err) t.Fatalf("json.decode processing failed: %s", err)
} }
@@ -75,7 +75,7 @@ func TestGoodJsonDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := jsonDecoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := jsonDecoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("json.decode processing failed: %s", err) t.Fatalf("json.decode processing failed: %s", err)
} }
@@ -113,7 +113,7 @@ func TestBadJsonDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := stringEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := stringEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("json.decode expected to fail but got payload: %+v", got) t.Fatalf("json.decode expected to fail but got payload: %+v", got)
+3 -3
View File
@@ -35,7 +35,7 @@ func TestJsonEncodeFromRegistry(t *testing.T) {
expected := []byte("{\"property\":\"hello\"}") expected := []byte("{\"property\":\"hello\"}")
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("json.encode processing failed: %s", err) t.Fatalf("json.encode processing failed: %s", err)
} }
@@ -69,7 +69,7 @@ func TestGoodJsonEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := jsonEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := jsonEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("json.encode processing failed: %s", err) t.Fatalf("json.encode processing failed: %s", err)
} }
@@ -102,7 +102,7 @@ func TestBadJsonEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := stringEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := stringEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("json.encode expected to fail but got payload: %+v", got) t.Fatalf("json.encode expected to fail but got payload: %+v", got)
+28 -29
View File
@@ -1,7 +1,6 @@
package processor_test package processor_test
import ( import (
"context"
"reflect" "reflect"
"testing" "testing"
@@ -35,12 +34,12 @@ func TestKvGetFromRegistry(t *testing.T) {
payload := "hello" payload := "hello"
expected := "test" expected := "test"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
t.Context(), Modules: map[string]common.Module{
map[string]common.Module{
"test": test.NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}, },
), payload)) Payload: payload,
})
if err != nil { if err != nil {
t.Fatalf("kv.get processing failed: %s", err) t.Fatalf("kv.get processing failed: %s", err)
} }
@@ -95,12 +94,12 @@ func TestGoodKvGet(t *testing.T) {
t.Fatalf("kv.get failed to create processor: %s", err) t.Fatalf("kv.get failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
t.Context(), Modules: map[string]common.Module{
map[string]common.Module{
"test": test.NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}, },
), testCase.payload)) Payload: testCase.payload,
})
if err != nil { if err != nil {
t.Fatalf("kv.get processing failed: %s", err) t.Fatalf("kv.get processing failed: %s", err)
@@ -115,11 +114,11 @@ func TestGoodKvGet(t *testing.T) {
func TestBadKvGet(t *testing.T) { func TestBadKvGet(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
wrappedPayloadCtx context.Context wrappedPayloadModules map[string]common.Module
errorString string errorString string
}{ }{
{ {
name: "no module param", name: "no module param",
@@ -127,9 +126,9 @@ func TestBadKvGet(t *testing.T) {
params: map[string]any{ params: map[string]any{
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}), },
errorString: "kv.get module error: not found", errorString: "kv.get module error: not found",
}, },
{ {
@@ -139,9 +138,9 @@ func TestBadKvGet(t *testing.T) {
"module": 1, "module": 1,
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}), },
errorString: "kv.get module error: not a string", errorString: "kv.get module error: not a string",
}, },
{ {
@@ -150,9 +149,9 @@ func TestBadKvGet(t *testing.T) {
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}), },
errorString: "kv.get key error: not found", errorString: "kv.get key error: not found",
}, },
{ {
@@ -162,9 +161,9 @@ func TestBadKvGet(t *testing.T) {
"module": "test", "module": "test",
"key": 1, "key": 1,
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}), },
errorString: "kv.get key error: not a string", errorString: "kv.get key error: not a string",
}, },
{ {
@@ -174,8 +173,8 @@ func TestBadKvGet(t *testing.T) {
"module": "test", "module": "test",
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: t.Context(), wrappedPayloadModules: nil,
errorString: "kv.get wrapped payload has no modules", errorString: "kv.get wrapped payload has no modules",
}, },
{ {
name: "module not found in context", name: "module not found in context",
@@ -184,8 +183,8 @@ func TestBadKvGet(t *testing.T) {
"module": "test", "module": "test",
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{}), wrappedPayloadModules: map[string]common.Module{},
errorString: "kv.get unable to find module with id: test", errorString: "kv.get unable to find module with id: test",
}, },
{ {
name: "module not a kv module", name: "module not a kv module",
@@ -194,9 +193,9 @@ func TestBadKvGet(t *testing.T) {
"module": "test", "module": "test",
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), },
errorString: "kv.get module with id test is not a KeyValueModule", errorString: "kv.get module with id test is not a KeyValueModule",
}, },
} }
@@ -221,7 +220,7 @@ func TestBadKvGet(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.wrappedPayloadCtx, test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Modules: test.wrappedPayloadModules, Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("kv.get expected to fail but got payload: %+v", got) t.Fatalf("kv.get expected to fail but got payload: %+v", got)
+36 -37
View File
@@ -1,7 +1,6 @@
package processor_test package processor_test
import ( import (
"context"
"reflect" "reflect"
"testing" "testing"
@@ -36,12 +35,12 @@ func TestKvSetFromRegistry(t *testing.T) {
payload := "" payload := ""
expected := "" expected := ""
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
t.Context(), Modules: map[string]common.Module{
map[string]common.Module{
"test": &test.TestKVModule{}, "test": &test.TestKVModule{},
}, },
), payload)) Payload: payload,
})
if err != nil { if err != nil {
t.Fatalf("kv.set processing failed: %s", err) t.Fatalf("kv.set processing failed: %s", err)
} }
@@ -86,12 +85,12 @@ func TestGoodKvSet(t *testing.T) {
t.Fatalf("kv.set failed to create processor: %s", err) t.Fatalf("kv.set failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
t.Context(), Modules: map[string]common.Module{
map[string]common.Module{
"test": &test.TestKVModule{}, "test": &test.TestKVModule{},
}, },
), testCase.payload)) Payload: testCase.payload,
})
if err != nil { if err != nil {
t.Fatalf("kv.set processing failed: %s", err) t.Fatalf("kv.set processing failed: %s", err)
@@ -106,11 +105,11 @@ func TestGoodKvSet(t *testing.T) {
func TestBadKvSet(t *testing.T) { func TestBadKvSet(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
wrappedPayloadCtx context.Context wrappedPayloadModules map[string]common.Module
errorString string errorString string
}{ }{
{ {
name: "no module param", name: "no module param",
@@ -119,9 +118,9 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": &test.TestKVModule{}, "test": &test.TestKVModule{},
}), },
errorString: "kv.set module error: not found", errorString: "kv.set module error: not found",
}, },
{ {
@@ -132,9 +131,9 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": &test.TestKVModule{}, "test": &test.TestKVModule{},
}), },
errorString: "kv.set module error: not a string", errorString: "kv.set module error: not a string",
}, },
{ {
@@ -144,9 +143,9 @@ func TestBadKvSet(t *testing.T) {
"module": "test", "module": "test",
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": &test.TestKVModule{}, "test": &test.TestKVModule{},
}), },
errorString: "kv.set key error: not found", errorString: "kv.set key error: not found",
}, },
{ {
@@ -157,9 +156,9 @@ func TestBadKvSet(t *testing.T) {
"key": 1, "key": 1,
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": &test.TestKVModule{}, "test": &test.TestKVModule{},
}), },
errorString: "kv.set key error: not a string", errorString: "kv.set key error: not a string",
}, },
{ {
@@ -169,9 +168,9 @@ func TestBadKvSet(t *testing.T) {
"module": "test", "module": "test",
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": &test.TestKVModule{}, "test": &test.TestKVModule{},
}), },
errorString: "kv.set value error: not found", errorString: "kv.set value error: not found",
}, },
{ {
@@ -182,9 +181,9 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": 1, "value": 1,
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": &test.TestKVModule{}, "test": &test.TestKVModule{},
}), },
errorString: "kv.set value error: not a string", errorString: "kv.set value error: not a string",
}, },
{ {
@@ -195,8 +194,8 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": "hello", "value": "hello",
}, },
wrappedPayloadCtx: t.Context(), wrappedPayloadModules: nil,
errorString: "kv.set wrapped payload has no modules", errorString: "kv.set wrapped payload has no modules",
}, },
{ {
name: "value template syntax error", name: "value template syntax error",
@@ -206,9 +205,9 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": "{{", "value": "{{",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": &test.TestKVModule{}, "test": &test.TestKVModule{},
}), },
errorString: "template: template:1: unclosed action", errorString: "template: template:1: unclosed action",
}, },
{ {
@@ -219,9 +218,9 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": "{{.Data}}", "value": "{{.Data}}",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": &test.TestKVModule{}, "test": &test.TestKVModule{},
}), },
errorString: "template: template:1:2: executing \"template\" at <.Data>: can't evaluate field Data in type common.WrappedPayload", errorString: "template: template:1:2: executing \"template\" at <.Data>: can't evaluate field Data in type common.WrappedPayload",
}, },
{ {
@@ -232,8 +231,8 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": "hello", "value": "hello",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{}), wrappedPayloadModules: map[string]common.Module{},
errorString: "kv.set unable to find module with id: test", errorString: "kv.set unable to find module with id: test",
}, },
{ {
name: "module not a kv module", name: "module not a kv module",
@@ -243,9 +242,9 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": "hello", "value": "hello",
}, },
wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadModules: map[string]common.Module{
"test": test.NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), },
errorString: "kv.set module with id test is not a KeyValueModule", errorString: "kv.set module with id test is not a KeyValueModule",
}, },
} }
@@ -270,7 +269,7 @@ func TestBadKvSet(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(testCase.wrappedPayloadCtx, testCase.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Modules: testCase.wrappedPayloadModules, Payload: testCase.payload})
if err == nil { if err == nil {
t.Fatalf("kv.set expected to fail but got payload: %+v", got) t.Fatalf("kv.set expected to fail but got payload: %+v", got)
@@ -71,7 +71,7 @@ func TestGoodMIDIControlChangeCreate(t *testing.T) {
t.Fatalf("midi.control_change.create failed to create processor: %s", err) t.Fatalf("midi.control_change.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("midi.control_change.create processing failed: %s", err) t.Fatalf("midi.control_change.create processing failed: %s", err)
} }
@@ -147,7 +147,7 @@ func TestBadMIDIControlChangeCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("midi.control_change.create expected to fail but succeeded, got: %v", got) t.Fatalf("midi.control_change.create expected to fail but succeeded, got: %v", got)
@@ -45,7 +45,7 @@ func TestGoodMIDIMessageDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("midi.message.decode processing failed: %s", err) t.Fatalf("midi.message.decode processing failed: %s", err)
} }
@@ -79,7 +79,7 @@ func TestBadMIDIMessageDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("midi.message.decode expected to fail but succeeded, got: %v", got) t.Fatalf("midi.message.decode expected to fail but succeeded, got: %v", got)
@@ -45,7 +45,7 @@ func TestGoodMIDIMessageEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := midiMessageEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := midiMessageEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("midi.message.encode processing failed: %s", err) t.Fatalf("midi.message.encode processing failed: %s", err)
} }
@@ -78,7 +78,7 @@ func TestBadMIDIMessageEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := midiMessageEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := midiMessageEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("midi.message.encode expected to fail but got payload: %+v", got) t.Fatalf("midi.message.encode expected to fail but got payload: %+v", got)
@@ -85,7 +85,7 @@ func TestGoodMIDIMessageUnpack(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("midi.message.unpack processing failed: %s", err) t.Fatalf("midi.message.unpack processing failed: %s", err)
} }
@@ -138,7 +138,7 @@ func TestBadMIDIMessageUnpack(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("midi.message.unpack expected to fail but succeeded, got: %v", got) t.Fatalf("midi.message.unpack expected to fail but succeeded, got: %v", got)
@@ -70,7 +70,7 @@ func TestGoodMIDINoteOffCretea(t *testing.T) {
t.Fatalf("midi.note_off.create failed to create processor: %s", err) t.Fatalf("midi.note_off.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("midi.note_off.create processing failed: %s", err) t.Fatalf("midi.note_off.create processing failed: %s", err)
} }
@@ -146,7 +146,7 @@ func TestBadMIDINoteOffCretea(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("midi.note_off.create expected to fail but succeeded, got: %v", got) t.Fatalf("midi.note_off.create expected to fail but succeeded, got: %v", got)
@@ -70,7 +70,7 @@ func TestGoodMIDINoteOnCreate(t *testing.T) {
t.Fatalf("midi.note_on.create failed to create processor: %s", err) t.Fatalf("midi.note_on.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("midi.note_on.create processing failed: %s", err) t.Fatalf("midi.note_on.create processing failed: %s", err)
} }
@@ -143,7 +143,7 @@ func TestBadMIDINoteOnCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("midi.note_on.create expected to fail but succeeded, got: %v", got) t.Fatalf("midi.note_on.create expected to fail but succeeded, got: %v", got)
@@ -69,7 +69,7 @@ func TestGoodMIDIProgramChangeCreate(t *testing.T) {
t.Fatalf("midi.program_change.create failed to create processor: %s", err) t.Fatalf("midi.program_change.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("midi.program_change.create processing failed: %s", err) t.Fatalf("midi.program_change.create processing failed: %s", err)
} }
@@ -133,7 +133,7 @@ func TestBadMIDIProgramChangeCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("midi.program_change.create expected to fail but succeeded, got: %v", got) t.Fatalf("midi.program_change.create expected to fail but succeeded, got: %v", got)
@@ -104,7 +104,7 @@ func TestGoodMQTTMessageCreate(t *testing.T) {
t.Fatalf("mqtt.message.create failed to create processor: %s", err) t.Fatalf("mqtt.message.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("mqtt.message.create processing failed: %s", err) t.Fatalf("mqtt.message.create processing failed: %s", err)
@@ -228,7 +228,7 @@ func TestBadMQTTMessageCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("mqtt.message.create expected to fail but succeeded, got: %v", got) t.Fatalf("mqtt.message.create expected to fail but succeeded, got: %v", got)
@@ -85,7 +85,7 @@ func TestGoodNATSMessageCreate(t *testing.T) {
t.Fatalf("nats.message.create failed to create processor: %s", err) t.Fatalf("nats.message.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("nats.message.create processing failed: %s", err) t.Fatalf("nats.message.create processing failed: %s", err)
} }
@@ -196,7 +196,7 @@ func TestBadNATSMessageCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("nats.message.create expected to fail but succeeded, got: %v", got) t.Fatalf("nats.message.create expected to fail but succeeded, got: %v", got)
@@ -160,7 +160,7 @@ func TestGoodOSCMessageCreate(t *testing.T) {
t.Fatalf("osc.message.create failed to create processor: %s", err) t.Fatalf("osc.message.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("osc.message.create processing failed: %s", err) t.Fatalf("osc.message.create processing failed: %s", err)
@@ -388,7 +388,7 @@ func TestBadOSCMessageCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("osc.message.create expected to fail but succeeded, got: %v", got) t.Fatalf("osc.message.create expected to fail but succeeded, got: %v", got)
@@ -61,7 +61,7 @@ func TestGoodOSCMessageDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("osc.message.decode processing failed: %s", err) t.Fatalf("osc.message.decode processing failed: %s", err)
@@ -110,7 +110,7 @@ func TestBadOSCMessageDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("osc.message.decode expected to fail but got payload: %+v", got) t.Fatalf("osc.message.decode expected to fail but got payload: %+v", got)
@@ -60,7 +60,7 @@ func TestGoodOSCMessageEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("osc.message.encode processing failed: %s", err) t.Fatalf("osc.message.encode processing failed: %s", err)
} }
@@ -101,7 +101,7 @@ func TestBadOSCMessageEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("osc.message.encode expected to fail but got payload: %+v", got) t.Fatalf("osc.message.encode expected to fail but got payload: %+v", got)
+23 -24
View File
@@ -1,7 +1,6 @@
package processor_test package processor_test
import ( import (
"context"
"reflect" "reflect"
"testing" "testing"
@@ -35,7 +34,10 @@ func TestRouterOutputFromRegistry(t *testing.T) {
payload := "test" payload := "test"
expected := "test" expected := "test"
got, err := processorInstance.Process(test.GetContextWithRouter(t.Context()), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
Router: test.GetNewTestRouter(),
Payload: payload,
})
if err != nil { if err != nil {
t.Fatalf("router.output processing failed: %s", err) t.Fatalf("router.output processing failed: %s", err)
} }
@@ -71,7 +73,7 @@ func TestGoodRouterOutput(t *testing.T) {
t.Fatalf("router.output failed to create processor: %s", err) t.Fatalf("router.output failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithRouter(t.Context()), testCase.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: testCase.payload})
if err != nil { if err != nil {
t.Fatalf("router.output processing failed: %s", err) t.Fatalf("router.output processing failed: %s", err)
} }
@@ -85,40 +87,37 @@ func TestGoodRouterOutput(t *testing.T) {
func TestBadRouterOutput(t *testing.T) { func TestBadRouterOutput(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
processCtx context.Context router common.RouteIO
wrappedPayloadCtx context.Context errorString string
errorString string
}{ }{
{ {
name: "no module param", name: "no module param",
params: map[string]any{}, params: map[string]any{},
payload: "test", payload: "test",
processCtx: test.GetContextWithRouter(t.Context()), router: test.GetNewTestRouter(),
wrappedPayloadCtx: t.Context(), errorString: "router.output module error: not found",
errorString: "router.output module error: not found",
}, },
{ {
name: "non-string module", name: "non-string module",
params: map[string]any{ params: map[string]any{
"module": 123, "module": 123,
}, },
payload: "test", payload: "test",
processCtx: test.GetContextWithRouter(t.Context()), router: test.GetNewTestRouter(),
wrappedPayloadCtx: t.Context(),
errorString: "router.output module error: not a string", errorString: "router.output module error: not a string",
}, },
{ {
name: "router not found in context", name: "router not found in context",
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
}, },
payload: "test", payload: "test",
processCtx: t.Context(), router: nil,
wrappedPayloadCtx: t.Context(), errorString: "router.output no router found",
errorString: "router.output no router found",
}, },
} }
@@ -142,7 +141,7 @@ func TestBadRouterOutput(t *testing.T) {
return return
} }
got, err := processorInstance.Process(testCase.processCtx, common.GetWrappedPayload(testCase.wrappedPayloadCtx, testCase.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Router: testCase.router, Payload: testCase.payload})
if err == nil { if err == nil {
t.Fatalf("router.output expected to fail but succeeded, got: %v", got) t.Fatalf("router.output expected to fail but succeeded, got: %v", got)
+22 -24
View File
@@ -1,7 +1,6 @@
package processor_test package processor_test
import ( import (
"context"
"reflect" "reflect"
"testing" "testing"
@@ -35,7 +34,10 @@ func TestRouterInputFromRegistry(t *testing.T) {
payload := "test" payload := "test"
expected := "test" expected := "test"
got, err := processorInstance.Process(test.GetContextWithRouter(t.Context()), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
Router: test.GetNewTestRouter(),
Payload: payload,
})
if err != nil { if err != nil {
t.Fatalf("router.input processing failed: %s", err) t.Fatalf("router.input processing failed: %s", err)
} }
@@ -71,7 +73,7 @@ func TestGoodRouterInput(t *testing.T) {
t.Fatalf("router.input failed to create processor: %s", err) t.Fatalf("router.input failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithRouter(t.Context()), testCase.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: testCase.payload})
if err != nil { if err != nil {
t.Fatalf("router.input processing failed: %s", err) t.Fatalf("router.input processing failed: %s", err)
} }
@@ -85,40 +87,36 @@ func TestGoodRouterInput(t *testing.T) {
func TestBadRouterInput(t *testing.T) { func TestBadRouterInput(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
processCtx context.Context router common.RouteIO
wrappedPayloadCtx context.Context errorString string
errorString string
}{ }{
{ {
name: "no source param", name: "no source param",
params: map[string]any{}, params: map[string]any{},
payload: "test", payload: "test",
processCtx: test.GetContextWithRouter(t.Context()), router: test.GetNewTestRouter(),
wrappedPayloadCtx: t.Context(), errorString: "router.input source error: not found",
errorString: "router.input source error: not found",
}, },
{ {
name: "non-string source", name: "non-string source",
params: map[string]any{ params: map[string]any{
"source": 123, "source": 123,
}, },
payload: "test", payload: "test",
processCtx: test.GetContextWithRouter(t.Context()), router: test.GetNewTestRouter(),
wrappedPayloadCtx: t.Context(), errorString: "router.input source error: not a string",
errorString: "router.input source error: not a string",
}, },
{ {
name: "router not found in context", name: "router not found in context",
params: map[string]any{ params: map[string]any{
"source": "test", "source": "test",
}, },
payload: "test", payload: "test",
processCtx: t.Context(), router: nil,
wrappedPayloadCtx: t.Context(), errorString: "router.input no router found",
errorString: "router.input no router found",
}, },
} }
@@ -142,7 +140,7 @@ func TestBadRouterInput(t *testing.T) {
return return
} }
got, err := processorInstance.Process(testCase.processCtx, common.GetWrappedPayload(testCase.wrappedPayloadCtx, testCase.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Router: testCase.router, Payload: testCase.payload})
if err == nil { if err == nil {
t.Fatalf("router.input expected to fail but succeeded, got: %v", got) t.Fatalf("router.input expected to fail but succeeded, got: %v", got)
+2 -2
View File
@@ -76,7 +76,7 @@ func TestGoodScriptExpr(t *testing.T) {
t.Fatalf("script.expr failed to create processor: %s", err) t.Fatalf("script.expr failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("script.expr processing failed: %s", err) t.Fatalf("script.expr processing failed: %s", err)
@@ -134,7 +134,7 @@ func TestBadScriptExpr(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("script.expr expected to fail but succeeded, got: %v", got) t.Fatalf("script.expr expected to fail but succeeded, got: %v", got)
+3 -3
View File
@@ -34,7 +34,7 @@ func TestScriptJSFromRegistry(t *testing.T) {
payload := 1 payload := 1
expected := 2 expected := 2
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("script.js processing failed: %s", err) t.Fatalf("script.js processing failed: %s", err)
} }
@@ -165,7 +165,7 @@ func TestGoodScriptJS(t *testing.T) {
t.Fatalf("script.js failed to create processor: %s", err) t.Fatalf("script.js failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("script.js processing failed: %s", err) t.Fatalf("script.js processing failed: %s", err)
@@ -209,7 +209,7 @@ func TestBadScriptJS(t *testing.T) {
Params: test.params, Params: test.params,
}) })
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("script.js expected to fail but succeeded, got: %v", got) t.Fatalf("script.js expected to fail but succeeded, got: %v", got)
+2 -2
View File
@@ -73,7 +73,7 @@ func TestGoodScriptWASM(t *testing.T) {
t.Fatalf("script.wasm failed to create processor: %s", err) t.Fatalf("script.wasm failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("script.wasm processing failed: %s", err) t.Fatalf("script.wasm processing failed: %s", err)
@@ -176,7 +176,7 @@ func TestBadScriptWASM(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("script.wasm expected to fail but succeeded, got: %v", got) t.Fatalf("script.wasm expected to fail but succeeded, got: %v", got)
@@ -90,7 +90,7 @@ func TestGoodSipResponseAudioCreate(t *testing.T) {
t.Fatalf("sip.response.audio.create failed to create processor: %s", err) t.Fatalf("sip.response.audio.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("sip.response.audio.create processing failed: %s", err) t.Fatalf("sip.response.audio.create processing failed: %s", err)
} }
@@ -200,7 +200,7 @@ func TestBadSipResponseAudioCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("sip.response.audio.create expected to fail but succeeded, got: %v", got) t.Fatalf("sip.response.audio.create expected to fail but succeeded, got: %v", got)
@@ -88,7 +88,7 @@ func TestGoodSipResponseDTMFCreate(t *testing.T) {
t.Fatalf("sip.response.dtmf.create failed to create processor: %s", err) t.Fatalf("sip.response.dtmf.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("sip.response.dtmf.create processing failed: %s", err) t.Fatalf("sip.response.dtmf.create processing failed: %s", err)
} }
@@ -208,7 +208,7 @@ func TestBadSipResponseDTMFCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("sip.response.dtmf.create expected to fail but succeeded, got: %v", got) t.Fatalf("sip.response.dtmf.create expected to fail but succeeded, got: %v", got)
@@ -32,7 +32,7 @@ func TestStringCreateFromRegistry(t *testing.T) {
payload := "hello" payload := "hello"
expected := "hello" expected := "hello"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("string.create processing failed: %s", err) t.Fatalf("string.create processing failed: %s", err)
} }
@@ -98,7 +98,7 @@ func TestGoodStringCreate(t *testing.T) {
t.Fatalf("string.create failed to create processor: %s", err) t.Fatalf("string.create failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("string.create processing failed: %s", err) t.Fatalf("string.create processing failed: %s", err)
@@ -174,7 +174,7 @@ func TestBadStringCreate(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("string.create expected to fail but got payload: %+v", got) t.Fatalf("string.create expected to fail but got payload: %+v", got)
@@ -28,7 +28,7 @@ func TestStringDecodeFromRegistry(t *testing.T) {
payload := []byte{'h', 'e', 'l', 'l', 'o'} payload := []byte{'h', 'e', 'l', 'l', 'o'}
expected := "hello" expected := "hello"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("string.decode processing failed: %s", err) t.Fatalf("string.decode processing failed: %s", err)
} }
@@ -54,7 +54,7 @@ func TestGoodStringDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := stringDecoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := stringDecoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("string.decode processing failed: %s", err) t.Fatalf("string.decode processing failed: %s", err)
} }
@@ -87,7 +87,7 @@ func TestBadStringDecode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := stringDecoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := stringDecoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("string.decode expected to fail but got payload: %+v", got) t.Fatalf("string.decode expected to fail but got payload: %+v", got)
@@ -29,7 +29,7 @@ func TestStringEncodeFromRegistry(t *testing.T) {
payload := "hello" payload := "hello"
expected := []byte{'h', 'e', 'l', 'l', 'o'} expected := []byte{'h', 'e', 'l', 'l', 'o'}
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("string.encode processing failed: %s", err) t.Fatalf("string.encode processing failed: %s", err)
} }
@@ -61,7 +61,7 @@ func TestGoodStringEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := stringEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := stringEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("string.encode processing failed: %s", err) t.Fatalf("string.encode processing failed: %s", err)
} }
@@ -93,7 +93,7 @@ func TestBadStringEncode(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := stringEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := stringEncoder.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("string.encode expected to fail but got payload: %+v", got) t.Fatalf("string.encode expected to fail but got payload: %+v", got)
+3 -3
View File
@@ -32,7 +32,7 @@ func TestStringSplitFromRegistry(t *testing.T) {
payload := "part1,part2,part3" payload := "part1,part2,part3"
expected := []string{"part1", "part2", "part3"} expected := []string{"part1", "part2", "part3"}
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("string.split processing failed: %s", err) t.Fatalf("string.split processing failed: %s", err)
} }
@@ -79,7 +79,7 @@ func TestGoodStringSplit(t *testing.T) {
t.Fatalf("string.split failed to create processor: %s", err) t.Fatalf("string.split failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("string.split processing failed: %s", err) t.Fatalf("string.split processing failed: %s", err)
} }
@@ -142,7 +142,7 @@ func TestBadStringSplit(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("string.split expected error but got none, payload: %+v", got) t.Fatalf("string.split expected error but got none, payload: %+v", got)
@@ -33,7 +33,7 @@ func TestStructFieldGetFromRegistry(t *testing.T) {
payload := test.TestStruct{Data: "hello"} payload := test.TestStruct{Data: "hello"}
expected := "hello" expected := "hello"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("struct.field.get processing failed: %s", err) t.Fatalf("struct.field.get processing failed: %s", err)
} }
@@ -107,7 +107,7 @@ func TestGoodStructFieldGet(t *testing.T) {
t.Fatalf("struct.field.get failed to create processor: %s", err) t.Fatalf("struct.field.get failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("struct.field.get processing failed: %s", err) t.Fatalf("struct.field.get processing failed: %s", err)
@@ -179,7 +179,7 @@ func TestBadStructFieldGet(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("struct.field.get expected to fail but got payload: %+v", got) t.Fatalf("struct.field.get expected to fail but got payload: %+v", got)
@@ -33,7 +33,7 @@ func TestStructMethodGetFromRegistry(t *testing.T) {
payload := test.TestStruct{Data: "hello"} payload := test.TestStruct{Data: "hello"}
expected := "hello" expected := "hello"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: payload})
if err != nil { if err != nil {
t.Fatalf("struct.method.get processing failed: %s", err) t.Fatalf("struct.method.get processing failed: %s", err)
} }
@@ -132,7 +132,7 @@ func TestGoodStructMethodGet(t *testing.T) {
t.Fatalf("struct.method.get failed to create processor: %s", err) t.Fatalf("struct.method.get failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("struct.method.get processing failed: %s", err) t.Fatalf("struct.method.get processing failed: %s", err)
@@ -204,7 +204,7 @@ func TestBadStructMethodGet(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("struct.method.get expected to fail but got payload: %+v", got) t.Fatalf("struct.method.get expected to fail but got payload: %+v", got)
+2 -2
View File
@@ -59,7 +59,7 @@ func TestGoodTimeSleep(t *testing.T) {
t.Fatalf("time.sleep failed to create processor: %s", err) t.Fatalf("time.sleep failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err != nil { if err != nil {
t.Fatalf("time.sleep processing failed: %s", err) t.Fatalf("time.sleep processing failed: %s", err)
@@ -114,7 +114,7 @@ func TestBadTimeSleep(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
if err == nil { if err == nil {
t.Fatalf("time.sleep expected to fail but succeeded, got: %v", got) t.Fatalf("time.sleep expected to fail but succeeded, got: %v", got)
+2 -3
View File
@@ -43,10 +43,9 @@ func (r *Route) Input() string {
return r.input return r.input
} }
func (r *Route) ProcessPayload(ctx context.Context, payload any) (any, error) { func (r *Route) ProcessPayload(ctx context.Context, wrappedPayload common.WrappedPayload) (any, error) {
wrappedPayload := common.GetWrappedPayload(ctx, payload)
tracer := otel.Tracer("route") tracer := otel.Tracer("route")
processCtx, processSpan := tracer.Start(ctx, "ProcessPayload", trace.WithAttributes(attribute.String("payload.type", fmt.Sprintf("%T", payload)))) processCtx, processSpan := tracer.Start(ctx, "ProcessPayload", trace.WithAttributes(attribute.String("payload.type", fmt.Sprintf("%T", wrappedPayload.Payload))))
defer processSpan.End() defer processSpan.End()
for processorIndex, processor := range r.processors { for processorIndex, processor := range r.processors {
processorCtx, processorSpan := otel.Tracer("processor").Start(processCtx, "process", trace.WithAttributes(attribute.Int("processor.index", processorIndex), attribute.String("processor.type", processor.Type()))) processorCtx, processorSpan := otel.Tracer("processor").Start(processCtx, "process", trace.WithAttributes(attribute.Int("processor.index", processorIndex), attribute.String("processor.type", processor.Type())))
+16 -4
View File
@@ -55,7 +55,10 @@ func TestGoodRouteHandleInput(t *testing.T) {
} }
inputData := "test input data" inputData := "test input data"
payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), common.RouterContextKey, &MockRouter{}), inputData) payload, err := testRoute.ProcessPayload(t.Context(), common.WrappedPayload{
Router: &MockRouter{},
Payload: inputData,
})
if err != nil { if err != nil {
t.Fatalf("route ProcessPayload returned error: %v", err) t.Fatalf("route ProcessPayload returned error: %v", err)
} }
@@ -90,7 +93,10 @@ func TestRouteHandleInputWithProcessorError(t *testing.T) {
} }
inputData := "test input data" inputData := "test input data"
_, err = testRoute.ProcessPayload(context.WithValue(t.Context(), common.RouterContextKey, &MockRouter{}), inputData) _, err = testRoute.ProcessPayload(t.Context(), common.WrappedPayload{
Router: &MockRouter{},
Payload: inputData,
})
if err == nil { if err == nil {
t.Fatalf("route HandleOutput did not return error for bad processor") t.Fatalf("route HandleOutput did not return error for bad processor")
} }
@@ -115,7 +121,10 @@ func TestRouteHandleNilPayload(t *testing.T) {
return return
} }
payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), common.RouterContextKey, &MockRouter{}), nil) payload, err := testRoute.ProcessPayload(t.Context(), common.WrappedPayload{
Router: &MockRouter{},
Payload: nil,
})
if err != nil { if err != nil {
t.Fatalf("route ProcessPayload returned error: %v", err) t.Fatalf("route ProcessPayload returned error: %v", err)
} }
@@ -143,7 +152,10 @@ func TestRouteHandleNilPayloadFromProcessor(t *testing.T) {
t.Fatalf("route failed to create: %v", err) t.Fatalf("route failed to create: %v", err)
} }
_, err = testRoute.ProcessPayload(context.WithValue(t.Context(), common.RouterContextKey, &MockRouter{}), "test") _, err = testRoute.ProcessPayload(t.Context(), common.WrappedPayload{
Router: &MockRouter{},
Payload: "test",
})
if err != nil { if err != nil {
t.Fatalf("route HandleOutput returned error for nil payload: %v", err) t.Fatalf("route HandleOutput returned error for nil payload: %v", err)
} }
-27
View File
@@ -1,27 +0,0 @@
package test
import (
"context"
"github.com/jwetzell/showbridge-go/internal/common"
)
func GetContextWithModules(ctx context.Context, modules map[string]common.Module) context.Context {
ctx = context.WithValue(ctx, common.ModulesContextKey, modules)
return ctx
}
func GetContextWithRouter(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, common.RouterContextKey, GetNewTestRouter())
return ctx
}
func GetContextWithSender(ctx context.Context, sender any) context.Context {
ctx = context.WithValue(ctx, common.SenderContextKey, sender)
return ctx
}
func GetContextWithSource(ctx context.Context, source string) context.Context {
ctx = context.WithValue(ctx, common.SourceContextKey, source)
return ctx
}
+4 -3
View File
@@ -4,13 +4,14 @@ import (
"context" "context"
"database/sql" "database/sql"
"github.com/jwetzell/showbridge-go/internal/common"
_ "modernc.org/sqlite" _ "modernc.org/sqlite"
) )
type TestModule struct { type TestModule struct {
} }
func (m *TestModule) Start(ctx context.Context) error { func (m *TestModule) Start(ctx context.Context, router common.RouteIO) error {
<-ctx.Done() <-ctx.Done()
return nil return nil
} }
@@ -36,7 +37,7 @@ type TestKVModule struct {
kvData map[string]any kvData map[string]any
} }
func (m *TestKVModule) Start(ctx context.Context) error { func (m *TestKVModule) Start(ctx context.Context, router common.RouteIO) error {
<-ctx.Done() <-ctx.Done()
return nil return nil
} }
@@ -73,7 +74,7 @@ type TestDBModule struct {
db *sql.DB db *sql.DB
} }
func (m *TestDBModule) Start(ctx context.Context) error { func (m *TestDBModule) Start(ctx context.Context, router common.RouteIO) error {
<-ctx.Done() <-ctx.Done()
return nil return nil
} }
+10 -8
View File
@@ -74,7 +74,7 @@ func (r *Router) startModule(ctx context.Context, moduleId string) error {
return errors.New("module id not found") return errors.New("module id not found")
} }
r.moduleWait.Go(func() { r.moduleWait.Go(func() {
err := moduleInstance.Start(ctx) err := moduleInstance.Start(ctx, r)
if err != nil { if err != nil {
// TODO(jwetzell): propagate module run errors better // TODO(jwetzell): propagate module run errors better
r.logger.Error("error encountered running module", "moduleId", moduleId, "error", err) r.logger.Error("error encountered running module", "moduleId", moduleId, "error", err)
@@ -210,12 +210,15 @@ func (r *Router) HandleInput(ctx context.Context, sourceId string, payload any)
routeWaitGroup.Go(func() { routeWaitGroup.Go(func() {
routeFound = true routeFound = true
routeContext := context.WithValue(spanCtx, common.SourceContextKey, sourceId)
routeContext = context.WithValue(routeContext, common.RouterContextKey, r)
routeContext = context.WithValue(routeContext, common.ModulesContextKey, r.ModuleInstances)
routeCtx, routeSpan := otel.Tracer("router").Start(routeContext, "route", trace.WithAttributes(attribute.Int("route.index", routeIndex), attribute.String("route.input", routeInstance.Input()))) routeCtx, routeSpan := otel.Tracer("router").Start(spanCtx, "route", trace.WithAttributes(attribute.Int("route.index", routeIndex), attribute.String("route.input", routeInstance.Input())))
_, err := routeInstance.ProcessPayload(routeCtx, payload) _, err := routeInstance.ProcessPayload(routeCtx, common.WrappedPayload{
Payload: payload,
Source: sourceId,
Modules: r.ModuleInstances,
Router: r,
End: false,
})
if err != nil { if err != nil {
if routeIOErrors == nil { if routeIOErrors == nil {
routeIOErrors = []common.RouteIOError{} routeIOErrors = []common.RouteIOError{}
@@ -298,11 +301,10 @@ func (r *Router) HandleOutput(ctx context.Context, destinationId string, payload
} }
func (r *Router) startModules() { func (r *Router) startModules() {
contextWithRouter := context.WithValue(r.Context, common.RouterContextKey, r)
for moduleId := range r.ModuleInstances { for moduleId := range r.ModuleInstances {
// TODO(jwetzell): handle module run errors // TODO(jwetzell): handle module run errors
err := r.startModule(contextWithRouter, moduleId) err := r.startModule(r.Context, moduleId)
if err != nil { if err != nil {
r.logger.Error("error starting module", "moduleId", moduleId, "error", err) r.logger.Error("error starting module", "moduleId", moduleId, "error", err)
} }
+1 -6
View File
@@ -33,12 +33,7 @@ func (mcm *MockCounterModule) Output(context.Context, any) error {
return nil return nil
} }
func (mcm *MockCounterModule) Start(ctx context.Context) error { func (mcm *MockCounterModule) Start(ctx context.Context, router common.RouteIO) error {
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return fmt.Errorf("mock.counter could not get router from context")
}
mcm.router = router mcm.router = router
moduleContext, cancel := context.WithCancel(ctx) moduleContext, cancel := context.WithCancel(ctx)
mcm.ctx = moduleContext mcm.ctx = moduleContext