From d3ec9382d07899f09c2bf74c1ce150df34d6f3aa Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 16 Apr 2022 13:45:52 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Fix=20failing=20memory=20database=20?= =?utf8?q?test?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The test failed because of timing issues with the rate limiter when saving post replies. The first part of the fix is to inject the rate limiter in the constructor. The second part would be to create a special rate limiter that only ever lets the first request pass and not the second one but for now a rate limit of 1 element per 1000 seconds should be enough, even for the slowest tests… unless it isn’t. There are other rate limiters in the memory database which can cause other tests to fail at random times in the future. Those will be left as an exercise for another day. --- .../net/pterodactylus/sone/database/memory/MemoryDatabase.kt | 5 +++-- .../net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt | 7 ++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt index 2ab0938..04a94c7 100644 --- a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt +++ b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt @@ -58,7 +58,9 @@ import kotlin.concurrent.withLock * Memory-based [PostDatabase] implementation. */ @Singleton -class MemoryDatabase @Inject constructor(private val configuration: Configuration) : AbstractService(), Database { +class MemoryDatabase constructor(private val configuration: Configuration, private val saveKnownPostRepliesRateLimiter: RateLimiter) : AbstractService(), Database { + + @javax.inject.Inject constructor(configuration: Configuration): this(configuration, RateLimiter.create(1.0)) private val lock = ReentrantReadWriteLock() private val readLock: ReadLock by lazy { lock.readLock() } @@ -79,7 +81,6 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio private val memoryFriendDatabase = MemoryFriendDatabase(configurationLoader) private val saveRateLimiter: RateLimiter = RateLimiter.create(1.0) private val saveKnownPostsRateLimiter: RateLimiter = RateLimiter.create(1.0) - private val saveKnownPostRepliesRateLimiter: RateLimiter = RateLimiter.create(1.0) override val soneLoader get() = this::getSone diff --git a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt index caacb68..2535f3c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt @@ -17,8 +17,7 @@ package net.pterodactylus.sone.database.memory -import com.google.common.base.* -import com.google.common.base.Optional.* +import com.google.common.util.concurrent.RateLimiter import net.pterodactylus.sone.data.* import net.pterodactylus.sone.data.impl.* import net.pterodactylus.sone.test.* @@ -31,7 +30,6 @@ import org.mockito.ArgumentMatchers.anyString import org.mockito.Mockito.* import org.mockito.invocation.* import java.util.Arrays.* -import java.util.UUID.* import kotlin.test.* /** @@ -40,7 +38,7 @@ import kotlin.test.* class MemoryDatabaseTest { private val configuration = deepMock() - private val memoryDatabase = MemoryDatabase(configuration) + private val memoryDatabase = MemoryDatabase(configuration, RateLimiter.create(0.001)) private val sone = mock() @BeforeTest @@ -413,7 +411,6 @@ class MemoryDatabaseTest { } @Test - @Dirty("the rate limiter should be mocked") fun `setting post replies as knows twice in a row only saves the database once`() { prepareConfigurationValues() val postReply = mock() -- 2.7.4