Skip to content

Commit

Permalink
Make tests for syslog_batch more reliable
Browse files Browse the repository at this point in the history
This change introduces to reduce the default batch size for testing.
This will make it less cpu intensive to perform the testcases in
short timeframes.

It also replaces most sleeps with Consistently and Eventually.
It makes the timings more forgiving.

This should make it reliable on weak hardware.
  • Loading branch information
nicklas-dohrn committed Feb 20, 2025
1 parent fb5fe34 commit 366f9f1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
5 changes: 2 additions & 3 deletions src/pkg/egress/syslog/https_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/egress"
)

const BATCHSIZE = 256 * 1024

var DefaultBatchSize = 256 * 1024
var DefaultSendInterval = 1 * time.Second

type HTTPSBatchWriter struct {
Expand Down Expand Up @@ -41,7 +40,7 @@ func NewHTTPSBatchWriter(
egressMetric: egressMetric,
syslogConverter: c,
},
batchSize: BATCHSIZE,
batchSize: DefaultBatchSize,
sendInterval: DefaultSendInterval,
egrMsgCount: 0,
msgs: make(chan []byte),
Expand Down
43 changes: 24 additions & 19 deletions src/pkg/egress/syslog/https_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ import (
. "github.com/onsi/gomega"
)

var string_to_1024_chars = "saljdflajsdssdfsdfljkfkajafjajlköflkjöjaklgljksdjlakljkflkjweljklkwjejlkfekljwlkjefjklwjklsdajkljklwerlkaskldgjksakjekjwrjkljasdjkgfkljwejklrkjlklasdkjlsadjlfjlkadfljkajklsdfjklslkdfjkllkjasdjkflsdlakfjklasldfkjlasdjfkjlsadlfjklaljsafjlslkjawjklerkjljklasjkdfjklwerjljalsdjkflwerjlkwejlkarjklalkklfsdjlfhkjsdfkhsewhkjjasdjfkhwkejrkjahjefkhkasdjhfkashfkjwehfkksadfjaskfkhjdshjfhewkjhasdfjdajskfjwehkfajkankaskjdfasdjhfkkjhjjkasdfjhkjahksdf"
var string_to_256_chars string

func init() {
syslog.DefaultSendInterval = 100 * time.Millisecond // Modify behavior for tests
// Modify behavior for tests
syslog.DefaultSendInterval = 100 * time.Millisecond
syslog.DefaultBatchSize = 5000

//With the rest of the syslog, this results in a syslogenvelope of the size 400
for i := 0; i < 256; i++ {
string_to_256_chars += "a"
}
}

var _ = Describe("HTTPS_batch", func() {
Expand All @@ -34,10 +41,10 @@ var _ = Describe("HTTPS_batch", func() {
b *syslog.URLBinding
writer egress.WriteCloser
)
string_to_1024_chars += string_to_1024_chars

BeforeEach(func() {
drain = newBatchMockDrain(200)
drain.Reset()
b = buildURLBinding(
drain.URL,
"test-app-id",
Expand All @@ -57,7 +64,7 @@ var _ = Describe("HTTPS_batch", func() {
Expect(writer.Write(env1)).To(Succeed())
env2 := buildLogEnvelope("APP", "2", "message 2", loggregator_v2.Log_OUT)
Expect(writer.Write(env2)).To(Succeed())
time.Sleep(1050 * time.Millisecond)
time.Sleep(150 * time.Millisecond)

Expect(drain.getMessagesSize()).Should(Equal(2))
expected := &rfc5424.Message{
Expand Down Expand Up @@ -87,24 +94,25 @@ var _ = Describe("HTTPS_batch", func() {
})

It("test batch dispatching with all logs in a given timeframe", func() {
env1 := buildLogEnvelope("APP", "1", "string to get log to 1024 characters:"+string_to_1024_chars, loggregator_v2.Log_OUT)
env1 := buildLogEnvelope("APP", "1", "string to get log to 400 characters:"+string_to_256_chars, loggregator_v2.Log_OUT)
for i := 0; i < 10; i++ {
Expect(writer.Write(env1)).To(Succeed())
time.Sleep(5 * time.Millisecond)
}
Expect(drain.getMessagesSize()).Should(Equal(0))
time.Sleep(100 * time.Millisecond)
Expect(drain.getMessagesSize()).Should(Equal(10))
Expect(drain.getMessagesSize()).To(Equal(0))
Eventually(drain.getMessagesSize, 180*time.Millisecond).Should(Equal(10))
})

It("test dispatching for batches before timewindow is finished", func() {
env1 := buildLogEnvelope("APP", "1", "string to get log to 1024 characters:"+string_to_1024_chars, loggregator_v2.Log_OUT)
for i := 0; i < 300; i++ {
// One envelope has the size of 400byte
env1 := buildLogEnvelope("APP", "1", "string to get log to 400 characters:"+string_to_256_chars, loggregator_v2.Log_OUT)

for i := 0; i < 20; i++ {
Expect(writer.Write(env1)).To(Succeed())
}
Expect(drain.getMessagesSize()).Should(Equal(256))
time.Sleep(101 * time.Millisecond)
Expect(drain.getMessagesSize()).Should(Equal(300))
// DefaultBatchSize = 5000byte, 12 * 400byte = 4800byte, 13 * 400byte = 5200byte
// -> The batch will trigger after 13 messages, and this is not a direct hit to prevent inconsistencies.
Expect(drain.getMessagesSize()).Should(Equal(13))
Eventually(drain.getMessagesSize, 120*time.Millisecond).Should(Equal(20))
})

It("test for hanging after some ticks", func() {
Expand All @@ -113,10 +121,9 @@ var _ = Describe("HTTPS_batch", func() {
env1 := buildLogEnvelope("APP", "1", "only a short test message", loggregator_v2.Log_OUT)
for i := 0; i < 5; i++ {
Expect(writer.Write(env1)).To(Succeed())
time.Sleep(300 * time.Millisecond)
time.Sleep(220 * time.Millisecond) // this sleeps at least 2 ticks, to trigger once without events
}
time.Sleep(101 * time.Millisecond)
Expect(drain.getMessagesSize()).Should(Equal(5))
Eventually(drain.getMessagesSize, 120*time.Millisecond).Should(Equal(5))
})
})

Expand All @@ -128,8 +135,6 @@ func newBatchMockDrain(status int) *SpyDrain {
Expect(err).ToNot(HaveOccurred())
defer r.Body.Close()

println(body)

message := &rfc5424.Message{}

messages := bytes.SplitAfter(body, []byte("\n"))
Expand Down
5 changes: 5 additions & 0 deletions src/pkg/egress/syslog/https_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ type SpyDrain struct {
headers []http.Header
}

func (d *SpyDrain) Reset() {
d.messages = nil
d.headers = nil
}

func (d *SpyDrain) appendMessage(message *rfc5424.Message) {
d.mu.Lock()
defer d.mu.Unlock()
Expand Down

0 comments on commit 366f9f1

Please sign in to comment.