Skip to content

Commit

Permalink
ARCO-202: Addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kameshraj23 committed Nov 5, 2024
1 parent 07dda8e commit b7de229
Showing 1 changed file with 61 additions and 27 deletions.
88 changes: 61 additions & 27 deletions internal/callbacker/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/bitcoin-sv/arc/internal/callbacker"
"github.com/stretchr/testify/assert"
Expand All @@ -26,62 +27,75 @@ func TestCallbackSender_Send(t *testing.T) {
expectedSuccess: true,
expectedRetries: 1,
},
{
name: "retry - server error and fails after retries",
responseStatus: http.StatusInternalServerError,
expectedSuccess: true,
expectedRetries: 3, // Adjust based on your retry logic in `Send`
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Given
client := &http.Client{Timeout: 500 * time.Second}

retries := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(tc.responseStatus)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Check failure on line 44 in internal/callbacker/sender_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it as _ (revive)
if retries < tc.expectedRetries-1 {
w.WriteHeader(tc.responseStatus)
} else {
w.WriteHeader(http.StatusOK)
}
retries++
time.Sleep(100 * time.Millisecond)
}))
defer server.Close()

logger := slog.New(slog.NewTextHandler(new(bytes.Buffer), nil))
sender, err := callbacker.NewSender(server.Client(), logger)
sut, err := callbacker.NewSender(client, logger)
require.NoError(t, err)
defer sender.GracefulStop()

// When
success := sender.Send(server.URL, "test-token", &callbacker.Callback{TxID: "1234", TxStatus: "SEEN_ON_NETWORK"})
defer sut.GracefulStop()

// Then
require.Equal(t, tc.expectedSuccess, success)
require.Equal(t, tc.expectedRetries, retries)
//When
success := sut.Send(server.URL, "test-token", &callbacker.Callback{TxID: "1234", TxStatus: "SEEN_ON_NETWORK"})
//Then
require.Equal(t, tc.expectedSuccess, success, "Expected success to be %v, but got %v", tc.expectedSuccess, success)
require.Equal(t, tc.expectedRetries, retries, "Expected retries to be %d, but got %d", tc.expectedRetries, retries)
})
}
}

func TestCallbackSender_GracefulStop(t *testing.T) {
// Given
logger := slog.New(slog.NewTextHandler(new(bytes.Buffer), nil))
sender, err := callbacker.NewSender(http.DefaultClient, logger)
sut, err := callbacker.NewSender(http.DefaultClient, logger)
require.NoError(t, err)

// When: Call GracefulStop twice to ensure it handles double stop gracefully
sender.GracefulStop()
sender.GracefulStop()
sut.GracefulStop()
sut.GracefulStop()

// Then: Assert that sender is disposed
err = sender.Health()
err = sut.Health()
require.EqualError(t, err, "callbacker is disposed already")
}

func TestCallbackSender_Health(t *testing.T) {
// Given
logger := slog.New(slog.NewTextHandler(new(bytes.Buffer), nil))
sender, err := callbacker.NewSender(http.DefaultClient, logger)
sut, err := callbacker.NewSender(http.DefaultClient, logger)
require.NoError(t, err)

// When: Test Health on active sender
err = sender.Health()
err = sut.Health()

// Then: Expect no error when sender is active
require.NoError(t, err)

// When: Stop sender and check Health error
sender.GracefulStop()
err = sender.Health()
sut.GracefulStop()
err = sut.Health()

// Then: Expect error indicating the sender is disposed
require.EqualError(t, err, "callbacker is disposed already")
Expand All @@ -95,51 +109,71 @@ func TestCallbackSender_SendBatch(t *testing.T) {
dtos []*callbacker.Callback
responseStatus int
expectedSuccess bool
expectedRetries int
}{
{
name: "success - batch sent",
name: "success - batch sent with retries",
url: "/batch",
token: "test-token",
dtos: []*callbacker.Callback{{TxID: "1234", TxStatus: "SEEN_ON_NETWORK"}, {TxID: "5678", TxStatus: "MINED"}},
responseStatus: http.StatusOK,
expectedSuccess: true,
expectedRetries: 1,
},
{
name: "failure - server error on batch with retries",
url: "/batch",
token: "test-token",
dtos: []*callbacker.Callback{{TxID: "1234", TxStatus: "SEEN_ON_NETWORK"}, {TxID: "5678", TxStatus: "MINED"}},
responseStatus: http.StatusInternalServerError,
expectedSuccess: true,
expectedRetries: 3,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Given
retries := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Check failure on line 138 in internal/callbacker/sender_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

empty-lines: extra empty line at the start of a block (revive)

require.Equal(t, "Bearer "+tc.token, r.Header.Get("Authorization"))

var batch callbacker.BatchCallback
err := json.NewDecoder(r.Body).Decode(&batch)
require.NoError(t, err)
require.Equal(t, len(tc.dtos), batch.Count)

w.WriteHeader(tc.responseStatus)
if retries < tc.expectedRetries-1 {
w.WriteHeader(tc.responseStatus)
} else {
w.WriteHeader(http.StatusOK)
}
retries++
time.Sleep(100 * time.Millisecond)
}))
defer server.Close()

client := &http.Client{Timeout: 2 * time.Second}
logger := slog.New(slog.NewTextHandler(new(bytes.Buffer), nil))
sender, err := callbacker.NewSender(server.Client(), logger)
sut, err := callbacker.NewSender(client, logger)
require.NoError(t, err)
defer sender.GracefulStop()
defer sut.GracefulStop()

// When
success := sender.SendBatch(server.URL+tc.url, tc.token, tc.dtos)
success := sut.SendBatch(server.URL+tc.url, tc.token, tc.dtos)

// Then
require.Equal(t, tc.expectedSuccess, success)
require.Equal(t, tc.expectedSuccess, success, "Expected success to be %v, but got %v", tc.expectedSuccess, success)
require.Equal(t, tc.expectedRetries, retries, "Expected retries to be %d, but got %d", tc.expectedRetries, retries)
})
}
}

func TestCallbackSender_Send_WithRetries(t *testing.T) {
// Given
client := &http.Client{}
logger := slog.New(slog.NewJSONHandler(bytes.NewBuffer([]byte{}), nil))
sender, _ := callbacker.NewSender(client, logger)
sut, _ := callbacker.NewSender(client, logger)

retryCount := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
Expand All @@ -154,7 +188,7 @@ func TestCallbackSender_Send_WithRetries(t *testing.T) {

// When
callback := &callbacker.Callback{TxID: "test-txid", TxStatus: "SEEN_ON_NETWORK"}
ok := sender.Send(server.URL, "test-token", callback)
ok := sut.Send(server.URL, "test-token", callback)

// Then
assert.True(t, ok)
Expand Down

0 comments on commit b7de229

Please sign in to comment.