2 * Sone - WebOfTrustPinger.kt - Copyright © 2019 David ‘Bombe’ Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet.wot
20 import com.google.common.eventbus.*
21 import net.pterodactylus.sone.core.event.*
22 import net.pterodactylus.sone.freenet.plugin.*
23 import net.pterodactylus.sone.utils.*
24 import org.hamcrest.MatcherAssert.*
25 import org.hamcrest.Matchers.*
26 import java.util.concurrent.atomic.*
30 * Unit test for [WebOfTrustPinger].
32 class WebOfTrustPingerTest {
34 private val eventBus = EventBus()
35 private val webOfTrustReachable = AtomicBoolean()
36 private val webOfTrustReacher: () -> Unit = { webOfTrustReachable.get().onFalse { throw PluginException() } }
37 private val rescheduled = AtomicBoolean()
38 private val reschedule: () -> Unit = { rescheduled.set(true) }
39 private val pinger = WebOfTrustPinger(eventBus, webOfTrustReacher, reschedule)
42 fun `pinger sends wot appeared event when run first and wot is reachable`() {
43 webOfTrustReachable.set(true)
44 val appearedReceived = AtomicBoolean()
45 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
47 assertThat(appearedReceived.get(), equalTo(true))
51 fun `pinger reschedules when wot is reachable`() {
52 webOfTrustReachable.set(true)
54 assertThat(rescheduled.get(), equalTo(true))
58 fun `pinger sends wot disappeared event when run first and wot is not reachable`() {
59 val appearedReceived = AtomicBoolean()
60 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
62 assertThat(appearedReceived.get(), equalTo(false))
66 fun `pinger reschedules when wot is not reachable`() {
68 assertThat(rescheduled.get(), equalTo(true))
72 fun `pinger sends wot disappeared event when run twice and wot is not reachable on second call`() {
73 val disappearedReceived = AtomicBoolean()
74 eventBus.register(WebOfTrustDisappearedCatcher { disappearedReceived.set(true) })
75 webOfTrustReachable.set(true)
77 webOfTrustReachable.set(false)
79 assertThat(disappearedReceived.get(), equalTo(true))
83 fun `pinger sends wot appeared event only once`() {
84 webOfTrustReachable.set(true)
85 val appearedReceived = AtomicBoolean()
86 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
88 appearedReceived.set(false)
90 assertThat(appearedReceived.get(), equalTo(false))
94 fun `pinger sends wot disappeared event only once`() {
95 val disappearedReceived = AtomicBoolean()
96 eventBus.register(WebOfTrustDisappearedCatcher { disappearedReceived.set(true) })
98 disappearedReceived.set(false)
100 assertThat(disappearedReceived.get(), equalTo(false))
105 private class WebOfTrustAppearedCatcher(private val received: () -> Unit) {
107 fun webOfTrustAppeared(webOfTrustAppeared: WebOfTrustAppeared) {
112 private class WebOfTrustDisappearedCatcher(private val received: () -> Unit) {
114 fun webOfTrustDisappeared(webOfTrustDisappeared: WebOfTrustDisappeared) {