I have a simple restarting Runnable:
static void launchThreads(){
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
try {
exec.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
try {
System.out.println("line"); <--breakpoint
}
catch (Exception e) {
e.printStackTrace(); <--breakpoint
}
}
}, 1, 1000, TimeUnit.MILLISECONDS);
}catch (Exception e) {
e.printStackTrace(); <--breakpoint
}
}
If I launch that method from the main() method of the class, it works as expected - writes a line that looks like "line", once a second, forever.
line
line
line
line
...
But if I launch that from a TestNG test method:
@Test
public class PostpackagesIntegratorTest {
@Test
public void testLaunchThreads10SmallestWithoutFees() {
PostpackagesIntegrator.launchThreads();
}
}
,it outputs only one "line" and the test is passed. "Successfully".
If I make a JUnit4 test to launch the same method,
public class PostpackagesIntegratorJUnit4Test {
@Test
public void launchThreadsTest() {
PostpackagesIntegrator.launchThreads();
}
}
, the test is also passed, again with only one "line" in output.
If I am not running, but debugging the tests, my IntelliJ stops at printing the "line", but does not notice any catch
content.
I do not understand, what prevents the ScheduledExecutorService from repetitions. According to docs, such non-repeating should happen at an exception, but no exception happens.
Is it possible to make ScheduledExecutorService in TestNG tests or must I use other classes? Due to the whole project, I am limited by Java 6 version and TestNG.
Edit: @Eugene advised to declare exec
as private static final ScheduledExecutorService exec, for blocking erroneous GC, but it did not help and even didn't change anything - the problem is elsewhere.