Skip to content

Commit 8d9135e

Browse files
committed
Made MessageRecorder thread-safe.
1 parent f80f3e5 commit 8d9135e

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

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

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,30 +132,25 @@ 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+
@volatile private var messages = List[(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])] = synchronized {
139+
messages.reverse
140+
}
147141

148142
def apply(message: String, payload: Option[Any], eventFun: RecordedMessageEventFun, location: Option[Location]): Unit = {
149143
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
144+
synchronized {
145+
messages ::= (message, payload, eventFun, location)
146+
}
154147
}
155148

156149
def recordedEvents(testWasPending: Boolean, testWasCanceled: Boolean): collection.immutable.IndexedSeq[RecordableEvent] = {
157-
Vector.empty ++ recordedMessages.map { case (message, payload, eventFun, location) =>
158-
eventFun(message, payload, true, testWasPending, testWasCanceled, location)
150+
synchronized {
151+
Vector.empty ++ recordedMessages.map { case (message, payload, eventFun, location) =>
152+
eventFun(message, payload, true, testWasPending, testWasCanceled, location)
153+
}
159154
}
160155
}
161156
}

0 commit comments

Comments
 (0)