### Question:
TrailingPeerLimiter implements BlockAddedObserver { public void enforceTrailingPeerLimit() { final TrailingPeerRequirements requirements = trailingPeerRequirementsCalculator.get(); if (requirements.getMaxTrailingPeers() == Long.MAX_VALUE) { return; } final long minimumHeightToBeUpToDate = requirements.getMinimumHeightToBeUpToDate(); final long maxTrailingPeers = requirements.getMaxTrailingPeers(); final List<EthPeer> trailingPeers = ethPeers .streamAvailablePeers() .filter(peer -> peer.chainState().hasEstimatedHeight()) .filter(peer -> peer.chainState().getEstimatedHeight() < minimumHeightToBeUpToDate) .sorted(BY_CHAIN_HEIGHT) .collect(Collectors.toList()); while (!trailingPeers.isEmpty() && trailingPeers.size() > maxTrailingPeers) { final EthPeer peerToDisconnect = trailingPeers.remove(0); LOG.debug("Enforcing trailing peers limit by disconnecting {}", peerToDisconnect); peerToDisconnect.disconnect(DisconnectReason.TOO_MANY_PEERS); } } TrailingPeerLimiter(
      final EthPeers ethPeers,
      final Supplier<TrailingPeerRequirements> trailingPeerRequirementsCalculator); void enforceTrailingPeerLimit(); @Override void onBlockAdded(final BlockAddedEvent event);  }### Answer:
@Test public void shouldDisconnectFurthestBehindPeerWhenTrailingPeerLimitExceeded() { final EthPeer ethPeer1 = addPeerWithEstimatedHeight(1); addPeerWithEstimatedHeight(3); addPeerWithEstimatedHeight(2); trailingPeerLimiter.enforceTrailingPeerLimit(); assertDisconnections(ethPeer1); }
@Test public void shouldDisconnectMultiplePeersWhenTrailingPeerLimitExceeded() { final EthPeer ethPeer1 = addPeerWithEstimatedHeight(1); final EthPeer ethPeer2 = addPeerWithEstimatedHeight(2); addPeerWithEstimatedHeight(3); addPeerWithEstimatedHeight(4); trailingPeerLimiter.enforceTrailingPeerLimit(); assertDisconnections(ethPeer1, ethPeer2); }
@Test public void shouldNotDisconnectPeersWhenLimitNotReached() { addPeerWithEstimatedHeight(1); addPeerWithEstimatedHeight(2); trailingPeerLimiter.enforceTrailingPeerLimit(); assertDisconnections(); }
@Test public void shouldNotDisconnectPeersWithinToleranceOfChainHead() { addPeerWithEstimatedHeight(CHAIN_HEAD); addPeerWithEstimatedHeight(CHAIN_HEAD); addPeerWithEstimatedHeight(CHAIN_HEAD); addPeerWithEstimatedHeight(CHAIN_HEAD - TRAILING_PEER_BLOCKS_BEHIND_THRESHOLD); addPeerWithEstimatedHeight(CHAIN_HEAD - TRAILING_PEER_BLOCKS_BEHIND_THRESHOLD); trailingPeerLimiter.enforceTrailingPeerLimit(); assertDisconnections(); }