This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleak_regression_test.go
103 lines (79 loc) · 2.59 KB
/
leak_regression_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package gomol
import (
"fmt"
"time"
"github.com/aphistic/sweet"
. "github.com/onsi/gomega"
)
func (s *GomolSuite) TestLeakRegressionTest(t sweet.T) {
var (
blocker = make(chan struct{})
l1 = newDefaultMemLogger()
l2 = &BlockingLogger{ch: blocker}
ch = make(chan error)
errors = make(chan int)
)
go func() {
defer close(errors)
count := 0
for range ch {
count++
}
errors <- count
}()
testBase = NewBase()
testBase.SetConfig(&Config{MaxQueueSize: TestMaxQueueSize})
testBase.SetErrorChan(ch)
testBase.AddLogger(l1)
testBase.AddLogger(l2)
testBase.InitLoggers()
for i := 0; i < TestMaxQueueSize; i++ {
testBase.Infof("test %d", i)
}
// l1 gets a message, but then l2 blocks immediately
// after. We should not have any additional messages
// sent to the first logger.
Eventually(l1.Messages()).Should(HaveLen(1))
Expect(l1.Messages()[0].Message).To(Equal("test 0"))
// Send additional messages while the loggers are blocked
// and the queue is full. This should NOT block the main
// app routine.
for i := TestMaxQueueSize; i < TestMaxQueueSize*3; i++ {
testBase.Infof("test %d", i)
}
// Now, unblock the logger, publish another chunk, and
// wait for the messages to drain so we can inspect what
// messages made it through during the time the logger
// was naughty.
close(blocker)
<-time.After(time.Millisecond * 100)
for i := TestMaxQueueSize * 3; i < TestMaxQueueSize*4; i++ {
testBase.Infof("test %d", i)
}
testBase.ShutdownLoggers()
// We ran into a situation where in order to keep the bound
// of the queue some messages had to be dropped - these must
// be the _oldest_ messages and should. In this case, it is
// the first two chunks (except the first message). The next
// message we should see that wasn't dropped should be the
// first message in the third chunk.
Expect(l1.Messages()).To(HaveLen(2*TestMaxQueueSize + 1))
// Skip checking "test 0" message
for i := 0; i < len(l1.Messages())-1; i++ {
Expect(l1.Messages()[i+1].Message).To(Equal(fmt.Sprintf("test %d", i+2*TestMaxQueueSize)))
}
Eventually(errors).Should(Receive(Equal(2*TestMaxQueueSize - 1)))
}
//
// Logger that blocks all messages
type BlockingLogger struct {
ch chan struct{}
}
func (l *BlockingLogger) SetBase(base *Base) {}
func (l *BlockingLogger) InitLogger() error { return nil }
func (l *BlockingLogger) IsInitialized() bool { return true }
func (l *BlockingLogger) ShutdownLogger() error { return nil }
func (l *BlockingLogger) Logm(timestamp time.Time, level LogLevel, attrs map[string]interface{}, msg string) error {
<-l.ch
return nil
}