Skip to content

Commit 9f789db

Browse files
committed
move spyLogClient to testhelper package
1 parent 1c03a02 commit 9f789db

File tree

4 files changed

+103
-96
lines changed

4 files changed

+103
-96
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package testhelper
2+
3+
import (
4+
"code.cloudfoundry.org/go-loggregator/v10"
5+
v2 "code.cloudfoundry.org/go-loggregator/v10/rpc/loggregator_v2"
6+
"sync"
7+
)
8+
9+
type spyLogClient struct {
10+
mu sync.Mutex
11+
_message []string
12+
_appID []string
13+
14+
// We use maps to ensure that we can query the keys
15+
_sourceType map[string]struct{}
16+
_sourceInstance map[string]struct{}
17+
}
18+
19+
func NewSpyLogClient() *spyLogClient {
20+
return &spyLogClient{
21+
_sourceType: make(map[string]struct{}),
22+
_sourceInstance: make(map[string]struct{}),
23+
}
24+
}
25+
26+
func (s *spyLogClient) EmitLog(message string, opts ...loggregator.EmitLogOption) {
27+
s.mu.Lock()
28+
defer s.mu.Unlock()
29+
30+
env := &v2.Envelope{
31+
Tags: make(map[string]string),
32+
}
33+
34+
for _, o := range opts {
35+
o(env)
36+
}
37+
38+
s._message = append(s._message, message)
39+
s._appID = append(s._appID, env.SourceId)
40+
s._sourceType[env.GetTags()["source_type"]] = struct{}{}
41+
s._sourceInstance[env.GetInstanceId()] = struct{}{}
42+
}
43+
44+
func (s *spyLogClient) Message() []string {
45+
s.mu.Lock()
46+
defer s.mu.Unlock()
47+
48+
return s._message
49+
}
50+
51+
func (s *spyLogClient) AppID() []string {
52+
s.mu.Lock()
53+
defer s.mu.Unlock()
54+
55+
return s._appID
56+
}
57+
58+
func (s *spyLogClient) SourceType() map[string]struct{} {
59+
s.mu.Lock()
60+
defer s.mu.Unlock()
61+
62+
// Copy map so the orig does not escape the mutex and induce a race.
63+
m := make(map[string]struct{})
64+
for k := range s._sourceType {
65+
m[k] = struct{}{}
66+
}
67+
68+
return m
69+
}
70+
71+
func (s *spyLogClient) SourceInstance() map[string]struct{} {
72+
s.mu.Lock()
73+
defer s.mu.Unlock()
74+
75+
// Copy map so the orig does not escape the mutex and induce a race.
76+
m := make(map[string]struct{})
77+
for k := range s._sourceInstance {
78+
m[k] = struct{}{}
79+
}
80+
81+
return m
82+
}

src/pkg/egress/syslog/app_log_emitter_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package syslog_test
22

