Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tools to record/replay Java method calls

Our Java application has a number of modules which implement a common interface. By design, each module is well-encapsulated and interchangeable. All calls to/from go via a small set of interfaces.

We need to instrument this mechanism to add,

  1. Record the method calls and results - in a structured format
  2. Replay those calls against the module, when debugging an issue, or testing a fix.
  3. (Nice-to-have) Use the recorded interactions as an 'Expected' result, have them compared to 'Actual' when running JUnit tests.
  4. (Probably-should-have) Be fully thread-safe.

Are there recommended options for this?

A few options I glanced at:

  • Chronon - Looks like a different concept. 'No code of your program is being executed when you playback the recordings'
  • Some academic papers on JSnoopy, SCARPE; these don't seem to be public projects?
  • Write by hand e.g. capture the calls with AspectJ - Should work but I'm wondering if there's something off-the-shelf.
like image 268
Iain Avatar asked Nov 14 '22 08:11

Iain


1 Answers

Assuming you are very strict about adhering to your small set of interfaces, then you could make use of wrapper objects that do the following:

  • Implement the interface
  • Log/record the inputs
  • Delegate the call to the underlying object
  • Log/record the result
  • Return result to caller

You could also modify factory methods to return wrapped objects rather than the underlying objects so that the usage of these wrappers is fairly transparent.

Note that you will need to be very careful about mutability of parameters: ideally all parameters would be immutable but if not you would need to take deep copies (perhaps via serialization).

Theoretically you can then also use the logged inputs to enable playback and/or testing.... but I'd be cautious about expecting too much from this since playback would require you to capture all relevant state (including external state like DB transaction, filesystem access etc.) In general this is hard to achieve, though you might be able to make it work in your specific case.

like image 66
mikera Avatar answered Nov 16 '22 04:11

mikera