Skip to content

Commit

Permalink
Add test for batched http target requests
Browse files Browse the repository at this point in the history
Adapted existing tests to suit implementation of batching tests without implementing the same/similar test logic again.

This adapts things to rely on acks to manage the waitgroup as opposed to the test server. Originally it was constructed that way because the test also tests the acking, so if that was broken the test might hang and it wouldn't be obvious that acking was responsible.

Got around that issue by adding a timeout to wait for the waitgroup. So now, if it doesn't ack enough, that timeout will occur, and if it acks too many times waitgroup will panic (which was the case already).

We could just as easily preserve the old behaviour and simply add a new test for this, if things are getting too complex here.
  • Loading branch information
colmsnowplow committed Jun 26, 2024
1 parent e8c8877 commit f0e1338
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions pkg/target/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,12 @@ func TestHttpWrite_Simple(t *testing.T) {
testCases := []struct {
Name string
ResponseCode int
BatchSize int
}{
{Name: "200 response Code", ResponseCode: 200},
{Name: "201 response Code", ResponseCode: 201},
{Name: "226 response Code", ResponseCode: 226},
{Name: "200 response Code", ResponseCode: 200, BatchSize: 1},
{Name: "201 response Code", ResponseCode: 201, BatchSize: 1},
{Name: "226 response Code", ResponseCode: 226, BatchSize: 1},
{Name: "Batched", ResponseCode: 200, BatchSize: 20},
}

for _, tt := range testCases {
Expand All @@ -346,7 +348,7 @@ func TestHttpWrite_Simple(t *testing.T) {
server := createTestServerWithResponseCode(&results, tt.ResponseCode)
defer server.Close()

target, err := newHTTPTarget(server.URL, 5, 1, 1048576, 1048576, "application/json", "", "", "", "", "", "", true, false, "", "", "", "")
target, err := newHTTPTarget(server.URL, 5, tt.BatchSize, 1048576, 1048576, "application/json", "", "", "", "", "", "", true, false, "", "", "", "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -358,22 +360,29 @@ func TestHttpWrite_Simple(t *testing.T) {
wg.Done()
}

messages := testutil.GetTestMessages(501, "Hello Server!!", ackFunc)
wg.Add(501)
messages := testutil.GetTestMessages(200, "Hello Server!!", ackFunc)
wg.Add(200)
writeResult, err1 := target.Write(messages)

if ok := WaitForAcksWithTimeout(2*time.Second, &wg); !ok {
assert.Fail("Timed out waiting for acks")
}

assert.Nil(err1)
assert.Equal(501, len(writeResult.Sent))
assert.Equal(501, len(results))
assert.Equal(200, len(writeResult.Sent))
assert.Equal(200/tt.BatchSize, len(results))
for _, result := range results {
assert.Equal("[\"Hello Server!!\"]", string(result))
var res []string
err := json.Unmarshal(result, &res)
if err != nil {
assert.Fail("Request not an array as expected - got error unmarshalling: " + err.Error())
}
for _, r := range res {
assert.Equal("Hello Server!!", string(r))
}
}

assert.Equal(int64(501), ackOps)
assert.Equal(int64(200), ackOps)
})
}
}
Expand Down

0 comments on commit f0e1338

Please sign in to comment.