Search in sources :

Example 11 with AsyncEvent

use of net.sergeych.tools.AsyncEvent in project universa by UniversaBlockchain.

the class DatagramAdapterTest method createManyNodesToOne.

@Test
public void createManyNodesToOne() throws Exception {
    int numNodes = 100;
    NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 10, "test_node_10", "localhost", 16201, 16202, 16301);
    NetConfig nc = new NetConfig(asList(node1));
    SymmetricKey symmetricKey1 = new SymmetricKey();
    // create implemented class with node1
    DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), symmetricKey1, node1, nc);
    List symmetricKeyErrors = new ArrayList();
    d1.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    byte[] payload = "test data set 1".getBytes();
    int attempts = 5;
    int numSends = 5;
    ArrayList<byte[]> receviedFor = new ArrayList<>();
    AsyncEvent<Void> ae = new AsyncEvent<>();
    d1.receive(data -> {
        receviedFor.add(data);
        if ((receviedFor.size()) == attempts * numSends * numNodes)
            ae.fire();
    });
    List<NodeInfo> nodes = new ArrayList<>();
    List<DatagramAdapter> adapters = new ArrayList<>();
    for (int i = 0; i < numNodes; i++) {
        int keyIndex = new Random().nextInt(2);
        NodeInfo n = new NodeInfo(TestKeys.publicKey(keyIndex), 2 + i, "test_node_2" + i, "localhost", 16203 + i, 16204 + i, 16302 + i);
        nc.addNode(n);
        nodes.add(n);
        SymmetricKey sk = new SymmetricKey();
        // create implemented class with node1
        DatagramAdapter d = new UDPAdapter(TestKeys.privateKey(keyIndex), sk, n, nc);
        adapters.add(d);
        d.addErrorsCallback(m -> {
            System.err.println(m);
            if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
                symmetricKeyErrors.add(m);
            return m;
        });
        d.receive(data -> {
            receviedFor.add(data);
            if ((receviedFor.size()) == attempts * numSends * numNodes)
                ae.fire();
        });
    }
    for (int i = 0; i < attempts; i++) {
        System.out.println("Send part: " + i);
        // send from adapter d1, to d2 as it is connected with node2 credentials:
        for (int j = 0; j < numSends; j++) {
            for (int k = 0; k < numNodes; k++) {
                adapters.get(k).send(node1, payload);
            }
        }
        Thread.sleep(new Random().nextInt(200));
    // if(new Random().nextBoolean()) ((UDPAdapter)d1).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d2).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d3).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d4).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d5).brakeSessions();
    }
    try {
        ae.await(30000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    assertEquals(0, symmetricKeyErrors.size());
    System.out.println("all got: " + (receviedFor.size()));
    d1.shutdown();
    for (int i = 0; i < numNodes; i++) {
        adapters.get(i).shutdown();
    }
    assertEquals(attempts * numSends * numNodes, receviedFor.size());
}
Also used : ArrayList(java.util.ArrayList) SymmetricKey(com.icodici.crypto.SymmetricKey) AsyncEvent(net.sergeych.tools.AsyncEvent) Random(java.util.Random) NodeInfo(com.icodici.universa.node2.NodeInfo) NetConfig(com.icodici.universa.node2.NetConfig) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 12 with AsyncEvent

use of net.sergeych.tools.AsyncEvent in project universa by UniversaBlockchain.

the class DatagramAdapterTest method create5NodesSend10Times.

public void create5NodesSend10Times() throws Exception {
    NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 10, "test_node_10", "localhost", 16201, 16202, 16301);
    NodeInfo node2 = new NodeInfo(TestKeys.publicKey(1), 11, "test_node_11", "localhost", 16203, 16204, 16302);
    NodeInfo node3 = new NodeInfo(TestKeys.publicKey(2), 12, "test_node_12", "localhost", 16204, 16205, 16303);
    NodeInfo node4 = new NodeInfo(TestKeys.publicKey(0), 13, "test_node_13", "localhost", 16205, 16206, 16304);
    NodeInfo node5 = new NodeInfo(TestKeys.publicKey(1), 14, "test_node_14", "localhost", 16206, 16207, 16305);
    List<NodeInfo> nodes = new ArrayList<>();
    nodes.add(node1);
    nodes.add(node2);
    nodes.add(node3);
    nodes.add(node4);
    nodes.add(node5);
    NetConfig nc = new NetConfig(nodes);
    SymmetricKey symmetricKey1 = new SymmetricKey();
    SymmetricKey symmetricKey2 = new SymmetricKey();
    SymmetricKey symmetricKey3 = new SymmetricKey();
    SymmetricKey symmetricKey4 = new SymmetricKey();
    SymmetricKey symmetricKey5 = new SymmetricKey();
    // create implemented class with node1
    DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), symmetricKey1, node1, nc);
    // create implemented class with node1
    DatagramAdapter d2 = new UDPAdapter(TestKeys.privateKey(1), symmetricKey2, node2, nc);
    // create implemented class with node1
    DatagramAdapter d3 = new UDPAdapter(TestKeys.privateKey(2), symmetricKey3, node3, nc);
    // create implemented class with node1
    DatagramAdapter d4 = new UDPAdapter(TestKeys.privateKey(0), symmetricKey4, node4, nc);
    // create implemented class with node1
    DatagramAdapter d5 = new UDPAdapter(TestKeys.privateKey(1), symmetricKey5, node5, nc);
    // d1.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d2.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d3.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d4.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d5.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
    // d1.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
    // d2.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
    // d3.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
    // d4.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
    // d5.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
    // 
    // d1.setLostPacketsPercentInTestMode(75);
    // d2.setLostPacketsPercentInTestMode(75);
    // d3.setLostPacketsPercentInTestMode(75);
    // d4.setLostPacketsPercentInTestMode(75);
    // d5.setLostPacketsPercentInTestMode(75);
    List symmetricKeyErrors = new ArrayList();
    d1.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    d2.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    d3.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    d4.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    d5.addErrorsCallback(m -> {
        System.err.println(m);
        if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
            symmetricKeyErrors.add(m);
        return m;
    });
    byte[] payload1 = "test data set 1".getBytes();
    byte[] payload2 = "test data set 2".getBytes();
    byte[] payload3 = "test data set 3".getBytes();
    int attempts = 100;
    int numSends = 10;
    ArrayList<byte[]> receviedFor1 = new ArrayList<>();
    ArrayList<byte[]> receviedFor2 = new ArrayList<>();
    ArrayList<byte[]> receviedFor3 = new ArrayList<>();
    ArrayList<byte[]> receviedFor4 = new ArrayList<>();
    ArrayList<byte[]> receviedFor5 = new ArrayList<>();
    AsyncEvent<Void> ae = new AsyncEvent<>();
    d1.receive(d -> {
        receviedFor1.add(d);
        if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
            ae.fire();
    });
    d2.receive(d -> {
        receviedFor2.add(d);
        if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
            ae.fire();
    });
    d3.receive(d -> {
        receviedFor3.add(d);
        if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
            ae.fire();
    });
    d4.receive(d -> {
        receviedFor4.add(d);
        if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
            ae.fire();
    });
    d5.receive(d -> {
        receviedFor5.add(d);
        if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
            ae.fire();
    });
    for (int i = 0; i < attempts; i++) {
        System.out.println("Send part: " + i);
        // send from adapter d1, to d2 as it is connected with node2 credentials:
        for (int j = 0; j < numSends; j++) {
            int rnd1 = new Random().nextInt(3);
            int rnd2 = 0;
            int rnd3 = 0;
            while (rnd2 == rnd3) {
                rnd2 = new Random().nextInt(5);
                rnd3 = new Random().nextInt(5);
            }
            byte[] payload;
            DatagramAdapter sender;
            NodeInfo receiverNode;
            if (rnd1 == 0)
                payload = payload1;
            else if (rnd1 == 1)
                payload = payload2;
            else
                payload = payload3;
            if (rnd2 == 0)
                sender = d1;
            else if (rnd2 == 1)
                sender = d2;
            else if (rnd2 == 2)
                sender = d3;
            else if (rnd2 == 3)
                sender = d4;
            else
                sender = d5;
            if (rnd3 == 0)
                receiverNode = node1;
            else if (rnd3 == 1)
                receiverNode = node2;
            else if (rnd3 == 2)
                receiverNode = node3;
            else if (rnd3 == 3)
                receiverNode = node4;
            else
                receiverNode = node5;
            sender.send(receiverNode, payload);
        }
        Thread.sleep(new Random().nextInt(200));
    // if(new Random().nextBoolean()) ((UDPAdapter)d1).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d2).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d3).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d4).brakeSessions();
    // if(new Random().nextBoolean()) ((UDPAdapter)d5).brakeSessions();
    }
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    System.out.println("receviedFor1 got: " + (receviedFor1.size()));
    System.out.println("receviedFor2 got: " + (receviedFor2.size()));
    System.out.println("receviedFor3 got: " + (receviedFor3.size()));
    System.out.println("receviedFor4 got: " + (receviedFor4.size()));
    System.out.println("receviedFor5 got: " + (receviedFor5.size()));
    System.out.println("all got: " + (receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()));
    // assertEquals(numSends * attempts, receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size());
    assertEquals(0, symmetricKeyErrors.size());
    d1.shutdown();
    d2.shutdown();
    d3.shutdown();
    d4.shutdown();
    d5.shutdown();
}
Also used : ArrayList(java.util.ArrayList) SymmetricKey(com.icodici.crypto.SymmetricKey) AsyncEvent(net.sergeych.tools.AsyncEvent) Random(java.util.Random) NodeInfo(com.icodici.universa.node2.NodeInfo) NetConfig(com.icodici.universa.node2.NetConfig) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) TimeoutException(java.util.concurrent.TimeoutException)

