From 0d996e74a27341c2faea458a408209888ea97aed Mon Sep 17 00:00:00 2001 From: Markus Rathgeb Date: Mon, 6 May 2019 22:42:41 +0200 Subject: [PATCH] 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 --- .../core/audio/internal/AudioServletTest.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/itests/org.openhab.core.audio.tests/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioServletTest.java b/itests/org.openhab.core.audio.tests/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioServletTest.java index a5c0856ce..aaca36e00 100644 --- a/itests/org.openhab.core.audio.tests/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioServletTest.java +++ b/itests/org.openhab.core.audio.tests/src/main/java/org/eclipse/smarthome/core/audio/internal/AudioServletTest.java @@ -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 {