diff --git a/sdktests/server_side_hooks.go b/sdktests/server_side_hooks.go index d7634b1..425c1ad 100644 --- a/sdktests/server_side_hooks.go +++ b/sdktests/server_side_hooks.go @@ -275,7 +275,6 @@ func beforeEvaluationDataPropagatesToAfterMigration(t *ldtest.T) { // The client MUST handle exceptions which are thrown (or errors returned, if idiomatic for the language) // during the execution of a stage or handler allowing operations to complete unaffected. func errorInBeforeStageDoesNotAffectAfterStage(t *ldtest.T) { - const numHooks = 100 // why not? // We're configuring the beforeEvaluation stage with some data, but we don't expect @@ -304,10 +303,7 @@ func errorInBeforeStageDoesNotAffectAfterStage(t *ldtest.T) { DefaultValue: ldvalue.Bool(false), }) - // Since we shouldn't receive any beforeEvaluation calls, the number of calls to expect is simply - // the number of hooks configured. - const numAfterCalls = numHooks - calls := hooks.ExpectSingleCallForEachHook(t, names, numAfterCalls) + calls := hooks.ExpectAtLeastOneCallForEachHook(t, names) for _, call := range calls { assert.Equal(t, servicedef.AfterEvaluation, call.Stage.Value(), "HOOKS:1.3.7: beforeEvaluation "+ diff --git a/sdktests/testapi_hooks.go b/sdktests/testapi_hooks.go index 21999e1..82ae6ff 100644 --- a/sdktests/testapi_hooks.go +++ b/sdktests/testapi_hooks.go @@ -84,23 +84,27 @@ func (h *Hooks) ExpectCall(t *ldtest.T, hookName string, } } -func (h *Hooks) ExpectSingleCallForEachHook(t *ldtest.T, hookNames []string, count int) []servicedef.HookExecutionPayload { +// ExpectAtLeastOneCallForEachHook waits for a single call from N hooks. If there are fewer calls recorded, +// the test will fail. However, this helper cannot detect if there were more calls waiting to be recorded. +func (h *Hooks) ExpectAtLeastOneCallForEachHook(t *ldtest.T, hookNames []string) []servicedef.HookExecutionPayload { out := make(chan o.Maybe[servicedef.HookExecutionPayload]) + totalCalls := len(hookNames) + for _, hookName := range hookNames { go func(name string) { out <- helpers.TryReceive(h.instances[name].hookService.CallChannel, hookReceiveTimeout) }(hookName) } - payloads := make([]servicedef.HookExecutionPayload, 0) - for i := 0; i < count; i++ { + payloads := make([]servicedef.HookExecutionPayload, totalCalls) + for i := 0; i < totalCalls; i++ { if val := <-out; val.IsDefined() { payloads = append(payloads, val.Value()) } } - assert.Len(t, payloads, count, "Expected %d hook calls, got %d", count, len(payloads)) + assert.Len(t, payloads, totalCalls, "Expected %d hook calls, got %d", totalCalls, len(payloads)) return payloads }