0

so I have a function that deletes some object, thing. This can take a while, like a half hour ish, and I want to check if it is successfully deleted.

@Test
public void successfulThingDelete(){
    Thing thing = new Thing();
    deleteThing(thing);
    if (thing.getStatus() == 'deleted'){
        pass
    }
    else {
        fail
    }

I want to be able to continually check the status of thing (i.e thing.getStatus()) and pass the test if it is deleted. But if a certain time elapses and it's not deleted then the code has failed and it should fail. I'm assuming I need to introduce a new thread for this pinging of the status but I'm not sure how to add that within this method. Thanks for any help!

cbrad97
  • 83
  • 1
  • 1
  • 5
  • Possible duplicate of [How do I time a method's execution in Java?](https://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java) – dave Aug 01 '18 at 15:12

2 Answers2

0

I'm guessing that this is a Junit test. The Test annotation allows a timeout attribute in the form "...@Test(timeout=1000)..." - the value is in milliseconds. So calculate the milliseconds in thirty minutes - 1800000 and use that. Junit will fail the test if it isn't finished in that time.

    @Test(timeout=1800000)
public void successfulThingDelete(){...

If the test runs its course and finishes before the time limit then the usual coded assertions happen and the test ends. if the test actions take longer then Junit will interrupt whatever's running and fail the test overall. Ref - https://github.com/junit-team/junit4/wiki/timeout-for-tests

Pete Kelley
  • 3,713
  • 2
  • 16
  • 17
  • Of course this doesn't continually test as your original post says, but I think it accomplishes your goal. Will the code that reads "...deleteThing(thing)..." wait while it is working - or does that method complete while some other process is actually doing the "delete-ing"? – Pete Kelley Aug 01 '18 at 15:18
  • But if the deletion happened quickly, I would still have to spend thirty minutes on the test. With this I'd have to let the clock run out – cbrad97 Aug 01 '18 at 17:51
  • If the deletion happens quickly - that is to say the deleteThing(thing) call finishes - then the if-then would check the status immediately afterwards. the whole thing would finish then, not in thirty minutes. – Pete Kelley Aug 01 '18 at 18:29
  • I must ask to clarify - when the method deleteThing(...) finishes - is the process finished too? or is there some other procress that gets going on in the background? The latter case would require something like what Chris311 was talking about with asynchronous handling. But if the deleteThing(...) method has nothing more to do then you're ready to check the status. – Pete Kelley Aug 01 '18 at 18:36
0

I would go for awaitility. With that you can write tests like

@Test
public void updatesCustomerStatus() throws Exception {
    // Publish an asynchronous event:
    publishEvent(updateCustomerStatusEvent);
    // Awaitility lets you wait until the asynchronous operation completes:
    await().atMost(5, SECONDS).until(customerStatusIsUpdated());
    ...
}
Chris311
  • 3,794
  • 9
  • 46
  • 80
  • That looks useful for a number of asynchronous tasks I've needed to test. It's a big improvement to read a function like that over making loops to check-status-and-recheck-until-timesup. – Pete Kelley Aug 03 '18 at 11:45