Skip to content

Commit 4966266

Browse files
committed
Make tests for syslog_batch more reliable
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.
1 parent fb5fe34 commit 4966266

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/pkg/egress/syslog/https_batch.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package syslog
33
import (
44
"bytes"
55
"crypto/tls"
6+
"fmt"
67
"log"
78
"time"
89

@@ -11,8 +12,7 @@ import (
1112
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/egress"
1213
)
1314

14-
const BATCHSIZE = 256 * 1024
15-
15+
var DefaultBatchSize = 256 * 1024
1616
var DefaultSendInterval = 1 * time.Second
1717

1818
type HTTPSBatchWriter struct {
@@ -41,7 +41,7 @@ func NewHTTPSBatchWriter(
4141
egressMetric: egressMetric,
4242
syslogConverter: c,
4343
},
44-
batchSize: BATCHSIZE,
44+
batchSize: DefaultBatchSize,
4545
sendInterval: DefaultSendInterval,
4646
egrMsgCount: 0,
4747
msgs: make(chan []byte),
@@ -53,6 +53,7 @@ func NewHTTPSBatchWriter(
5353
// Modified Write function
5454
func (w *HTTPSBatchWriter) Write(env *loggregator_v2.Envelope) error {
5555
msgs, err := w.syslogConverter.ToRFC5424(env, w.hostname)
56+
fmt.Printf("Value: %d\n", len(msgs[0]))
5657
if err != nil {
5758
log.Printf("Failed to parse syslog, dropping faulty message, err: %s", err)
5859
return nil

src/pkg/egress/syslog/https_batch_test.go

+20-16
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import (
1717
. "github.com/onsi/gomega"
1818
)
1919

20-
var string_to_1024_chars = "saljdflajsdssdfsdfljkfkajafjajlköflkjöjaklgljksdjlakljkflkjweljklkwjejlkfekljwlkjefjklwjklsdajkljklwerlkaskldgjksakjekjwrjkljasdjkgfkljwejklrkjlklasdkjlsadjlfjlkadfljkajklsdfjklslkdfjkllkjasdjkflsdlakfjklasldfkjlasdjfkjlsadlfjklaljsafjlslkjawjklerkjljklasjkdfjklwerjljalsdjkflwerjlkwejlkarjklalkklfsdjlfhkjsdfkhsewhkjjasdjfkhwkejrkjahjefkhkasdjhfkashfkjwehfkksadfjaskfkhjdshjfhewkjhasdfjdajskfjwehkfajkankaskjdfasdjhfkkjhjjkasdfjhkjahksdf"
20+
var string_to_256_chars string
2121

2222
func init() {
2323
syslog.DefaultSendInterval = 100 * time.Millisecond // Modify behavior for tests
24+
syslog.DefaultBatchSize = 5000
2425
}
2526

2627
var _ = Describe("HTTPS_batch", func() {
@@ -34,7 +35,10 @@ var _ = Describe("HTTPS_batch", func() {
3435
b *syslog.URLBinding
3536
writer egress.WriteCloser
3637
)
37-
string_to_1024_chars += string_to_1024_chars
38+
//With the rest of the syslog, this results in a syslogenvelope of the size 400
39+
for i := 0; i < 256; i++ {
40+
string_to_256_chars += "a"
41+
}
3842

3943
BeforeEach(func() {
4044
drain = newBatchMockDrain(200)
@@ -57,7 +61,7 @@ var _ = Describe("HTTPS_batch", func() {
5761
Expect(writer.Write(env1)).To(Succeed())
5862
env2 := buildLogEnvelope("APP", "2", "message 2", loggregator_v2.Log_OUT)
5963
Expect(writer.Write(env2)).To(Succeed())
60-
time.Sleep(1050 * time.Millisecond)
64+
time.Sleep(150 * time.Millisecond)
6165

6266
Expect(drain.getMessagesSize()).Should(Equal(2))
6367
expected := &rfc5424.Message{
@@ -87,24 +91,25 @@ var _ = Describe("HTTPS_batch", func() {
8791
})
8892

8993
It("test batch dispatching with all logs in a given timeframe", func() {
90-
env1 := buildLogEnvelope("APP", "1", "string to get log to 1024 characters:"+string_to_1024_chars, loggregator_v2.Log_OUT)
94+
env1 := buildLogEnvelope("APP", "1", "string to get log to 400 characters:"+string_to_256_chars, loggregator_v2.Log_OUT)
9195
for i := 0; i < 10; i++ {
9296
Expect(writer.Write(env1)).To(Succeed())
93-
time.Sleep(5 * time.Millisecond)
9497
}
95-
Expect(drain.getMessagesSize()).Should(Equal(0))
96-
time.Sleep(100 * time.Millisecond)
97-
Expect(drain.getMessagesSize()).Should(Equal(10))
98+
Consistently(drain.getMessagesSize, 50*time.Millisecond).Should(Equal(0))
99+
Eventually(drain.getMessagesSize, 100*time.Millisecond).Should(Equal(10))
98100
})
99101

100102
It("test dispatching for batches before timewindow is finished", func() {
101-
env1 := buildLogEnvelope("APP", "1", "string to get log to 1024 characters:"+string_to_1024_chars, loggregator_v2.Log_OUT)
102-
for i := 0; i < 300; i++ {
103+
// One envelope has the size of 400byte
104+
env1 := buildLogEnvelope("APP", "1", "string to get log to 400 characters:"+string_to_256_chars, loggregator_v2.Log_OUT)
105+
106+
for i := 0; i < 20; i++ {
103107
Expect(writer.Write(env1)).To(Succeed())
104108
}
105-
Expect(drain.getMessagesSize()).Should(Equal(256))
106-
time.Sleep(101 * time.Millisecond)
107-
Expect(drain.getMessagesSize()).Should(Equal(300))
109+
// DefaultBatchSize = 5000byte, 12 * 400byte = 4800byte, 13 * 400byte = 5200byte
110+
// -> The batch will trigger after 13 messages, and this is not a direct hit to prevent inconsistencies.
111+
Consistently(drain.getMessagesSize, 50*time.Millisecond).Should(Equal(13))
112+
Eventually(drain.getMessagesSize, 100*time.Millisecond).Should(Equal(20))
108113
})
109114

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

0 commit comments

Comments
 (0)