Created
March 10, 2014 04:38
-
-
Save stephenh/9459564 to your computer and use it in GitHub Desktop.
TestBaton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.mutable.Buffer | |
/** | |
* An object into a process under to test to, at test time, purposefully cause it to fail. | |
* | |
* A typical usage might be: | |
* | |
* class SomeProcess(..., testBaton = NoopBaton() { | |
* def run() { | |
* val items = findSomeWorkItems() | |
* for (item <- items) { | |
* try { | |
* // real work | |
* testBaton.failIfTestingRecovery(item.getId) | |
* } catch { | |
* case e: Exception => | |
* testBaton.recordRecoveredFailure(e) | |
* } | |
* } | |
* } | |
* } | |
*/ | |
trait TestBaton { | |
def failIfTestingRecovery(o: Object): Unit | |
def recordRecoveredFailure(e: Exception): Unit | |
} | |
/** The baton for production. */ | |
class NoopBaton extends TestBaton { | |
override def failIfTestingRecovery(o: Object) {} | |
override def recordRecoveredFailure(e: Exception) {} | |
} | |
/** The baton for testing. */ | |
class StubTestBaton extends TestBaton { | |
var failOn: Option[Object] = None | |
var numberOfIterations = 0 | |
val recoveredFailures = Buffer[Exception]() | |
def assertNoErrors() { | |
require(recoveredFailures == 0) | |
} | |
override def failIfTestingRecovery(o: Object) { | |
if (failOn == Some(o)) { | |
sys.error("Dummy failure") | |
} | |
} | |
override def recordRecoveredFailure(e: Exception): Unit = { | |
recoveredFailures += e | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment