Skip to content

Commit

Permalink
Fix cloudevent adapter panic (#5425)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Moss <benm@vmware.com>
  • Loading branch information
knative-prow-robot and Ben Moss authored May 21, 2021
1 parent e3ad363 commit aa5d54e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/adapter/v2/cloudevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ func (c *client) reportMetrics(ctx context.Context, event cloudevents.Event, res
var res *http.Result
if !cloudevents.ResultAs(retryResult, &res) {
c.reportError(reportArgs, result)
} else {
c.reporter.ReportRetryEventCount(reportArgs, res.StatusCode)
}
c.reporter.ReportRetryEventCount(reportArgs, res.StatusCode)
}
}
}
Expand Down
25 changes: 21 additions & 4 deletions pkg/adapter/v2/cloudevents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ func TestNewCloudEventsClient_send(t *testing.T) {
timeout int
wantErr bool
wantRetryCount bool
wantEventCount int
}{
"timeout": {timeout: 13},
"none": {},
"send": {
event: demoEvent(),
event: demoEvent(),
wantEventCount: 1,
},
"send with retries": {
event: func() *cloudevents.Event {
Expand All @@ -79,12 +81,14 @@ func TestNewCloudEventsClient_send(t *testing.T) {
return &event
}(),
wantRetryCount: true,
wantEventCount: 1,
},
"send with ceOverrides": {
event: demoEvent(),
ceOverrides: &duckv1.CloudEventOverrides{Extensions: map[string]string{
"foo": "bar",
}},
wantEventCount: 1,
},
"send fails": {
event: func() *cloudevents.Event {
Expand All @@ -94,7 +98,8 @@ func TestNewCloudEventsClient_send(t *testing.T) {
event.SetType("unit.sendFail")
return &event
}(),
wantErr: true,
wantErr: true,
wantEventCount: 1,
},
"not a http result": {
event: func() *cloudevents.Event {
Expand All @@ -104,7 +109,19 @@ func TestNewCloudEventsClient_send(t *testing.T) {
event.SetType("unit.wantErr")
return &event
}(),
wantErr: true,
wantErr: true,
wantEventCount: 1,
},
"retry that is not a http result": {
event: func() *cloudevents.Event {
event := cloudevents.NewEvent()
event.SetID("abc-123")
event.SetSource("unit/test")
event.SetType("unit.nonHttpRetry")
return &event
}(),
wantErr: false,
wantEventCount: 2,
},
}

Expand Down Expand Up @@ -181,7 +198,7 @@ func TestNewCloudEventsClient_send(t *testing.T) {
t.Fatal(err)
}
validateSent(t, innerClient, tc.event.Type())
validateMetric(t, got.reporter, 1, tc.wantRetryCount)
validateMetric(t, got.reporter, tc.wantEventCount, tc.wantRetryCount)
} else {
validateNotSent(t, innerClient)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/adapter/v2/test/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func (c *TestCloudEventsClient) Send(ctx context.Context, out event.Event) proto
return errors.New("totally not an http result")
} else if eventData.Type == "unit.sendFail" {
return http.NewResult(400, "%w", protocol.ResultNACK)
} else if eventData.Type == "unit.nonHttpRetry" {
var attempts []protocol.Result
attempts = append(attempts, errors.New("totally not an http result"))
return http.NewRetriesResult(http.NewResult(200, "%w", protocol.ResultACK), 1, time.Now(), attempts)
}
return http.NewResult(200, "%w", protocol.ResultACK)
}
Expand Down

0 comments on commit aa5d54e

Please sign in to comment.