PeerModel { public synchronized void checkListForExpiredPeers() { final Timestamp timestampNow = new Timestamp(new Date().getTime()); Iterator iterator = mDiscoveredPeers.entrySet().iterator(); CopyOnWriteArrayList<PeerProperties> expiredPeers = new CopyOnWriteArrayList<>(); while (iterator.hasNext()) { HashMap.Entry entry = (HashMap.Entry)iterator.next(); PeerProperties entryPeerProperties = (PeerProperties) entry.getKey(); Timestamp entryTimestamp = (Timestamp) entry.getValue(); if (timestampNow.getTime() - entryTimestamp.getTime() > mSettings.getPeerExpiration()) { Log.d(TAG, "checkListForExpiredPeers: Peer " + entryPeerProperties.toString() + " expired"); expiredPeers.add(entryPeerProperties); } } if (expiredPeers.size() > 0) { for (PeerProperties expiredPeer : expiredPeers) { removePeer(expiredPeer); } for (PeerProperties expiredPeer : expiredPeers) { for (Listener listener : mListeners) { listener.onPeerExpiredAndRemoved(expiredPeer); } } expiredPeers.clear(); } } PeerModel(Listener listener, DiscoveryManagerSettings settings); void addListener(Listener listener); void removeListener(Listener listener); void clear(); void onPeerExpirationTimeChanged(); synchronized PeerProperties getDiscoveredPeerByBluetoothMacAddress(final String bluetoothMacAddress); synchronized PeerProperties getDiscoveredPeerByDeviceAddress(final String deviceAddress); synchronized PeerProperties removePeer(PeerProperties peerPropertiesToRemove); void addOrUpdateDiscoveredPeer(PeerProperties peerPropertiesToAddOrUpdate); synchronized void checkListForExpiredPeers();  }
@Test public void testCheckListForExpiredPeers() throws Exception { HashMap<PeerProperties, Timestamp> mDiscoveredPeers = new HashMap<>(); long hourBefore = new Date().getTime() - 3600 * 1000; long dayBefore = new Date().getTime() - 24 * 3600 * 1000; long halfDay = 12 * 3600 * 1000; when(mMockDiscoveryManagerSettings.getPeerExpiration()).thenReturn(halfDay); mDiscoveredPeers.put(mMockPeerProperties, new Timestamp(hourBefore)); mDiscoveredPeers.put(mMockPeerProperties2, new Timestamp(dayBefore)); Field mDiscoveredPeersField = mPeerModel.getClass().getDeclaredField("mDiscoveredPeers"); mDiscoveredPeersField.setAccessible(true); mDiscoveredPeersField.set(mPeerModel, mDiscoveredPeers); mPeerModel.checkListForExpiredPeers(); @SuppressWarnings("unchecked") HashMap<PeerProperties, Timestamp> peers = (HashMap<PeerProperties, Timestamp>) mDiscoveredPeersField.get(mPeerModel); assertThat("The expired peer is removed", peers.size(), is(1)); verify(mMockListener, times(1)).onPeerExpiredAndRemoved(isA(PeerProperties.class)); }