audio servlet test: harden against slow test systems (#802)

The test suite for the audio servlet contains a test that will fail on
systems under heavy load (or just imperformant systems).

An audio stream should be added for 1 (streamTimeout) second.
The stream is requested and it is tested that it can be accessed.
After that the test waits until the stream is no more available (this
will be the cause after "streamTimeout").

If the "add stream" and "get request for stream" operations already
exceed the "streamTimeout" limit, the test will fail.
This can be handled in the test case itself if we check the timespan we
need to get the stream and if we know that the stream is allowed to be
non present already, we continue with the next step without failing.

Fixes: https://github.com/openhab/openhab-core/issues/799

Signed-off-by: Markus Rathgeb <maggu2810@gmail.com>
This commit is contained in:
Markus Rathgeb 2019-05-06 22:42:41 +02:00 committed by Kai Kreuzer
parent 52c2456461
commit 0d996e74a2

View File

@ -12,10 +12,11 @@
*/
package org.eclipse.smarthome.core.audio.internal;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
@ -111,22 +112,33 @@ public class AudioServletTest extends AbstractAudioServeltTest {
@Test
public void requestToMultitimeStreamCannotBeDoneAfterTheTimeoutOfTheStreamHasExipred() throws Exception {
final int streamTimeout = 1;
AudioStream audioStream = getByteArrayAudioStream(testByteArray, AudioFormat.CONTAINER_NONE,
AudioFormat.CODEC_MP3);
int streamTimeout = 1;
final long beg = System.currentTimeMillis();
String url = serveStream(audioStream, streamTimeout);
Request request = getHttpRequest(url);
ContentResponse response = request.send();
assertThat("The response status was not as expected", response.getStatus(), is(HttpStatus.OK_200));
assertThat("The response content was not as expected", response.getContent(), is(testByteArray));
assertThat("The response media type was not as expected", response.getMediaType(), is(MEDIA_TYPE_AUDIO_MPEG));
final long end = System.currentTimeMillis();
assertThat("The audio stream was not added to the multitime streams",
audioServlet.getMultiTimeStreams().containsValue(audioStream), is(true));
if (response.getStatus() == HttpStatus.NOT_FOUND_404) {
assertThat("Response status 404 is only allowed if streamTimeout is exceeded.",
TimeUnit.MILLISECONDS.toSeconds(end - beg), greaterThan((long) streamTimeout));
} else {
assertThat("The response status was not as expected", response.getStatus(), is(HttpStatus.OK_200));
assertThat("The response content was not as expected", response.getContent(), is(testByteArray));
assertThat("The response media type was not as expected", response.getMediaType(),
is(MEDIA_TYPE_AUDIO_MPEG));
assertThat("The audio stream was not added to the multitime streams",
audioServlet.getMultiTimeStreams().containsValue(audioStream), is(true));
}
waitForAssert(() -> {
try {