Skip to content

Commit a8298b0

Browse files
author
Christopher Swenson
committed
Avoid race detector in test
1 parent 5e6314e commit a8298b0

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

builtin/logical/database/rotation_test.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -385,19 +385,6 @@ func TestBackend_StaticRole_Rotation_Schedule_ErrorRecover(t *testing.T) {
385385
// should match because rotations should not occur outside the rotation window
386386
t.Fatalf("expected passwords to match, got (%s)", checkPassword)
387387
}
388-
if len(eventSender.Events) == 0 {
389-
t.Fatal("Expected to have some events but got none")
390-
}
391-
// check that we got a rotate-fail event
392-
found := false
393-
for _, event := range eventSender.Events {
394-
if string(event.Type) == "database/rotate-fail" {
395-
found = true
396-
break
397-
}
398-
}
399-
assert.True(t, found)
400-
401388
// Verify new username/password
402389
verifyPgConn(t, username, checkPassword, connURL)
403390

@@ -425,7 +412,20 @@ func TestBackend_StaticRole_Rotation_Schedule_ErrorRecover(t *testing.T) {
425412
// Verify new username/password
426413
verifyPgConn(t, username, checkPassword, connURL)
427414

415+
eventSender.Stop() // avoid race detector
428416
// check that we got a successful rotation event
417+
if len(eventSender.Events) == 0 {
418+
t.Fatal("Expected to have some events but got none")
419+
}
420+
// check that we got a rotate-fail event
421+
found := false
422+
for _, event := range eventSender.Events {
423+
if string(event.Type) == "database/rotate-fail" {
424+
found = true
425+
break
426+
}
427+
}
428+
assert.True(t, found)
429429
found = false
430430
for _, event := range eventSender.Events {
431431
if string(event.Type) == "database/rotate" {

sdk/logical/events_mock.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
// meant to be used in testing. It is thread-safe.
1313
type MockEventSender struct {
1414
sync.Mutex
15-
Events []MockEvent
15+
Events []MockEvent
16+
Stopped bool
1617
}
1718

1819
// MockEvent is a container for an event type + event pair.
@@ -25,10 +26,18 @@ type MockEvent struct {
2526
func (m *MockEventSender) SendEvent(_ context.Context, eventType EventType, event *EventData) error {
2627
m.Lock()
2728
defer m.Unlock()
28-
m.Events = append(m.Events, MockEvent{Type: eventType, Event: event})
29+
if !m.Stopped {
30+
m.Events = append(m.Events, MockEvent{Type: eventType, Event: event})
31+
}
2932
return nil
3033
}
3134

35+
func (m *MockEventSender) Stop() {
36+
m.Lock()
37+
defer m.Unlock()
38+
m.Stopped = true
39+
}
40+
3241
var _ EventSender = (*MockEventSender)(nil)
3342

3443
// NewMockEventSender returns a new MockEventSender ready to be used.

0 commit comments

Comments
 (0)