Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be31358

Browse files
committed
returning the error when doing a nack in the message
Signed-off-by: cpanato <[email protected]>
1 parent a32af47 commit be31358

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

protocol/pubsub/v2/message.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ func (m *Message) GetExtension(name string) interface{} {
120120
}
121121

122122
// Finish marks the message to be forgotten.
123-
// If err is nil, the underlying Psubsub message will be acked;
124-
// otherwise nacked.
123+
// If err is nil, the underlying Pubsub message will be acked;
124+
// otherwise nacked and return the error.
125125
func (m *Message) Finish(err error) error {
126126
if err != nil {
127127
m.internal.Nack()
128-
} else {
129-
m.internal.Ack()
128+
return err
130129
}
130+
131+
m.internal.Ack()
131132
return nil
132133
}

protocol/pubsub/v2/message_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package pubsub
77

88
import (
99
"context"
10+
"fmt"
1011
"testing"
1112

1213
"cloud.google.com/go/pubsub"
@@ -45,3 +46,42 @@ func TestReadStructured(t *testing.T) {
4546
})
4647
}
4748
}
49+
50+
func TestFinish(t *testing.T) {
51+
tests := []struct {
52+
name string
53+
pm *pubsub.Message
54+
err error
55+
wantErr bool
56+
}{
57+
{
58+
name: "return error",
59+
pm: &pubsub.Message{
60+
ID: "testid",
61+
},
62+
err: fmt.Errorf("error"),
63+
wantErr: true,
64+
},
65+
{
66+
name: "no errors",
67+
pm: &pubsub.Message{
68+
ID: "testid",
69+
},
70+
wantErr: false,
71+
},
72+
}
73+
for _, tc := range tests {
74+
t.Run(tc.name, func(t *testing.T) {
75+
msg := NewMessage(tc.pm)
76+
err := msg.Finish(tc.err)
77+
if tc.wantErr {
78+
if err != tc.err {
79+
t.Errorf("Error mismatch. got: %v, want: %v", err, tc.err)
80+
}
81+
}
82+
if !tc.wantErr && err != nil {
83+
t.Errorf("Should not error but got: %v", err)
84+
}
85+
})
86+
}
87+
}

0 commit comments

Comments
 (0)