Trait for detecting timeouts

28 views
Skip to first unread message

Dieter Bogdoll

unread,
Nov 10, 2011, 3:14:29 PM11/10/11
to specs-users
I started to play around with scala and specs. I wanted to write test
cases where I test for blocking
butndidn't found anything and wrote one of my own.

The problem I have is to mark a run as success. To mark it as failed
is easy, but I have
no clue how to mark it as passed.

My question would be now: how can I mark my test as success?
Here is some snippet of my code:

def withTimeout[T](timeout: Long)(block: => T ) : T = {
val executor = Executors.newSingleThreadExecutor()
val future : Future[T] = executor.submit(() => block)

try {
future.get(timeout, TimeUnit.MILLISECONDS)
}
catch {
case e:TimeoutException => fail("Timeout reached "+timeout+" ms")
case e:Exception => throw e
}
finally {
executor.shutdownNow()
}
}

And here is a snippet of the test case:

"pass if sleep is shorter then timeout" in {
withTimeout(1000) {
Thread.sleep(500)
}
}
Message has been deleted

etorreborre

unread,
Nov 10, 2011, 7:51:36 PM11/10/11
to specs...@googlegroups.com
Hi Dieter,

Since you just started with Scala and specs, I propose that you have a look at specs2 instead.

I've used your suggestion to add a trait, "org.specs2.matcher.TerminationMatchers", doing what you want (in the latest 1.7-SNAPSHOT version):

  // succeeds (default retries = 0, sleep=100.millis)
  Thread.sleep(50) must terminate

  // fails
  Thread.sleep(100) must terminate(retries=3, sleep=20.millis)

I hope that works ok for you.

Eric.

Dieter Bogdoll

unread,
Nov 11, 2011, 8:59:54 AM11/11/11
to specs-users
Eric,

another question. In case the block doesn't terminate. What happens
with the thread? Do you call interrupt on it?

On 11 Nov., 01:51, etorreborre <etorrebo...@gmail.com> wrote:
> Hi Dieter,
>
> Since you just started with Scala and specs, I propose that you have a look
> at specs2 instead.
>
> I've used your suggestion to add a trait,
> "org.specs2.matcher.TerminationMatchers", doing what you want (in the
> latest 1.7-SNAPSHOT<http://scala-tools.org/repo-snapshots/org/specs2/specs2_2.9.1/1.7-SNA...>

Dieter Bogdoll

unread,
Nov 11, 2011, 8:56:23 AM11/11/11
to specs-users
Hi Eric,

thanks for the fast response. I switched to spec2 and rewrote my tests
to your implementation.
All looks fine.

Now I have another question :)
Further tests of mine not only want to test that something is
blocking, but they also want
to see that the blocking goes away when in another thread a specific
operation is done.
Lets take a empty queue. The reading should block, but when another
thread now writes
some objects into the queue the first thread should unblock.

I have a working solution for that, but have again the problem that
the test won't be shown as pass.
Here is the testing code:

"an empty Queue" should {
val q = new Queue

"block the reading of an event on an emtpy queue" in {
q.get() match terminate(sleep=1.seconds)
}

"than unblock when a data was added" in {
withDelay(1000,5000) firstDo { // after 1 second thanDo will be
started, after 5000 the executiorservice will be called with
shutdownNow()
q.get()
} thanDo {
q.add(new Object())
}
}

-Dieter
Reply all
Reply to author
Forward
0 new messages