Skip to content

Commit 742a4f2

Browse files
prestonvasquezblink1073
authored andcommitted
GODRIVER-2986 Resolve failures in Race Detector Test (#1380)
(cherry picked from commit 59f7519)
1 parent 3cc28f4 commit 742a4f2

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

mongo/integration/change_stream_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ func TestChangeStream_ReplicaSet(t *testing.T) {
770770
require.NoError(mt, err, "failed to update idValue")
771771
}()
772772

773-
nextCtx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
773+
nextCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
774774
t.Cleanup(cancel)
775775

776776
type splitEvent struct {

mongo/integration/unified/client_entity.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type clientEntity struct {
6666

6767
eventsCountLock sync.RWMutex
6868
serverDescriptionChangedEventsCountLock sync.RWMutex
69+
eventProcessMu sync.RWMutex
6970

7071
entityMap *EntityMap
7172

@@ -471,6 +472,9 @@ func (c *clientEntity) processPoolEvent(evt *event.PoolEvent) {
471472
}
472473

473474
func (c *clientEntity) processServerDescriptionChangedEvent(evt *event.ServerDescriptionChangedEvent) {
475+
c.eventProcessMu.Lock()
476+
defer c.eventProcessMu.Unlock()
477+
474478
if !c.getRecordEvents() {
475479
return
476480
}
@@ -487,6 +491,9 @@ func (c *clientEntity) processServerDescriptionChangedEvent(evt *event.ServerDes
487491
}
488492

489493
func (c *clientEntity) processServerHeartbeatFailedEvent(evt *event.ServerHeartbeatFailedEvent) {
494+
c.eventProcessMu.Lock()
495+
defer c.eventProcessMu.Unlock()
496+
490497
if !c.getRecordEvents() {
491498
return
492499
}
@@ -499,6 +506,9 @@ func (c *clientEntity) processServerHeartbeatFailedEvent(evt *event.ServerHeartb
499506
}
500507

501508
func (c *clientEntity) processServerHeartbeatStartedEvent(evt *event.ServerHeartbeatStartedEvent) {
509+
c.eventProcessMu.Lock()
510+
defer c.eventProcessMu.Unlock()
511+
502512
if !c.getRecordEvents() {
503513
return
504514
}
@@ -511,6 +521,9 @@ func (c *clientEntity) processServerHeartbeatStartedEvent(evt *event.ServerHeart
511521
}
512522

513523
func (c *clientEntity) processServerHeartbeatSucceededEvent(evt *event.ServerHeartbeatSucceededEvent) {
524+
c.eventProcessMu.Lock()
525+
defer c.eventProcessMu.Unlock()
526+
514527
if !c.getRecordEvents() {
515528
return
516529
}
@@ -523,6 +536,9 @@ func (c *clientEntity) processServerHeartbeatSucceededEvent(evt *event.ServerHea
523536
}
524537

525538
func (c *clientEntity) processTopologyDescriptionChangedEvent(evt *event.TopologyDescriptionChangedEvent) {
539+
c.eventProcessMu.Lock()
540+
defer c.eventProcessMu.Unlock()
541+
526542
if !c.getRecordEvents() {
527543
return
528544
}

mongo/integration/unified/logger.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package unified
88

99
import (
10+
"sync"
11+
1012
"go.mongodb.org/mongo-driver/internal/logger"
1113
)
1214

@@ -20,9 +22,19 @@ type orderedLogMessage struct {
2022
// Logger is the Sink used to captured log messages for logger verification in
2123
// the unified spec tests.
2224
type Logger struct {
25+
// bufSize is the number of logs expected to be sent to the logger for a
26+
// unified spec test.
27+
bufSize int
28+
29+
// lastOrder increments each time the "Info" method is called, and is used to
30+
// determine when to close the logQueue.
2331
lastOrder int
24-
logQueue chan orderedLogMessage
25-
bufSize int
32+
33+
// orderMu guards the order value, which increments each time the "Info"
34+
// method is called. This is necessary since "Info" could be called from
35+
// multiple go routines, e.g. SDAM logs.
36+
orderMu sync.RWMutex
37+
logQueue chan orderedLogMessage
2638
}
2739

2840
func newLogger(olm *observeLogMessages, bufSize int) *Logger {
@@ -44,14 +56,17 @@ func (log *Logger) Info(level int, msg string, args ...interface{}) {
4456
return
4557
}
4658

47-
defer func() { log.lastOrder++ }()
48-
4959
// If the order is greater than the buffer size, we must return. This
5060
// would indicate that the logQueue channel has been closed.
5161
if log.lastOrder > log.bufSize {
5262
return
5363
}
5464

65+
log.orderMu.Lock()
66+
defer log.orderMu.Unlock()
67+
68+
defer func() { log.lastOrder++ }()
69+
5570
// Add the Diff back to the level, as there is no need to create a
5671
// logging offset.
5772
level = level + logger.DiffToInfo
@@ -68,7 +83,7 @@ func (log *Logger) Info(level int, msg string, args ...interface{}) {
6883
logMessage: logMessage,
6984
}
7085

71-
// If the order has reached the buffer size, then close the channe.
86+
// If the order has reached the buffer size, then close the channel.
7287
if log.lastOrder == log.bufSize {
7388
close(log.logQueue)
7489
}

mongo/integration/unified/unified_spec_runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (tc *TestCase) Run(ls LoggerSkipper) error {
224224
}
225225

226226
// Count the number of expected log messages over all clients.
227-
expectedLogCount := 0
227+
var expectedLogCount int
228228
for _, clientLog := range tc.ExpectLogMessages {
229229
expectedLogCount += len(clientLog.LogMessages)
230230
}

0 commit comments

Comments
 (0)