Skip to content

Commit

Permalink
feat: Implement retry logic test
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMwita committed Jul 5, 2024
1 parent bf5e62b commit ed9adad
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions pkg/sms/sms_sender_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sms

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -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")
}
}

0 comments on commit ed9adad

Please sign in to comment.