-
Notifications
You must be signed in to change notification settings - Fork 684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add retries to sendgrid emailer (#618) #6164
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
package implementations | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/sendgrid/rest" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/flyteorg/flyte/flyteadmin/pkg/async/notifications/mocks" | ||
runtimeInterfaces "github.com/flyteorg/flyte/flyteadmin/pkg/runtime/interfaces" | ||
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/admin" | ||
"github.com/flyteorg/flyte/flytestdlib/promutils" | ||
) | ||
|
||
func TestAddresses(t *testing.T) { | ||
addresses := []string{"[email protected]", "[email protected]"} | ||
sgAddresses := getEmailAddresses(addresses) | ||
assert.Equal(t, sgAddresses[0].Address, "[email protected]") | ||
assert.Equal(t, sgAddresses[1].Address, "[email protected]") | ||
} | ||
|
||
func TestGetEmail(t *testing.T) { | ||
emailNotification := &admin.EmailMessage{ | ||
var ( | ||
emailNotification = &admin.EmailMessage{ | ||
Comment on lines
+22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider test data scope placement
Consider keeping test data within test functions rather than as package-level variables to maintain test isolation and clarity. Each test should be self-contained with its own test data. Code suggestionCheck the AI-generated fix before applying
Code Review Run #255086 Is this a valid issue, or was it incorrectly flagged by the Agent?
|
||
SubjectLine: "Notice: Execution \"name\" has succeeded in \"domain\".", | ||
SenderEmail: "[email protected]", | ||
RecipientsEmail: []string{ | ||
|
@@ -32,7 +31,16 @@ func TestGetEmail(t *testing.T) { | |
"<a href=\"https://example.com/executions/T/B/D\">" + | ||
"https://example.com/executions/T/B/D</a>.", | ||
} | ||
) | ||
|
||
func TestAddresses(t *testing.T) { | ||
addresses := []string{"[email protected]", "[email protected]"} | ||
sgAddresses := getEmailAddresses(addresses) | ||
assert.Equal(t, sgAddresses[0].Address, "[email protected]") | ||
assert.Equal(t, sgAddresses[1].Address, "[email protected]") | ||
} | ||
|
||
func TestGetEmail(t *testing.T) { | ||
sgEmail := getSendgridEmail(emailNotification) | ||
assert.Equal(t, `Notice: Execution "name" has succeeded in "domain".`, sgEmail.Personalizations[0].Subject) | ||
assert.Equal(t, "[email protected]", sgEmail.Personalizations[0].To[1].Address) | ||
|
@@ -98,3 +106,65 @@ func TestNoFile(t *testing.T) { | |
// shouldn't reach here | ||
t.Errorf("did not panic") | ||
} | ||
|
||
func TestSendEmail(t *testing.T) { | ||
ctx := context.TODO() | ||
expectedErr := errors.New("expected") | ||
t.Run("exhaust all retry attempts", func(t *testing.T) { | ||
sendgridClient := &mocks.SendgridClient{} | ||
expectedEmail := getSendgridEmail(emailNotification) | ||
sendgridClient.OnSendMatch(expectedEmail). | ||
Return(nil, expectedErr).Times(3) | ||
sendgridClient.OnSendMatch(expectedEmail). | ||
Return(&rest.Response{Body: "email body"}, nil).Once() | ||
scope := promutils.NewScope("bademailer") | ||
emailerMetrics := newEmailMetrics(scope) | ||
|
||
emailer := SendgridEmailer{ | ||
client: sendgridClient, | ||
systemMetrics: emailerMetrics, | ||
cfg: &runtimeInterfaces.NotificationsConfig{ | ||
ReconnectAttempts: 1, | ||
}, | ||
} | ||
|
||
err := emailer.SendEmail(ctx, emailNotification) | ||
assert.EqualError(t, err, expectedErr.Error()) | ||
|
||
assert.NoError(t, testutil.CollectAndCompare(emailerMetrics.SendError, strings.NewReader(` | ||
# HELP bademailer:send_error Number of errors when sending email via Emailer | ||
# TYPE bademailer:send_error counter | ||
bademailer:send_error 1 | ||
`))) | ||
}) | ||
t.Run("succeed within allowed retry attempts", func(t *testing.T) { | ||
t.Run("exhaust all retry attempts", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider removing duplicate test case description
Consider removing the nested Code suggestionCheck the AI-generated fix before applying
Code Review Run #63207a Is this a valid issue, or was it incorrectly flagged by the Agent?
|
||
ctx := context.TODO() | ||
sendgridClient := &mocks.SendgridClient{} | ||
expectedEmail := getSendgridEmail(emailNotification) | ||
sendgridClient.OnSendMatch(expectedEmail). | ||
Return(nil, expectedErr).Once() | ||
sendgridClient.OnSendMatch(expectedEmail). | ||
Return(&rest.Response{Body: "email body"}, nil).Once() | ||
scope := promutils.NewScope("goodemailer") | ||
emailerMetrics := newEmailMetrics(scope) | ||
|
||
emailer := SendgridEmailer{ | ||
client: sendgridClient, | ||
systemMetrics: emailerMetrics, | ||
cfg: &runtimeInterfaces.NotificationsConfig{ | ||
ReconnectAttempts: 1, | ||
}, | ||
} | ||
|
||
err := emailer.SendEmail(ctx, emailNotification) | ||
assert.NoError(t, err) | ||
|
||
assert.NoError(t, testutil.CollectAndCompare(emailerMetrics.SendError, strings.NewReader(` | ||
# HELP goodemailer:send_error Number of errors when sending email via Emailer | ||
# TYPE goodemailer:send_error counter | ||
goodemailer:send_error 0 | ||
`))) | ||
}) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider initializing the
cfg
field with just the requiredNotificationsEmailerConfig
instead of storing the entire config struct. This would reduce memory usage and coupling.Code suggestion
Code Review Run #63207a
Is this a valid issue, or was it incorrectly flagged by the Agent?