Skip to content

Commit

Permalink
introduce DI for logging, test warnings produced by plan.go
Browse files Browse the repository at this point in the history
  • Loading branch information
justadreamer committed Nov 26, 2024
1 parent e66f5a8 commit 0af99e5
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
18 changes: 18 additions & 0 deletions di/di.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package di

import (
"github.com/golang/glog"
)

type ILogger interface {
Warningf(format string, args ...interface{})
}

type DefaultLogger struct{}

func (d *DefaultLogger) Warningf(format string, args ...interface{}) {
glog.Warningf(format, args...)
}

var defaultLogger = DefaultLogger{}
var Logger ILogger = &defaultLogger
4 changes: 2 additions & 2 deletions hooks/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"slices"
"time"

"github.com/golang/glog"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/di"
"github.com/prebid/prebid-server/v3/hooks/hookstage"
)

Expand Down Expand Up @@ -211,7 +211,7 @@ func getGroup[T any](getHookFn hookFn[T], cfg config.HookExecutionGroup, disable
if h, ok := getHookFn(hookCfg.ModuleCode); ok {
group.Hooks = append(group.Hooks, HookWrapper[T]{Module: hookCfg.ModuleCode, Code: hookCfg.HookImplCode, Hook: h})
} else {
glog.Warningf("Not found hook while building hook execution plan: %s %s", hookCfg.ModuleCode, hookCfg.HookImplCode)
di.Logger.Warningf("Not found hook while building hook execution plan: %s %s", hookCfg.ModuleCode, hookCfg.HookImplCode)
}
}

Expand Down
89 changes: 89 additions & 0 deletions modules/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/prebid/prebid-server/v3/di"
"github.com/prebid/prebid-server/v3/util/jsonutil"
"math"
"net/http"
"testing"
Expand Down Expand Up @@ -118,6 +120,93 @@ func TestModuleBuilderBuild(t *testing.T) {
}
}

func TestPlanForDisabledModule(t *testing.T) {
testCases := map[string]struct {
moduleCode string
enabled bool
expectedPlanLen int
expectedLogOutput string
}{
"Correct module_code and module enabled = plan contains one hook": {
moduleCode: "prebid.ortb2blocking",
enabled: true,
expectedPlanLen: 1,
expectedLogOutput: "",
},
"Incorrect module_code but module enabled = plan contains no hooks": {
moduleCode: "prebid_ortb2blocking",
enabled: true,
expectedPlanLen: 0,
expectedLogOutput: "Not found hook while building hook execution plan: prebid_ortb2blocking foo",
},
"Correct module_code but module disabled = plan contains no hooks": {
moduleCode: "prebid.ortb2blocking",
enabled: false,
expectedPlanLen: 0,
expectedLogOutput: "",
},
}

old_logger := di.Logger
testLogger := TestLogger{}
di.Logger = &testLogger
defer func() { di.Logger = old_logger }()

for name, test := range testCases {
t.Run(name, func(t *testing.T) {
testLogger.log = ""
hooksCfgData, accountCfgData := constructCfg(test.moduleCode, test.enabled)
var hooksCfg config.Hooks
var accountCfg config.Account
var err = jsonutil.UnmarshalValid([]byte(hooksCfgData), &hooksCfg)
assert.Nil(t, err)

err = jsonutil.UnmarshalValid([]byte(accountCfgData), &accountCfg)
assert.Nil(t, err)

planBuilder, err := constructPlanBuilder(hooksCfg)
assert.Nil(t, err)

plan := planBuilder.PlanForRawBidderResponseStage("/openrtb2/auction", &accountCfg)
assert.Equal(t, test.expectedPlanLen, len(plan))
assert.Equal(t, test.expectedLogOutput, testLogger.log)

})
}
}

type TestLogger struct {
log string
}

func (logger *TestLogger) Warningf(format string, args ...interface{}) {
logger.log = logger.log + fmt.Sprintf(format, args...)
}

func constructCfg(module_code string, enabled bool) (string, string) {
group := `{"timeout": 5, "hook_sequence": [{"module_code": "` + module_code + `", "hook_impl_code": "foo"}]}`
executionPlanData := `{"endpoints": {"/openrtb2/auction": {"stages": {"raw_bidder_response": {"groups": [` + group + `]}}}}}`
enabledS := `false`
if enabled {
enabledS = `true`
}
modules := `"modules": {"prebid": {"ortb2blocking": {"enabled": ` + enabledS + `}}}`
hooksCfgData := `{"enabled":true, ` + modules + `, "execution_plan": ` + executionPlanData + `}`
accountCfgData := `{"hooks":` + hooksCfgData + `}`
return hooksCfgData, accountCfgData
}

func constructPlanBuilder(cfgHooks config.Hooks) (hooks.ExecutionPlanBuilder, error) {
moduleDeps := moduledeps.ModuleDeps{}
repo, _, disabledModuleCodes, err := NewBuilder().Build(cfgHooks.Modules, moduleDeps)
if err != nil {
return nil, err
}

planBuilder := hooks.NewExecutionPlanBuilder(cfgHooks, repo, disabledModuleCodes)
return planBuilder, nil
}

type module struct{}

func (h module) HandleEntrypointHook(_ context.Context, _ hookstage.ModuleInvocationContext, _ hookstage.EntrypointPayload) (hookstage.HookResult[hookstage.EntrypointPayload], error) {
Expand Down

0 comments on commit 0af99e5

Please sign in to comment.