Fixed cases where one failing test broke all following tests

Select a random port for the http server, so we do not get EADDRINUSE
This commit is contained in:
ByteHamster 2019-12-02 09:37:33 +01:00
parent 0d5e3307b0
commit e77ecda206
4 changed files with 34 additions and 36 deletions

View File

@ -29,10 +29,9 @@ public class HttpDownloaderTest {
private static final String TAG = "HttpDownloaderTest";
private static final String DOWNLOAD_DIR = "testdownloads";
private static boolean successful = true;
private String url404;
private String urlAuth;
private File destDir;
private HTTPBin httpServer;
public HttpDownloaderTest() {
@ -57,6 +56,8 @@ public class HttpDownloaderTest {
assertTrue(destDir.exists());
httpServer = new HTTPBin();
httpServer.start();
url404 = httpServer.getBaseUrl() + "/status/404";
urlAuth = httpServer.getBaseUrl() + "/basic-auth/user/passwd";
}
private FeedFileImpl setupFeedFile(String downloadUrl, String title, boolean deleteExisting) {
@ -88,33 +89,29 @@ public class HttpDownloaderTest {
return downloader;
}
private static final String URL_404 = HTTPBin.BASE_URL + "/status/404";
private static final String URL_AUTH = HTTPBin.BASE_URL + "/basic-auth/user/passwd";
@Test
public void testPassingHttp() {
download(HTTPBin.BASE_URL + "/status/200", "test200", true);
download(httpServer.getBaseUrl() + "/status/200", "test200", true);
}
@Test
public void testRedirect() {
download(HTTPBin.BASE_URL + "/redirect/4", "testRedirect", true);
download(httpServer.getBaseUrl() + "/redirect/4", "testRedirect", true);
}
@Test
public void testGzip() {
download(HTTPBin.BASE_URL + "/gzip/100", "testGzip", true);
download(httpServer.getBaseUrl() + "/gzip/100", "testGzip", true);
}
@Test
public void test404() {
download(URL_404, "test404", false);
download(url404, "test404", false);
}
@Test
public void testCancel() {
final String url = HTTPBin.BASE_URL + "/delay/3";
final String url = httpServer.getBaseUrl() + "/delay/3";
FeedFileImpl feedFile = setupFeedFile(url, "delay", true);
final Downloader downloader = new HttpDownloader(new DownloadRequest(feedFile.getFile_url(), url, "delay", 0, feedFile.getTypeAsInt()));
Thread t = new Thread() {
@ -139,7 +136,7 @@ public class HttpDownloaderTest {
@Test
public void testDeleteOnFailShouldDelete() {
Downloader downloader = download(URL_404, "testDeleteOnFailShouldDelete", false, true, null, null, true);
Downloader downloader = download(url404, "testDeleteOnFailShouldDelete", false, true, null, null, true);
assertFalse(new File(downloader.getDownloadRequest().getDestination()).exists());
}
@ -149,18 +146,18 @@ public class HttpDownloaderTest {
File dest = new File(destDir, filename);
dest.delete();
assertTrue(dest.createNewFile());
Downloader downloader = download(URL_404, filename, false, false, null, null, false);
Downloader downloader = download(url404, filename, false, false, null, null, false);
assertTrue(new File(downloader.getDownloadRequest().getDestination()).exists());
}
@Test
public void testAuthenticationShouldSucceed() throws InterruptedException {
download(URL_AUTH, "testAuthSuccess", true, true, "user", "passwd", true);
download(urlAuth, "testAuthSuccess", true, true, "user", "passwd", true);
}
@Test
public void testAuthenticationShouldFail() {
Downloader downloader = download(URL_AUTH, "testAuthSuccess", false, true, "user", "Wrong passwd", true);
Downloader downloader = download(urlAuth, "testAuthSuccess", false, true, "user", "Wrong passwd", true);
assertEquals(DownloadError.ERROR_UNAUTHORIZED, downloader.getResult().getReason());
}

View File

@ -45,13 +45,12 @@ import static org.junit.Assert.fail;
*/
@MediumTest
public class PlaybackServiceMediaPlayerTest {
private static final String PLAYABLE_FILE_URL = "http://127.0.0.1:" + HTTPBin.PORT + "/files/0";
private static final String PLAYABLE_DEST_URL = "psmptestfile.mp3";
private String PLAYABLE_LOCAL_URL = null;
private static final int LATCH_TIMEOUT_SECONDS = 3;
private HTTPBin httpServer;
private String playableFileUrl;
private volatile AssertionFailedError assertionError;
@After
@ -71,6 +70,7 @@ public class PlaybackServiceMediaPlayerTest {
httpServer = new HTTPBin();
httpServer.start();
playableFileUrl = httpServer.getBaseUrl() + "/files/0";
File cacheDir = context.getExternalFilesDir("testFiles");
if (cacheDir == null)
@ -167,7 +167,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, null);
Playable p = writeTestPlayable(playableFileUrl, null);
psmp.playMediaObject(p, true, false, false);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (assertionError != null)
@ -207,7 +207,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, null);
Playable p = writeTestPlayable(playableFileUrl, null);
psmp.playMediaObject(p, true, true, false);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
@ -251,7 +251,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, null);
Playable p = writeTestPlayable(playableFileUrl, null);
psmp.playMediaObject(p, true, false, true);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (assertionError != null)
@ -296,7 +296,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, null);
Playable p = writeTestPlayable(playableFileUrl, null);
psmp.playMediaObject(p, true, true, true);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (assertionError != null)
@ -334,7 +334,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
Playable p = writeTestPlayable(playableFileUrl, PLAYABLE_LOCAL_URL);
psmp.playMediaObject(p, false, false, false);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (assertionError != null)
@ -373,7 +373,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
Playable p = writeTestPlayable(playableFileUrl, PLAYABLE_LOCAL_URL);
psmp.playMediaObject(p, false, true, false);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (assertionError != null)
@ -415,7 +415,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
Playable p = writeTestPlayable(playableFileUrl, PLAYABLE_LOCAL_URL);
psmp.playMediaObject(p, false, false, true);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (assertionError != null)
@ -460,7 +460,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
Playable p = writeTestPlayable(playableFileUrl, PLAYABLE_LOCAL_URL);
psmp.playMediaObject(p, false, true, true);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (assertionError != null)
@ -523,7 +523,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
Playable p = writeTestPlayable(playableFileUrl, PLAYABLE_LOCAL_URL);
if (initialState == PlayerStatus.PLAYING) {
psmp.playMediaObject(p, stream, true, true);
}
@ -616,7 +616,7 @@ public class PlaybackServiceMediaPlayerTest {
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
if (initialState == PlayerStatus.PREPARED || initialState == PlayerStatus.PLAYING || initialState == PlayerStatus.PAUSED) {
boolean startWhenPrepared = (initialState != PlayerStatus.PREPARED);
psmp.playMediaObject(writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL), false, startWhenPrepared, true);
psmp.playMediaObject(writeTestPlayable(playableFileUrl, PLAYABLE_LOCAL_URL), false, startWhenPrepared, true);
}
if (initialState == PlayerStatus.PAUSED) {
psmp.pause(false, false);
@ -673,7 +673,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
Playable p = writeTestPlayable(playableFileUrl, PLAYABLE_LOCAL_URL);
if (initialState == PlayerStatus.INITIALIZED
|| initialState == PlayerStatus.PLAYING
|| initialState == PlayerStatus.PREPARED
@ -747,7 +747,7 @@ public class PlaybackServiceMediaPlayerTest {
}
});
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
Playable p = writeTestPlayable(playableFileUrl, PLAYABLE_LOCAL_URL);
boolean prepareImmediately = initialState != PlayerStatus.INITIALIZED;
boolean startImmediately = initialState != PlayerStatus.PREPARED;
psmp.playMediaObject(p, false, startImmediately, prepareImmediately);

View File

@ -87,13 +87,13 @@ public class UITestUtils {
out.close();
int id = server.serveFile(feedFile);
Assert.assertTrue(id != -1);
return String.format("%s/files/%d", HTTPBin.BASE_URL, id);
return String.format("%s/files/%d", server.getBaseUrl(), id);
}
private String hostFile(File file) {
int id = server.serveFile(file);
Assert.assertTrue(id != -1);
return String.format("%s/files/%d", HTTPBin.BASE_URL, id);
return String.format("%s/files/%d", server.getBaseUrl(), id);
}
private File newBitmapFile(String name) throws IOException {

View File

@ -39,9 +39,6 @@ import de.danoeh.antennapod.BuildConfig;
*/
public class HTTPBin extends NanoHTTPD {
private static final String TAG = "HTTPBin";
public static final int PORT = 8124;
public static final String BASE_URL = "http://127.0.0.1:" + HTTPBin.PORT;
private static final String MIME_HTML = "text/html";
private static final String MIME_PLAIN = "text/plain";
@ -49,10 +46,14 @@ public class HTTPBin extends NanoHTTPD {
private final List<File> servedFiles;
public HTTPBin() {
super(PORT);
super(0); // Let system pick a free port
this.servedFiles = new ArrayList<>();
}
public String getBaseUrl() {
return "http://127.0.0.1:" + getListeningPort();
}
/**
* Adds the given file to the server.
*