mirror of
https://github.com/f-droid/fdroidclient.git
synced 2026-06-14 10:55:47 -04:00
create testable LocalHTTPDManager for controlling the webserver
The RxJava tricks were a nightmare...
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package org.fdroid.fdroid.localrepo;
|
||||
|
||||
import android.content.Context;
|
||||
import org.fdroid.fdroid.FDroidApp;
|
||||
import org.fdroid.fdroid.Utils;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.shadows.ShadowLog;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class LocalHTTPDManagerTest {
|
||||
|
||||
@Test
|
||||
public void testStartStop() throws InterruptedException {
|
||||
ShadowLog.stream = System.out;
|
||||
Context context = RuntimeEnvironment.application;
|
||||
|
||||
final String host = "localhost";
|
||||
final int port = 8888;
|
||||
assertFalse(Utils.isServerSocketInUse(port));
|
||||
LocalHTTPDManager.stop(context);
|
||||
|
||||
FDroidApp.ipAddressString = host;
|
||||
FDroidApp.port = port;
|
||||
|
||||
LocalHTTPDManager.start(context, false);
|
||||
final CountDownLatch startLatch = new CountDownLatch(1);
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (!Utils.isServerSocketInUse(port)) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
startLatch.countDown();
|
||||
}
|
||||
}).start();
|
||||
assertTrue(startLatch.await(30, TimeUnit.SECONDS));
|
||||
assertTrue(Utils.isServerSocketInUse(port));
|
||||
assertTrue(Utils.canConnectToSocket(host, port));
|
||||
|
||||
LocalHTTPDManager.stop(context);
|
||||
final CountDownLatch stopLatch = new CountDownLatch(1);
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (!Utils.isServerSocketInUse(port)) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
stopLatch.countDown();
|
||||
}
|
||||
}).start();
|
||||
assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user