Skip to content

Commit

Permalink
log/loki: harden client test
Browse files Browse the repository at this point in the history
In case of slow `httptest` handler execution, don't block and provoke client retransmission, which would result in entriesChan to have two "normal log line" entries.
  • Loading branch information
gsora committed Aug 8, 2023
1 parent 97b946a commit 2b29118
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/log/loki/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ type logFunc func(string, error)

// NewForT returns a new Client for testing.
func NewForT(endpoint string, serviceLabel string, batchWait time.Duration, batchMax, maxLogLineLen int,
moreLabelsFunc lazyLabelsFunc,
moreLabelsFunc lazyLabelsFunc, logFunc logFunc,
) *Client {
return newInternal(endpoint, serviceLabel, batchWait, batchMax, maxLogLineLen, func(string, error) {}, moreLabelsFunc)
return newInternal(endpoint, serviceLabel, batchWait, batchMax, maxLogLineLen, logFunc, moreLabelsFunc)
}

// New returns a new Client.
Expand Down
21 changes: 14 additions & 7 deletions app/log/loki/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestLoki(t *testing.T) {
}, true
}

cl := loki.NewForT(srv.URL, serviceLabel, time.Millisecond, 1024, 0, lazyLabels)
cl := loki.NewForT(srv.URL, serviceLabel, time.Millisecond, 1024, 0, lazyLabels, func(_ string, _ error) {})

const n = 4

