Skip to content

Commit

Permalink
chore: Add support for hook errors contract test. (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Apr 3, 2024
1 parent eb2ffb1 commit a8a07a2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion testservice/sdk_client_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func makeSDKConfig(config servicedef.SDKConfigParams, sdkLog ldlog.Loggers) ld.C
if config.Hooks != nil {
hooks := make([]ldhooks.Hook, 0)
for _, hookConfig := range config.Hooks.Hooks {
hookInstance := newTestHook(hookConfig.Name, hookConfig.CallbackURI, hookConfig.Data)
hookInstance := newTestHook(hookConfig.Name, hookConfig.CallbackURI, hookConfig.Data, hookConfig.Errors)
hooks = append(hooks, hookInstance)
}
ret.Hooks = hooks
Expand Down
1 change: 1 addition & 0 deletions testservice/servicedef/sdk_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type SDKConfigHookInstance struct {
Name string `json:"name"`
CallbackURI string `json:"callbackUri"`
Data map[HookStage]SDKConfigEvaluationHookData `json:"data,omitempty"`
Errors map[HookStage]string `json:"errors,omitempty"`
}

type SDKConfigHooksParams struct {
Expand Down
12 changes: 12 additions & 0 deletions testservice/test_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"

"github.com/launchdarkly/go-sdk-common/v3/ldreason"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
Expand All @@ -13,18 +14,21 @@ type testHook struct {
ldhooks.Unimplemented
metadata ldhooks.Metadata
dataPayloads map[servicedef.HookStage]servicedef.SDKConfigEvaluationHookData
errors map[servicedef.HookStage]string
callbackService callbackService
}

func newTestHook(
name string,
endpoint string,
data map[servicedef.HookStage]servicedef.SDKConfigEvaluationHookData,
errors map[servicedef.HookStage]string,
) testHook {
return testHook{
metadata: ldhooks.NewMetadata(name),
dataPayloads: data,
callbackService: callbackService{baseURL: endpoint},
errors: errors,
}
}

Expand All @@ -37,6 +41,10 @@ func (t testHook) BeforeEvaluation(
seriesContext ldhooks.EvaluationSeriesContext,
data ldhooks.EvaluationSeriesData,
) (ldhooks.EvaluationSeriesData, error) {
errString, hasErr := t.errors[servicedef.BeforeEvaluation]
if hasErr {
return data, errors.New(errString)
}
err := t.callbackService.post("", servicedef.HookExecutionPayload{
EvaluationSeriesContext: evaluationSeriesContextToService(seriesContext),
EvaluationSeriesData: evaluationSeriesDataToService(data),
Expand All @@ -59,6 +67,10 @@ func (t testHook) AfterEvaluation(
data ldhooks.EvaluationSeriesData,
detail ldreason.EvaluationDetail,
) (ldhooks.EvaluationSeriesData, error) {
errString, hasErr := t.errors[servicedef.AfterEvaluation]
if hasErr {
return data, errors.New(errString)
}
err := t.callbackService.post("", servicedef.HookExecutionPayload{
EvaluationSeriesContext: evaluationSeriesContextToService(seriesContext),
EvaluationSeriesData: evaluationSeriesDataToService(data),
Expand Down

0 comments on commit a8a07a2

Please sign in to comment.