From 1e2dacc786c992e4e486d2db16eb019e0f6e2e0f Mon Sep 17 00:00:00 2001 From: Sayan Hazra Date: Fri, 20 Nov 2020 14:39:38 +0100 Subject: [PATCH] Make tests run in parallel --- .../event-publisher-proxy/pkg/env/config.go | 2 +- .../pkg/handler/handler_test.go | 48 +++++++++++++++++-- .../pkg/legacy-events/legacy.go | 6 ++- .../pkg/legacy-events/legacy_test.go | 6 +-- 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/components/event-publisher-proxy/pkg/env/config.go b/components/event-publisher-proxy/pkg/env/config.go index bf8ccdf6e042..494adf58fac1 100644 --- a/components/event-publisher-proxy/pkg/env/config.go +++ b/components/event-publisher-proxy/pkg/env/config.go @@ -18,7 +18,7 @@ type Config struct { // BEBNamespace is the name of the namespace in BEB which is used as the event source for legacy events BEBNamespace string `envconfig:"BEB_NAMESPACE" required:"true"` // EventTypePrefix is the prefix of each event as per the eventing specification - // It follows the eventType format: .. + // It follows the eventType format: ... EventTypePrefix string `envconfig:"EVENT_TYPE_PREFIX" default:""` } diff --git a/components/event-publisher-proxy/pkg/handler/handler_test.go b/components/event-publisher-proxy/pkg/handler/handler_test.go index da71430967d3..06e474d062b6 100644 --- a/components/event-publisher-proxy/pkg/handler/handler_test.go +++ b/components/event-publisher-proxy/pkg/handler/handler_test.go @@ -2,12 +2,15 @@ package handler import ( "context" + "crypto/rand" "encoding/json" "fmt" + "io" "io/ioutil" "net/http" "reflect" "regexp" + "strconv" "testing" "time" @@ -120,8 +123,12 @@ func TestHandler(t *testing.T) { }, } + port, err := generatePort() + if err != nil { + t.Fatalf("failed to generate port: %v", err) + } + var ( - port = 8888 healthEndpoint = fmt.Sprintf("http://localhost:%d/healthz", port) publishEndpoint = fmt.Sprintf("http://localhost:%d/publish", port) ) @@ -175,14 +182,16 @@ func TestHandler(t *testing.T) { func TestHandlerTimeout(t *testing.T) { t.Parallel() + port, err := generatePort() + if err != nil { + t.Fatalf("failed to generate port: %v", err) + } var ( - port = 9999 requestTimeout = time.Nanosecond // short request timeout serverResponseTime = time.Millisecond // long server response time healthEndpoint = fmt.Sprintf("http://localhost:%d/healthz", port) publishEndpoint = fmt.Sprintf("http://localhost:%d/publish", port) ) - mockServer := testingutils.NewMockServer(testingutils.WithResponseTime(serverResponseTime)) mockServer.Start(t, tokenEndpoint, eventsEndpoint, "") defer mockServer.Close() @@ -249,8 +258,12 @@ func waitForHandlerToStart(t *testing.T, healthEndpoint string) { } func TestHandlerForLegacyEvents(t *testing.T) { + t.Parallel() + port, err := generatePort() + if err != nil { + t.Fatalf("failed to generate port: %v", err) + } var ( - port = 8888 healthEndpoint = fmt.Sprintf("http://localhost:%d/healthz", port) publishLegacyEndpoint = fmt.Sprintf("http://localhost:%d/app/v1/events", port) bebNs = "/beb.namespace" @@ -704,3 +717,30 @@ func validateOkResponse(t *testing.T, resp http.Response, tcWantResponse *legacy t.Errorf("invalid status, want: %v, got: %v", tcWantResponse.Ok.Status, legacyResponse.Ok.Status) } } + +// generatePort generates a random 5 digit port +func generatePort() (int, error) { + max := 4 + // Add 4 as prefix to make it 5 digits but less than 65535 + add4AsPrefix := "4" + b := make([]byte, max) + n, err := io.ReadAtLeast(rand.Reader, b, max) + if n != max { + return 0, err + } + if err != nil { + return 0, err + } + for i := 0; i < len(b); i++ { + b[i] = table[int(b[i])%len(table)] + } + + num, err := strconv.Atoi(fmt.Sprintf("%s%s", add4AsPrefix, string(b))) + if err != nil { + return 0, err + } + + return num, nil +} + +var table = [...]byte{'1', '2', '3', '4', '5', '6', '7', '8', '9'} diff --git a/components/event-publisher-proxy/pkg/legacy-events/legacy.go b/components/event-publisher-proxy/pkg/legacy-events/legacy.go index 1189e4c6da9c..0684adc9bffb 100644 --- a/components/event-publisher-proxy/pkg/legacy-events/legacy.go +++ b/components/event-publisher-proxy/pkg/legacy-events/legacy.go @@ -18,6 +18,10 @@ import ( var ( isValidEventTypeVersion = regexp.MustCompile(AllowedEventTypeVersionChars).MatchString isValidEventID = regexp.MustCompile(AllowedEventIDChars).MatchString + // eventTypePrefixFormat is driven by BEB specification. + // An eventtype must have atleast 4 segments separated by dots in the form of: + // ... + eventTypePrefixFormat = "%s.%s.%s.%s" ) type Transformer struct { @@ -203,6 +207,6 @@ func (t Transformer) convertPublishRequestToCloudEvent(appName string, publishRe // formatEventType4BEB format eventType as per BEB spec func formatEventType4BEB(eventTypePrefix, app, eventType, version string) string { - eventType4BEB := fmt.Sprintf("%s%s.%s.%s", eventTypePrefix, app, eventType, version) + eventType4BEB := fmt.Sprintf(eventTypePrefixFormat, eventTypePrefix, app, eventType, version) return strings.ReplaceAll(eventType4BEB, "-", ".") } diff --git a/components/event-publisher-proxy/pkg/legacy-events/legacy_test.go b/components/event-publisher-proxy/pkg/legacy-events/legacy_test.go index acb6d7ce6bf6..9994bb576f59 100644 --- a/components/event-publisher-proxy/pkg/legacy-events/legacy_test.go +++ b/components/event-publisher-proxy/pkg/legacy-events/legacy_test.go @@ -42,14 +42,14 @@ func TestFormatEventType4BEB(t *testing.T) { wantedEventType string }{ { - eventTypePrefix: "prefix.", + eventTypePrefix: "prefix", app: "app", eventType: "order.foo", version: "v1", wantedEventType: "prefix.app.order.foo.v1", }, { - eventTypePrefix: "prefix.", + eventTypePrefix: "prefix", app: "app", eventType: "order-foo", version: "v1", @@ -68,7 +68,7 @@ func TestFormatEventType4BEB(t *testing.T) { func TestConvertPublishRequestToCloudEvent(t *testing.T) { bebNs := "beb.namespace" - eventTypePrefix := "event.type.prefix." + eventTypePrefix := "event.type.prefix" legacyTransformer := NewTransformer(bebNs, eventTypePrefix) eventID := "id" appName := "foo-app"