33
import (
4+
"code.cloudfoundry.org/loggregator-agent-release/src/internal/testhelper"
45
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/egress/syslog"
56
. "github.com/onsi/ginkgo/v2"
67
. "github.com/onsi/gomega"
@@ -9,14 +10,14 @@ import (
910
var _ = Describe("Loggregator Emitter", func() {
1011
Describe("EmitLog()", func() {
1112
It("emits a log message", func() {
12-
logClient := NewSpyLogClient()
13+
logClient := testhelper.NewSpyLogClient()
1314
emitter := syslog.NewAppLogEmitter(logClient, "0")
1415

1516
emitter.EmitLog("app-id", "some-message")
1617

17-
messages := logClient.message()
18-
appIDs := logClient.appID()
19-
sourceTypes := logClient.sourceType()
18+
messages := logClient.Message()
19+
appIDs := logClient.AppID()
20+
sourceTypes := logClient.SourceType()
2021
Expect(messages).To(HaveLen(2))
2122
Expect(messages[0]).To(Equal("some-message"))
2223
Expect(messages[1]).To(Equal("some-message"))
@@ -27,14 +28,14 @@ var _ = Describe("Loggregator Emitter", func() {
2728
})
2829

2930
It("does not emit a log message if the appID is empty", func() {
30-
logClient := NewSpyLogClient()
31+
logClient := testhelper.NewSpyLogClient()
3132
emitter := syslog.NewAppLogEmitter(logClient, "0")
3233

3334
emitter.EmitLog("", "some-message")
3435

35-
messages := logClient.message()
36-
appIDs := logClient.appID()
37-
sourceTypes := logClient.sourceType()
36+
messages := logClient.Message()
37+
appIDs := logClient.AppID()
38+
sourceTypes := logClient.SourceType()
3839
Expect(messages).To(HaveLen(0))
3940
Expect(appIDs).To(HaveLen(0))
4041
Expect(sourceTypes).ToNot(HaveKey("LGR"))

src/pkg/egress/syslog/retry_writer_test.go

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package syslog_test
33
import (
44
"errors"
55
"net/url"
6-
"sync"
76
"sync/atomic"
87
"time"
98

10-
"code.cloudfoundry.org/go-loggregator/v10"
119
v2 "code.cloudfoundry.org/go-loggregator/v10/rpc/loggregator_v2"
1210
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/egress"
1311
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/egress/syslog"
@@ -165,81 +163,6 @@ func (s *spyWriteCloser) WriteAttempts() int {
165163
return int(atomic.LoadInt64(&s.writeAttempts))
166164
}
167165

168-
type spyLogClient struct {
169-
mu sync.Mutex
170-
_message []string
171-
_appID []string
172-
173-
// We use maps to ensure that we can query the keys
174-
_sourceType map[string]struct{}
175-
_sourceInstance map[string]struct{}
176-
}
177-
178-
func NewSpyLogClient() *spyLogClient {
179-
return &spyLogClient{
180-
_sourceType: make(map[string]struct{}),
181-
_sourceInstance: make(map[string]struct{}),
182-
}
183-
}
184-
185-
func (s *spyLogClient) EmitLog(message string, opts ...loggregator.EmitLogOption) {
186-
s.mu.Lock()
187-
defer s.mu.Unlock()
188-
189-
env := &v2.Envelope{
190-
Tags: make(map[string]string),
191-
}
192-
193-
for _, o := range opts {
194-
o(env)
195-
}
196-
197-
s._message = append(s._message, message)
198-
s._appID = append(s._appID, env.SourceId)
199-
s._sourceType[env.GetTags()["source_type"]] = struct{}{}
200-
s._sourceInstance[env.GetInstanceId()] = struct{}{}
201-
}
202-
203-
func (s *spyLogClient) message() []string {
204-
s.mu.Lock()
205-
defer s.mu.Unlock()
206-
207-
return s._message
208-
}
209-
210-
func (s *spyLogClient) appID() []string {
211-
s.mu.Lock()
212-
defer s.mu.Unlock()
213-
214-
return s._appID
215-
}
216-
217-
func (s *spyLogClient) sourceType() map[string]struct{} {
218-
s.mu.Lock()
219-
defer s.mu.Unlock()
220-
221-
// Copy map so the orig does not escape the mutex and induce a race.
222-
m := make(map[string]struct{})
223-
for k := range s._sourceType {
224-
m[k] = struct{}{}
225-
}
226-
227-
return m
228-
}
229-
230-
func (s *spyLogClient) sourceInstance() map[string]struct{} {
231-
s.mu.Lock()
232-
defer s.mu.Unlock()
233-
234-
// Copy map so the orig does not escape the mutex and induce a race.
235-
m := make(map[string]struct{})
236-
for k := range s._sourceInstance {
237-
m[k] = struct{}{}
238-
}
239-
240-
return m
241-
}
242-
243166
func buildDelay(multiplier time.Duration) func(int) time.Duration {
244167
return func(attempt int) time.Duration {
245168
return time.Duration(attempt) * multiplier

src/pkg/egress/syslog/syslog_connector_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package syslog_test
22

33
import (
4+
"code.cloudfoundry.org/loggregator-agent-release/src/internal/testhelper"
45
"errors"
56
"fmt"
67
"io"
@@ -172,7 +173,7 @@ var _ = Describe("SyslogConnector", func() {
172173
})
173174

174175
It("emits a LGR and SYS log to the log client about logs that have been dropped", func() {
175-
logClient := NewSpyLogClient()
176+
logClient := testhelper.NewSpyLogClient()
176177
connector := syslog.NewSyslogConnector(
177178
true,
178179
spyWaitGroup,
@@ -201,20 +202,20 @@ var _ = Describe("SyslogConnector", func() {
201202
}
202203
}(writer)
203204

204-
Eventually(logClient.message).Should(ContainElement(MatchRegexp("\\d messages lost for application (.*) in user provided syslog drain with url")))
205-
Eventually(logClient.appID).Should(ContainElement("app-id"))
205+
Eventually(logClient.Message).Should(ContainElement(MatchRegexp("\\d messages lost for application (.*) in user provided syslog drain with url")))
206+
Eventually(logClient.AppID).Should(ContainElement("app-id"))
206207

207-
Eventually(logClient.sourceType).Should(HaveLen(2))
208-
Eventually(logClient.sourceType).Should(HaveKey("LGR"))
209-
Eventually(logClient.sourceType).Should(HaveKey("SYS"))
208+
Eventually(logClient.SourceType).Should(HaveLen(2))
209+
Eventually(logClient.SourceType).Should(HaveKey("LGR"))
210+
Eventually(logClient.SourceType).Should(HaveKey("SYS"))
210211

211-
Eventually(logClient.sourceInstance).Should(HaveLen(2))
212-
Eventually(logClient.sourceInstance).Should(HaveKey(""))
213-
Eventually(logClient.sourceInstance).Should(HaveKey("3"))
212+
Eventually(logClient.SourceInstance).Should(HaveLen(2))
213+
Eventually(logClient.SourceInstance).Should(HaveKey(""))
214+
Eventually(logClient.SourceInstance).Should(HaveKey("3"))
214215
})
215216

216217
It("doesn't emit LGR and SYS log to the log client about aggregate drains drops", func() {
217-
logClient := NewSpyLogClient()
218+
logClient := testhelper.NewSpyLogClient()
218219
connector := syslog.NewSyslogConnector(
219220
true,
220221
spyWaitGroup,
@@ -239,7 +240,7 @@ var _ = Describe("SyslogConnector", func() {
239240
}
240241
}(writer)
241242

242-
Consistently(logClient.message).ShouldNot(ContainElement(MatchRegexp("\\d messages lost for application (.*) in user provided syslog drain with url")))
243+
Consistently(logClient.Message()).ShouldNot(ContainElement(MatchRegexp("\\d messages lost for application (.*) in user provided syslog drain with url")))
243244
})
244245

245246
It("does not panic on unknown dropped metrics", func() {

0 commit comments

Comments
 (0)