Example 13 with AsyncEvent

use of net.sergeych.tools.AsyncEvent in project universa by UniversaBlockchain.

the class RateCounterTest method timedCountsOneRoundPass.

@Test
public void timedCountsOneRoundPass() throws Exception {
    int limit = 5;
    int seconds = 1;
    RateCounter rc = new RateCounter(limit, Duration.ofSeconds(seconds));
    Timer timer = new Timer();
    AsyncEvent<Void> ae = new AsyncEvent<>();
    assertEquals(limit, rc.pulsesLeft());
    assertEquals(limit, rc.getPulseLimit());
    assertEquals(seconds, rc.getDuration().getSeconds());
    System.out.println("timedCountsOneRoundPass started");
    timer.scheduleAtFixedRate(new TimerTask() {

        private int counts = 0;

        private int maxCounts = 5;

        @Override
        public void run() {
            assertEquals(true, rc.countPulse());
            counts++;
            System.out.println("counts: " + counts);
            if (counts >= maxCounts) {
                cancel();
                ae.fire();
            }
        }
    }, 0, 100);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    assertEquals(0, rc.pulsesLeft());
    // 6
    assertEquals(false, rc.countPulse());
    assertEquals(-1, rc.pulsesLeft());
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) AsyncEvent(net.sergeych.tools.AsyncEvent) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 14 with AsyncEvent

