From a233876c4e12d7690d0366a46bfc332eb4867743 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 22 Jul 2018 12:31:00 +0100 Subject: [PATCH] more tests --- .../core/hostcache/HostCacheTest.groovy | 96 ++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy b/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy index 28b2ac4f..6274cd30 100644 --- a/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy +++ b/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy @@ -11,7 +11,10 @@ import com.muwire.core.connection.ConnectionEvent import com.muwire.core.trust.TrustLevel import com.muwire.core.trust.TrustService +import groovy.json.JsonOutput +import groovy.json.JsonSlurper import groovy.mock.interceptor.MockFor +import groovy.mock.interceptor.StubFor class HostCacheTest { @@ -95,11 +98,11 @@ class HostCacheTest { @Test void testOnDiscoverNeutralHostsProhibited() { - trustMock.demand.getLevel { d -> + trustMock.ignore.getLevel { d -> assert d == destinations.dest1 TrustLevel.NEUTRAL } - settingsMock.demand.allowUntrusted { false } + settingsMock.ignore.allowUntrusted { false } initMocks() @@ -145,4 +148,93 @@ class HostCacheTest { assert cache.getHosts(5).size() == 0 } + + @Test + void testFailedHostSuceeds() { + trustMock.demand.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.TRUSTED + } + 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.SUCCESSFUL)) + cache.onConnectionEvent( new ConnectionEvent(destination: destinations.dest1, status: ConnectionAttemptStatus.FAILED)) + cache.onConnectionEvent( new ConnectionEvent(destination: destinations.dest1, status: ConnectionAttemptStatus.FAILED)) + + def rv = cache.getHosts(5) + assert rv.size() == 1 + assert rv.contains(destinations.dest1) + } + + + @Test + void testDuplicateHostDiscovered() { + trustMock.demand.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.TRUSTED + } + trustMock.demand.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.TRUSTED + } + + initMocks() + cache.onHostDiscoveredEvent(new HostDiscoveredEvent(destination: destinations.dest1)) + cache.onHostDiscoveredEvent(new HostDiscoveredEvent(destination: destinations.dest1)) + + def rv = cache.getHosts(5) + assert rv.size() == 1 + assert rv.contains(destinations.dest1) + } + + @Test + void testSaving() { + trustMock.ignore.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.TRUSTED + } + initMocks() + cache.onHostDiscoveredEvent(new HostDiscoveredEvent(destination: destinations.dest1)) + Thread.sleep(150) + + assert persist.exists() + int lines = 0 + persist.eachLine { + lines++ + JsonSlurper slurper = new JsonSlurper() + def json = slurper.parseText(it) + assert json.destination == destinations.dest1.toBase64() + assert json.failures == 0 + } + assert lines == 1 + } + + @Test + void testLoading() { + def json = [:] + json.destination = destinations.dest1.toBase64() + json.failures = 0 + json = JsonOutput.toJson(json) + persist.append("${json}\n") + + trustMock.ignore.getLevel { d -> + assert d == destinations.dest1 + TrustLevel.TRUSTED + } + + initMocks() + def rv = cache.getHosts(5) + assert rv.size() == 1 + assert rv.contains(destinations.dest1) + + + } }