Reduce the maximum number of stacks at runtime in Java

I am trying to write a junit test to protect against a piece of code stuck in an endless iteration that will eventually call StackOverflow.

so I'm looking for a way to reduce the size of the stack at runtime, so Junittest will work faster.

setting the maximum stack as the jvm argument is not possible because the test is part of a much larger set of tests.

+3
source share
4 answers

You can run a recursive method that will run itself a certain number of times, and then perform the specified action. Sounds pretty flaky though :(

Sort of:

public void eatStackThenExecute(int depth, Runnable action)
{
    // Maybe put some locals here (and use them) to eat more stack per iteration?
    if (depth == 0)
    {
        action();
    }
    else
    {
        eatStackThenExecute(depth - 1, action);
    }
}

EDIT: , JVM , , - , ...

Ick 'n stuff: (

+5

, , , :

  • - ;
  • thread.getStackTrace() , , x;
  • , .

( ):

AtomicBoolean success = new AtomicBoolean(false);

Thread t= new Thread(new Runnable() {
   public void run() {
       codeToTest();
       success.set(true);
   }

});

t.start();

while ( t.isAlive() ) {
     if ( t.getStackTrace().length > 50 )
          fail("Stack trace too large");

     Thread.sleep(50);
}

assertTrue(sucess.get());
+2

, Java. , , - .

+1

It's a little ... well, interesting ... but it might be worth it.

  • Take the .class file and decompile it with jasper .
  • Edit the resulting assembly JVM code and add or modify the ".limit stack x" argument for the procedure you want to test this way.
  • Recompilation with Jasmin .
  • Run it and check.
0
source

Source: https://habr.com/ru/post/1710240/


All Articles