use of net.sergeych.tools.AsyncEvent in project universa by UniversaBlockchain.

the class RateCounterTest method timedCountsOneRoundNotPass.

@Test
public void timedCountsOneRoundNotPass() throws Exception {
    int limit = 5;
    int seconds = 1;
    RateCounter rc = new RateCounter(limit, Duration.ofSeconds(seconds));
    Timer timer = new Timer();
    AsyncEvent<Void> ae = new AsyncEvent<>();
    assertEquals(limit, rc.pulsesLeft());
    assertEquals(limit, rc.getPulseLimit());
    assertEquals(seconds, rc.getDuration().getSeconds());
    System.out.println("timedCountsOneRoundNotPass started");
    timer.scheduleAtFixedRate(new TimerTask() {

        private int counts = 0;

        private int maxCounts = 8;

        @Override
        public void run() {
            if (counts < limit)
                assertEquals(true, rc.countPulse());
            else
                assertEquals(false, rc.countPulse());
            counts++;
            System.out.println("counts: " + counts);
            if (counts >= maxCounts) {
                cancel();
                ae.fire();
            }
        }
    }, 0, 100);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    assertEquals(-3, rc.pulsesLeft());
    assertEquals(false, rc.countPulse());
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) AsyncEvent(net.sergeych.tools.AsyncEvent) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 15 with AsyncEvent

use of net.sergeych.tools.AsyncEvent in project universa by UniversaBlockchain.

the class RateCounterTest method timedCountsTwoRoundsPass.

@Test
public void timedCountsTwoRoundsPass() throws Exception {
    int limit = 5;
    int seconds = 1;
    RateCounter rc = new RateCounter(limit, Duration.ofSeconds(seconds));
    Timer timer = new Timer();
    AsyncEvent<Void> ae = new AsyncEvent<>();
    assertEquals(limit, rc.pulsesLeft());
    assertEquals(limit, rc.getPulseLimit());
    assertEquals(seconds, rc.getDuration().getSeconds());
    System.out.println("timedCountsTwoRoundsPass started");
    timer.scheduleAtFixedRate(new TimerTask() {

        private int counts = 0;

        private int maxCounts = 8;

        @Override
        public void run() {
            assertEquals(true, rc.countPulse());
            counts++;
            System.out.println("counts: " + counts);
            if (counts >= maxCounts) {
                cancel();
                ae.fire();
            }
        }
    }, 0, 300);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    assertEquals(1, rc.pulsesLeft());
    assertEquals(true, rc.countPulse());
    assertEquals(false, rc.countPulse());
    assertEquals(-1, rc.pulsesLeft());
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) AsyncEvent(net.sergeych.tools.AsyncEvent) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

AsyncEvent (net.sergeych.tools.AsyncEvent)26 TimeoutException (java.util.concurrent.TimeoutException)22 Contract (com.icodici.universa.contract.Contract)17 Test (org.junit.Test)8 Parcel (com.icodici.universa.contract.Parcel)6 SymmetricKey (com.icodici.crypto.SymmetricKey)5 NetConfig (com.icodici.universa.node2.NetConfig)5 NodeInfo (com.icodici.universa.node2.NodeInfo)5 ArrayList (java.util.ArrayList)5 Arrays.asList (java.util.Arrays.asList)4 List (java.util.List)4 Random (java.util.Random)4 Timer (java.util.Timer)3 TimerTask (java.util.TimerTask)3 PrivateKey (com.icodici.crypto.PrivateKey)2 Duration (java.time.Duration)2 JsonObject (com.eclipsesource.json.JsonObject)1 EncryptionError (com.icodici.crypto.EncryptionError)1 PublicKey (com.icodici.crypto.PublicKey)1 Approvable (com.icodici.universa.Approvable)1