Skip to content

Commit

Permalink
wip test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zevdg committed Jul 14, 2022
1 parent af02bb3 commit 82c5a38
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions internal/api.go
Expand Up @@ -73,13 +73,13 @@ func apiURL(ctx netcontext.Context) *url.URL {
if h := os.Getenv("API_HOST"); h != "" {
host = h
}
if hostOverride := ctx.Value(&apiHostOverrideKey); hostOverride != nil {
if hostOverride := ctx.Value(apiHostOverrideKey); hostOverride != nil {
host = hostOverride.(string)
}
if p := os.Getenv("API_PORT"); p != "" {
port = p
}
if portOverride := ctx.Value(&apiPortOverrideKey); portOverride != nil {
if portOverride := ctx.Value(apiPortOverrideKey); portOverride != nil {
port = portOverride.(string)
}
return &url.URL{
Expand Down
14 changes: 10 additions & 4 deletions internal/api_common.go
Expand Up @@ -12,6 +12,12 @@ import (
"github.com/golang/protobuf/proto"
)

type ctxKey string

func (c ctxKey) String() string {
return "appengine context key: " + string(c)
}

var errNotAppEngineContext = errors.New("not an App Engine context")

type CallOverrideFunc func(ctx netcontext.Context, service, method string, in, out proto.Message) error
Expand Down Expand Up @@ -55,16 +61,16 @@ func WithAppIDOverride(ctx netcontext.Context, appID string) netcontext.Context
return netcontext.WithValue(ctx, &appIDOverrideKey, appID)
}

var apiHostOverrideKey = "holds a string, being the alternate API_HOST"
var apiHostOverrideKey = ctxKey("holds a string, being the alternate API_HOST")

func withAPIHostOverride(ctx netcontext.Context, apiHost string) netcontext.Context {
return netcontext.WithValue(ctx, &apiHostOverrideKey, apiHost)
return netcontext.WithValue(ctx, apiHostOverrideKey, apiHost)
}

var apiPortOverrideKey = "holds a string, being the alternate API_PORT"
var apiPortOverrideKey = ctxKey("holds a string, being the alternate API_PORT")

func withAPIPortOverride(ctx netcontext.Context, apiPort string) netcontext.Context {
return netcontext.WithValue(ctx, &apiPortOverrideKey, apiPort)
return netcontext.WithValue(ctx, apiPortOverrideKey, apiPort)
}

var namespaceKey = "holds the namespace string"
Expand Down
8 changes: 7 additions & 1 deletion internal/api_test.go
Expand Up @@ -19,6 +19,7 @@ import (
"net/url"
"os"
"os/exec"
"runtime"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -467,7 +468,12 @@ func TestAPICallAllocations(t *testing.T) {
}

// Lots of room for improvement...
const min, max float64 = 60, 86
var min, max float64 = 60, 86
if strings.HasPrefix(runtime.Version(), "go1.11.") || strings.HasPrefix(runtime.Version(), "go1.12.") {
// add a bit more overhead for versions before go1.13
// see https://go.dev/doc/go1.13#compilers
max = 90
}
if avg < min || max < avg {
t.Errorf("Allocations per API call = %g, want in [%g,%g]", avg, min, max)
}
Expand Down
4 changes: 2 additions & 2 deletions v2/internal/api.go
Expand Up @@ -68,13 +68,13 @@ func apiURL(ctx netcontext.Context) *url.URL {
if h := os.Getenv("API_HOST"); h != "" {
host = h
}
if hostOverride := ctx.Value(&apiHostOverrideKey); hostOverride != nil {
if hostOverride := ctx.Value(apiHostOverrideKey); hostOverride != nil {
host = hostOverride.(string)
}
if p := os.Getenv("API_PORT"); p != "" {
port = p
}
if portOverride := ctx.Value(&apiPortOverrideKey); portOverride != nil {
if portOverride := ctx.Value(apiPortOverrideKey); portOverride != nil {
port = portOverride.(string)
}
return &url.URL{
Expand Down
14 changes: 10 additions & 4 deletions v2/internal/api_common.go
Expand Up @@ -12,6 +12,12 @@ import (
"github.com/golang/protobuf/proto"
)

type ctxKey string

func (c ctxKey) String() string {
return "appengine context key: " + string(c)
}

var errNotAppEngineContext = errors.New("not an App Engine context")

type CallOverrideFunc func(ctx netcontext.Context, service, method string, in, out proto.Message) error
Expand Down Expand Up @@ -55,16 +61,16 @@ func WithAppIDOverride(ctx netcontext.Context, appID string) netcontext.Context
return netcontext.WithValue(ctx, &appIDOverrideKey, appID)
}

var apiHostOverrideKey = "holds a string, being the alternate API_HOST"
var apiHostOverrideKey = ctxKey("holds a string, being the alternate API_HOST")

func withAPIHostOverride(ctx netcontext.Context, apiHost string) netcontext.Context {
return netcontext.WithValue(ctx, &apiHostOverrideKey, apiHost)
return netcontext.WithValue(ctx, apiHostOverrideKey, apiHost)
}

var apiPortOverrideKey = "holds a string, being the alternate API_PORT"
var apiPortOverrideKey = ctxKey("holds a string, being the alternate API_PORT")

func withAPIPortOverride(ctx netcontext.Context, apiPort string) netcontext.Context {
return netcontext.WithValue(ctx, &apiPortOverrideKey, apiPort)
return netcontext.WithValue(ctx, apiPortOverrideKey, apiPort)
}

var namespaceKey = "holds the namespace string"
Expand Down

0 comments on commit 82c5a38

Please sign in to comment.