From ef7ec5f1a8328c854b49a760d0ca159a7ca1b74f Mon Sep 17 00:00:00 2001 From: Tom Wadeson Date: Tue, 7 Feb 2017 08:27:22 +0000 Subject: [PATCH] Fix bug in dequeuing according to a `MaxNumberOfMessages` parameter --- .../scala/io/findify/sqsmock/model/QueueCache.scala | 2 +- .../scala/io/findify/sqsmock/SendReceiveTest.scala | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/scala/io/findify/sqsmock/model/QueueCache.scala b/src/main/scala/io/findify/sqsmock/model/QueueCache.scala index 60fb2df..04c4ace 100644 --- a/src/main/scala/io/findify/sqsmock/model/QueueCache.scala +++ b/src/main/scala/io/findify/sqsmock/model/QueueCache.scala @@ -22,7 +22,7 @@ class QueueCache(params:Queue) { msg } def dequeue(count:Int):List[ReceivedMessage] = { - (0 to count).flatMap(_ => dequeue).toList + (0 until count).flatMap(_ => dequeue).toList } def delete(handle:String) = received.get(handle) match { case Some(lease) => diff --git a/src/test/scala/io/findify/sqsmock/SendReceiveTest.scala b/src/test/scala/io/findify/sqsmock/SendReceiveTest.scala index 5b2799f..67986a4 100644 --- a/src/test/scala/io/findify/sqsmock/SendReceiveTest.scala +++ b/src/test/scala/io/findify/sqsmock/SendReceiveTest.scala @@ -1,6 +1,7 @@ package io.findify.sqsmock import com.amazonaws.services.sqs.AmazonSQSClient +import com.amazonaws.services.sqs.model.ReceiveMessageRequest import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} import scala.collection.JavaConversions._ @@ -25,4 +26,15 @@ class SendReceiveTest extends FlatSpec with Matchers with SQSStartStop { val result = client.receiveMessage(queue) assert(result.getMessages.isEmpty) } + it should "dequeue a number of messages according to a `MaxNumberOfMessages` parameter" in { + client.sendMessage(queue, "message1") + client.sendMessage(queue, "message2") + val receiveMessageRequest = new ReceiveMessageRequest(queue).withMaxNumberOfMessages(1) + val result1 = client.receiveMessage(receiveMessageRequest) + assert(result1.getMessages.size == 1) + val result2 = client.receiveMessage(receiveMessageRequest) + assert(result2.getMessages.size == 1) + val result3 = client.receiveMessage(receiveMessageRequest) + assert(result3.getMessages.size == 0) + } }