Search in sources :

Example 1 with TCPObjectSocket

use of org.nustaq.net.TCPObjectSocket in project kontraktor by RuedigerMoeller.

the class AsyncServerSocketTest method serial.

@Test
public void serial() throws Exception {
    TA ta = Actors.AsActor(TA.class);
    ta.serve2().await();
    ExecutorService executorService = Executors.newCachedThreadPool();
    HashMap testMap = new HashMap();
    int MSG_COUNT = 10_000_000;
    int NUM_CLIENTS = 100;
    testMap.put("pok", 13);
    testMap.put("yes", "no");
    testMap.put("true", "maybe");
    for (int ii = 0; ii < NUM_CLIENTS; ii++) {
        final int finalIi = ii;
        executorService.execute(() -> {
            TCPObjectSocket sock = null;
            try {
                sock = new TCPObjectSocket("localhost", 8080);
                for (int i = 0; i < MSG_COUNT / NUM_CLIENTS; i++) {
                    sock.writeObject(testMap);
                    sock.flush();
                }
                sock.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    Integer count = ta.getReceiveCount().await();
    System.out.println("COUNT " + count);
    int timer = 0;
    while (count != MSG_COUNT && timer < 60) {
        timer++;
        System.out.println("waiting .. " + count);
        Thread.sleep(1000);
        count = ta.getReceiveCount().await();
    }
    executorService.shutdown();
    executorService.awaitTermination(10, TimeUnit.DAYS);
    Assert.assertTrue(count == MSG_COUNT);
    ta.stop();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) ExecutorService(java.util.concurrent.ExecutorService) TCPObjectSocket(org.nustaq.net.TCPObjectSocket) Test(org.junit.Test)

Example 2 with TCPObjectSocket

use of org.nustaq.net.TCPObjectSocket in project kontraktor by RuedigerMoeller.

the class AsyncServerSocketTest method serialSync.

@Test
public void serialSync() throws Exception {
    AtomicInteger COUNT = new AtomicInteger(0);
    TCPObjectServer tcpObjectServer = new TCPObjectServer(8082);
    RateMeasure measure = new RateMeasure("count");
    tcpObjectServer.start((client) -> {
        try {
            while (true) {
                Object request = client.readObject();
                if (request == null)
                    // connection closed
                    return;
                COUNT.incrementAndGet();
                measure.count();
            }
        } catch (EOFException eof) {
        // e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    Thread.sleep(1000);
    HashMap testMap = new HashMap();
    testMap.put("pok", 13);
    testMap.put("yes", "no");
    testMap.put("true", "maybe");
    ExecutorService executorService = Executors.newCachedThreadPool();
    int MSG_COUNT = 10_000_000;
    int NUM_CLIENTS = 100;
    for (int ii = 0; ii < NUM_CLIENTS; ii++) {
        final int finalIi = ii;
        executorService.execute(() -> {
            TCPObjectSocket sock = null;
            try {
                sock = new TCPObjectSocket("localhost", 8082);
                // System.out.println("start "+finalIi);
                for (int i = 0; i < MSG_COUNT / NUM_CLIENTS; i++) {
                    sock.writeObject(testMap);
                    sock.flush();
                }
                sock.close();
            // System.out.println("finished "+ finalIi);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    Thread.sleep(3000);
    executorService.shutdown();
    executorService.awaitTermination(10, TimeUnit.DAYS);
    int to = 0;
    while (to < 7 && COUNT.get() < MSG_COUNT) {
        Thread.sleep(1000);
        System.out.println("COUNT " + COUNT.get());
        to++;
    }
    Assert.assertTrue(COUNT.get() == MSG_COUNT);
}
Also used : TCPObjectServer(org.nustaq.net.TCPObjectServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) ExecutorService(java.util.concurrent.ExecutorService) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) TCPObjectSocket(org.nustaq.net.TCPObjectSocket) Test(org.junit.Test)

Example 3 with TCPObjectSocket

use of org.nustaq.net.TCPObjectSocket in project kontraktor by RuedigerMoeller.

the class AsyncServerSocketTest method writeTest.

@Test
public void writeTest() throws IOException, InterruptedException {
    File f = new File("/tmp/data.test");
    int NUMINT = 10_000_000;
    if (!f.exists()) {
        FileOutputStream fout = new FileOutputStream(f);
        DataOutputStream dout = new DataOutputStream(fout);
        for (int i = 0; i < NUMINT; i++) {
            dout.writeInt(i);
        }
        fout.close();
    }
    System.out.println("file len " + f.length());
    TA ta = Actors.AsActor(TA.class);
    ta.serve3(8081).await();
    RateMeasure intFreq = new RateMeasure("int per sec");
    TCPObjectSocket sock = null;
    try {
        sock = new TCPObjectSocket("localhost", 8081);
        sock.writeObject(f);
        sock.flush();
        byte[] res = new byte[1024];
        int count = 0;
        int read;
        InputStream in = sock.getIn();
        DataInputStream din = new DataInputStream(in);
        while ((read = din.readInt()) >= 0) {
            Assert.assertTrue(read == count);
            count++;
            if (count == NUMINT)
                break;
            // if ( count % 10000 == 0 )
            // System.out.println("read int "+count);
            intFreq.count();
        }
        in.close();
        System.out.println("received " + count + " KB");
    } catch (Exception e) {
        e.printStackTrace();
    }
    ta.stop();
    Thread.sleep(1000);
}
Also used : RateMeasure(org.nustaq.kontraktor.util.RateMeasure) TCPObjectSocket(org.nustaq.net.TCPObjectSocket) AsyncFile(org.nustaq.kontraktor.asyncio.AsyncFile) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 TCPObjectSocket (org.nustaq.net.TCPObjectSocket)3 HashMap (java.util.HashMap)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RateMeasure (org.nustaq.kontraktor.util.RateMeasure)2 AsyncFile (org.nustaq.kontraktor.asyncio.AsyncFile)1 TCPObjectServer (org.nustaq.net.TCPObjectServer)1