Skip to content

Commit

Permalink
Use volatile instead of Atomic things in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zsxwing committed Jan 14, 2015
1 parent 227bf33 commit 460f7b3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/org/apache/spark/util/EventLoop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private[spark] abstract class EventLoop[E](name: String) extends Logging {
}

def stop(): Unit = {
if (stopped.compareAndSet(false ,true)) {
if (stopped.compareAndSet(false, true)) {
eventThread.interrupt()
eventThread.join()
// Call onStop after the event thread exits to make sure onReceive happens before onStop
Expand Down
16 changes: 7 additions & 9 deletions core/src/test/scala/org/apache/spark/util/EventLoopSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.spark.util

import java.util.concurrent.atomic.AtomicReference

import scala.collection.mutable
import scala.concurrent.duration._
import scala.language.postfixOps
Expand Down Expand Up @@ -62,43 +60,43 @@ class EventLoopSuite extends FunSuite {

test("EventLoop: onError") {
val e = new RuntimeException("Oops")
val receivedError = new AtomicReference[Throwable]()
@volatile var receivedError: Throwable = null
val eventLoop = new EventLoop[Int]("test") {

override def onReceive(event: Int): Unit = {
throw e
}

override def onError(e: Throwable): Unit = {
receivedError.set(e)
receivedError = e
}
}
eventLoop.start()
eventLoop.post(1)
eventually(timeout(5 seconds), interval(200 millis)) {
assert(e === receivedError.get)
assert(e === receivedError)
}
eventLoop.stop()
}

test("EventLoop: error thrown from onError should not crash the event thread") {
val e = new RuntimeException("Oops")
val receivedError = new AtomicReference[Throwable]()
@volatile var receivedError: Throwable = null
val eventLoop = new EventLoop[Int]("test") {

override def onReceive(event: Int): Unit = {
throw e
}

override def onError(e: Throwable): Unit = {
receivedError.set(e)
receivedError = e
throw new RuntimeException("Oops")
}
}
eventLoop.start()
eventLoop.post(1)
eventually(timeout(5 seconds), interval(200 millis)) {
assert(e === receivedError.get)
assert(e === receivedError)
assert(eventLoop.isActive)
}
eventLoop.stop()
Expand Down Expand Up @@ -129,7 +127,7 @@ class EventLoopSuite extends FunSuite {
}

test("EventLoop: post event in multiple threads") {
var receivedEventsCount = 0
@volatile var receivedEventsCount = 0
val eventLoop = new EventLoop[Int]("test") {

override def onReceive(event: Int): Unit = {
Expand Down

0 comments on commit 460f7b3

Please sign in to comment.