Search in sources :

Example 1 with AsyncEvent

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

the class FarcallTest method asyncMethods.

@Test
public void asyncMethods() throws Exception {
    Interconnection ic = new Interconnection(10);
    Farcall a = new Farcall(ic.getConnectorA());
    Farcall b = new Farcall(ic.getConnectorB());
    Object lock = new Object();
    AsyncEvent done = new AsyncEvent();
    a.asyncCommands();
    a.start(command -> {
        switch(command.getName()) {
            case "wait":
                synchronized (lock) {
                    lock.wait();
                }
                return null;
            case "interrupt":
                synchronized (lock) {
                    lock.notifyAll();
                }
                return null;
        }
        return null;
    });
    b.start();
    boolean[] waitDone = new boolean[] { false };
    b.send("wait").done((data) -> {
        waitDone[0] = true;
        done.fire(null);
    });
    b.send("interrupt").waitSuccess();
    done.waitFired();
    assertTrue(waitDone[0]);
}
Also used : JsonObject(com.eclipsesource.json.JsonObject) AsyncEvent(net.sergeych.tools.AsyncEvent) Test(org.junit.Test)

Example 2 with AsyncEvent

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

the class Node2EmulatedNetworkTest method checkRegisterContractOnTemporaryOffedNetwork.

@Test
public void checkRegisterContractOnTemporaryOffedNetwork() throws Exception {
    // switch off half network
    for (int i = 0; i < NODES / 2; i++) {
        ((TestEmulatedNetwork) network).switchOffNodeTestMode(nodes.get(NODES - i - 1));
    }
    AsyncEvent ae = new AsyncEvent();
    Contract contract = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    contract.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
    contract.addSignerKeyFromFile(Config.tuKeyPath);
    contract.seal();
    addDetailsToAllLedgers(contract);
    contract.check();
    contract.traceErrors();
    assertTrue(contract.isOk());
    node.registerItem(contract);
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            System.out.println("-----------nodes state--------------");
            boolean all_is_approved = true;
            for (Node n : nodes) {
                ItemResult r = n.checkItem(contract.getId());
                System.out.println("Node: " + n.toString() + " state: " + r.state);
                if (r.state != ItemState.APPROVED) {
                    all_is_approved = false;
                }
            }
            assertEquals(all_is_approved, false);
        }
    }, 0, 1000);
    // wait and now switch on full network
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        timer.cancel();
        System.out.println("switching on network");
        ((TestEmulatedNetwork) network).switchOnAllNodesTestMode();
    }
    Timer timer2 = new Timer();
    timer2.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            System.out.println("-----------nodes state--------------");
            boolean all_is_approved = true;
            for (Node n : nodes) {
                ItemResult r = n.checkItem(contract.getId());
                System.out.println("Node: " + n.toString() + " state: " + r.state);
                if (r.state != ItemState.APPROVED) {
                    all_is_approved = false;
                }
                if (all_is_approved) {
                    ae.fire();
                }
            }
        }
    }, 0, 1000);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer2.cancel();
    boolean all_is_approved = true;
    for (Node n : nodes) {
        ItemResult r = n.waitItem(contract.getId(), 2000);
        if (r.state != ItemState.APPROVED) {
            all_is_approved = false;
        }
    }
    assertEquals(all_is_approved, true);
}
Also used : AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with AsyncEvent

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

the class Node2LocalNetworkTest method checkRegisterContractOnTemporaryOffedAndHalfOnedNetwork.

