Skip to content

Commit

Permalink
Merge pull request #142 from flipkart-incubator/bugFix
Browse files Browse the repository at this point in the history
bug fix while removing object from queue
  • Loading branch information
anirudhramanan authored Mar 6, 2018
2 parents 6b5ab6d + 3676670 commit 0b5c046
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.flipkart.batching.tape;

import com.flipkart.batching.toolbox.LogUtil;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -27,6 +29,7 @@
* @param <T> The type of elements in the queue.
*/
public class InMemoryObjectQueue<T> implements ObjectQueue<T> {
private static final String TAG = "InMemoryObjectQueue";
private final Queue<T> tasks;
private Listener<T> listener;

Expand All @@ -53,8 +56,10 @@ public int size() {

@Override
public void remove() {
tasks.remove();
if (listener != null) listener.onRemove(this);
removeFromQueue();
if (listener != null) {
listener.onRemove(this);
}
}

@Override
Expand All @@ -64,6 +69,15 @@ public void remove(int n) {
}
}

private void removeFromQueue() {
if (tasks.isEmpty()) {
LogUtil.log(TAG, "The queue is empty");
return;
}

tasks.remove();
}

@Override
public void close() {
tasks.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.flipkart.batching.tape;

import com.flipkart.batching.BuildConfig;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;

/**
* Test for {@link InMemoryObjectQueue}
*/

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class InMemoryObjectQueueTest {

/**
* Test for {@link InMemoryObjectQueue#remove()} )
*/
@Test
public void testRemove() {
InMemoryObjectQueue<String> inMemoryObjectQueue = new InMemoryObjectQueue<>();

ArrayList<String> arrayList = new ArrayList<>(4);
arrayList.add("test1");
arrayList.add("test2");
arrayList.add("test3");
arrayList.add("test4");

for (String sample : arrayList) {
inMemoryObjectQueue.add(sample);
}

//remove all the 4 elements added to the queue
inMemoryObjectQueue.remove(arrayList.size());

//try removing another element from the queue.
inMemoryObjectQueue.remove();
}
}

0 comments on commit 0b5c046

Please sign in to comment.