Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It should be possibly to give a timeout value to receivedOnce() #100

Open
Kreinoee opened this issue Jan 26, 2016 · 3 comments · May be fixed by #129
Open

It should be possibly to give a timeout value to receivedOnce() #100

Kreinoee opened this issue Jan 26, 2016 · 3 comments · May be fixed by #129

Comments

@Kreinoee
Copy link

This would make jadler a lot more usefull for testing async code. In this test:

initJadler()

val instance = new ClassToTest()
instance.startAsync()

verifyThatRequest()
  .havingMethodEqualTo("POST")
  .havingPathEqualTo(s"/start")
  .receivedOnce()

closeJadler()

instance.startAsync() starts an async task running on another thread, while the main thread returns immidiatly. The async task runs some logic, that ends up doing the expected HTTP request.

But even though the startAsync() method works as it should, the test will fail, as jadler tries to verify before the async task has had enough time to complete.

A current workaround is to add a wait, so that the test ends up looking like this:

initJadler()

val instance = new ClassToTest()
instance.startAsync()

synchronized {
  wait(2000)
}
verifyThatRequest()
  .havingMethodEqualTo("POST")
  .havingPathEqualTo(s"/start")
  .receivedOnce()

closeJadler()

But this is not a nice solution. Setting the wait to low, will make the test fail when run under heavy load, and setting it to high will make the test take much more time than needed.

I would instead suggest that it would be possibly to give jadler a duration of time, in where the expected http request is allowed to be performed. The call should return if the http request is received, or throw an exception if the duration runs out.

Some suggestion to how it could look:
1)

verifyThatRequest()
  .havingMethodEqualTo("POST")
  .havingPathEqualTo(s"/start")
  .receivedOnce()
  .withinDuration(2, TimeUnit.seconds)
verifyThatRequest()
  .havingMethodEqualTo("POST")
  .havingPathEqualTo(s"/start")
  .receivedOnce(2, TimeUnit.seconds)
verifyThatRequest()
  .havingMethodEqualTo("POST")
  .havingPathEqualTo(s"/start")
  .withingDuration(2, TimeUnit.seconds)
  .receivedOnce()
@jandudek
Copy link
Contributor

Hi @Kreinoee, thank you for this issue report! To fully understand it I need to clarify something first. Is there any real difference between:

synchronized {
  wait(2000)
}
verifyThatRequest()
  .havingMethodEqualTo("POST")
  .havingPathEqualTo(s"/start")
  .receivedOnce()

...and:

verifyThatRequest()
  .havingMethodEqualTo("POST")
  .havingPathEqualTo(s"/start")
  .receivedOnce()
  .withinDuration(2, TimeUnit.seconds)

As I see it the second example is just a syntactic sugar for the 2 seconds waiting. So if the tested async code is slower then usual (more then 2 seconds before the http request), both these verifications will fail.

@Kreinoee
Copy link
Author

Its a bit more than syntactic sugar.

In case the async code takes more than 2 seconds before it fires the http request, then the two examples will behave identical.

In case where the async code only use 15 milliseconds before it fires the http request, then the first example (using wait), will take 2 seconds before it reports the test as successfull, where the second using withinDuration should report success as soon as it has received and verified the http request.

It will properly make more sense to use one of the other suggestions as syntaxt (suggestion 2 or 3), as a normal call to receivedOnce() should return immidiatly. In this case it should not return before it has either received the expected call, or the timeout expires. I guess that this information must be given before or directly to the receivedOnce() call.

@Kreinoee
Copy link
Author

Kreinoee commented Oct 7, 2016

Just to avoid potential duoble work on this. I have made an implementation of this, based on suggesten 2. Onces I am done using it in my current project ( for testing it), I will make a pull request.

Kreinoee added a commit to Kreinoee/jadler that referenced this issue Oct 11, 2016
Kreinoee added a commit to Kreinoee/jadler that referenced this issue Oct 26, 2017
@Kreinoee Kreinoee linked a pull request Oct 30, 2017 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants