Skip to content

Commit

Permalink
Add check to return null BrokeredMessage in ReceiveMessageResult when
Browse files Browse the repository at this point in the history
retrieving Message on Empty Queue
Add test cases when Retrieving from Empty Queue in both RECEIVE/DELETE
and PEEK/LOCK modes.
Update existing test to be compatible with receiving null
BrokeredMessage instead of uninitialized one.
  • Loading branch information
wdixon committed Apr 18, 2013
1 parent 6e5475c commit cf7b4b6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ else if (options.isPeekLock()) {
throw new RuntimeException("Unknown ReceiveMode");
}

//No Messages Available scenario
//Q is empty or contains only LOCKED Messages during period specified by timeout
if (clientResult.getStatus() == 204) {
return null;
}

BrokerProperties brokerProperties;
if (clientResult.getHeaders().containsKey("BrokerProperties")) {
brokerProperties = mapper.fromString(clientResult.getHeaders().getFirst("BrokerProperties"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.microsoft.windowsazure.services.serviceBus.models.ListTopicsResult;
import com.microsoft.windowsazure.services.serviceBus.models.QueueInfo;
import com.microsoft.windowsazure.services.serviceBus.models.ReceiveMessageOptions;
import com.microsoft.windowsazure.services.serviceBus.models.ReceiveQueueMessageResult;
import com.microsoft.windowsazure.services.serviceBus.models.RuleInfo;
import com.microsoft.windowsazure.services.serviceBus.models.SubscriptionInfo;
import com.microsoft.windowsazure.services.serviceBus.models.TopicInfo;
Expand Down Expand Up @@ -172,6 +173,20 @@ public void receiveMessageWorks() throws Exception {
assertArrayEquals("Hello World".getBytes(), Arrays.copyOf(data, size));
}

@Test
public void receiveMessageEmptyQueueWorks() throws Exception {
// Arrange
String queueName = "TestReceiveMessageEmptyQueueWorks";
service.createQueue(new QueueInfo(queueName));

// Act
ReceiveQueueMessageResult result = service.receiveQueueMessage(queueName, RECEIVE_AND_DELETE_5_SECONDS);

//Assert
assertNotNull(result);
assertNull(result.getValue());
}

@Test
public void peekLockMessageWorks() throws Exception {
// Arrange
Expand All @@ -189,6 +204,20 @@ public void peekLockMessageWorks() throws Exception {
assertEquals("Hello Again", new String(data, 0, size));
}

@Test
public void peekLockMessageEmptyQueueWorks() throws Exception {
// Arrange
String queueName = "TestPeekLockMessageEmptyQueueWorks";
service.createQueue(new QueueInfo(queueName));

// Act
ReceiveQueueMessageResult result = service.receiveQueueMessage(queueName, PEEK_LOCK_5_SECONDS);

// Assert
assertNotNull(result);
assertNull(result.getValue());
}

@Test
public void peekLockedMessageCanBeCompleted() throws Exception {
// Arrange
Expand Down Expand Up @@ -245,15 +274,16 @@ public void peekLockedMessageCanBeDeleted() throws Exception {
String lockToken = peekedMessage.getLockToken();
Date lockedUntil = peekedMessage.getLockedUntilUtc();

service.deleteMessage(peekedMessage);
BrokeredMessage receivedMessage = service.receiveQueueMessage(queueName, RECEIVE_AND_DELETE_5_SECONDS)
.getValue();

// Assert
assertNotNull(lockToken);
assertNotNull(lockedUntil);
assertNull(receivedMessage.getLockToken());
assertNull(receivedMessage.getLockedUntilUtc());

// Act
service.deleteMessage(peekedMessage);
ReceiveQueueMessageResult result = service.receiveQueueMessage(queueName, RECEIVE_AND_DELETE_5_SECONDS);

//Assert
assertNull(result.getValue());
}

@Test
Expand Down

0 comments on commit cf7b4b6

Please sign in to comment.