* Refactor JettyIntegrationTest

* Refactor RxJava samples

* Refactor RxJava samples
This commit is contained in:
Grzegorz Piwowarek 2017-10-09 23:51:39 +02:00 committed by GitHub
parent 0f3df12fed
commit 47783354dd
3 changed files with 60 additions and 43 deletions

View File

@ -7,7 +7,9 @@ import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.nio.charset.StandardCharsets;
@ -15,17 +17,16 @@ import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
public class JettyIntegrationTest {
private JettyServer jettyServer;
private static JettyServer jettyServer;
@Before
public void setup() throws Exception {
@BeforeClass
public static void setup() throws Exception {
jettyServer = new JettyServer();
jettyServer.start();
}
@After
public void cleanup() throws Exception {
Thread.sleep(2000);
@AfterClass
public static void cleanup() throws Exception {
jettyServer.stop();
}

View File

@ -1,7 +1,6 @@
package com.baeldung.rxjava;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import rx.Observable;
@ -16,9 +15,11 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import static com.jayway.awaitility.Awaitility.await;
import static java.util.concurrent.Executors.newFixedThreadPool;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class SchedulersTest {
private String result = "";
@ -31,7 +32,8 @@ public class SchedulersTest {
Scheduler scheduler = Schedulers.immediate();
Scheduler.Worker worker = scheduler.createWorker();
worker.schedule(() -> result += "action");
Assert.assertTrue(result.equals("action"));
assertTrue(result.equals("action"));
}
@Test
@ -44,8 +46,9 @@ public class SchedulersTest {
worker.unsubscribe();
});
worker.schedule(() -> result += "Second_Action");
Thread.sleep(500);
Assert.assertTrue(result.equals("First_Action"));
await()
.until(() -> assertTrue(result.equals("First_Action")));
}
@Ignore //it's not safe, not every time is running correctly
@ -59,8 +62,9 @@ public class SchedulersTest {
worker.schedule(() -> result += "_worker_");
result += "_End";
});
Thread.sleep(2000);
Assert.assertTrue(result.equals("RxNewThreadScheduler-1_Start_End_worker_"));
await()
.until(() -> assertTrue(result.equals("RxNewThreadScheduler-1_Start_End_worker_")));
}
@Test
@ -75,9 +79,11 @@ public class SchedulersTest {
.subscribe(s ->
result1 += Thread.currentThread().getName()
);
Thread.sleep(500);
Assert.assertTrue(result1.equals("RxNewThreadScheduler-1"));
Assert.assertTrue(result2.equals("RxNewThreadScheduler-2"));
await()
.until(() -> {
assertTrue(result1.equals("RxNewThreadScheduler-1"));
assertTrue(result2.equals("RxNewThreadScheduler-2"));
});
}
@Test
@ -90,8 +96,9 @@ public class SchedulersTest {
worker.schedule(() -> result += "_worker_");
result += "_End";
});
Thread.sleep(500);
Assert.assertTrue(result.equals("main_Start_worker__End"));
await()
.until(() -> assertTrue(result.equals("main_Start_worker__End")));
}
@Test
@ -102,8 +109,9 @@ public class SchedulersTest {
.subscribe(s ->
result += Thread.currentThread().getName()
);
Thread.sleep(500);
Assert.assertTrue(result.equals("main"));
await()
.until(() -> assertTrue(result.equals("main")));
}
@Test
@ -115,8 +123,9 @@ public class SchedulersTest {
Observable.just(1, 3, 5, 7, 9)
.subscribeOn(Schedulers.trampoline())
.subscribe(i -> result += "" + i);
Thread.sleep(500);
Assert.assertTrue(result.equals("246813579"));
await()
.until(() -> assertTrue(result.equals("246813579")));
}
@Test
@ -135,9 +144,9 @@ public class SchedulersTest {
});
result += "_mainEnd";
});
Thread.sleep(500);
Assert.assertTrue(result
.equals("mainStart_mainEnd_middleStart_middleEnd_worker_"));
await()
.until(() -> assertTrue(result.equals("mainStart_mainEnd_middleStart_middleEnd_worker_")));
}
private ThreadFactory threadFactory(String pattern) {
@ -159,7 +168,6 @@ public class SchedulersTest {
subscriber.onNext("Beta");
subscriber.onCompleted();
});
;
observable
.subscribeOn(schedulerA)
@ -169,8 +177,9 @@ public class SchedulersTest {
Throwable::printStackTrace,
() -> result += "_Completed"
);
Thread.sleep(2000);
Assert.assertTrue(result.equals("Sched-A-0Alfa_Sched-A-0Beta__Completed"));
await()
.until(() -> assertTrue(result.equals("Sched-A-0Alfa_Sched-A-0Beta__Completed")));
}
@Test
@ -179,8 +188,9 @@ public class SchedulersTest {
Observable.just("io")
.subscribeOn(Schedulers.io())
.subscribe(i -> result += Thread.currentThread().getName());
Thread.sleep(500);
Assert.assertTrue(result.equals("RxIoScheduler-2"));
await()
.until(() -> assertTrue(result.equals("RxIoScheduler-2")));
}
@Test
@ -189,8 +199,9 @@ public class SchedulersTest {
Observable.just("computation")
.subscribeOn(Schedulers.computation())
.subscribe(i -> result += Thread.currentThread().getName());
Thread.sleep(500);
Assert.assertTrue(result.equals("RxComputationScheduler-1"));
await()
.until(() -> assertTrue(result.equals("RxComputationScheduler-1")));
}
@Test
@ -229,7 +240,7 @@ public class SchedulersTest {
.delay(1, TimeUnit.SECONDS, schedulerA)
.subscribe(i -> result += Thread.currentThread().getName() + i + " ");
Thread.sleep(2000);
Assert.assertTrue(result.equals("Sched1-A Sched1-B "));
await()
.until(() -> assertTrue(result.equals("Sched1-A Sched1-B ")));
}
}

View File

@ -11,6 +11,7 @@ import rx.schedulers.Timestamped;
import java.util.concurrent.TimeUnit;
import static com.jayway.awaitility.Awaitility.await;
import static org.junit.Assert.assertTrue;
public class UtilityOperatorsTest {
@ -40,9 +41,11 @@ public class UtilityOperatorsTest {
+ Thread.currentThread().getName());
});
Thread.sleep(2000);
assertTrue(emittedTotal == 1500);
assertTrue(receivedTotal == 15000);
await().until(() -> {
assertTrue(emittedTotal == 1500);
assertTrue(receivedTotal == 15000);
}
);
}
@Test
@ -63,9 +66,10 @@ public class UtilityOperatorsTest {
+ Thread.currentThread().getName());
});
Thread.sleep(2000);
assertTrue(emittedTotal == 1500);
assertTrue(receivedTotal == 15000);
await().until(() -> {
assertTrue(emittedTotal == 1500);
assertTrue(receivedTotal == 15000);
});
}
@Test
@ -86,9 +90,10 @@ public class UtilityOperatorsTest {
+ Thread.currentThread().getName());
});
Thread.sleep(2000);
assertTrue(emittedTotal == 1500);
assertTrue(receivedTotal == 15000);
await().until(() -> {
assertTrue(emittedTotal == 1500);
assertTrue(receivedTotal == 15000);
});
}
@Test
@ -210,7 +215,7 @@ public class UtilityOperatorsTest {
value -> System.out.println("delay : " + value),
t -> System.out.println("delay error"),
() -> System.out.println("delay completed"));
Thread.sleep(8000);
//Thread.sleep(8000);
}
@Test