From c96580ad5c450530aecdc9e9e99ae6ab4134ff2b Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 5 Sep 2023 21:38:31 +1000 Subject: [PATCH] Remove internal map for queue indexes. Use the underlying medium to determine the next index. So that multiple MultiQueue instances could be used together. --- .../configuration/QueueConfiguration.kt | 10 -- .../kilemon/messagequeue/queue/MultiQueue.kt | 55 +------ .../queue/cache/redis/RedisMultiQueue.kt | 24 ++- .../queue/inmemory/InMemoryMultiQueue.kt | 25 +++- .../queue/nosql/mongo/MongoMultiQueue.kt | 73 ++-------- .../repository/MongoQueueMessageRepository.kt | 7 + .../messagequeue/queue/sql/SqlMultiQueue.kt | 22 +-- .../settings/MessageQueueSettings.kt | 13 -- .../queue/AbstractMultiQueueTest.kt | 137 +++++++----------- .../inmemory/InMemoryMockMultiQueueTest.kt | 6 - .../queue/nosql/mongo/MongoMultiQueueTest.kt | 3 +- .../MessageQueueControllerMockTest.kt | 1 - .../rest/controller/SettingsControllerTest.kt | 1 - 13 files changed, 99 insertions(+), 278 deletions(-) diff --git a/src/main/kotlin/au/kilemon/messagequeue/configuration/QueueConfiguration.kt b/src/main/kotlin/au/kilemon/messagequeue/configuration/QueueConfiguration.kt index a550a67..7641f06 100644 --- a/src/main/kotlin/au/kilemon/messagequeue/configuration/QueueConfiguration.kt +++ b/src/main/kotlin/au/kilemon/messagequeue/configuration/QueueConfiguration.kt @@ -70,16 +70,6 @@ class QueueConfiguration : HasLogger } } - if (!messageQueueSettings.multiQueueLazyInitialise.toBoolean()) - { - LOG.trace("Lazy initialising is disabled, initialising queue indexes for existing messages now.") - queue.initialiseQueueIndex() - } - else - { - LOG.debug("Lazy initialise enabled with provided argument [{}], delaying initialisation.", messageQueueSettings.multiQueueLazyInitialise) - } - LOG.info("Initialising [{}] queue as the [{}] is set to [{}].", queue::class.java.name, MessageQueueSettings.MULTI_QUEUE_TYPE, messageQueueSettings.multiQueueType) return queue diff --git a/src/main/kotlin/au/kilemon/messagequeue/queue/MultiQueue.kt b/src/main/kotlin/au/kilemon/messagequeue/queue/MultiQueue.kt index 7202a42..910e202 100644 --- a/src/main/kotlin/au/kilemon/messagequeue/queue/MultiQueue.kt +++ b/src/main/kotlin/au/kilemon/messagequeue/queue/MultiQueue.kt @@ -51,35 +51,7 @@ interface MultiQueue: Queue, HasLogger */ /** - * A holder for the current max [QueueMessage] index per sub-queue type. - * This is used to track the next ID of the queue index that should be set and gives order to the - * received messages as they are created in storage mechanisms that don't guarantee order. - */ - var maxQueueIndex: HashMap? - - /** - * Lazy initialise the index for use in tests when the backing mechanism is not ready, but we are creating - * the [MultiQueue]. - * - * This could be extended to provide a property to force lazy initialisation. - */ - fun getMaxQueueMap(): HashMap - - /** - * Initialise the [maxQueueIndex] based on any existing [QueueMessage]s in the storage mechanism. - */ - fun initialiseQueueIndex() - { - maxQueueIndex = HashMap() - keys(false).forEach{ queueType -> - val queueForType = getQueueForType(queueType) - val maxIndex = queueForType.last().id - maxQueueIndex!![queueType] = AtomicLong(maxIndex?.plus(1) ?: 1) - } - } - - /** - * Get the [maxQueueIndex] then increment it. + * Get the next queue index. * If it does not exist yet, a default value of 1 will be set and returned. * * This can be overridden to return [Optional.EMPTY] to not override the ID of the @@ -88,16 +60,7 @@ interface MultiQueue: Queue, HasLogger * * @return the current value of the index before it was incremented */ - fun getAndIncrementQueueIndex(queueType: String): Optional - { - var index = getMaxQueueMap()[queueType] - if (index == null) - { - index = AtomicLong(1) - getMaxQueueMap()[queueType] = index - } - return Optional.of(index.getAndIncrement()) - } + fun getNextQueueIndex(queueType: String): Optional /** * Used to persist the updated [QueueMessage] to the storage mechanism. @@ -271,20 +234,9 @@ interface MultiQueue: Queue, HasLogger */ fun clearForType(queueType: String): Int { - clearQueueIndexForType(queueType) return clearForTypeInternal(queueType) } - /** - * Clear the [MultiQueue.maxQueueIndex] entry matching the provided key [queueType]. - * - * @param queueType the [String] of the [Queue] to clear - */ - fun clearQueueIndexForType(queueType: String) - { - getMaxQueueMap().remove(queueType) - } - /** * Indicates whether the underlying [Queue] for the provided [String] is empty. By calling [Queue.isEmpty]. * @@ -381,7 +333,7 @@ interface MultiQueue: Queue, HasLogger { if (element.id == null) { - val index = getAndIncrementQueueIndex(element.type) + val index = getNextQueueIndex(element.type) if (index.isPresent) { element.id = index.get() @@ -531,7 +483,6 @@ interface MultiQueue: Queue, HasLogger val amountRemovedForQueue = clearForType(key) removedEntryCount += amountRemovedForQueue } - getMaxQueueMap().clear() LOG.debug("Cleared multi-queue, removed [{}] message entries over [{}] queue types.", removedEntryCount, keys) } diff --git a/src/main/kotlin/au/kilemon/messagequeue/queue/cache/redis/RedisMultiQueue.kt b/src/main/kotlin/au/kilemon/messagequeue/queue/cache/redis/RedisMultiQueue.kt index 34b7afd..03d461e 100644 --- a/src/main/kotlin/au/kilemon/messagequeue/queue/cache/redis/RedisMultiQueue.kt +++ b/src/main/kotlin/au/kilemon/messagequeue/queue/cache/redis/RedisMultiQueue.kt @@ -25,18 +25,6 @@ class RedisMultiQueue(private val prefix: String = "", private val redisTemplate { override val LOG: Logger = initialiseLogger() - override var maxQueueIndex: HashMap? = null - - override fun getMaxQueueMap(): HashMap - { - if (maxQueueIndex == null) - { - initialiseQueueIndex() - } - - return maxQueueIndex!! - } - /** * Append the [MessageQueueSettings.redisPrefix] to the provided [queueType] [String]. * @@ -109,9 +97,17 @@ class RedisMultiQueue(private val prefix: String = "", private val redisTemplate /** * Overriding to pass in the [queueType] into [appendPrefix]. */ - override fun getAndIncrementQueueIndex(queueType: String): Optional + override fun getNextQueueIndex(queueType: String): Optional { - return super.getAndIncrementQueueIndex(appendPrefix(queueType)) + val queueForType = getQueueForType(appendPrefix(queueType)) + return if (queueForType.isNotEmpty()) + { + Optional.ofNullable(queueForType.last().id?.plus(1) ?: 1) + } + else + { + Optional.of(1) + } } override fun removeInternal(element: QueueMessage): Boolean diff --git a/src/main/kotlin/au/kilemon/messagequeue/queue/inmemory/InMemoryMultiQueue.kt b/src/main/kotlin/au/kilemon/messagequeue/queue/inmemory/InMemoryMultiQueue.kt index ced9743..c4776d4 100644 --- a/src/main/kotlin/au/kilemon/messagequeue/queue/inmemory/InMemoryMultiQueue.kt +++ b/src/main/kotlin/au/kilemon/messagequeue/queue/inmemory/InMemoryMultiQueue.kt @@ -33,19 +33,23 @@ open class InMemoryMultiQueue : MultiQueue, HasLogger */ private val messageQueue: ConcurrentHashMap> = ConcurrentHashMap() - override var maxQueueIndex: HashMap? = null + private val maxQueueIndex: HashMap = HashMap() - override fun getMaxQueueMap(): HashMap + /** + * This index is special compared to the other types it will be incremented once retrieved. So we could be skipping + * indexes, but it should be fine since it's only used for message ordering. + */ + override fun getNextQueueIndex(queueType: String): Optional { - if (maxQueueIndex == null) + var index = maxQueueIndex[queueType] + if (index == null) { - initialiseQueueIndex() + index = AtomicLong(1) + maxQueueIndex[queueType] = index } - - return maxQueueIndex!! + return Optional.of(index.getAndIncrement()) } - override fun getQueueForType(queueType: String): Queue { var queueForType: Queue? = messageQueue[queueType] @@ -82,6 +86,7 @@ open class InMemoryMultiQueue : MultiQueue, HasLogger { var amountRemoved = 0 val queueForType: Queue? = messageQueue[queueType] + maxQueueIndex.remove(queueType) if (queueForType != null) { amountRemoved = queueForType.size @@ -97,6 +102,12 @@ open class InMemoryMultiQueue : MultiQueue, HasLogger return amountRemoved } + override fun clear() + { + super.clear() + maxQueueIndex.clear() + } + @Throws(DuplicateMessageException::class) override fun add(element: QueueMessage): Boolean { diff --git a/src/main/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/MongoMultiQueue.kt b/src/main/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/MongoMultiQueue.kt index 5dd39fd..ecf3fcb 100644 --- a/src/main/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/MongoMultiQueue.kt +++ b/src/main/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/MongoMultiQueue.kt @@ -28,52 +28,10 @@ class MongoMultiQueue : MultiQueue, HasLogger override val LOG: Logger = initialiseLogger() - override var maxQueueIndex: HashMap? = null - - override fun getMaxQueueMap(): HashMap - { - if (maxQueueIndex == null) - { - initialiseQueueIndex() - } - - return maxQueueIndex!! - } - @Lazy @Autowired private lateinit var queueMessageRepository: MongoQueueMessageRepository - /** - * Special initialisation for mongo, since the max ID is shared for all sub queues we need to set the [INDEX_ID] - * to the highest ID of all elements in all the queues. - */ - override fun initialiseQueueIndex() - { - maxQueueIndex = HashMap() - var maxIndex: Long? = null - keys(false).forEach{ queueType -> - val queueForType = getQueueForType(queueType) - val index = queueForType.last().id - if (index != null) - { - maxIndex = if (maxIndex == null) - { - index - } - else - { - Math.max(maxIndex!!, index) - } - } - } - - if (maxIndex != null) - { - maxQueueIndex!![INDEX_ID] = AtomicLong(maxIndex!!) - } - } - override fun persistMessage(message: QueueMessage) { val queueMessageDocument = QueueMessageDocument(message) @@ -179,27 +137,16 @@ class MongoMultiQueue : MultiQueue, HasLogger * Overriding to use the constant [INDEX_ID] for all look-ups since the ID is shared and needs to be assigned to * the [QueueMessageDocument] before it is created. */ - override fun getAndIncrementQueueIndex(queueType: String): Optional + override fun getNextQueueIndex(queueType: String): Optional { - return super.getAndIncrementQueueIndex(INDEX_ID) - } - - /** - * Override to never clear the queue index for the type, since it's a shared index map. - */ - override fun clearQueueIndexForType(queueType: String) - { - - } - - /** - * Clear the [maxQueueIndex] if the entire map is cleared. - * - * Since [MongoMultiQueue.clearQueueIndexForType] is not clearing any of map entries. - */ - override fun clear() - { - super.clear() - getMaxQueueMap().clear() + val largestIdMessage = queueMessageRepository.findTopByOrderByIdDesc() + return if (largestIdMessage.isPresent) + { + Optional.ofNullable(largestIdMessage.get().id?.plus(1) ?: 1) + } + else + { + Optional.of(1) + } } } diff --git a/src/main/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/repository/MongoQueueMessageRepository.kt b/src/main/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/repository/MongoQueueMessageRepository.kt index edd998f..70a4000 100644 --- a/src/main/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/repository/MongoQueueMessageRepository.kt +++ b/src/main/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/repository/MongoQueueMessageRepository.kt @@ -55,4 +55,11 @@ interface MongoQueueMessageRepository: MongoRepository } diff --git a/src/main/kotlin/au/kilemon/messagequeue/queue/sql/SqlMultiQueue.kt b/src/main/kotlin/au/kilemon/messagequeue/queue/sql/SqlMultiQueue.kt index e2ae97b..74e2b5a 100644 --- a/src/main/kotlin/au/kilemon/messagequeue/queue/sql/SqlMultiQueue.kt +++ b/src/main/kotlin/au/kilemon/messagequeue/queue/sql/SqlMultiQueue.kt @@ -26,26 +26,6 @@ class SqlMultiQueue : MultiQueue, HasLogger @Autowired private lateinit var queueMessageRepository: SQLQueueMessageRepository - override var maxQueueIndex: HashMap? = null - - override fun getMaxQueueMap(): HashMap - { - if (maxQueueIndex == null) - { - initialiseQueueIndex() - } - - return maxQueueIndex!! - } - - /** - * Just initialise map, so it's not null, but the SQL [QueueMessage] ID is maintained by the database. - */ - override fun initialiseQueueIndex() - { - maxQueueIndex = HashMap() - } - override fun getQueueForType(queueType: String): Queue { val entries = queueMessageRepository.findByTypeOrderByIdAsc(queueType) @@ -172,7 +152,7 @@ class SqlMultiQueue : MultiQueue, HasLogger * Overriding to return [Optional.EMPTY] so that the [MultiQueue.add] does set an `id` into the [QueueMessage] * even if the id is `null`. */ - override fun getAndIncrementQueueIndex(queueType: String): Optional + override fun getNextQueueIndex(queueType: String): Optional { return Optional.empty() } diff --git a/src/main/kotlin/au/kilemon/messagequeue/settings/MessageQueueSettings.kt b/src/main/kotlin/au/kilemon/messagequeue/settings/MessageQueueSettings.kt index a46959b..b26d63a 100644 --- a/src/main/kotlin/au/kilemon/messagequeue/settings/MessageQueueSettings.kt +++ b/src/main/kotlin/au/kilemon/messagequeue/settings/MessageQueueSettings.kt @@ -28,7 +28,6 @@ class MessageQueueSettings { const val MULTI_QUEUE_TYPE: String = "MULTI_QUEUE_TYPE" const val MULTI_QUEUE_TYPE_DEFAULT: String = "IN_MEMORY" - const val MULTI_QUEUE_LAZY_INITIALISE: String = "MULTI_QUEUE_LAZY_INITIALISE" /** * Start redis related properties @@ -80,18 +79,6 @@ class MessageQueueSettings @set:Generated lateinit var multiQueueType: String - /** - * `Optional` uses the [MULTI_QUEUE_TYPE] environment variable to determine where - * the underlying multi queue is persisted. It can be any value of [MultiQueueType]. - * Defaults to [MultiQueueType.IN_MEMORY] ([MULTI_QUEUE_TYPE_DEFAULT]). - */ - @SerializedName(MULTI_QUEUE_LAZY_INITIALISE) - @JsonProperty(MULTI_QUEUE_LAZY_INITIALISE) - @Value("\${$MULTI_QUEUE_LAZY_INITIALISE:false}") - @get:Generated - @set:Generated - lateinit var multiQueueLazyInitialise: String - /** * `Optional` when [MULTI_QUEUE_TYPE] is set to [MultiQueueType.REDIS]. diff --git a/src/test/kotlin/au/kilemon/messagequeue/queue/AbstractMultiQueueTest.kt b/src/test/kotlin/au/kilemon/messagequeue/queue/AbstractMultiQueueTest.kt index d97144e..c536f12 100644 --- a/src/test/kotlin/au/kilemon/messagequeue/queue/AbstractMultiQueueTest.kt +++ b/src/test/kotlin/au/kilemon/messagequeue/queue/AbstractMultiQueueTest.kt @@ -232,122 +232,83 @@ abstract class AbstractMultiQueueTest } /** - * Ensure that when the [MultiQueue] is empty, that the underlying [MultiQueue.maxQueueIndex] is empty too. + * Ensuring that only the [InMemoryMultiQueue] will auto increment the index as its retrieved but others will not + * and [SqlMultiQueue] will always return [Optional.empty]. */ @Test - fun testInitialiseQueueIndex_allEmpty() - { - Assertions.assertTrue(multiQueue.isEmpty()) - Assertions.assertNotNull(multiQueue.maxQueueIndex) - Assertions.assertTrue(multiQueue.maxQueueIndex!!.isEmpty()) - } - - /** - * This is to cover a scenario where a new [MultiQueue] is created or existing messages exist. To ensure the underlying - * [MultiQueue.maxQueueIndex] is cleared and initialised correctly. - */ - @Test - fun testInitialiseQueueIndex_reInitialise() + fun testGetNextQueueIndex_doesNotIncrement() { + val queueType = "testGetNextQueueIndex_doesNotIncrement" if (multiQueue is SqlMultiQueue) { - return + Assertions.assertTrue(multiQueue.getNextQueueIndex(queueType).isEmpty) } + else if (multiQueue is InMemoryMultiQueue) + { + Assertions.assertEquals(1, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(2, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(3, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(4, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(5, multiQueue.getNextQueueIndex(queueType).get()) - Assertions.assertTrue(multiQueue.isEmpty()) - - val queueType1 = "testInitialiseQueueIndex_reInitialise1" - val queueType2 = "testInitialiseQueueIndex_reInitialise2" - - val list1 = listOf(QueueMessage(81273648, queueType1), QueueMessage("test test test", queueType1), QueueMessage(false, queueType1)) - val list2 = listOf(QueueMessage("test", queueType2), QueueMessage(123, queueType2)) - Assertions.assertTrue(multiQueue.addAll(list1)) - Assertions.assertTrue(multiQueue.addAll(list2)) + multiQueue.clearForType(queueType) + Assertions.assertEquals(1, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(2, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(3, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(4, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(5, multiQueue.getNextQueueIndex(queueType).get()) - if (multiQueue is MongoMultiQueue) - { - Assertions.assertEquals(list1.size + list2.size + 1, multiQueue.maxQueueIndex!![MongoMultiQueue.INDEX_ID]!!.toInt()) - Assertions.assertEquals(list1.size + list2.size + 1, multiQueue.maxQueueIndex!![MongoMultiQueue.INDEX_ID]!!.toInt()) - Assertions.assertEquals(list1.size + list2.size + 1, multiQueue.getAndIncrementQueueIndex(MongoMultiQueue.INDEX_ID).get().toInt()) - Assertions.assertEquals(list1.size + list2.size + 2, multiQueue.getAndIncrementQueueIndex(queueType1).get().toInt()) - Assertions.assertEquals(list1.size + list2.size + 3, multiQueue.getAndIncrementQueueIndex(queueType2).get().toInt()) - Assertions.assertEquals(list1.size + list2.size + 4, multiQueue.maxQueueIndex!![MongoMultiQueue.INDEX_ID]!!.toInt()) - Assertions.assertEquals(list1.size + list2.size + 4, multiQueue.maxQueueIndex!![MongoMultiQueue.INDEX_ID]!!.toInt()) - - multiQueue.initialiseQueueIndex() - // The mongo ID is based on the store entries - Assertions.assertEquals(list1.size + list2.size, multiQueue.maxQueueIndex!![MongoMultiQueue.INDEX_ID]!!.toInt()) - Assertions.assertEquals(list1.size + list2.size, multiQueue.maxQueueIndex!![MongoMultiQueue.INDEX_ID]!!.toInt()) + multiQueue.clear() + Assertions.assertEquals(1, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(2, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(3, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(4, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(5, multiQueue.getNextQueueIndex(queueType).get()) } else { - Assertions.assertEquals(list1.size + 1, multiQueue.maxQueueIndex!![queueType1]!!.toInt()) - Assertions.assertEquals(list2.size + 1, multiQueue.maxQueueIndex!![queueType2]!!.toInt()) - Assertions.assertEquals(list1.size + 1, multiQueue.getAndIncrementQueueIndex(queueType1).get().toInt()) - Assertions.assertEquals(list2.size + 1, multiQueue.getAndIncrementQueueIndex(queueType2).get().toInt()) - Assertions.assertEquals(list1.size + 2, multiQueue.maxQueueIndex!![queueType1]!!.toInt()) - Assertions.assertEquals(list2.size + 2, multiQueue.maxQueueIndex!![queueType2]!!.toInt()) - - multiQueue.initialiseQueueIndex() - Assertions.assertEquals(list1.size + 1, multiQueue.maxQueueIndex!![queueType1]!!.toInt()) - Assertions.assertEquals(list2.size + 1, multiQueue.maxQueueIndex!![queueType2]!!.toInt()) + Assertions.assertTrue(multiQueue.getNextQueueIndex(queueType).isPresent) + Assertions.assertEquals(1, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(1, multiQueue.getNextQueueIndex(queueType).get()) + Assertions.assertEquals(1, multiQueue.getNextQueueIndex(queueType).get()) } } /** - * Ensure that [MultiQueue.getAndIncrementQueueIndex] starts at `1` and increments properly as called. - * - * Ensure that [MultiQueue.clearForType] also clears the [MultiQueue.maxQueueIndex] for the provided `queueType`. + * Ensure that [MultiQueue.getNextQueueIndex] starts at `1` and increments properly as called once entries are added. */ @Test - fun testGetAndIncrementQueueIndex() + fun testGetNextQueueIndex_withMessages() { - val queueType = "testGetAndIncrementQueueIndex" Assertions.assertTrue(multiQueue.isEmpty()) - Assertions.assertNotNull(multiQueue.maxQueueIndex) + + val queueType1 = "testGetNextQueueIndex_reInitialise1" + val queueType2 = "testGetNextQueueIndex_reInitialise2" + + val list1 = listOf(QueueMessage(81273648, queueType1), QueueMessage("test test test", queueType1), QueueMessage(false, queueType1)) + val list2 = listOf(QueueMessage("test", queueType2), QueueMessage(123, queueType2)) + Assertions.assertTrue(multiQueue.addAll(list1)) + Assertions.assertTrue(multiQueue.addAll(list2)) if (multiQueue is SqlMultiQueue) { - // Ensure that we always return an empty optional - Assertions.assertFalse(multiQueue.getAndIncrementQueueIndex(queueType).isPresent) + Assertions.assertTrue(multiQueue.getNextQueueIndex(queueType1).isEmpty) + Assertions.assertTrue(multiQueue.getNextQueueIndex(queueType2).isEmpty) + } + else if (multiQueue is InMemoryMultiQueue) + { + Assertions.assertEquals((list1.size + 1).toLong(), multiQueue.getNextQueueIndex(queueType1).get()) + Assertions.assertEquals((list2.size + 1).toLong(), multiQueue.getNextQueueIndex(queueType2).get()) } else if (multiQueue is MongoMultiQueue) { - // Ensure that no matter the provided queueType argument, that the single entry is always incremented - Assertions.assertNull(multiQueue.maxQueueIndex!![queueType]) - Assertions.assertNull(multiQueue.maxQueueIndex!![MongoMultiQueue.INDEX_ID]) - - Assertions.assertEquals(1, multiQueue.getAndIncrementQueueIndex(queueType).get()) - Assertions.assertEquals(2, multiQueue.getAndIncrementQueueIndex(MongoMultiQueue.INDEX_ID).get()) - Assertions.assertEquals(3, multiQueue.getAndIncrementQueueIndex(queueType).get()) - Assertions.assertEquals(4, multiQueue.getAndIncrementQueueIndex(MongoMultiQueue.INDEX_ID).get()) - Assertions.assertEquals(5, multiQueue.getAndIncrementQueueIndex(queueType).get()) - - multiQueue.clearForType(queueType) - Assertions.assertFalse(multiQueue.maxQueueIndex!!.isEmpty()) - Assertions.assertNull(multiQueue.maxQueueIndex!![queueType]) - Assertions.assertNotNull(multiQueue.maxQueueIndex!![MongoMultiQueue.INDEX_ID]) - - multiQueue.clear() - Assertions.assertTrue(multiQueue.maxQueueIndex!!.isEmpty()) - Assertions.assertNull(multiQueue.maxQueueIndex!![queueType]) - Assertions.assertNull(multiQueue.maxQueueIndex!![MongoMultiQueue.INDEX_ID]) + Assertions.assertEquals((list1.size + list2.size + 1).toLong(), multiQueue.getNextQueueIndex(queueType1).get()) + Assertions.assertEquals((list1.size + list2.size + 1).toLong(), multiQueue.getNextQueueIndex(queueType2).get()) } else { - - Assertions.assertTrue(multiQueue.maxQueueIndex!!.isEmpty()) - Assertions.assertNull(multiQueue.maxQueueIndex!![queueType]) - - Assertions.assertEquals(1, multiQueue.getAndIncrementQueueIndex(queueType).get()) - Assertions.assertEquals(2, multiQueue.getAndIncrementQueueIndex(queueType).get()) - Assertions.assertEquals(3, multiQueue.getAndIncrementQueueIndex(queueType).get()) - Assertions.assertEquals(4, multiQueue.getAndIncrementQueueIndex(queueType).get()) - Assertions.assertEquals(5, multiQueue.getAndIncrementQueueIndex(queueType).get()) - - multiQueue.clearForType(queueType) - Assertions.assertTrue(multiQueue.maxQueueIndex!!.isEmpty()) - Assertions.assertNull(multiQueue.maxQueueIndex!![queueType]) + Assertions.assertEquals((list1.size + 1).toLong(), multiQueue.getNextQueueIndex(queueType1).get()) + Assertions.assertEquals((list2.size + 1).toLong(), multiQueue.getNextQueueIndex(queueType2).get()) } } diff --git a/src/test/kotlin/au/kilemon/messagequeue/queue/inmemory/InMemoryMockMultiQueueTest.kt b/src/test/kotlin/au/kilemon/messagequeue/queue/inmemory/InMemoryMockMultiQueueTest.kt index 9428eb5..7cb8f27 100644 --- a/src/test/kotlin/au/kilemon/messagequeue/queue/inmemory/InMemoryMockMultiQueueTest.kt +++ b/src/test/kotlin/au/kilemon/messagequeue/queue/inmemory/InMemoryMockMultiQueueTest.kt @@ -18,12 +18,6 @@ class InMemoryMockMultiQueueTest { private val multiQueue: InMemoryMultiQueue = Mockito.spy(InMemoryMultiQueue::class.java) - @BeforeEach - fun setUp() - { - multiQueue.initialiseQueueIndex() - } - /** * Test [InMemoryMultiQueue.add] to ensure that `false` is returned when [InMemoryMultiQueue.addInternal] returns `false`. */ diff --git a/src/test/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/MongoMultiQueueTest.kt b/src/test/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/MongoMultiQueueTest.kt index ffb675a..e9ddd6f 100644 --- a/src/test/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/MongoMultiQueueTest.kt +++ b/src/test/kotlin/au/kilemon/messagequeue/queue/nosql/mongo/MongoMultiQueueTest.kt @@ -28,7 +28,7 @@ import org.testcontainers.utility.DockerImageName */ @ExtendWith(SpringExtension::class) @Testcontainers -@DataMongoTest(properties = ["${MessageQueueSettings.MULTI_QUEUE_TYPE}=MONGO", "${MessageQueueSettings.MULTI_QUEUE_LAZY_INITIALISE}=true"]) +@DataMongoTest(properties = ["${MessageQueueSettings.MULTI_QUEUE_TYPE}=MONGO"]) @ContextConfiguration(initializers = [MongoMultiQueueTest.Initializer::class]) @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) @Import( *[QueueConfiguration::class, LoggingConfiguration::class, AbstractMultiQueueTest.AbstractMultiQueueTestConfiguration::class] ) @@ -103,6 +103,5 @@ class MongoMultiQueueTest: AbstractMultiQueueTest() { Assertions.assertTrue(mongoDb.isRunning) multiQueue.clear() - multiQueue.getMaxQueueMap() } } diff --git a/src/test/kotlin/au/kilemon/messagequeue/rest/controller/MessageQueueControllerMockTest.kt b/src/test/kotlin/au/kilemon/messagequeue/rest/controller/MessageQueueControllerMockTest.kt index 6f2a417..7e654da 100644 --- a/src/test/kotlin/au/kilemon/messagequeue/rest/controller/MessageQueueControllerMockTest.kt +++ b/src/test/kotlin/au/kilemon/messagequeue/rest/controller/MessageQueueControllerMockTest.kt @@ -77,7 +77,6 @@ class MessageQueueControllerMockTest fun testCreateMessage_addFails() { val message = QueueMessage("payload", "type") - multiQueue.initialiseQueueIndex() Mockito.`when`(multiQueue.add(message)).thenReturn(false) diff --git a/src/test/kotlin/au/kilemon/messagequeue/rest/controller/SettingsControllerTest.kt b/src/test/kotlin/au/kilemon/messagequeue/rest/controller/SettingsControllerTest.kt index e6505db..4a2fd76 100644 --- a/src/test/kotlin/au/kilemon/messagequeue/rest/controller/SettingsControllerTest.kt +++ b/src/test/kotlin/au/kilemon/messagequeue/rest/controller/SettingsControllerTest.kt @@ -68,7 +68,6 @@ class SettingsControllerTest val settings = gson.fromJson(mvcResult.response.contentAsString, MessageQueueSettings::class.java) Assertions.assertEquals(MultiQueueType.IN_MEMORY.toString(), settings.multiQueueType) - Assertions.assertEquals("false", settings.multiQueueLazyInitialise) Assertions.assertTrue(settings.redisPrefix.isEmpty()) Assertions.assertEquals(MessageQueueSettings.REDIS_ENDPOINT_DEFAULT, settings.redisEndpoint)