Skip to content

Commit 2222aa3

Browse files
committed
Merge branch 'cheeseng-thread-safe-message-recorder' into 3.1.x
2 parents f80f3e5 + cfc2c5f commit 2222aa3

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

scalatest/src/main/scala/org/scalatest/ConcurrentInformer.scala

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,17 @@ private[scalatest] object ConcurrentDocumenter {
132132
//
133133
private[scalatest] class MessageRecorder(dispatch: Reporter) extends ThreadAwareness {
134134

135-
private var messages = List[(String, Option[Any], RecordedMessageEventFun, Option[Location])]()
136-
137-
// Should only be called by the thread that constructed this
138-
// ConcurrentInformer, because don't want to worry about synchronization here. Just send stuff from
139-
// other threads whenever they come in. So only call record after first checking isConstructingThread
140-
private def record(message: String, payload: Option[Any], eventFun: RecordedMessageEventFun, location: Option[Location]): Unit = {
141-
require(isConstructingThread)
142-
messages ::= (message, payload, eventFun, location)
143-
}
135+
private var messages = List.empty[(String, Option[Any], RecordedMessageEventFun, Option[Location])]
144136

145137
// Returns them in order recorded
146-
private def recordedMessages: List[(String, Option[Any], RecordedMessageEventFun, Option[Location])] = messages.reverse
138+
private def recordedMessages: List[(String, Option[Any], RecordedMessageEventFun, Option[Location])] =
139+
synchronized { messages.reverse }
147140

148141
def apply(message: String, payload: Option[Any], eventFun: RecordedMessageEventFun, location: Option[Location]): Unit = {
149142
requireNonNull(message, payload)
150-
if (isConstructingThread)
151-
record(message, payload, eventFun, location)
152-
else
153-
dispatch(eventFun(message, payload, false, false, false, location)) // Fire the info provided event using the passed function
143+
synchronized {
144+
messages ::= (message, payload, eventFun, location)
145+
}
154146
}
155147

156148
def recordedEvents(testWasPending: Boolean, testWasCanceled: Boolean): collection.immutable.IndexedSeq[RecordableEvent] = {

0 commit comments

Comments
 (0)