Skip to content

Commit

Permalink
#2: Unit test for looking up msg by delivery receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 6, 2012
1 parent 6536bc2 commit e902c4f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ trait NativeQueueModule {
}

def lookupMessage(deliveryReceipt: DeliveryReceipt) = {
None // TODO
val id = deliveryReceipt.extractId
val messageOpt = lookupMessage(id)
messageOpt.flatMap(message => {
if (message.lastDeliveryReceipt == Some(deliveryReceipt)) {
Some(message)
} else {
None
}
})
}

def updateDefaultVisibilityTimeout(defaultVisibilityTimeout: MillisVisibilityTimeout): Queue = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,41 @@ class NativeMessageClientImplTestSuite extends FunSuite with MustMatchers with M
mockExecutor.findExecutedCommand[ReceiveMessageCommand].newNextDelivery must be (MillisNextDelivery(Now + 1000L))
}

test("looking up a message by delivery receipt should succeed if delivery receipts match") {
// Given
val (queueOperations, mockExecutor) = createQueueOperationsWithMockStorage(createQueueData("q1", MillisVisibilityTimeout(123L)))

val id = MessageId("abc")
val receipt = DeliveryReceipt.generate(id)

val m = createMessageData(id.id, "z", MillisNextDelivery(123L), Some(receipt))
mockExecutor.returnWhenCommandClass(classOf[LookupMessageCommand], Some(m))

// When
val msg = queueOperations.lookupMessage(receipt)

// Then
msg.map(_.id) must be (Some(id))
}

test("looking up a message by delivery receipt should return none if delivery don't receipts match") {
// Given
val (queueOperations, mockExecutor) = createQueueOperationsWithMockStorage(createQueueData("q1", MillisVisibilityTimeout(123L)))

val id = MessageId("abc")
val receipt1 = DeliveryReceipt.generate(id)
val receipt2 = DeliveryReceipt.generate(id)

val m = createMessageData(id.id, "z", MillisNextDelivery(123L), Some(receipt1))
mockExecutor.returnWhenCommandClass(classOf[LookupMessageCommand], Some(m))

// When
val msg = queueOperations.lookupMessage(receipt2)

// Then
msg must be (None)
}

def createQueueOperationsWithMockStorage(queueData: QueueData): (QueueOperations, MockStorageCommandExecutor) = {
val env = new NativeModule
with StorageModule
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.elasticmq.test

import org.elasticmq.data.{MessageData, QueueData}
import org.elasticmq.{MessageId, MillisNextDelivery, MillisVisibilityTimeout}
import org.elasticmq.{DeliveryReceipt, MessageId, MillisNextDelivery, MillisVisibilityTimeout}
import org.joda.time.{DateTime, Duration}

trait DataCreationHelpers {
def createQueueData(name: String, defaultVisibilityTimeout: MillisVisibilityTimeout) =
QueueData(name, defaultVisibilityTimeout, Duration.ZERO, new DateTime(0), new DateTime(0))

def createMessageData(id: String, content: String, nextDelivery: MillisNextDelivery) =
MessageData(MessageId(id), None, content, nextDelivery, new DateTime(0))
def createMessageData(id: String, content: String, nextDelivery: MillisNextDelivery,
deliveryReceipt: Option[DeliveryReceipt] = None) =
MessageData(MessageId(id), deliveryReceipt, content, nextDelivery, new DateTime(0))
}

0 comments on commit e902c4f

Please sign in to comment.