Skip to content

Commit

Permalink
Make tests run in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
sayanh committed Nov 20, 2020
1 parent b6a29fe commit 1e2dacc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion components/event-publisher-proxy/pkg/env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: <eventTypePrefix><appName>.<event-name>.<version>
// It follows the eventType format: <eventTypePrefix>.<appName>.<event-name>.<version>
EventTypePrefix string `envconfig:"EVENT_TYPE_PREFIX" default:""`
}

Expand Down
48 changes: 44 additions & 4 deletions components/event-publisher-proxy/pkg/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package handler

import (
"context"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"regexp"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -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)
)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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'}
6 changes: 5 additions & 1 deletion components/event-publisher-proxy/pkg/legacy-events/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// <domainNamespace>.<businessObjectName>.<operation>.<version>
eventTypePrefixFormat = "%s.%s.%s.%s"
)

type Transformer struct {
Expand Down Expand Up @@ -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, "-", ".")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down

0 comments on commit 1e2dacc

Please sign in to comment.