mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
load router from context in mock module
This commit is contained in:
@@ -127,6 +127,7 @@ func (r *Router) Run(ctx context.Context) {
|
|||||||
r.moduleWait.Go(func() {
|
r.moduleWait.Go(func() {
|
||||||
err := moduleInstance.Run(contextWithRouter)
|
err := moduleInstance.Run(contextWithRouter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// TODO(jwetzell): handle module run errors better
|
||||||
r.logger.Error("error encountered running module", "error", err)
|
r.logger.Error("error encountered running module", "error", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ var (
|
|||||||
tracer = otel.Tracer("showbridge.test")
|
tracer = otel.Tracer("showbridge.test")
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockModule struct {
|
type MockCounterModule struct {
|
||||||
config config.ModuleConfig
|
config config.ModuleConfig
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
outputCount int
|
outputCount int
|
||||||
@@ -27,31 +27,36 @@ type MockModule struct {
|
|||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockModule) Id() string {
|
func (mcm *MockCounterModule) Id() string {
|
||||||
return m.config.Id
|
return mcm.config.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockModule) Output(context.Context, any) error {
|
func (mcm *MockCounterModule) Output(context.Context, any) error {
|
||||||
m.outputCount += 1
|
mcm.outputCount += 1
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockModule) Run(ctx context.Context) error {
|
func (mcm *MockCounterModule) Run(ctx context.Context) error {
|
||||||
m.ctx = ctx
|
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||||
<-m.ctx.Done()
|
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("mock.counter could not get router from context")
|
||||||
|
}
|
||||||
|
mcm.router = router
|
||||||
|
mcm.ctx = ctx
|
||||||
|
<-mcm.ctx.Done()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockModule) Type() string {
|
func (mcm *MockCounterModule) Type() string {
|
||||||
return m.config.Type
|
return mcm.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
module.RegisterModule(module.ModuleRegistration{
|
module.RegisterModule(module.ModuleRegistration{
|
||||||
Type: "mock.counter",
|
Type: "mock.counter",
|
||||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
New: func(config config.ModuleConfig) (module.Module, error) {
|
||||||
|
return &MockCounterModule{config: config, logger: slog.Default()}, nil
|
||||||
return &MockModule{config: config, logger: slog.Default()}, nil
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -162,6 +167,7 @@ func TestRouterInputSingleRoute(t *testing.T) {
|
|||||||
|
|
||||||
routerRunner.Go(func() {
|
routerRunner.Go(func() {
|
||||||
router.Run(t.Context())
|
router.Run(t.Context())
|
||||||
|
fmt.Println("router stopped")
|
||||||
})
|
})
|
||||||
|
|
||||||
time.Sleep(time.Second * 1)
|
time.Sleep(time.Second * 1)
|
||||||
@@ -183,7 +189,7 @@ func TestRouterInputSingleRoute(t *testing.T) {
|
|||||||
|
|
||||||
for _, moduleInstance := range router.ModuleInstances {
|
for _, moduleInstance := range router.ModuleInstances {
|
||||||
if moduleInstance.Id() == "mock" {
|
if moduleInstance.Id() == "mock" {
|
||||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
mockModuleInstance, ok := moduleInstance.(*MockCounterModule)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("couldn't get mock module")
|
t.Fatalf("couldn't get mock module")
|
||||||
}
|
}
|
||||||
@@ -253,7 +259,7 @@ func TestRouterInputMultipleRoutes(t *testing.T) {
|
|||||||
|
|
||||||
for _, moduleInstance := range router.ModuleInstances {
|
for _, moduleInstance := range router.ModuleInstances {
|
||||||
if moduleInstance.Id() == "mock" {
|
if moduleInstance.Id() == "mock" {
|
||||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
mockModuleInstance, ok := moduleInstance.(*MockCounterModule)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("couldn't get mock module")
|
t.Fatalf("couldn't get mock module")
|
||||||
}
|
}
|
||||||
@@ -338,7 +344,7 @@ func TestRouterInputMultipleModules(t *testing.T) {
|
|||||||
|
|
||||||
for _, moduleInstance := range router.ModuleInstances {
|
for _, moduleInstance := range router.ModuleInstances {
|
||||||
if moduleInstance.Id() == "mock1" {
|
if moduleInstance.Id() == "mock1" {
|
||||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
mockModuleInstance, ok := moduleInstance.(*MockCounterModule)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("couldn't get mock module")
|
t.Fatalf("couldn't get mock module")
|
||||||
}
|
}
|
||||||
@@ -349,7 +355,7 @@ func TestRouterInputMultipleModules(t *testing.T) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if moduleInstance.Id() == "mock2" {
|
if moduleInstance.Id() == "mock2" {
|
||||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
mockModuleInstance, ok := moduleInstance.(*MockCounterModule)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("couldn't get mock module")
|
t.Fatalf("couldn't get mock module")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user