Skip to content

Commit

Permalink
add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Jun 12, 2024
1 parent 5f01e89 commit 344fd28
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
2 changes: 1 addition & 1 deletion funcframework/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func convertBackgroundToCloudEvent(ceHandler http.Handler) http.Handler {
return
}
}
r, cancel := setTimeoutContextIfRequested(r)
r, cancel := setContextTimeoutIfRequested(r)

Check failure on line 195 in funcframework/events.go

View workflow job for this annotation

GitHub Actions / test (1.20, ubuntu-latest)

undefined: setContextTimeoutIfRequested

Check failure on line 195 in funcframework/events.go

View workflow job for this annotation

GitHub Actions / test (1.20, macos-latest)

undefined: setContextTimeoutIfRequested

Check failure on line 195 in funcframework/events.go

View workflow job for this annotation

GitHub Actions / test (1.21, ubuntu-latest)

undefined: setContextTimeoutIfRequested

Check failure on line 195 in funcframework/events.go

View workflow job for this annotation

GitHub Actions / test (1.21, macos-latest)

undefined: setContextTimeoutIfRequested

Check failure on line 195 in funcframework/events.go

View workflow job for this annotation

GitHub Actions / build (1.13)

undefined: setContextTimeoutIfRequested

Check failure on line 195 in funcframework/events.go

View workflow job for this annotation

GitHub Actions / build (1.16)

undefined: setContextTimeoutIfRequested

Check failure on line 195 in funcframework/events.go

View workflow job for this annotation

GitHub Actions / build (1.18)

undefined: setContextTimeoutIfRequested
if cancel != nil {
defer cancel()
}
Expand Down
2 changes: 1 addition & 1 deletion funcframework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func setTimeoutContextIfRequested(r *http.Request) (*http.Request, func()) {
}
timeoutSecs, err := strconv.Atoi(timeoutStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not parse CLOUD_RUN_TIMEOUT_SECONDS as an integer value in seconds: %v", err)
fmt.Fprintf(os.Stderr, "Could not parse CLOUD_RUN_TIMEOUT_SECONDS as an integer value in seconds: %v\n", err)
return r, nil
}
ctx, cancel := context.WithTimeout(r.Context(), time.Duration(timeoutSecs)*time.Second)
Expand Down
116 changes: 116 additions & 0 deletions funcframework/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/registry"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -995,6 +997,120 @@ func TestServeMultipleFunctions(t *testing.T) {
}
}

func TestHTTPRequestTimeout(t *testing.T) {
timeoutEnvVar := "CLOUD_RUN_TIMEOUT_SECONDS"
prev := os.Getenv(timeoutEnvVar)
defer os.Setenv(timeoutEnvVar, prev)
defer cleanup()

cloudeventsJSON := []byte(`{
"specversion" : "1.0",
"type" : "com.github.pull.create",
"source" : "https://github.com/cloudevents/spec/pull",
"subject" : "123",
"id" : "A234-1234-1234",
"time" : "2018-04-05T17:31:00Z",
"comexampleextension1" : "value",
"datacontenttype" : "application/xml",
"data" : "<much wow=\"xml\"/>"
}`)

tcs := []struct {
name string
wantDeadline bool
wantExpired bool
timeout string
}{
{
name: "deadline not requested",
wantDeadline: false,
wantExpired: false,
timeout: "",
},
{
name: "NaN deadline",
wantDeadline: false,
wantExpired: false,
timeout: "aaa",
},
{
name: "zero deadline",
wantDeadline: true,
wantExpired: true,
timeout: "0",
},
{
name: "very long deadline",
wantDeadline: true,
wantExpired: false,
timeout: "3600",
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
defer cleanup()
os.Setenv(timeoutEnvVar, tc.timeout)

var httpReqCtx context.Context
functions.HTTP("http", func(w http.ResponseWriter, r *http.Request) {
httpReqCtx = r.Context()
})
var ceReqCtx context.Context
functions.CloudEvent("cloudevent", func(ctx context.Context, event event.Event) error {
ceReqCtx = ctx
return nil
})
server, err := initServer()
if err != nil {
t.Fatalf("initServer(): %v", err)
}
srv := httptest.NewServer(server)
defer srv.Close()

t.Run("http", func(t *testing.T) {
_, err = http.Get(srv.URL + "/http")
if err != nil {
t.Fatalf("expected success")
}
if httpReqCtx == nil {
t.Fatalf("expected non-nil request context")
}
deadline, ok := httpReqCtx.Deadline()
if ok != tc.wantDeadline {
t.Fatalf("expected deadline %v but got %v", tc.wantDeadline, ok)
}
if expired := deadline.Before(time.Now()); ok && expired != tc.wantExpired {
t.Fatalf("expected expired %v but got %v", tc.wantExpired, expired)
}
})

t.Run("cloudevent", func(t *testing.T) {
req, err := http.NewRequest("POST", srv.URL+"/cloudevent", bytes.NewBuffer(cloudeventsJSON))
if err != nil {
t.Fatalf("failed to create request")
}
req.Header.Add("Content-Type", "application/cloudevents+json")
_, err = (&http.Client{}).Do(req)
if err != nil {
t.Fatalf("request failed")
}

if ceReqCtx == nil {
t.Fatalf("expected non-nil request context")
}
deadline, ok := ceReqCtx.Deadline()
if ok != tc.wantDeadline {
t.Errorf("expected deadline %v but got %v", tc.wantDeadline, ok)
}
if expired := deadline.Before(time.Now()); ok && expired != tc.wantExpired {
t.Errorf("expected expired %v but got %v", tc.wantExpired, expired)
}
})
})
}
}

func cleanup() {
os.Unsetenv("FUNCTION_TARGET")
registry.Default().Reset()
Expand Down

0 comments on commit 344fd28

Please # to comment.