Skip to content

Commit

Permalink
refactor expect call
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Apr 2, 2024
1 parent 8e0f475 commit 2577036
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 1 addition & 5 deletions sdktests/server_side_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "+
Expand Down
12 changes: 8 additions & 4 deletions sdktests/testapi_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 2577036

Please sign in to comment.