SHARE
TWEET

Untitled

a guest Jan 16th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Test
  2.   public void deconstructedPeerBondedObserverTriggered() throws Exception {
  3.     final Map<BytesValue, MockPeerDiscoveryAgent> agents = new HashMap<>();
  4.     final AtomicInteger nextAvailablePort = new AtomicInteger(1);
  5.     final PeerBlacklist peerBlacklist = new PeerBlacklist();
  6.  
  7.     // Start an agent.
  8.     final DiscoveryConfiguration discoveryConfiguration_0 = new DiscoveryConfiguration();
  9.     discoveryConfiguration_0.setBindPort(nextAvailablePort.incrementAndGet());
  10.     discoveryConfiguration_0.setActive(true);
  11.  
  12.     final PeerDiscoveryAgent peerDiscoveryAgent_0 =
  13.             new MockPeerDiscoveryAgent(
  14.                     SECP256K1.KeyPair.generate(),
  15.                     discoveryConfiguration_0,
  16.                     () -> true,
  17.                     peerBlacklist,
  18.                     new NodeWhitelistController(PermissioningConfiguration.createDefault()),
  19.                     agents);
  20.     peerDiscoveryAgent_0.start().get(5, TimeUnit.SECONDS);
  21.     final BytesValue id_0 = peerDiscoveryAgent_0.getAdvertisedPeer().getId();
  22.  
  23.     // Start an agent.
  24.     final DiscoveryConfiguration discoveryConfiguration_1 = new DiscoveryConfiguration();
  25.     discoveryConfiguration_1.setBindPort(nextAvailablePort.incrementAndGet());
  26.     discoveryConfiguration_1.setActive(true);
  27.  
  28.     final PeerDiscoveryAgent peerDiscoveryAgent_1 =
  29.             new MockPeerDiscoveryAgent(
  30.                     SECP256K1.KeyPair.generate(),
  31.                     discoveryConfiguration_1,
  32.                     () -> true,
  33.                     peerBlacklist,
  34.                     new NodeWhitelistController(PermissioningConfiguration.createDefault()),
  35.                     agents);
  36.     peerDiscoveryAgent_1.start().get(5, TimeUnit.SECONDS);
  37.     final BytesValue id_1 = peerDiscoveryAgent_1.getAdvertisedPeer().getId();
  38.  
  39.     // Start an agent.
  40.     final DiscoveryConfiguration discoveryConfiguration_2 = new DiscoveryConfiguration();
  41.     discoveryConfiguration_2.setBindPort(1);
  42.     final PeerDiscoveryAgent peerDiscoveryAgent_2 =
  43.             new MockPeerDiscoveryAgent(
  44.                     SECP256K1.KeyPair.generate(),
  45.                     discoveryConfiguration_2,
  46.                     () -> true,
  47.                     peerBlacklist,
  48.                     new NodeWhitelistController(PermissioningConfiguration.createDefault()),
  49.                     agents);
  50.     peerDiscoveryAgent_2.start().get(5, TimeUnit.SECONDS);
  51.     final BytesValue id_2 = peerDiscoveryAgent_2.getAdvertisedPeer().getId();
  52.  
  53.     // A list of the above peers.
  54.     final List<DiscoveryPeer> peerDiscoveryAgentList_A = Arrays.asList(peerDiscoveryAgent_0.getAdvertisedPeer(), peerDiscoveryAgent_1.getAdvertisedPeer(), peerDiscoveryAgent_2.getAdvertisedPeer());
  55.  
  56.     // Start an agent.
  57.     final DiscoveryConfiguration discoveryConfiguration_3 = new DiscoveryConfiguration();
  58.     discoveryConfiguration_3.setBootstrapPeers(peerDiscoveryAgentList_A);
  59.     discoveryConfiguration_3.setBindPort(nextAvailablePort.incrementAndGet());
  60.     discoveryConfiguration_3.setActive(true);
  61.  
  62.     final PeerDiscoveryAgent peerDiscoveryAgent_3 =
  63.             new MockPeerDiscoveryAgent(
  64.                     SECP256K1.KeyPair.generate(),
  65.                     discoveryConfiguration_3,
  66.                     () -> true,
  67.                     peerBlacklist,
  68.                     new NodeWhitelistController(PermissioningConfiguration.createDefault()),
  69.                     agents);
  70.     peerDiscoveryAgent_3.start().get(5, TimeUnit.SECONDS);
  71.     final BytesValue id_3 = peerDiscoveryAgent_3.getAdvertisedPeer().getId();
  72.  
  73.     // Start an agent.
  74.     final DiscoveryConfiguration discoveryConfiguration_4 = new DiscoveryConfiguration();
  75.     discoveryConfiguration_4.setBootstrapPeers(peerDiscoveryAgentList_A);
  76.     discoveryConfiguration_4.setBindPort(nextAvailablePort.incrementAndGet());
  77.     discoveryConfiguration_4.setActive(true);
  78.  
  79.     final PeerDiscoveryAgent peerDiscoveryAgent_4 =
  80.             new MockPeerDiscoveryAgent(
  81.                     SECP256K1.KeyPair.generate(),
  82.                     discoveryConfiguration_4,
  83.                     () -> true,
  84.                     peerBlacklist,
  85.                     new NodeWhitelistController(PermissioningConfiguration.createDefault()),
  86.                     agents);
  87.     peerDiscoveryAgent_4.start().get(5, TimeUnit.SECONDS);
  88.     final BytesValue id_4 = peerDiscoveryAgent_4.getAdvertisedPeer().getId();
  89.  
  90.     // A list of these two peers.
  91.     final List<DiscoveryPeer> peerDiscoveryAgentList_B = Arrays.asList(peerDiscoveryAgent_3.getAdvertisedPeer(), peerDiscoveryAgent_4.getAdvertisedPeer());
  92.  
  93.     // ...
  94.     final DiscoveryConfiguration discoveryConfiguration_TEST = new DiscoveryConfiguration();
  95.     discoveryConfiguration_TEST.setBootstrapPeers(peerDiscoveryAgentList_B);
  96.     discoveryConfiguration_TEST.setBindPort(nextAvailablePort.incrementAndGet());
  97.     discoveryConfiguration_TEST.setActive(true);
  98.     final PeerDiscoveryAgent peerDiscoveryAgent_TEST =
  99.             new MockPeerDiscoveryAgent(
  100.                     SECP256K1.KeyPair.generate(),
  101.                     discoveryConfiguration_TEST,
  102.                     () -> true,
  103.                     peerBlacklist,
  104.                     new NodeWhitelistController(PermissioningConfiguration.createDefault()),
  105.                     agents);
  106.  
  107.     // A queue for storing peer bonded events.
  108.     final ArrayBlockingQueue<PeerBondedEvent> peerBondedEventQueue = new ArrayBlockingQueue<>(10);
  109.     peerDiscoveryAgent_TEST.observePeerBondedEvents(peerBondedEventQueue::add);
  110.     assertThatCode(() -> peerDiscoveryAgent_TEST.start().get(5, TimeUnit.SECONDS))
  111.             .doesNotThrowAnyException();
  112.  
  113.     // Wait until we've received 5 events.
  114.     try {
  115.       await()
  116.               .atMost(5, TimeUnit.SECONDS)
  117.               .untilAsserted(() -> assertThat(peerBondedEventQueue.size()).isEqualTo(5));
  118.     } catch (final ConditionTimeoutException | AssertionError e) {
  119.       final List<String> events = new ArrayList<>();
  120.       peerBondedEventQueue.forEach(evt -> events.add(evt.toString()));
  121.       LOG.error("Queue:\n" + String.join("\n", events), e);
  122.       throw e;
  123.     }
  124.     // Wait one second and check we've received no more events.
  125.     Thread.sleep(1000);
  126.     assertThat(peerBondedEventQueue.size()).isEqualTo(5);
  127.  
  128.     // Extract all events and perform asserts on them.
  129.     final List<PeerBondedEvent> events = new ArrayList<>(5);
  130.     peerBondedEventQueue.drainTo(events, 5);
  131.  
  132.  
  133.     final List<DiscoveryPeer> peerDiscoveryAgentList = Arrays.asList(peerDiscoveryAgent_0.getAdvertisedPeer(),
  134.             peerDiscoveryAgent_1.getAdvertisedPeer(),
  135.             peerDiscoveryAgent_2.getAdvertisedPeer(),
  136.             peerDiscoveryAgent_3.getAdvertisedPeer(),
  137.             peerDiscoveryAgent_4.getAdvertisedPeer());
  138.  
  139.  
  140.     assertThat(events)
  141.             .extracting(PeerDiscoveryEvent::getPeer)
  142.             .extracting(DiscoveryPeer::getId)
  143.             .containsExactlyInAnyOrderElementsOf(
  144.                     peerDiscoveryAgentList.stream().map(DiscoveryPeer::getId).collect(Collectors.toList()));
  145.     assertThat(events).extracting(PeerDiscoveryEvent::getTimestamp).isSorted();
  146.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top