Skip to content

Commit 2b94cad

Browse files
committed
Added test for rate limiting
1 parent f6ed999 commit 2b94cad

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/github.com/getlantern/flashlight/logging/logging.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ func shouldReport(location string) bool {
319319
lastReportedLock.Lock()
320320
defer lastReportedLock.Unlock()
321321
shouldReport := now.Sub(lastReported[location]) > logglyRateLimit
322-
lastReported[location] = now
322+
if shouldReport {
323+
lastReported[location] = now
324+
}
323325
return shouldReport
324326
}
325327

src/github.com/getlantern/flashlight/logging/logging_test.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,31 @@ func TestLoggly(t *testing.T) {
6060
golog.RegisterReporter(r.Report)
6161
log := golog.LoggerFor("test")
6262

63-
log.Error("")
64-
log.Debugf("**************** %v", buf.String())
65-
if assert.NoError(t, json.Unmarshal(buf.Bytes(), &result), "Unmarshal error") {
66-
assert.Regexp(t, "test: logging_test.go:([0-9]+)", result["locationInfo"])
67-
assert.Equal(t, "", result["message"])
63+
origLogglyRateLimit := logglyRateLimit
64+
defer func() {
65+
logglyRateLimit = origLogglyRateLimit
66+
}()
67+
logglyRateLimit = 100 * time.Millisecond
68+
for i := 0; i < 2; i++ {
69+
buf.Reset()
70+
log.Error("short message")
71+
if i == 1 {
72+
assert.Equal(t, 0, buf.Len(), "duplicate shouldn't have been reported")
73+
time.Sleep(logglyRateLimit)
74+
} else {
75+
if assert.NoError(t, json.Unmarshal(buf.Bytes(), &result), "Unmarshal error") {
76+
assert.Regexp(t, "test: logging_test.go:([0-9]+)", result["locationInfo"])
77+
assert.Equal(t, "short message", result["message"])
78+
}
79+
}
6880
}
6981

7082
buf.Reset()
71-
log.Error("short message")
83+
log.Error("")
84+
log.Debugf("**************** %v", buf.String())
7285
if assert.NoError(t, json.Unmarshal(buf.Bytes(), &result), "Unmarshal error") {
7386
assert.Regexp(t, "test: logging_test.go:([0-9]+)", result["locationInfo"])
74-
assert.Equal(t, "short message", result["message"])
87+
assert.Equal(t, "", result["message"])
7588
}
7689

7790
buf.Reset()

0 commit comments

Comments
 (0)