Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hooks/hookexecution/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (mc *moduleContexts) put(moduleName string, mCtx *hookstage.ModuleContext)
defer mc.Unlock()

existingCtx, ok := mc.ctxs[moduleName]
if !ok {
if !ok || existingCtx == nil {
mc.ctxs[moduleName] = mCtx
return
}
Expand Down
39 changes: 39 additions & 0 deletions hooks/hookexecution/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package hookexecution

import (
"testing"

"github.com/prebid/prebid-server/v4/hooks/hookstage"
"github.com/stretchr/testify/assert"
)

func TestModuleContextsPutAfterNil(t *testing.T) {
mcs := &moduleContexts{ctxs: map[string]*hookstage.ModuleContext{}}
mcs.put("module", nil)

later := hookstage.NewModuleContext()
later.Set("key", "value")
mcs.put("module", later)

got, ok := mcs.get("module")
assert.True(t, ok)
v, found := got.Get("key")
assert.True(t, found)
assert.Equal(t, "value", v)
}

func TestModuleContextsPutMergesAndKeepsNilHarmless(t *testing.T) {
mcs := &moduleContexts{ctxs: map[string]*hookstage.ModuleContext{}}

first := hookstage.NewModuleContext()
first.Set("a", 1)
mcs.put("module", first)
mcs.put("module", nil)

second := hookstage.NewModuleContext()
second.Set("b", 2)
mcs.put("module", second)

got, _ := mcs.get("module")
assert.Equal(t, map[string]any{"a": 1, "b": 2}, got.GetAll())
}