Search in sources :

Example 1 with XMLEvent

use of com.dexels.navajo.document.stream.xml.XMLEvent in project navajo by Dexels.

the class TestRx method testBackpressure.

@Test
public void testBackpressure() {
    final int DELAY = 1;
    final int REQUESTCOUNT = 1;
    final Thread me = Thread.currentThread();
    final long started = System.currentTimeMillis();
    AtomicLong count = new AtomicLong();
    Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("tml.xml"), 1).lift(XML.parseFlowable(1)).flatMap(e -> e).subscribe(new Subscriber<XMLEvent>() {

        Disposable timer;

        @Override
        public void onComplete() {
            long complete = System.currentTimeMillis();
            System.err.println("Total run took: " + (complete - started) + " for " + count.get() + " elements. Should be little more than: " + (DELAY * count.get() / REQUESTCOUNT));
            timer.dispose();
            me.interrupt();
        }

        @Override
        public void onError(Throwable e) {
            timer.dispose();
            e.printStackTrace();
            me.interrupt();
        }

        @Override
        public void onSubscribe(Subscription s) {
            s.request(1);
            timer = Observable.interval(DELAY, TimeUnit.MILLISECONDS).subscribe(l -> s.request(REQUESTCOUNT));
        }

        @Override
        public void onNext(XMLEvent x) {
            count.incrementAndGet();
        }
    });
    try {
        Thread.sleep(500000);
    } catch (InterruptedException e1) {
    }
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamCompress(com.dexels.navajo.document.stream.StreamCompress) BufferedOutputStream(java.io.BufferedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XML(com.dexels.navajo.document.stream.xml.XML) Observable(io.reactivex.Observable) Subscriber(org.reactivestreams.Subscriber) OutputStream(java.io.OutputStream) XMLEvent(com.dexels.navajo.document.stream.xml.XMLEvent) IOException(java.io.IOException) Test(org.junit.Test) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Ignore(org.junit.Ignore) Subscription(org.reactivestreams.Subscription) StreamDocument(com.dexels.navajo.document.stream.StreamDocument) Assert(org.junit.Assert) Bytes(com.github.davidmoten.rx2.Bytes) InputStream(java.io.InputStream) Disposable(io.reactivex.disposables.Disposable) AtomicLong(java.util.concurrent.atomic.AtomicLong) XMLEvent(com.dexels.navajo.document.stream.xml.XMLEvent) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Aggregations

StreamCompress (com.dexels.navajo.document.stream.StreamCompress)1 StreamDocument (com.dexels.navajo.document.stream.StreamDocument)1 XML (com.dexels.navajo.document.stream.xml.XML)1 XMLEvent (com.dexels.navajo.document.stream.xml.XMLEvent)1 Bytes (com.github.davidmoten.rx2.Bytes)1 Flowable (io.reactivex.Flowable)1 Observable (io.reactivex.Observable)1 Disposable (io.reactivex.disposables.Disposable)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Arrays (java.util.Arrays)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1