
com.darylteo.rx.promises.test.PromiseRxJavaTests Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava-promises Show documentation
Show all versions of rxjava-promises Show documentation
Promises implementation for RxJava
The newest version!
package com.darylteo.rx.promises.test;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.CountDownLatch;
import org.junit.Test;
import rx.Observable;
import rx.util.functions.Action1;
import rx.util.functions.Func1;
import com.darylteo.rx.promises.Promise;
public class PromiseRxJavaTests {
@Test
public void testPromiseToRxSubscribe() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Result result = new Result<>();
makePromise("Hello World")
.subscribe(new Action1() {
@Override
public void call(String value) {
result.value = value;
latch.countDown();
}
});
latch.await();
assertEquals("Hello World", result.value);
}
@Test
public void testPromiseToRxMap() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Result result = new Result<>();
makePromise("Hello World")
.map(new Func1() {
@Override
public String call(String value) {
return value.toUpperCase();
}
})
.subscribe(new Action1() {
@Override
public void call(String value) {
result.value = value;
latch.countDown();
}
});
latch.await();
assertEquals("HELLO WORLD", result.value);
}
@Test
public void testRxToPromise() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Result result = new Result<>();
Observable obs = Observable.from("Hello", "World");
Promise p = Promise.defer();
p.then(new Action1() {
@Override
public void call(String value) {
result.value = value;
latch.countDown();
}
});
obs.subscribe(p);
latch.await();
assertEquals("Hello", result.value);
}
public Promise makePromise(final String value) {
final Promise promise = Promise.defer();
new Thread() {
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
promise.fulfill(value);
}
}
}.start();
return promise;
}
private class Result {
T value;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy