Skip to content

Commit

Permalink
fix: pubsub ack must rely on protocol.IsAck
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Chapurlat <nc@coorganix.com>
  • Loading branch information
chapurlatn committed Jun 20, 2024
1 parent 972dfc7 commit 61c7e78
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
15 changes: 8 additions & 7 deletions protocol/pubsub/v2/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/binding/format"
"github.com/cloudevents/sdk-go/v2/binding/spec"
"github.com/cloudevents/sdk-go/v2/protocol"
)

const (
Expand Down Expand Up @@ -120,14 +121,14 @@ func (m *Message) GetExtension(name string) interface{} {
}

// Finish marks the message to be forgotten.
// If err is nil, the underlying Pubsub message will be acked;
// otherwise nacked and return the error.
// Regarding protocol.IsAck, the Pubsub message will be acked or not.
// If not acked, the original error will be returned.
func (m *Message) Finish(err error) error {
if err != nil {
m.internal.Nack()
return err
if protocol.IsACK(err) {
m.internal.Ack()
return nil
}

m.internal.Ack()
return nil
m.internal.Nack()
return err
}
17 changes: 17 additions & 0 deletions protocol/pubsub/v2/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"cloud.google.com/go/pubsub"
"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/cloudevents/sdk-go/v2/protocol"
)

func TestReadStructured(t *testing.T) {
Expand Down Expand Up @@ -62,6 +63,22 @@ func TestFinish(t *testing.T) {
err: fmt.Errorf("error"),
wantErr: true,
},
{
name: "result not acked",
pm: &pubsub.Message{
ID: "testid",
},
err: protocol.NewReceipt(false, "error"),
wantErr: true,
},
{
name: "result acked",
pm: &pubsub.Message{
ID: "testid",
},
err: protocol.NewReceipt(true, "error"),
wantErr: false,
},
{
name: "no errors",
pm: &pubsub.Message{
Expand Down

0 comments on commit 61c7e78

Please sign in to comment.