From 68e669d6560074a1bdebe0162bce618b0817fd25 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 22 Jul 2018 10:56:00 +0100 Subject: [PATCH] hostcache tests and fixes --- .../muwire/core/hostcache/HostCache.groovy | 2 +- .../com/muwire/core/trust/TrustService.groovy | 2 + .../core/hostcache/HostCacheTest.groovy | 148 ++++++++++++++++++ 3 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy diff --git a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy index 2c3cbfa1..45d72cd1 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy @@ -66,7 +66,7 @@ class HostCache { List getHosts(int n) { List rv = new ArrayList<>(hosts.keySet()) - rv.retainAll {allowHost(it)} + rv.retainAll {allowHost(hosts[it])} if (rv.size() <= n) return rv Collections.shuffle(rv) diff --git a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy index 0e34a21e..9053148d 100644 --- a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy +++ b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy @@ -13,6 +13,8 @@ class TrustService { final Timer timer + TrustService() {} + TrustService(File persistGood, File persistBad, long persistInterval) { this.persistBad = persistBad this.persistGood = persistGood diff --git a/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy b/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy new file mode 100644 index 00000000..28b2ac4f --- /dev/null +++ b/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy @@ -0,0 +1,148 @@ +package com.muwire.core.hostcache + +import org.junit.After +import org.junit.Before +import org.junit.Test + +import com.muwire.core.Destinations +import com.muwire.core.MuWireSettings +import com.muwire.core.connection.ConnectionAttemptStatus +import com.muwire.core.connection.ConnectionEvent +import com.muwire.core.trust.TrustLevel +import com.muwire.core.trust.TrustService + +import groovy.mock.interceptor.MockFor + +class HostCacheTest { + + + File persist + HostCache cache + + def trustMock + TrustService trust + + def settingsMock + MuWireSettings settings + + Destinations destinations = new Destinations() + + @Before + void before() { + persist = new File("hostPersist") + persist.delete() + persist.deleteOnExit() + + trustMock = new MockFor(TrustService.class) + settingsMock = new MockFor(MuWireSettings.class) + } + + @After + void after() { + cache?.stop() + trustMock.verify trust + settingsMock.verify settings + } + + private void initMocks() { + trust = trustMock.proxyInstance() + settings = settingsMock.proxyInstance() + cache = new HostCache(trust, persist, 100, settings) + cache.start() + Thread.sleep(150) + } + + @Test + void testEmpty() { + initMocks() + assert cache.getHosts(5).size() == 0 + } + + @Test + void testOnDiscoveredEvent() { + trustMock.demand.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.NEUTRAL + } + trustMock.demand.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.NEUTRAL + } + settingsMock.demand.allowUntrusted { true } + settingsMock.demand.allowUntrusted { true } + + initMocks() + + cache.onHostDiscoveredEvent(new HostDiscoveredEvent(destination: destinations.dest1)) + + def rv = cache.getHosts(5) + assert rv.size() == 1 + assert rv.contains(destinations.dest1) + } + + @Test + void testOnDiscoveredUntrustedHost() { + trustMock.demand.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.DISTRUSTED + } + + initMocks() + + cache.onHostDiscoveredEvent(new HostDiscoveredEvent(destination: destinations.dest1)) + assert cache.getHosts(5).size() == 0 + } + + @Test + void testOnDiscoverNeutralHostsProhibited() { + trustMock.demand.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.NEUTRAL + } + settingsMock.demand.allowUntrusted { false } + + initMocks() + + cache.onHostDiscoveredEvent(new HostDiscoveredEvent(destination: destinations.dest1)) + assert cache.getHosts(5).size() == 0 + } + + @Test + void test2DiscoveredGoodHosts() { + trustMock.demand.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.TRUSTED + } + trustMock.demand.getLevel { d -> + assert d == destinations.dest2 + TrustLevel.TRUSTED + } + trustMock.demand.getLevel{ d -> TrustLevel.TRUSTED } + trustMock.demand.getLevel{ d -> TrustLevel.TRUSTED } + + initMocks() + cache.onHostDiscoveredEvent(new HostDiscoveredEvent(destination: destinations.dest1)) + cache.onHostDiscoveredEvent(new HostDiscoveredEvent(destination: destinations.dest2)) + + def rv = cache.getHosts(1) + assert rv.size() == 1 + assert rv.contains(destinations.dest1) || rv.contains(destinations.dest2) + } + + @Test + void testHostFailed() { + trustMock.demand.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.TRUSTED + } + + initMocks() + cache.onHostDiscoveredEvent(new HostDiscoveredEvent(destination: destinations.dest1)) + + cache.onConnectionEvent( new ConnectionEvent(destination: destinations.dest1, status: ConnectionAttemptStatus.FAILED)) + cache.onConnectionEvent( new ConnectionEvent(destination: destinations.dest1, status: ConnectionAttemptStatus.FAILED)) + cache.onConnectionEvent( new ConnectionEvent(destination: destinations.dest1, status: ConnectionAttemptStatus.FAILED)) + + assert cache.getHosts(5).size() == 0 + } +}