Expand Down Expand Up @@ -93,9 +93,11 @@ func TestLongLines(t *testing.T) {
huge = `ts=2023-07-26T11:38:47.922888118+02:00 level=debug caller=loki/client_test.go:124 msg=error data=0x03000000000000000c000000640000006b2839e6c4634ea95859035d4015217ff4fd04fe826db77ff3d9f6d940d3664b0d01dba3da9a6322ac63106e0fb2e05f35b3ec65ef53f12e1c708cd85b263dc83ce7f0cbfb6b0200689740241a2a873616125e0454ebb62a9becc12ee9f9b60f3d3224af96711900f07f5192b99f1200a38dddf858e822dcec4fd7fcc24b37609d1ad4deeadbfd0e45cc339cf4c5db643923599323f97b5209f079cbe32eb290bd9c6a40698ebf7bad15bcb7d25168c954000000c16669c4607dddf98e0810d1ca1d3db455dccd4abbe1d7e618b404e50c8d385690d0243a88976177c947f29d2c9b212981dfb36015bd81c4f9f8991c034db96d130cf3def3c936b758a18bb2f31e61f048b8e233006e8afc967f31dbd503f8aed73eb5adbe609662d6df696deaa3ede04f53f87432de73f73ec80bcd82bd63f80000000000000000dbee54d9dd388a8b31a6d5a3214a0b7e1766723a5eba409a6681eaf80ac4c8c4efb4668dec541dfd4ad53724414203bc4063f732d2f57877dd3085c0fcb34c648401000084010000840100009603000096030000d4438030f503129e62ba2c8c5106881b13589657506f7bbcfe50f80428ab00949c00ef52c0e982d71dba0334bf260cf6d6eaeee2d4a3d1d85c89ad251fb4397deb80bc4cf894c5e0cdd7f9f61f69d70964ab21058df89d2f5e54fa92b8a10aee2632dea72bb3fa0da2ffa6fe4e0afe49c6fa13609bfa44ba30b30470cb8e28112f2259f65ae5317a48d6ab7bbd14b357bd1cfc34a948c6a844a21377e72ae9cc96030000ee050000080000000d010000e4000000c0a32c3d104e0f00dfdf245254cd1e001ca9a04fa692be0969f3db2e449c1e59fc293863b733145c2f7b50145e0f5e5fbe9a7c1a6e59120002609f3634d68656968985a6328c1cebf881f4cc40aa0f3d5052931dbd6857747ba895b9a3290b00ba5787b5679750222f78a834ac8f1d28aab935294dd6efa04fff56062b53d85f0bdfefc6b8aae6027f29e972e80a344cfba4879520c83a4ed12c19f657397a3d9b2b6d20e93683890ea5936e276ffa9525fb4add29d698488f92ab6be3b3048aea55ace8b4c0f5961d45587b4edd7dbdbc30221fb6db9f2626646d63cc55d56f000000000000000000000000000000002000000000000000000000000000000001e4000000ee05f4523d0b16005c422d8201cd1d009e90eb16200b5c1844fce2966bd4a4fdcbb087622d95560e31b84fb31df9f4bbd67921c58678170096dbc7f35a5ca0a1dfe850c268db2209fcdf0b758b92e67e92ea1afc94c15ca4a562642b0b391b00d4b9818f6e3f3cf4bbe5392619b038e6f2bb09d8e276d8bc2e0bf98014c084f9d4e1f06199660c84628c402d0d9b06fbbac8897b01c095d1be800caabd8238de068a6fe49f721c2ab1dd67521aa6d08eb1026a9d80b8c7b315cee17d9328849e8554d4d4bb6eb7614edb5ebffa8eba85f1faf2e68973800db0de3ee5b64366d800000004000000000000000000000000000000000000000000000000000000000133e8d02630aca3c951cc3b88a5bfee6a4728cdf2ea15479e451d6674e0ccb338000000000000000000000000000000000000000020e3a87506703bf0ab292a9fbcf6eb2887a1695091534f0cd7865148d0dc63832cee9d28b865ca501cc56ad6b6017d74e451f6eadb8e47cc47fb7ed60c1c5f11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004ab5fb11437d1eeb4394333d581b72352f4c9c3266cbf2827fa4628468f47be00000000000000000000000000000000000000000000000000000000000000000380200009f3428aebf6a2346533c6a20d75fcf6731eccc22f36bc7dbaa334a6c3f6972ebc95ed111b516bc895b2fd90d6bcfb809ed6909f58ee488e4fda670fdbeeea335406b5b82d90b5d921d92afb21a2798aedbb5d8b6f377e51d2bce499696c3b63600000000000000000000000000000000000000000000000000000000000000005e6284ed098b6712ecdf23bb172e32add234e0a873046a37e6596b770d61f79e pretty="DEBG error {\"data\": \"0x03000000000000000c000000640000006b2839e6c4634ea95859035d4015217ff4fd04fe826db77ff3d9f6d940d3664b0d01dba3da9a6322ac63106e0fb2e05f35b3ec65ef53f12e1c708cd85b263dc83ce7f0cbfb6b0200689740241a2a873616125e0454ebb62a9becc12ee9f9b60f3d3224af96711900f07f5192b99f1200a38dddf858e822dcec4fd7fcc24b37609d1ad4deeadbfd0e45cc339cf4c5db643923599323f97b5209f079cbe32eb290bd9c6a40698ebf7bad15bcb7d25168c954000000c16669c4607dddf98e0810d1ca1d3db455dccd4abbe1d7e618b404e50c8d385690d0243a88976177c947f29d2c9b212981dfb36015bd81c4f9f8991c034db96d130cf3def3c936b758a18bb2f31e61f048b8e233006e8afc967f31dbd503f8aed73eb5adbe609662d6df696deaa3ede04f53f87432de73f73ec80bcd82bd63f80000000000000000dbee54d9dd388a8b31a6d5a3214a0b7e1766723a5eba409a6681eaf80ac4c8c4efb4668dec541dfd4ad53724414203bc4063f732d2f57877dd3085c0fcb34c648401000084010000840100009603000096030000d4438030f503129e62ba2c8c5106881b13589657506f7bbcfe50f80428ab00949c00ef52c0e982d71dba0334bf260cf6d6eaeee2d4a3d1d85c89ad251fb4397deb80bc4cf894c5e0cdd7f9f61f69d70964ab21058df89d2f5e54fa92b8a10aee2632dea72bb3fa0da2ffa6fe4e0afe49c6fa13609bfa44ba30b30470cb8e28112f2259f65ae5317a48d6ab7bbd14b357bd1cfc34a948c6a844a21377e72ae9cc96030000ee050000080000000d010000e4000000c0a32c3d104e0f00dfdf245254cd1e001ca9a04fa692be0969f3db2e449c1e59fc293863b733145c2f7b50145e0f5e5fbe9a7c1a6e59120002609f3634d68656968985a6328c1cebf881f4cc40aa0f3d5052931dbd6857747ba895b9a3290b00ba5787b5679750222f78a834ac8f1d28aab935294dd6efa04fff56062b53d85f0bdfefc6b8aae6027f29e972e80a344cfba4879520c83a4ed12c19f657397a3d9b2b6d20e93683890ea5936e276ffa9525fb4add29d698488f92ab6be3b3048aea55ace8b4c0f5961d45587b4edd7dbdbc30221fb6db9f2626646d63cc55d56f000000000000000000000000000000002000000000000000000000000000000001e4000000ee05f4523d0b16005c422d8201cd1d009e90eb16200b5c1844fce2966bd4a4fdcbb087622d95560e31b84fb31df9f4bbd67921c58678170096dbc7f35a5ca0a1dfe850c268db2209fcdf0b758b92e67e92ea1afc94c15ca4a562642b0b391b00d4b9818f6e3f3cf4bbe5392619b038e6f2bb09d8e276d8bc2e0bf98014c084f9d4e1f06199660c84628c402d0d9b06fbbac8897b01c095d1be800caabd8238de068a6fe49f721c2ab1dd67521aa6d08eb1026a9d80b8c7b315cee17d9328849e8554d4d4bb6eb7614edb5ebffa8eba85f1faf2e68973800db0de3ee5b64366d800000004000000000000000000000000000000000000000000000000000000000133e8d02630aca3c951cc3b88a5bfee6a4728cdf2ea15479e451d6674e0ccb338000000000000000000000000000000000000000020e3a87506703bf0ab292a9fbcf6eb2887a1695091534f0cd7865148d0dc63832cee9d28b865ca501cc56ad6b6017d74e451f6eadb8e47cc47fb7ed60c1c5f11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004ab5fb11437d1eeb4394333d581b72352f4c9c3266cbf2827fa4628468f47be00000000000000000000000000000000000000000000000000000000000000000380200009f3428aebf6a2346533c6a20d75fcf6731eccc22f36bc7dbaa334a6c3f6972ebc95ed111b516bc895b2fd90d6bcfb809ed6909f58ee488e4fda670fdbeeea335406b5b82d90b5d921d92afb21a2798aedbb5d8b6f377e51d2bce499696c3b63600000000000000000000000000000000000000000000000000000000000000005e6284ed098b6712ecdf23bb172e32add234e0a873046a37e6596b770d61f79e\"}"`
)

