Search in sources :

Example 1 with EventSink

use of org.nustaq.kontraktor.reactivestreams.EventSink in project kontraktor by RuedigerMoeller.

the class Java8Streams method remoteJava8EndlessStreams.

// just manual
@Test
// just manual
@org.junit.Ignore
public <T> void remoteJava8EndlessStreams() throws InterruptedException {
    EventSink sink = new EventSink();
    sink.serve(new TCPPublisher().port(8125));
    runTimeClient("c1");
    runTimeClient("c2");
    runTimeClient("c3");
    for (int i = 0; i < 1000; i++) {
        while (!sink.offer(new Date())) {
            Thread.sleep(1);
        }
        Thread.sleep(1000);
    }
}
Also used : TCPPublisher(org.nustaq.kontraktor.remoting.tcp.TCPPublisher) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) Date(java.util.Date) Test(org.junit.Test)

Example 2 with EventSink

use of org.nustaq.kontraktor.reactivestreams.EventSink in project kontraktor by RuedigerMoeller.

the class Basics method testStopThreadsError.

@Test
public void testStopThreadsError() throws InterruptedException {
    resetThreadCount();
    EventSink<Integer> eventSink = new EventSink<Integer>();
    AtomicInteger sum = new AtomicInteger(0);
    AtomicInteger elems = new AtomicInteger(0);
    RateMeasure ms = new RateMeasure("rate");
    eventSink.syncMap(i -> i * i).map(i -> "" + i).syncMap(s -> s.substring(0, s.length() / 2)).map(s -> s.length()).subscribe((len, err) -> {
        ms.count();
        if (len != null) {
            // complete signal
            elems.incrementAndGet();
            sum.addAndGet(len);
        }
    });
    for (int i = 0; i < MASMSG_NUM; i++) {
        while (!eventSink.offer(i)) {
            Thread.yield();
        }
    }
    eventSink.error(new RuntimeException("error"));
    System.out.println("count:" + DispatcherThread.activeDispatchers.get());
    int cnt = 0;
    while (sum.get() != 94803201 && cnt < 10) {
        Thread.sleep(1000);
        cnt++;
    }
    System.out.println("count:" + DispatcherThread.activeDispatchers.get());
    Thread.sleep(1000);
    System.out.println("elems:" + elems.get());
    Thread.sleep(5000);
    Assert.assertTrue(elems.get() == MASMSG_NUM);
    System.out.println("disp:" + DispatcherThread.activeDispatchers.get());
    Assert.assertTrue(DispatcherThread.activeDispatchers.get() == 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) IntStream(java.util.stream.IntStream) TCPServerConnector(org.nustaq.kontraktor.remoting.tcp.TCPServerConnector) ActorPublisher(org.nustaq.kontraktor.remoting.base.ActorPublisher) TCPPublisher(org.nustaq.kontraktor.remoting.tcp.TCPPublisher) DispatcherThread(org.nustaq.kontraktor.impl.DispatcherThread) Test(org.junit.Test) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assert(junit.framework.Assert) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActorServer(org.nustaq.kontraktor.remoting.base.ActorServer) Log(org.nustaq.kontraktor.util.Log) Actors(org.nustaq.kontraktor.Actors) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) Test(org.junit.Test)

Example 3 with EventSink

use of org.nustaq.kontraktor.reactivestreams.EventSink in project kontraktor by RuedigerMoeller.

the class Basics method testStopThreads.

@Test
public void testStopThreads() throws InterruptedException {
    resetThreadCount();
    EventSink<Integer> eventSink = new EventSink<Integer>();
    AtomicInteger sum = new AtomicInteger(0);
    AtomicInteger cnt = new AtomicInteger(0);
    RateMeasure ms = new RateMeasure("rate");
    eventSink.syncMap(i -> i * i).map(i -> "" + i).syncMap(s -> s.substring(0, s.length() / 2)).map(s -> s.length()).subscribe((len, err) -> {
        ms.count();
        if (len != null) {
            // complete signal
            cnt.incrementAndGet();
            sum.addAndGet(len);
        }
    });
    for (int i = 0; i < MASMSG_NUM; i++) {
        while (!eventSink.offer(i)) {
            Thread.yield();
        }
    }
    eventSink.complete();
    int count = 0;
    while (cnt.get() != MASMSG_NUM && count < 10) {
        Thread.sleep(1000);
        count++;
    }
    Thread.sleep(5000);
    System.out.println("threads:" + DispatcherThread.activeDispatchers.get());
    Assert.assertTrue(DispatcherThread.activeDispatchers.get() == 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) IntStream(java.util.stream.IntStream) TCPServerConnector(org.nustaq.kontraktor.remoting.tcp.TCPServerConnector) ActorPublisher(org.nustaq.kontraktor.remoting.base.ActorPublisher) TCPPublisher(org.nustaq.kontraktor.remoting.tcp.TCPPublisher) DispatcherThread(org.nustaq.kontraktor.impl.DispatcherThread) Test(org.junit.Test) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assert(junit.framework.Assert) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActorServer(org.nustaq.kontraktor.remoting.base.ActorServer) Log(org.nustaq.kontraktor.util.Log) Actors(org.nustaq.kontraktor.Actors) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) Test(org.junit.Test)