@Test
public void checkRegisterContractOnTemporaryOffedAndHalfOnedNetwork() throws Exception {
    AsyncEvent ae = new AsyncEvent();
    Contract contract = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    contract.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
    contract.seal();
    addDetailsToAllLedgers(contract);
    contract.check();
    contract.traceErrors();
    assertTrue(contract.isOk());
    Parcel parcel = registerWithNewParcel(contract);
    // switch off half network
    for (int i = 0; i < NODES / 2; i++) {
        networks_s.get(NODES - i - 1).setUDPAdapterTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
        networks_s.get(NODES - i - 1).setUDPAdapterLostPacketsPercentInTestMode(100);
    }
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            System.out.println("-----------nodes state--------------");
            boolean all_is_approved = true;
            for (Node n : nodesMap_s.values()) {
                ItemResult r = n.checkItem(contract.getId());
                System.out.println("Node: " + n.toString() + " state: " + r.state);
                if (r.state != ItemState.APPROVED) {
                    all_is_approved = false;
                }
            }
            assertEquals(all_is_approved, false);
        }
    }, 0, 1000);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        timer.cancel();
        System.out.println("switching on node 2");
        for (int i = 0; i < NODES / 2; i++) {
            networks_s.get(NODES - i - 1).setUDPAdapterTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
            networks_s.get(NODES - i - 1).setUDPAdapterLostPacketsPercentInTestMode(50);
        }
    }
    Timer timer2 = new Timer();
    timer2.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            System.out.println("-----------nodes state--------------");
            for (Node n : nodesMap_s.values()) {
                ItemResult r = n.checkItem(contract.getId());
                System.out.println("Node: " + n.toString() + " state: " + r.state);
            }
        }
    }, 0, 1000);
    try {
        ae.await(5000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer2.cancel();
    boolean all_is_approved = true;
    for (Node n : nodesMap_s.values()) {
        n.waitParcel(parcel.getId(), 13000);
        ItemResult r = n.waitItem(parcel.getPayloadContract().getId(), 3000);
        System.out.println("Node: " + n.toString() + " state: " + r.state);
        if (r.state != ItemState.APPROVED) {
            all_is_approved = false;
        }
    }
    assertEquals(all_is_approved, true);
    for (TestLocalNetwork ln : networks_s) {
        ln.setUDPAdapterTestMode(DatagramAdapter.TestModes.NONE);
        ln.setUDPAdapterVerboseLevel(DatagramAdapter.VerboseLevel.NOTHING);
    }
}
Also used : Parcel(com.icodici.universa.contract.Parcel) AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with AsyncEvent

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

the class Node2LocalNetworkTest method resyncOther.

@Test
public void resyncOther() throws Exception {
    AsyncEvent ae = new AsyncEvent();
    Contract c = new Contract(TestKeys.privateKey(0));
    c.seal();
    addToAllLedgers(c, ItemState.PENDING_POSITIVE);
    node.getLedger().getRecord(c.getId()).destroy();
    assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state);
    node.resync(c.getId());
    assertEquals(ItemState.PENDING, node.checkItem(c.getId()).state);
    // Start checking nodes
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            boolean all_is_approved = true;
            for (Node n : nodesMap_s.values()) {
                ItemResult r = n.checkItem(c.getId());
                System.out.println(">>>Node: " + n.toString() + " state: " + r.state);
                if (r.state != ItemState.UNDEFINED) {
                    all_is_approved = false;
                }
            }
            if (all_is_approved)
                ae.fire();
        }
    }, 0, 1000);
    try {
        ae.await(6000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer.cancel();
    assertEquals(ItemState.UNDEFINED, node.waitItem(c.getId(), 13000).state);
}
Also used : AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with AsyncEvent

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

the class Node2LocalNetworkTest method shouldNotResyncWithLessKnownContractsEx.

public void shouldNotResyncWithLessKnownContractsEx(ItemState undefinedState, ItemState definedState) throws Exception {
    // Test should broke condition to resync:
    // should be at least one known (APPROVED, DECLINED, LOCKED, REVOKED) subcontract to start resync
    AsyncEvent ae = new AsyncEvent();
    List<Contract> subContracts = new ArrayList<>();
    RunnableWithException<ItemState> addContract = (ItemState state) -> {
        Contract c = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
        c.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
        assertTrue(c.check());
        c.seal();
        addToAllLedgers(c, state);
        subContracts.add(c);
    };
    int wantedSubContracts = 5;
    int knownSubContractsToResync = config.getKnownSubContractsToResync();
    System.out.println("knownSubContractsToResync: " + knownSubContractsToResync);
    int numDefinedSubContracts = Math.min(wantedSubContracts, knownSubContractsToResync - 1);
    System.out.println("add " + numDefinedSubContracts + " defined subcontracts (with state=" + definedState + ")");
    for (int i = 0; i < numDefinedSubContracts; ++i) addContract.run(definedState);
    int numUndefinedSubContracts = Math.max(0, wantedSubContracts - subContracts.size());
    System.out.println("add " + numUndefinedSubContracts + " " + undefinedState + " subcontract");
    for (int i = 0; i < numUndefinedSubContracts; ++i) addContract.run(undefinedState);
    for (int i = 0; i < subContracts.size(); i++) {
        ItemResult r = node.checkItem(subContracts.get(i).getId());
        System.out.println("Contract: " + i + " state: " + r.state);
    }
    Contract contract = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    contract.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
    for (int i = 0; i < subContracts.size(); i++) {
        contract.addRevokingItems(subContracts.get(i));
    }
    contract.seal();
    contract.check();
    contract.traceErrors();
    assertTrue(contract.check());
    Parcel parcel = registerWithNewParcel(contract);
    node.waitParcel(parcel.getId(), 15000);
    ItemResult r = node.waitItem(parcel.getPayloadContract().getId(), 5000);
    System.out.println("Complex contract state: " + r.state);
    assertEquals(ItemState.DECLINED, r.state);
    for (TestLocalNetwork ln : networks_s) {
        ln.setUDPAdapterTestMode(DatagramAdapter.TestModes.NONE);
        ln.setUDPAdapterVerboseLevel(DatagramAdapter.VerboseLevel.NOTHING);
    }
}
Also used : Parcel(com.icodici.universa.contract.Parcel) AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract)

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