diff --git a/src/main/java/io/iron/ironmq/Cloud.java b/src/main/java/io/iron/ironmq/Cloud.java index ad2cb34..1a52ce0 100644 --- a/src/main/java/io/iron/ironmq/Cloud.java +++ b/src/main/java/io/iron/ironmq/Cloud.java @@ -9,10 +9,7 @@ public class Cloud { int port; String pathPrefix = ""; - public static final Cloud ironAWSUSEast = new Cloud("https", "mq-aws-us-east-1.iron.io", 443); - public static final Cloud ironAWSEUWest = new Cloud("https", "mq-aws-eu-west-1.iron.io", 443); - public static final Cloud ironRackspaceORD = new Cloud("https", "mq-rackspace-ord.iron.io", 443); - public static final Cloud ironRackspaceLON = new Cloud("https", "mq-rackspace-lon.iron.io", 443); + public static final Cloud ironAWSUSEast = new Cloud("https", "mq-v3-aws-us-east-1.iron.io", 443); public Cloud(String url) throws MalformedURLException { URL u = new URL(url); diff --git a/src/main/java/io/iron/ironmq/Message.java b/src/main/java/io/iron/ironmq/Message.java index 5900ae8..cf1b94b 100644 --- a/src/main/java/io/iron/ironmq/Message.java +++ b/src/main/java/io/iron/ironmq/Message.java @@ -9,7 +9,6 @@ */ public class Message extends MessageOptions implements Serializable { private String body; - private long timeout; // Long, not long, so that it's nullable. Gson doesn't serialize null, // so we can use the default on the server and not have to know about // it. @@ -30,18 +29,6 @@ public Message() {} */ public void setBody(String body) { this.body = body; } - /** - * Returns the Message's timeout. - */ - public long getTimeout() { return timeout; } - - /** - * Sets the Message's timeout. - * - * @param timeout The new timeout. - */ - public void setTimeout(long timeout) { this.timeout = timeout; } - /** * Returns the number of times the message has been reserved. */ diff --git a/src/main/java/io/iron/ironmq/MessageOptions.java b/src/main/java/io/iron/ironmq/MessageOptions.java index dcd4404..27834e8 100644 --- a/src/main/java/io/iron/ironmq/MessageOptions.java +++ b/src/main/java/io/iron/ironmq/MessageOptions.java @@ -10,6 +10,7 @@ public class MessageOptions implements Serializable { protected String id; protected Long delay; + private Long timeout; @SerializedName("reservation_id") protected String reservationId; @@ -31,6 +32,12 @@ public MessageOptions(String id, String reservationId) { this.reservationId = reservationId; } + public MessageOptions(String id, String reservationId, Long timeout) { + this.id = id; + this.reservationId = reservationId; + this.timeout = timeout; + } + /** * Returns Id of the Message. */ @@ -50,6 +57,11 @@ public String getReservationId() { */ public long getDelay() { return delay; } + /** + * Returns the Message's timeout. + */ + public long getTimeout() { return timeout; } + /** * Sets Id to the Message. * @@ -74,4 +86,11 @@ public void setReservationId(String reservationId) { * @param delay The new delay. */ public void setDelay(long delay) { this.delay = delay; } + + /** + * Sets the Message's timeout. + * + * @param timeout The new timeout. + */ + public void setTimeout(long timeout) { this.timeout = timeout; } } diff --git a/src/main/java/io/iron/ironmq/Queue.java b/src/main/java/io/iron/ironmq/Queue.java index 6141b9a..244abdd 100644 --- a/src/main/java/io/iron/ironmq/Queue.java +++ b/src/main/java/io/iron/ironmq/Queue.java @@ -172,27 +172,39 @@ public Messages peek(int numberOfMessages) throws IOException { /** * Touching a reserved message extends its timeout to the duration specified when the message was created. * - * @param id The ID of the message to delete. + * @param message The message to delete. * * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. * @throws java.io.IOException If there is an error accessing the IronMQ server. - * @deprecated It's not possible to touch a message without reservation id since v3 of IronMQ */ - @Deprecated - public void touchMessage(String id) throws IOException { - touchMessage(id, null); + public MessageOptions touchMessage(Message message) throws IOException { + return touchMessage(message, null); + } + + /** + * Touching a reserved message extends its timeout to the specified duration. + * + * @param message The message to delete. + * @param timeout After timeout (in seconds), item will be placed back onto queue. + * + * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws java.io.IOException If there is an error accessing the IronMQ server. + */ + public MessageOptions touchMessage(Message message, int timeout) throws IOException { + return touchMessage(message, (long) timeout); } /** * Touching a reserved message extends its timeout to the duration specified when the message was created. * * @param message The message to delete. + * @param timeout After timeout (in seconds), item will be placed back onto queue. * * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. * @throws java.io.IOException If there is an error accessing the IronMQ server. */ - public MessageOptions touchMessage(Message message) throws IOException { - MessageOptions messageOptions = touchMessage(message.getId(), message.getReservationId()); + public MessageOptions touchMessage(Message message, Long timeout) throws IOException { + MessageOptions messageOptions = touchMessage(message.getId(), message.getReservationId(), timeout); message.setReservationId(messageOptions.getReservationId()); return messageOptions; } @@ -201,12 +213,41 @@ public MessageOptions touchMessage(Message message) throws IOException { * Touching a reserved message extends its timeout to the duration specified when the message was created. * * @param id The ID of the message to delete. + * @param reservationId This id is returned when you reserve a message and must be provided to delete a message that is reserved. * * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. * @throws java.io.IOException If there is an error accessing the IronMQ server. */ public MessageOptions touchMessage(String id, String reservationId) throws IOException { - String payload = new Gson().toJson(new MessageOptions(reservationId)); + return touchMessage(id, reservationId, null); + } + + /** + * Touching a reserved message extends its timeout to the specified duration. + * + * @param id The ID of the message to delete. + * @param reservationId This id is returned when you reserve a message and must be provided to delete a message that is reserved. + * @param timeout After timeout (in seconds), item will be placed back onto queue. + * + * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws java.io.IOException If there is an error accessing the IronMQ server. + */ + public MessageOptions touchMessage(String id, String reservationId, int timeout) throws IOException { + return touchMessage(id, reservationId, (long) timeout); + } + + /** + * Touching a reserved message extends its timeout to the duration specified when the message was created. + * + * @param id The ID of the message to delete. + * @param reservationId This id is returned when you reserve a message and must be provided to delete a message that is reserved. + * @param timeout After timeout (in seconds), item will be placed back onto queue. + * + * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws java.io.IOException If there is an error accessing the IronMQ server. + */ + public MessageOptions touchMessage(String id, String reservationId, Long timeout) throws IOException { + String payload = new Gson().toJson(new MessageOptions(null, reservationId, timeout)); IronReader reader = client.post("queues/" + name + "/messages/" + id + "/touch", payload); try { return new Gson().fromJson(reader.reader, MessageOptions.class); @@ -237,7 +278,20 @@ public void deleteMessage(String id) throws IOException { * @throws java.io.IOException If there is an error accessing the IronMQ server. */ public void deleteMessage(String id, String reservationId) throws IOException { - String payload = new Gson().toJson(new MessageOptions(reservationId)); + deleteMessage(id, reservationId, null); + } + + /** + * Deletes a Message from the queue. + * + * @param id The ID of the message to delete. + * @param reservationId Reservation Id of the message. Reserved message could not be deleted without reservation Id. + * + * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws java.io.IOException If there is an error accessing the IronMQ server. + */ + public void deleteMessage(String id, String reservationId, String subscriberName) throws IOException { + String payload = new Gson().toJson(new SubscribedMessageOptions(reservationId, subscriberName)); IronReader reader = client.delete("queues/" + name + "/messages/" + id, payload); reader.close(); } @@ -460,22 +514,6 @@ public Delay(int delay) { } } - /** - * Release reserved message after specified time. If there is no message with such id on the queue, an - * EmptyQueueException is thrown. - * - * @param id The ID of the message to release. - * @param delay The time after which the message will be released. - * - * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. - * @throws java.io.IOException If there is an error accessing the IronMQ server. - * @deprecated Reservation Id is required for message releasing since v3 - */ - @Deprecated - public void releaseMessage(String id, int delay) throws IOException { - releaseMessage(id, null, new Long(delay)); - } - /** * Release reserved message after specified time. If there is no message with such id on the queue, an * EmptyQueueException is thrown. @@ -520,18 +558,6 @@ public void releaseMessage(String id, String reservationId, Long delay) throws I reader.close(); } - /** - * Add subscribers to Queue. If there is no queue, an EmptyQueueException is thrown. - * @param subscribersList The array list of subscribers. - * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. - * @throws java.io.IOException If there is an error accessing the IronMQ server. - * @deprecated Use updateSubscribers instead - */ - @Deprecated - public void addSubscribersToQueue(ArrayList subscribersList) throws IOException { - this.updateSubscribers(subscribersList); - } - /** * Add subscribers to Queue. If there is no queue, an EmptyQueueException is thrown. * @param subscribersList The array list of subscribers. @@ -565,14 +591,33 @@ public void addSubscribers(Subscribers subscribers) throws IOException { } /** - * Add subscribers to Queue. If there is no queue, an EmptyQueueException is thrown. + * Update old or add new subscribers to Queue. If there is no queue, an EmptyQueueException is thrown. * @param subscribersList The array list of subscribers. * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. * @throws java.io.IOException If there is an error accessing the IronMQ server. */ - public QueueModel updateSubscribers(ArrayList subscribersList) throws IOException { - QueueModel payload = new QueueModel(new QueuePushModel(subscribersList)); - return this.update(payload); + public void updateSubscribers(ArrayList subscribersList) throws IOException { + addSubscribers(subscribersList); + } + + /** + * Update old or add new subscribers to Queue. If there is no queue, an EmptyQueueException is thrown. + * @param subscribers The array of subscribers. + * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws java.io.IOException If there is an error accessing the IronMQ server. + */ + public void updateSubscribers(Subscriber[] subscribers) throws IOException { + addSubscribers(subscribers); + } + + /** + * Update old or add new subscribers to Queue. If there is no queue, an EmptyQueueException is thrown. + * @param subscribers The array of subscribers. + * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws java.io.IOException If there is an error accessing the IronMQ server. + */ + public void updateSubscribers(Subscribers subscribers) throws IOException { + addSubscribers(subscribers); } /** @@ -610,22 +655,6 @@ public void replaceSubscribers(Subscribers subscribers) throws IOException { reader.close(); } - - /** - * Remove subscribers from Queue. If there is no queue, an EmptyQueueException is thrown. - * @param subscribersList The array list of subscribers. - * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. - * @throws java.io.IOException If there is an error accessing the IronMQ server. - */ - public void removeSubscribersFromQueue(ArrayList subscribersList) throws IOException { - String url = "queues/" + name + "/subscribers"; - Subscribers subscribers = new Subscribers(subscribersList); - Gson gson = new Gson(); - String jsonMessages = gson.toJson(subscribers); - IronReader reader = client.delete(url, jsonMessages); - reader.close(); - } - /** * Remove subscribers from Queue. If there is no queue, an EmptyQueueException is thrown. * @param subscribersList The array list of subscribers. @@ -677,14 +706,13 @@ public SubscribersInfo getPushStatusForMessage(String messageId) throws IOExcept /** * Delete push message for subscriber by subscriber ID and message ID. If there is no message or subscriber, * an EmptyQueueException is thrown. - * @param subscriberId The Subscriber ID. + * @param subscriberName The name of Subscriber. * @param messageId The Message ID. * @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK. * @throws java.io.IOException If there is an error accessing the IronMQ server. */ - public void deletePushMessageForSubscriber(String messageId, String subscriberId) throws IOException { - IronReader reader = client.delete("queues/" + name + "/messages/" + messageId + "/subscribers/" + subscriberId); - reader.close(); + public void deletePushMessageForSubscriber(String messageId, String reservationId, String subscriberName) throws IOException { + deleteMessage(messageId, reservationId, subscriberName); } /** diff --git a/src/main/java/io/iron/ironmq/SubscribedMessageOptions.java b/src/main/java/io/iron/ironmq/SubscribedMessageOptions.java new file mode 100644 index 0000000..015689e --- /dev/null +++ b/src/main/java/io/iron/ironmq/SubscribedMessageOptions.java @@ -0,0 +1,45 @@ +package io.iron.ironmq; + +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; + +/** + * The Message class represents a message retrieved from an IronMQ queue. + */ +public class SubscribedMessageOptions extends MessageOptions { + @SerializedName("subscriber_name") + protected String subscriberName; + + public SubscribedMessageOptions() { + } + + public SubscribedMessageOptions(String reservationId, String subscriberName) { + super(reservationId); + this.subscriberName = subscriberName; + } + + public SubscribedMessageOptions(String id, String reservationId, String subscriberName) { + super(id, reservationId); + this.subscriberName = subscriberName; + } + + public SubscribedMessageOptions(String id, String reservationId, Long timeout, String subscriberName) { + super(id, reservationId, timeout); + this.subscriberName = subscriberName; + } + + /** + * Returns the name of Message's Subscriber. + */ + public String getSubscriberName() { + return subscriberName; + } + + /** + * Sets the name of Message's Subscriber. + */ + public void setSubscriberName(String subscriberName) { + this.subscriberName = subscriberName; + } +} diff --git a/src/test/java/io/iron/ironmq/IronMQLongRunningTest.java b/src/test/java/io/iron/ironmq/IronMQLongRunningTest.java new file mode 100644 index 0000000..f6ed55f --- /dev/null +++ b/src/test/java/io/iron/ironmq/IronMQLongRunningTest.java @@ -0,0 +1,118 @@ +package io.iron.ironmq; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import javax.sound.midi.VoiceStatus; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Date; +import java.util.Properties; + +public class IronMQLongRunningTest { + private String queueName = "java-testing-queue"; + private Client client; + private Queue queue; + + @Before + public void setUp() throws Exception { + client = new Client(null, null, null, 3, 1); + queue = new Queue(client, "my_queue_" + ts()); + } + + /** + * Test shows how to increase time of message reservation + * Expected that: + * - message will be available after 30 seconds (initial timeout is 30 seconds) + * @throws IOException + * @throws InterruptedException + */ + @Test + public void testReserveMessageWithTimeout() throws IOException, InterruptedException { + queue.push("Test message"); + Message reservedFirstTime = queue.reserve(1, 30).getMessage(0); + + Thread.sleep(32000); + Messages reservedAgain = queue.reserve(1); + Assert.assertEquals(1, reservedAgain.getSize()); + Assert.assertEquals(reservedAgain.getMessage(0).getId(), reservedFirstTime.getId()); + } + + /** + * Test shows how to increase time of message reservation + * Expected that: + * - message will not be available after 35 seconds (initial timeout is 30 seconds) + * @throws IOException + * @throws InterruptedException + */ + @Test + public void testTouchMessage() throws IOException, InterruptedException { + queue.push("Test message"); + Message message = queue.reserve(1, 30).getMessage(0); + + Thread.sleep(25000); + queue.touchMessage(message); + Thread.sleep(10000); + Assert.assertEquals(0, queue.reserve(1).getSize()); + } + + /** + * Test shows how to increase time of message reservation by specified amount of seconds + * Expected that: + * - message will be available only after specified amount of seconds. + * @throws IOException + * @throws InterruptedException + */ + @Test + public void testTouchMessageWithTimeout() throws IOException, InterruptedException { + queue.push("Test message"); + Message message = queue.reserve(1, 30).getMessage(0); + queue.touchMessage(message, 40); + + Thread.sleep(35000); + Assert.assertEquals(0, queue.reserve(1).getSize()); + Thread.sleep(10000); + Assert.assertEquals(1, queue.reserve(1).getSize()); + } + + /** + * Test shows how to touch a message multiple times. + * Expected that: + * - after second touch call message will have new reservation id + * - new reservation id will not equal to old one + * @throws IOException + * @throws InterruptedException + */ + @Test + public void testTouchMessageTwice() throws IOException, InterruptedException { + queue.push("Test message"); + Message message = queue.reserve(1, 5).getMessage(0); + + Thread.sleep(3500); + MessageOptions options1 = queue.touchMessage(message); + Thread.sleep(3500); + MessageOptions options2 = queue.touchMessage(message); + + Assert.assertFalse(options1.getReservationId().equals(options2.getReservationId())); + Assert.assertEquals(message.getReservationId(), options2.getReservationId()); + } + + private long ts() { + return new Date().getTime(); + } + + private Queue createQueueWithMessage(String queueName) throws IOException { + Queue queue = new Queue(client, queueName); + queue.push("Test message"); + return queue; + } + + private String repeatString(String s, int times) { + return new String(new char[times]).replace("\0", s); + } +} diff --git a/src/test/java/io/iron/ironmq/IronMQTest.java b/src/test/java/io/iron/ironmq/IronMQTest.java index a90a18f..f645de7 100644 --- a/src/test/java/io/iron/ironmq/IronMQTest.java +++ b/src/test/java/io/iron/ironmq/IronMQTest.java @@ -17,10 +17,12 @@ public class IronMQTest { private String queueName = "java-testing-queue"; private Client client; + private Queue queue; @Before public void setUp() throws Exception { client = new Client(null, null, null, 3, 1); + queue = new Queue(client, "my_queue_" + ts()); } /** @@ -42,7 +44,6 @@ public void testErrorResponse() throws IOException { */ @Test public void testPostMessage() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); String messageId = queue.push("Test message"); Assert.assertTrue(messageId.length() > 0); } @@ -54,7 +55,6 @@ public void testPostMessage() throws IOException { */ @Test public void testReserveMessageViaGet() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); String messageText = "Test message " + ts(); String messageId = queue.push(messageText); Message message = queue.get(); @@ -72,7 +72,6 @@ public void testReserveMessageViaGet() throws IOException { */ @Test(expected = EmptyQueueException.class) public void testReserveMessageFromEmptyQueue() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); queue.push(""); queue.clear(); Message message = queue.reserve(); @@ -85,7 +84,6 @@ public void testReserveMessageFromEmptyQueue() throws IOException { */ @Test public void testReserveMessagesFromEmptyQueue() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); queue.push(""); queue.clear(); Messages messages = queue.reserve(4); @@ -102,7 +100,6 @@ public void testReserveMessagesFromEmptyQueue() throws IOException { */ @Test public void testReserveMessage() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); String messageText = "Test message " + ts(); String messageId = queue.push(messageText); Message message = queue.reserve(); @@ -122,7 +119,6 @@ public void testReserveMessage() throws IOException { */ @Test public void testReserveMessages() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); String messageText = "Test message " + ts(); Ids ids = queue.pushMessages(new String[]{messageText + "0", messageText + "1", messageText + "2"}); Messages messages = queue.reserve(4); @@ -153,7 +149,6 @@ public void testReserveMessages() throws IOException { */ @Test public void testReserveMessageWithWait() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); queue.push("test"); queue.clear(); @@ -179,7 +174,6 @@ public void testReserveMessageWithWait() throws IOException { */ @Test public void testPeekMessage() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); String messageText = "Test message " + ts(); String messageId = queue.push(messageText); Message message = queue.peek(); @@ -205,7 +199,6 @@ public void testPeekMessage() throws IOException { */ @Test public void testGetMessageById() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); String messageText = "Test message " + ts(); String messageId = queue.push(messageText); Message message = queue.getMessageById(messageId); @@ -250,7 +243,6 @@ public void testDeleteReservedMessage() throws IOException { */ @Test(expected = HTTPException.class) public void testDeleteReservedMessageWithoutReservationId() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); queue.clear(); queue.push("Test message"); Message message = queue.reserve(); @@ -316,7 +308,6 @@ public void testDeleteReservedMessagesPartially() throws IOException { */ @Test(expected = HTTPException.class) public void testDeleteReservedMessagesWithoutReservationId() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); queue.clear(); queue.push("Test message 1"); queue.push("Test message 2"); @@ -326,48 +317,6 @@ public void testDeleteReservedMessagesWithoutReservationId() throws IOException queue.deleteMessages(messages); } - /** - * Test shows how to increase time of message reservation - * Expected that: - * - message will be available after 5 seconds (initial timeout is 5 seconds) - * @throws IOException - * @throws InterruptedException - */ - @Test - public void testTouchMessage() throws IOException, InterruptedException { - Queue queue = new Queue(client, "my_queue_" + ts()); - queue.push("Test message"); - Message message = queue.reserve(1, 5).getMessage(0); - - Thread.sleep(3500); - queue.touchMessage(message); - Thread.sleep(3500); - Assert.assertEquals(0, queue.reserve(1).getSize()); - } - - /** - * Test shows how to touch a message multiple times. - * Expected that: - * - after second touch call message will have new reservation id - * - new reservation id will not equal to old one - * @throws IOException - * @throws InterruptedException - */ - @Test - public void testTouchMessageTwice() throws IOException, InterruptedException { - Queue queue = new Queue(client, "my_queue_" + ts()); - queue.push("Test message"); - Message message = queue.reserve(1, 5).getMessage(0); - - Thread.sleep(3500); - MessageOptions options1 = queue.touchMessage(message); - Thread.sleep(3500); - MessageOptions options2 = queue.touchMessage(message); - - Assert.assertFalse(options1.getReservationId().equals(options2.getReservationId())); - Assert.assertEquals(message.getReservationId(), options2.getReservationId()); - } - /** * This test shows how to release a message back to queue if, for example, it could not be processed * Expected that: @@ -377,9 +326,8 @@ public void testTouchMessageTwice() throws IOException, InterruptedException { */ @Test public void testReleaseMessage() throws IOException, InterruptedException { - Queue queue = new Queue(client, "my_queue_" + ts()); queue.push("Test message"); - Message message = queue.reserve(1, 5).getMessage(0); + Message message = queue.reserve(1).getMessage(0); Thread.sleep(500); queue.releaseMessage(message); @@ -388,26 +336,6 @@ public void testReleaseMessage() throws IOException, InterruptedException { Assert.assertEquals(message.getId(), sameMessage.getId()); } - /** - * This test shows that a reserved message could not be released without a reservation id - * Note: - * - queue.releaseMessage(id, delay) has been deprecated - * - reserved message can't be touched without reservation id as well - * Expected: - * - HTTPException (403) Wrong reservation_id - * @throws IOException - * @throws InterruptedException - */ - @Test(expected = HTTPException.class) - public void testReleaseMessageWithoutReservationId() throws IOException, InterruptedException { - Queue queue = new Queue(client, "my_queue_" + ts()); - queue.push("Test message"); - Message message = queue.reserve(1, 5).getMessage(0); - - Thread.sleep(500); - queue.releaseMessage(message.getId(), 0); - } - /** * This test shows old way of listing queues. Don't use it. * @throws IOException @@ -532,7 +460,6 @@ public void testGetQueueInfo() throws IOException { */ @Test public void testClearQueue() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); queue.push("Some message"); Assert.assertTrue(queue.getInfoAboutQueue().getSize() > 0); queue.clear(); @@ -547,8 +474,7 @@ public void testClearQueue() throws IOException { */ @Test(expected = HTTPException.class) public void testGetInfoBeforeQueueCreated() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); - QueueModel info = queue.getInfoAboutQueue(); + queue.getInfoAboutQueue(); } /** @@ -646,9 +572,6 @@ public void testCreateQueueOverload3() throws IOException { */ @Test public void testUpdateQueue() throws IOException { - String name = "my_queue_" + ts(); - Queue queue = new Queue(client, name); - QueueModel payload = new QueueModel(); payload.setMessageTimeout(69); QueueModel info = queue.update(payload); @@ -657,16 +580,13 @@ public void testUpdateQueue() throws IOException { } /** - * This test shows how to update subscribers of a queue + * This test shows how to replace subscribers of a queue * Expected: * - old subscribers should be replaced by new subscribers * @throws IOException */ @Test - public void testUpdateQueueSubscribers() throws IOException { - String name = "my_queue_" + ts(); - Queue queue = new Queue(client, name); - + public void testReplaceQueueSubscribers() throws IOException { QueueModel payload = new QueueModel(); payload.addSubscriber(new Subscriber("http://localhost:3000", "test01")); payload.addSubscriber(new Subscriber("http://localhost:3030", "test02")); @@ -678,20 +598,48 @@ public void testUpdateQueueSubscribers() throws IOException { ArrayList subscribers = new ArrayList(); subscribers.add(new Subscriber("http://localhost:3000", "test04")); subscribers.add(new Subscriber("http://localhost:3030", "test05")); - QueueModel info2 = queue.updateSubscribers(subscribers); + queue.replaceSubscribers(subscribers); + QueueModel info2 = queue.getInfoAboutQueue(); Assert.assertEquals(2, info2.getPushInfo().getSubscribers().size()); } + /** + * This test shows how to update subscribers of a queue + * Expected: + * - old subscribers should be updated + * - one of 3 subscribers not affected by update should stay the same + * @throws IOException + */ + @Test + public void testUpdateQueueSubscribers() throws IOException { + QueueModel payload = new QueueModel(); + payload.addSubscriber(new Subscriber("http://localhost:3000", "test01")); + payload.addSubscriber(new Subscriber("http://localhost:3030", "test02")); + payload.addSubscriber(new Subscriber("http://localhost:3333", "test03")); + QueueModel info = queue.update(payload); + + Assert.assertEquals(3, info.getPushInfo().getSubscribers().size()); + + ArrayList subscribers = new ArrayList(); + subscribers.add(new Subscriber("http://localhost:3030", "test01")); + subscribers.add(new Subscriber("http://localhost:3030", "test03")); + queue.updateSubscribers(subscribers); + + QueueModel info2 = queue.getInfoAboutQueue(); + Assert.assertEquals(3, info2.getPushInfo().getSubscribers().size()); + for (int i = 0; i < info2.getSubscribers().size(); i++) { + Assert.assertEquals(info2.getSubscribers().get(i).getUrl(), "http://localhost:3030"); + } + } + /** * This test shows how to update parameters of a push queue * @throws IOException */ @Test public void testUpdateQueuePushParameters() throws IOException { - String name = "my_queue_" + ts(); final String url = "http://localhost:3000"; - Queue queue = new Queue(client, name); ArrayList subscribers = new ArrayList() {{ add(new Subscriber(url, "test")); }}; QueueModel payload = new QueueModel(new QueuePushModel(subscribers, 4, 7, "test_err")); @@ -714,8 +662,6 @@ public void testUpdateQueuePushParameters() throws IOException { */ @Test public void testUpdateQueueAlerts() throws IOException { - Queue queue = new Queue(client, "my_queue_" + ts()); - ArrayList alerts = new ArrayList(); alerts.add(new Alert(Alert.typeProgressive, Alert.directionAscending, 5, "some_q")); QueueModel info = queue.updateAlerts(alerts); @@ -736,11 +682,9 @@ public void testUpdateQueueAlerts() throws IOException { */ @Test(expected = HTTPException.class) public void testDeleteQueue() throws IOException, InterruptedException { - String queueName = "my_queue_" + ts(); - Queue queue = new Queue(client, queueName); queue.push("Some message"); queue.destroy(); - Queue sameQueue = new Queue(client, queueName); + Queue sameQueue = new Queue(client, queue.getName()); sameQueue.getInfoAboutQueue(); } @@ -751,9 +695,6 @@ public void testDeleteQueue() throws IOException, InterruptedException { */ @Test public void testAddSubscribers() throws IOException { - String name = "my_queue_" + ts(); - Queue queue = new Queue(client, name); - QueueModel payload = new QueueModel(); payload.addSubscriber(new Subscriber("http://localhost:3001", "test01")); queue.update(payload); @@ -777,9 +718,6 @@ public void testAddSubscribers() throws IOException { */ @Test public void testReplaceSubscribers() throws IOException { - String name = "my_queue_" + ts(); - Queue queue = new Queue(client, name); - QueueModel payload = new QueueModel(); payload.addSubscriber(new Subscriber("http://localhost:3001", "test01")); queue.update(payload); @@ -795,9 +733,6 @@ public void testReplaceSubscribers() throws IOException { @Test @Ignore // there is a bug in implementation of ironmq public void testRemoveSubscribers() throws IOException { - String name = "my_queue_" + ts(); - Queue queue = new Queue(client, name); - QueueModel payload = new QueueModel(); Subscriber[] subscribers = new Subscriber[]{ new Subscriber("http://localhost:3001", "test01"),