Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #93 from patientsknowbest/feature/11778-remove-nam…
Browse files Browse the repository at this point in the history
…espace-setting

PHR-11778 Add suspend/resume in place of namespace change
  • Loading branch information
MFAshby authored Apr 27, 2023
2 parents 9c8e1da + cb7da93 commit 090ad30
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 483 deletions.
7 changes: 7 additions & 0 deletions config/src/main/java/com/pkb/common/config/BaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ default String getTestControlCallbackUrl() {
return getConfigStorage().getString("testcontrol.callbackurl", "");
}

/**
* Should this application suspend any ongoing processing (e.g. camel routes, database polling, scheduled tasks)?
*/
default boolean isProcessingSuspended() {
return getConfigStorage().getBoolean("testcontrol.processing.suspended", false);
}

/**
* @param protocol defaults to http
* @param host valid value required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import com.pkb.common.testcontrol.message.InjectConfigRequest;
import com.pkb.common.testcontrol.message.LogTestNameRequest;
import com.pkb.common.testcontrol.message.MoveTimeRequest;
import com.pkb.common.testcontrol.message.NamespaceChangeRequest;
import com.pkb.common.testcontrol.message.ResumeProcessingRequest;
import com.pkb.common.testcontrol.message.SuspendProcessingRequest;
import okhttp3.OkHttpClient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -41,8 +42,7 @@ static TestControl create(OkHttpClient httpClient, String testControlBaseURL) {
.build()
.create(TestControl.class);
}
@PUT(IO_PKB_TESTCONTROL_PREFIX + "setNamespace")
Void setNamespace(@Body NamespaceChangeRequest request);

@PUT(IO_PKB_TESTCONTROL_PREFIX + "setFixedTimestamp")
Void setFixedTimestamp(@Body FixTimeRequest request);
@PUT(IO_PKB_TESTCONTROL_PREFIX + "moveTime")
Expand All @@ -57,6 +57,10 @@ static TestControl create(OkHttpClient httpClient, String testControlBaseURL) {
Void logTestName(@Body LogTestNameRequest request);
@PUT(IO_PKB_TESTCONTROL_PREFIX + "toggleDetailedLogging")
Void toggleDetailedLogging(@Body DetailedLoggingRequest request);
@PUT(IO_PKB_TESTCONTROL_PREFIX + "suspendProcessing")
Void suspendProcessing(@Body SuspendProcessingRequest request);
@PUT(IO_PKB_TESTCONTROL_PREFIX + "resumeProcessing")
Void resumeProcessing(@Body ResumeProcessingRequest request);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import org.immutables.value.Value;

@Value.Immutable
@JsonSerialize(as = ImmutableNamespaceChangeRequest.class)
@JsonDeserialize(as = ImmutableNamespaceChangeRequest.class)
public interface NamespaceChangeRequest {
String newNamespace();
}
@JsonSerialize(as = ImmutableResumeProcessingRequest.class)
@JsonDeserialize(as = ImmutableResumeProcessingRequest.class)
public interface ResumeProcessingRequest {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pkb.common.testcontrol.message;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;

@Value.Immutable
@JsonSerialize(as = ImmutableSuspendProcessingRequest.class)
@JsonDeserialize(as = ImmutableSuspendProcessingRequest.class)
public interface SuspendProcessingRequest {}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ static Object[][] testCases() {
{InjectConfigRequest.class, ImmutableInjectConfigRequest.builder().key("nofoo").value(null).build()},
{LogTestNameRequest.class, ImmutableLogTestNameRequest.builder().testName("zoo").build()},
{MoveTimeRequest.class, ImmutableMoveTimeRequest.builder().amount(1).unit("foo").build()},
{NamespaceChangeRequest.class, ImmutableNamespaceChangeRequest.builder().newNamespace("qoo").build()},
{SuspendProcessingRequest.class, ImmutableSuspendProcessingRequest.builder().build()},
{ResumeProcessingRequest.class, ImmutableResumeProcessingRequest.builder().build()},
{Startup.class, ImmutableStartup.builder().name("doo").callback("woo").build()},
};
}
Expand Down
32 changes: 20 additions & 12 deletions test-control-go/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (

// TestControl / applications should provide an implementation of this interface to participate in test-control.
type TestControl interface {
SetNamespace(ctx context.Context, newNamespace string) error
InjectConfig(ctx context.Context, key string, value string) error
SetFixedTimestamp(ctx context.Context, timestamp string) error
MoveTime(ctx context.Context, amount int, unit string) error
ClearInternalState(ctx context.Context, clearFixedTimestamp bool) error
ClearStorage(ctx context.Context) error
LogTestName(ctx context.Context, testName string) error
ToggleDetailedLogging(ctx context.Context, enable bool) error
SuspendProcessing(ctx context.Context) error
ResumeProcessing(ctx context.Context) error
}

// RunTestControl / applications should call this to optionally register with the test-control server
Expand All @@ -44,7 +45,6 @@ func RunTestControl(
impl := testControlImpl{control}
sm := http.NewServeMux()
IoPkbTestcontrolPrefix := "io-pkb-testcontrol-"
sm.HandleFunc("/"+IoPkbTestcontrolPrefix+"setNamespace", impl.handleSetNamespace)
sm.HandleFunc("/"+IoPkbTestcontrolPrefix+"injectConfig", impl.handleInjectConfig)
sm.HandleFunc("/"+IoPkbTestcontrolPrefix+"setFixedTimestamp", impl.handleSetFixedTimestamp)
sm.HandleFunc("/"+IoPkbTestcontrolPrefix+"moveTime", impl.handleMoveTime)
Expand All @@ -53,6 +53,8 @@ func RunTestControl(
sm.HandleFunc("/"+IoPkbTestcontrolPrefix+"clearStorage", impl.handleClearStorage)
sm.HandleFunc("/"+IoPkbTestcontrolPrefix+"logTestName", impl.handleLogTestName)
sm.HandleFunc("/"+IoPkbTestcontrolPrefix+"toggleDetailedLogging", impl.handleToggleDetailedLogging)
sm.HandleFunc("/"+IoPkbTestcontrolPrefix+"suspendProcessing", impl.handleSuspendProcessing)
sm.HandleFunc("/"+IoPkbTestcontrolPrefix+"resumeProcessing", impl.handleResumeProcessing)
sm.HandleFunc("/health", impl.handleHealth)
log.Printf("starting test-control API on %s", listenAddress)
svr := &http.Server{Addr: listenAddress, Handler: sm}
Expand Down Expand Up @@ -91,16 +93,6 @@ func handle[T any](res http.ResponseWriter, req *http.Request, t T, f func(conte
log.Printf("handling test control request %s complete", req.URL.Path)
}

func (t *testControlImpl) handleSetNamespace(res http.ResponseWriter, req *http.Request) {
type setNamespaceReq struct {
NewNamespace string `json:"newNamespace"`
}
v := &setNamespaceReq{}
handle(res, req, v, func(ctx context.Context, v *setNamespaceReq) error {
return t.SetNamespace(req.Context(), v.NewNamespace)
})
}

func (t *testControlImpl) handleToggleDetailedLogging(res http.ResponseWriter, req *http.Request) {
type request struct {
EnableDetailedLogging bool `json:"enableDetailedLogging"`
Expand Down Expand Up @@ -171,6 +163,22 @@ func (t *testControlImpl) handleLogTestName(res http.ResponseWriter, req *http.R
})
}

func (t *testControlImpl) handleSuspendProcessing(res http.ResponseWriter, req *http.Request) {
type request struct{}
v := &request{}
handle(res, req, v, func(ctx context.Context, v *request) error {
return t.SuspendProcessing(ctx)
})
}

func (t *testControlImpl) handleResumeProcessing(res http.ResponseWriter, req *http.Request) {
type request struct{}
v := &request{}
handle(res, req, v, func(ctx context.Context, v *request) error {
return t.ResumeProcessing(ctx)
})
}

func (t *testControlImpl) handleHealth(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
res.Write([]byte(`{
Expand Down
39 changes: 21 additions & 18 deletions test-control-go/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ import (
)

type testTestControl struct {
didSetNamespace bool
namespace string
errorOnSetNamespace error
didLogTestName bool
testName string
errorOnLogTestName error
}

func (t *testTestControl) SetNamespace(ctx context.Context, newNamespace string) error {
t.didSetNamespace = true
t.namespace = newNamespace
return t.errorOnSetNamespace
}
func (t *testTestControl) InjectConfig(ctx context.Context, key string, value string) error {
return nil
}
Expand All @@ -36,15 +31,23 @@ func (t *testTestControl) ClearStorage(ctx context.Context) error {
return nil
}
func (t *testTestControl) LogTestName(ctx context.Context, testName string) error {
return nil
t.didLogTestName = true
t.testName = testName
return t.errorOnLogTestName
}
func (t *testTestControl) ToggleDetailedLogging(ctx context.Context, enable bool) error {
return nil
}
func (t *testTestControl) SuspendProcessing(ctx context.Context) error {
return nil
}
func (t *testTestControl) ResumeProcessing(ctx context.Context) error {
return nil
}

func TestSetNamespace(t *testing.T) {
func TestLogTestName(t *testing.T) {
withTestControlServer(t, func(t *testing.T, testControlBaseUrl string, impl *testTestControl) {
request, err := http.NewRequest(http.MethodPut, testControlBaseUrl+"/io-pkb-testcontrol-setNamespace", strings.NewReader(`{"newNamespace": "fooNamespace"}`))
request, err := http.NewRequest(http.MethodPut, testControlBaseUrl+"/io-pkb-testcontrol-logTestName", strings.NewReader(`{"testName": "class#method"}`))
if err != nil {
t.Fatal(err)
}
Expand All @@ -57,19 +60,19 @@ func TestSetNamespace(t *testing.T) {
t.Fatalf("unexpected status %s", response.Status)
}

if !impl.didSetNamespace {
t.Errorf("expected didSetNamespace=true")
if !impl.didLogTestName {
t.Errorf("expected didLogTestName=true")
}
if impl.namespace != "fooNamespace" {
t.Errorf("expected namespace=fooNamespace")
if impl.testName != "class#method" {
t.Errorf("expected class#method")
}
})
}

func TestSetNamespaceError(t *testing.T) {
func TestLogTestNameError(t *testing.T) {
withTestControlServer(t, func(t *testing.T, testControlBaseUrl string, impl *testTestControl) {
impl.errorOnSetNamespace = fmt.Errorf("oh noes an error")
request, err := http.NewRequest(http.MethodPut, testControlBaseUrl+"/io-pkb-testcontrol-setNamespace", strings.NewReader(`{"newNamespace": "fooNamespace"}`))
impl.errorOnLogTestName = fmt.Errorf("oh noes an error")
request, err := http.NewRequest(http.MethodPut, testControlBaseUrl+"/io-pkb-testcontrol-logTestName", strings.NewReader(`{"testName": "class#method"}`))
if err != nil {
t.Fatal(err)
}
Expand Down

This file was deleted.

Loading

0 comments on commit 090ad30

Please sign in to comment.