Example 4 with EventSink

use of org.nustaq.kontraktor.reactivestreams.EventSink in project kontraktor by RuedigerMoeller.

the class TCPNIOKStreamsTest method simpleTest.

@Test
@Ignore
public void simpleTest() {
    AtomicLong counter = new AtomicLong(0);
    AtomicLong received = new AtomicLong(0);
    EventSink<String> stringSink = new EventSink<>();
    stringSink.syncMap(in -> in.length()).subscribe((str, err) -> {
        if (isErrorOrComplete(err)) {
            System.out.println("complete");
        } else if (isError(err)) {
            System.out.println("ERROR");
        } else {
            received.incrementAndGet();
            // simulate slow receiver
            LockSupport.parkNanos(1000 * 1000);
        }
    });
    long l = System.currentTimeMillis();
    long prev = 0;
    while (true) {
        if (stringSink.offer("" + counter.get())) {
            counter.incrementAndGet();
        }
        if (System.currentTimeMillis() - l > 1000) {
            long rate = received.get() - prev;
            prev = received.get();
            System.out.println("sent:" + counter.get() + " received:" + received.get() + " diff:" + (counter.get() - received.get()));
            System.out.println("      " + rate);
            l = System.currentTimeMillis();
        }
    }
}
Also used : ActorPublisher(org.nustaq.kontraktor.remoting.base.ActorPublisher) ActorClientConnector(org.nustaq.kontraktor.remoting.base.ActorClientConnector) org.nustaq.kontraktor.util(org.nustaq.kontraktor.util) Test(org.junit.Test) KxPublisher(org.nustaq.kontraktor.reactivestreams.KxPublisher) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) Callback(org.nustaq.kontraktor.Callback) LockSupport(java.util.concurrent.locks.LockSupport) AtomicLong(java.util.concurrent.atomic.AtomicLong) Ignore(org.junit.Ignore) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) org.nustaq.kontraktor.remoting.tcp(org.nustaq.kontraktor.remoting.tcp) AtomicLong(java.util.concurrent.atomic.AtomicLong) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with EventSink

use of org.nustaq.kontraktor.reactivestreams.EventSink in project kontraktor by RuedigerMoeller.

the class Basics method testConnectionCloseOnCompleteWithSink.

@Test
public void testConnectionCloseOnCompleteWithSink() throws InterruptedException {
    EventSink<Integer> sink = new EventSink<>();
    sink.serve(new TCPPublisher().port(7850));
    AtomicInteger cnt = new AtomicInteger(0);
    KxReactiveStreams.get().connect(Integer.class, new TCPConnectable().host("localhost").port(7850)).subscribe((r, e) -> {
        if (Actors.isResult(e)) {
            cnt.incrementAndGet();
        } else {
            System.out.println("not result " + r + " " + e);
        }
    });
    for (int i = 0; i < 100_000; i++) {
        while (!sink.offer(i)) {
            Thread.yield();
        }
    }
    sink.complete();
    System.out.println("received:" + cnt.get());
    Thread.sleep(1000);
    System.out.println("received:" + cnt.get());
    Assert.assertTrue(cnt.get() == 100_000);
    System.out.println("count:" + DispatcherThread.activeDispatchers.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) TCPPublisher(org.nustaq.kontraktor.remoting.tcp.TCPPublisher) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 EventSink (org.nustaq.kontraktor.reactivestreams.EventSink)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 TCPPublisher (org.nustaq.kontraktor.remoting.tcp.TCPPublisher)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 KxReactiveStreams (org.nustaq.kontraktor.reactivestreams.KxReactiveStreams)3 ActorPublisher (org.nustaq.kontraktor.remoting.base.ActorPublisher)3 ConnectableActor (org.nustaq.kontraktor.remoting.base.ConnectableActor)3 TCPConnectable (org.nustaq.kontraktor.remoting.tcp.TCPConnectable)3 IntStream (java.util.stream.IntStream)2 Assert (junit.framework.Assert)2 Ignore (org.junit.Ignore)2 Actors (org.nustaq.kontraktor.Actors)2 DispatcherThread (org.nustaq.kontraktor.impl.DispatcherThread)2 ActorServer (org.nustaq.kontraktor.remoting.base.ActorServer)2 WebSocketPublisher (org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher)2 TCPNIOPublisher (org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher)2 TCPServerConnector (org.nustaq.kontraktor.remoting.tcp.TCPServerConnector)2 WebSocketConnectable (org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)2 Log (org.nustaq.kontraktor.util.Log)2