Skip to content

Commit

Permalink
Add StartBackgroundServices as a new interface method for ExperimentR…
Browse files Browse the repository at this point in the history
…unner
  • Loading branch information
deadlycoconuts committed Nov 23, 2022
1 parent 3387ca1 commit d295d39
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 3 deletions.
1 change: 1 addition & 0 deletions engines/experiment/examples/plugins/hardcoded/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/caraml-dev/turing/engines/experiment v1.0.0
github.com/hashicorp/go-hclog v0.16.0
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced
)

replace github.com/caraml-dev/turing/engines/experiment => ../../../
7 changes: 7 additions & 0 deletions engines/experiment/examples/plugins/hardcoded/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/caraml-dev/turing/engines/experiment/examples/plugins/hardcoded/utils"
"github.com/caraml-dev/turing/engines/experiment/pkg/request"
"github.com/caraml-dev/turing/engines/experiment/runner"
"golang.org/x/net/context"
)

type ExperimentRunner struct {
Expand Down Expand Up @@ -74,3 +75,9 @@ func (e *ExperimentRunner) GetTreatmentForRequest(

return nil, errors.New("no experiment configured for the unit")
}

func (e *ExperimentRunner) StartBackgroundServices(
chan error,
) context.CancelFunc {
return nil
}
7 changes: 7 additions & 0 deletions engines/experiment/examples/plugins/nop/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/caraml-dev/turing/engines/experiment/runner"
"golang.org/x/net/context"
)

type ExperimentRunner struct{}
Expand All @@ -23,3 +24,9 @@ func (r *ExperimentRunner) GetTreatmentForRequest(
Config: json.RawMessage(`{"config-1": "value-a"}`),
}, nil
}

func (r *ExperimentRunner) StartBackgroundServices(
chan error,
) context.CancelFunc {
return nil
}
2 changes: 1 addition & 1 deletion engines/experiment/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/stretchr/testify v1.7.0
github.com/zaffka/zap-to-hclog v0.10.5
go.uber.org/zap v1.21.0
golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced
google.golang.org/grpc v1.48.0
)

Expand All @@ -35,7 +36,6 @@ require (
github.com/stretchr/objx v0.1.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220810155839-1856144b1d9c // indirect
Expand Down
7 changes: 7 additions & 0 deletions engines/experiment/plugin/inproc/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
runnerPlugin "github.com/caraml-dev/turing/engines/experiment/plugin/inproc/runner"
"github.com/caraml-dev/turing/engines/experiment/runner"
"github.com/caraml-dev/turing/engines/experiment/runner/mocks"
"golang.org/x/net/context"
)

type fakeRunner struct {
Expand All @@ -25,6 +26,12 @@ func (runner fakeRunner) GetTreatmentForRequest(
return nil, nil
}

func (runner fakeRunner) StartBackgroundServices(
chan error,
) context.CancelFunc {
return nil
}

func TestRegisterAndGet(t *testing.T) {
tests := []struct {
name string
Expand Down
19 changes: 18 additions & 1 deletion engines/experiment/plugin/rpc/mocks/experiment_runner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions engines/experiment/plugin/rpc/runner/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/caraml-dev/turing/engines/experiment/plugin/rpc/shared"
"github.com/caraml-dev/turing/engines/experiment/runner"
"golang.org/x/net/context"
)

// rpcClient implements ConfigurableExperimentRunner interface
Expand Down Expand Up @@ -37,6 +38,17 @@ func (c *rpcClient) GetTreatmentForRequest(
return &resp, nil
}

func (c *rpcClient) StartBackgroundServices(
errChannel chan error,
) context.CancelFunc {
var cancelBackgroundServices context.CancelFunc
err := c.Call("Plugin.StartBackgroundServices", &errChannel, cancelBackgroundServices)
if err != nil {
errChannel <- err
}
return cancelBackgroundServices
}

// rpcServer serves the implementation of a ConfigurableExperimentRunner
type rpcServer struct {
Impl ConfigurableExperimentRunner
Expand All @@ -55,3 +67,10 @@ func (s *rpcServer) GetTreatmentForRequest(req *GetTreatmentRequest, resp *runne
*resp = *treatment
return nil
}

func (s *rpcServer) StartBackgroundServices(errChannel *chan error, resp *context.CancelFunc) error {
cancelBackgroundServices := s.Impl.StartBackgroundServices(*errChannel)

*resp = cancelBackgroundServices
return nil
}
19 changes: 18 additions & 1 deletion engines/experiment/runner/mocks/experiment_runner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions engines/experiment/runner/nop/experiment_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/caraml-dev/turing/engines/experiment/runner"
"golang.org/x/net/context"
)

var nopTreatment = &runner.Treatment{}
Expand All @@ -21,6 +22,13 @@ func (ExperimentRunner) GetTreatmentForRequest(
return nopTreatment, nil
}

// StartBackgroundServices returns a dummy context cancellation function
func (r *ExperimentRunner) StartBackgroundServices(
chan error,
) context.CancelFunc {
return nil
}

// NewExperimentRunner is a creator for the experiment runners
func NewExperimentRunner(json.RawMessage) (runner.ExperimentRunner, error) {
return &ExperimentRunner{}, nil
Expand Down
5 changes: 5 additions & 0 deletions engines/experiment/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package runner
import (
"encoding/json"
"net/http"

"golang.org/x/net/context"
)

type GetTreatmentOptions struct {
Expand All @@ -23,4 +25,7 @@ type ExperimentRunner interface {
payload []byte,
options GetTreatmentOptions,
) (*Treatment, error)
StartBackgroundServices(
errChannel chan error,
) context.CancelFunc
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/caraml-dev/turing/engines/experiment/runner"
"golang.org/x/net/context"
)

// MockExperimentRunner is a mock implementation for the Planner interface
Expand Down Expand Up @@ -36,5 +37,11 @@ func (mp MockExperimentRunner) GetTreatmentForRequest(
return nil, errors.New("failed to retrieve experiment treatment")
}
return mp.Treatment, nil
}

// StartBackgroundServices returns a dummy context cancellation function
func (mp MockExperimentRunner) StartBackgroundServices(
chan error,
) context.CancelFunc {
return nil
}

0 comments on commit d295d39

Please sign in to comment.