ctx := context.Background()
var (
ctx = context.Background()
entriesChan = make(chan string)
)

entriesChan := make(chan string)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
require.NoError(t, err)
Expand All @@ -104,24 +106,29 @@ func TestLongLines(t *testing.T) {
require.Len(t, req.Streams, 1)
require.Contains(t, req.Streams[0].Labels, fmt.Sprintf(`service="%s"`, serviceLabel))
for _, entry := range req.Streams[0].Entries {
entriesChan <- entry.Line
go func(entry string) {
entriesChan <- entry
close(entriesChan)

This comment has been minimized.

Copy link
@corverroos

corverroos Aug 8, 2023

Contributor

this will panic?

}(entry.Line)
}
close(entriesChan)
}))

cl := loki.NewForT(srv.URL, serviceLabel, time.Millisecond, batchMax, maxLogLineLen, func() (map[string]string, bool) {
return map[string]string{}, true
}, func(s string, err error) {
// err will never be nil, use require package to pretty print it
require.NoError(t, err, "reason: %s", s)
})

go cl.Run()

cl.Add(normal)

for i := 0; i < 1000; i++ {
for i := 0; i < 10000000; i++ {
cl.Add(huge)
}

go cl.Stop(ctx)
cl.Stop(ctx)

var entries []string
for entry := range entriesChan {
Expand Down

0 comments on commit 2b29118

Please sign in to comment.