1

I am making an assumption that a code path is only executed once within the JVM. However, when writing complex, multi-threaded system sometimes the assumption can be wrong.

Are there any one liner assertion I can use to assert this. Something like Guava's Preconditions.checkState(expr) but more like assertAtMostTimes(1, "Should not be executing more than once"). Prefer something simple rather than tracking number of times in a class variable.

This is similar to how test cases can be written but I am looking for something in production code.

Community
  • 1
  • 1
kctang
  • 10,894
  • 8
  • 44
  • 63
  • For *production* or for *validation testing*? – user2864740 Apr 25 '14 at 23:57
  • @user2864740 Production code - during runtime execution (actual usage of the application. Something like how we can turn on Java's assertion during runtime to check for "wrong assumptions during runtime". – kctang Apr 26 '14 at 09:28

1 Answers1

1

Check out the JaCoCo Code coverage tool. It has the ability to watch code paths and it keeps count of how many times they are executed. It isn't trivially easy to set up, but it should be able to do what you want.

Cody S
  • 4,744
  • 8
  • 33
  • 64
  • I need something that can check for assertions during runtime execution throughout an application's lifecycle (much like how I can turn on Java's assertion during runtime). Test code coverage reports are not applicable in this case. Appreciate the effort to help. ;-) – kctang Apr 26 '14 at 09:31
  • @kctang Jacoco is capable of attaching during runtime. Basically, you put it on the Tomcat classpath, and start executing code against Tomcat, and it generates usage reports based on that code, and continues to do so until you terminate Tomcat (there's a way to pull the report from Tomcat while it's running too). I know its _default_ mode is to show Unit test coverage, but I'm talking about a secondary mode that it can run in. Hope this helps! – Cody S Apr 28 '14 at 16:54