-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement retry mechanism for known retriable errors (#2)
Wrote retry feature: - maxRetries: 5 Fixed Typo in README: - `go tidy` -> `go mod tidy`
- Loading branch information
1 parent
753d214
commit 769d008
Showing
7 changed files
with
119 additions
and
561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package email | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/smtp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAccEmail_basic(t *testing.T) { | ||
|
@@ -57,6 +60,59 @@ func testAccCheckEmailExists(n string) resource.TestCheckFunc { | |
return nil | ||
} | ||
} | ||
func mockSendMailReturn421(addr string, a smtp.Auth, from string, to []string, msg []byte) error { | ||
return errors.New("421 Service not available") | ||
} | ||
|
||
// function to test non-421 error | ||
func mockReturn500(addr string, a smtp.Auth, from string, to []string, msg []byte) error { | ||
return errors.New("500 Internal Server Error") | ||
} | ||
|
||
// email to return no error | ||
func mockSendMailSuccess(addr string, a smtp.Auth, from string, to []string, msg []byte) error { | ||
return nil | ||
} | ||
|
||
func TestRetryWorkflow(t *testing.T) { | ||
// define max retries | ||
maxRetries := 5 | ||
// write the tests | ||
tests := []struct { | ||
name string | ||
sendMailFunc SendMailFunc | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "Retry with 421 error", | ||
sendMailFunc: mockSendMailReturn421, | ||
expectedErr: errors.New("421 Service not available"), | ||
}, | ||
{ | ||
name: "Success on first try", | ||
sendMailFunc: mockSendMailSuccess, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "Other error", | ||
sendMailFunc: mockReturn500, | ||
expectedErr: errors.New("500 Internal Server Error"), | ||
}, | ||
} | ||
// execute the tests | ||
for _, test := range tests { | ||
// run subtests | ||
t.Run(test.name, func(t *testing.T) { | ||
err := sendMail(test.sendMailFunc, maxRetries, "localhost", "2525", "username", "password", "[email protected]", "[email protected]", "message") | ||
if test.expectedErr != nil { | ||
// assert that the errors are equal | ||
assert.EqualError(t, err, test.expectedErr.Error()) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Requires a local SMTP server running on port 2525 | ||
// `docker run --rm -it -p 3000:80 -p 2525:25 rnwood/smtp4dev:v3` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ terraform { | |
|
||
provider "email" {} | ||
|
||
|
||
resource "email_email" "example" { | ||
to = "[email protected]" | ||
from = "[email protected]" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.