Permalink
Browse files

Add Queue.size method

Also close some unclosed Readers and fix some whitespace.
  • Loading branch information...
1 parent b09469f commit d3234582f1efbfb54a8d7edf3832ff6d3f90c443 Evan Shaw committed Oct 4, 2012
Showing with 27 additions and 13 deletions.
  1. +22 −5 src/main/java/io/iron/ironmq/Queue.java
  2. +5 −8 src/test/java/io/iron/ironmq/IronMQTest.java
@@ -2,6 +2,7 @@
import java.io.IOException;
import java.io.Reader;
+import java.io.Serializable;
import com.google.gson.Gson;
@@ -59,12 +60,14 @@ public Messages get(int numberOfMessages) throws IOException {
* @throws IOException If there is an error accessing the IronMQ server.
*/
public Messages get(int numberOfMessages, int timeout) throws IOException {
- if (numberOfMessages < 0 || numberOfMessages > 100) {
- throw new IllegalArgumentException("numberOfMessages has to be within 1..100");
- }
+ if (numberOfMessages < 0 || numberOfMessages > 100) {
+ throw new IllegalArgumentException("numberOfMessages has to be within 1..100");
+ }
Reader reader = client.get("queues/" + name + "/messages?n="+numberOfMessages+"&timeout=" + timeout);
Gson gson = new Gson();
- return gson.fromJson(reader, Messages.class);
+ Messages messages = gson.fromJson(reader, Messages.class);
+ reader.close();
+ return messages;
}
/**
@@ -158,6 +161,7 @@ public String push(String msg, long timeout, long delay, long expiresIn) throws
Reader reader = client.post("queues/" + name + "/messages", body);
Ids ids = gson.fromJson(reader, Ids.class);
+ reader.close();
return ids.getId(0);
}
@@ -167,6 +171,19 @@ public String push(String msg, long timeout, long delay, long expiresIn) throws
* @throws IOException
*/
public void clear() throws IOException {
- client.post("queues/"+name+"/clear", "");
+ client.post("queues/"+name+"/clear", "").close();
+ }
+
+ static class Info implements Serializable {
+ int count;
+ int size;
+ }
+
+ public int getSize() throws IOException {
+ Reader reader = client.get("queues/"+name);
+ Gson gson = new Gson();
+ Info info = gson.fromJson(reader, Info.class);
+ reader.close();
+ return info.size;
}
}
@@ -21,19 +21,16 @@
Client c = new Client(projectId, token, Cloud.ironAWSUSEast);
Queue q = c.queue("test-queue");
- // clear out the queue
- try {
- while (true) {
- Message msg = q.get();
- q.deleteMessage(msg);
- }
- } catch (EmptyQueueException e) {
- }
+ q.clear();
+
+ Assert.assertEquals(0, q.getSize());
final String body = "Hello, IronMQ!";
String id = q.push(body);
+ Assert.assertEquals(1, q.getSize());
+
Message msg = q.get();
Assert.assertEquals(body, msg.getBody());
Assert.assertEquals(id, msg.getId());

0 comments on commit d323458

Please sign in to comment.