From bf5e62b45092087cf07d99a52cff07db8dcb7f65 Mon Sep 17 00:00:00 2001 From: MikeMwita Date: Sat, 6 Jul 2024 00:40:34 +0300 Subject: [PATCH 1/2] feat: Implement indempontecy-and-retries --- .idea/GitLink.xml | 6 ++++++ pkg/sms/sms_sender.go | 23 ++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 .idea/GitLink.xml diff --git a/.idea/GitLink.xml b/.idea/GitLink.xml new file mode 100644 index 0000000..009597c --- /dev/null +++ b/.idea/GitLink.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/pkg/sms/sms_sender.go b/pkg/sms/sms_sender.go index 1b6626e..955ad89 100644 --- a/pkg/sms/sms_sender.go +++ b/pkg/sms/sms_sender.go @@ -3,6 +3,7 @@ package sms import ( "encoding/json" "fmt" + "math/rand" "net/http" "net/url" "strings" @@ -143,17 +144,21 @@ func (s *SmsSender) SendSMS() (SmsSenderResponse, error) { return smsSenderResponse, fmt.Errorf("status code: %d", res.StatusCode) } -// Retry sends an SMS with exponential backoff + func (s *SmsSender) RetrySendSMS(maxRetries int) (SmsSenderResponse, error) { for retry := 0; retry < maxRetries; retry++ { - response, err := s.SendSMS() - if err == nil { - return response, nil - } + response, err := s.SendSMS() + if err == nil { + return response, nil + } - delay := time.Duration(1< Date: Sat, 6 Jul 2024 01:32:12 +0300 Subject: [PATCH 2/2] feat: Implement retry logic test --- pkg/sms/sms_sender_test.go | 103 +++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/pkg/sms/sms_sender_test.go b/pkg/sms/sms_sender_test.go index 2b0b08e..393538b 100644 --- a/pkg/sms/sms_sender_test.go +++ b/pkg/sms/sms_sender_test.go @@ -1,6 +1,7 @@ package sms import ( + "fmt" "testing" ) @@ -30,3 +31,105 @@ func TestSendSMS(t *testing.T) { t.Errorf("empty message received in response") } } + +func (s *SmsSender) MockSendSMS() (SmsSenderResponse, error) { + // Todo: Implement mock behavior + return SmsSenderResponse{ + SmsMessageData: SmsMessageData{ + Message: "Mocked success message", + Recipients: []Recipient{ + { + Key: "mock-recipient-key", + Cost: "0.05", + SmsKey: "mock-sms-key", + MessageId: "mock-message-id", + MessagePart: 1, + Number: "+254745617596", + Status: "Success", + StatusCode: "200", + }, + }, + }, + }, nil +} + +func TestRetrySendSMS(t *testing.T) { + sender := SmsSender{ + ApiKey: "your-api-key", + ApiUser: "your-api-user", + Recipients: []string{"+254745617596"}, + Message: "Hello, this is a test message.", + Sender: "YourSenderID", + } + + maxRetries := 5 + + sender.SendSMS = sender.MockSendSMS + + response, err := sender.RetrySendSMS(maxRetries) + + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + if response.SmsMessageData.Message != "Mocked success message" { + t.Errorf("unexpected message received in response") + } +} + +func TestRetrySendSMS_Success(t *testing.T) { + sender := SmsSender{ + ApiKey: "your-api-key", + ApiUser: "your-api-user", + Recipients: []string{"+254745617596"}, + Message: "Hello, this is a test message.", + Sender: "YourSenderID", + } + + maxRetries := 5 + + retryCount := 0 + sender.SendSMS = func() (SmsSenderResponse, error) { + if retryCount < 3 { + retryCount++ + return SmsSenderResponse{}, fmt.Errorf("mocked error") + } + return SmsSenderResponse{ + SmsMessageData: SmsMessageData{ + Message: "Success!", + }, + }, nil + } + + response, err := sender.RetrySendSMS(maxRetries) + + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + if response.SmsMessageData.Message != "Success!" { + t.Errorf("unexpected message received in response") + } +} + +func TestRetrySendSMS_Failure(t *testing.T) { + sender := SmsSender{ + ApiKey: "your-api-key", + ApiUser: "your-api-user", + Recipients: []string{"+254745617596"}, + Message: "Hello, this is a test message.", + Sender: "YourSenderID", + } + + maxRetries := 3 + + sender.SendSMS = func() (SmsSenderResponse, error) { + return SmsSenderResponse{}, fmt.Errorf("mocked error") + } + + _, err := sender.RetrySendSMS(maxRetries) + + if err == nil { + t.Errorf("expected error, got nil") + } +}