Search in sources :

Example 26 with AsyncEvent

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

the class Node2LocalNetworkTest method resyncApproved.

@Test
public void resyncApproved() throws Exception {
    AsyncEvent ae = new AsyncEvent();
    Contract c = new Contract(TestKeys.privateKey(0));
    c.seal();
    addToAllLedgers(c, ItemState.APPROVED);
    node.getLedger().getRecord(c.getId()).destroy();
    assertEquals(ItemState.UNDEFINED, 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()) {
                // System.out.println(n.getLedger().getRecord(c.getId()));
                ItemResult r = n.checkItem(c.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);
    node.resync(c.getId());
    try {
        ae.await(3000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer.cancel();
    assertEquals(ItemState.APPROVED, node.waitItem(c.getId(), 13000).state);
}
Also used : AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

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