Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding batch upper limit and unit test #4

Open
wants to merge 1 commit into
base: kafka_0.8.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/org/apache/flume/source/kafka/KafkaSource08.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class KafkaSource08 extends AbstractSource implements Configurable, Polla
private ConsumerConnector consumer;
private ConsumerIterator<byte[], byte[]> it;
private String topic;
private int batchUpperLimit = 1;

public Status process() throws EventDeliveryException {
List<Event> eventList = new ArrayList<Event>();
Expand All @@ -62,7 +63,8 @@ public Status process() throws EventDeliveryException {
Map<String, String> headers;
// byte [] bytes;
try {
if(it.hasNext()) {
int eventCounter = 0;
while (it.hasNext() && eventCounter++ <= batchUpperLimit) {
bytes = it.next().message();
event = new SimpleEvent();
headers = new HashMap<String, String>();
Expand All @@ -81,6 +83,8 @@ public Status process() throws EventDeliveryException {
}

public void configure(Context context) {

batchUpperLimit = context.getInteger("batch.upper.limit", 1);
topic = context.getString("topic");
if(topic == null) {
throw new ConfigurationException("Kafka topic must be specified.");
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/apache/flume/source/kafka/KafkaSourceTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ public void testProcessItNotEmpty() throws EventDeliveryException, SecurityExcep
when(mockIt.next()).thenReturn(mockMessageAndMetadata);
assertEquals(Status.READY, status);
}

@SuppressWarnings("unchecked")
@Test
public void testProcessItNotEmptyBatch() throws EventDeliveryException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {

Field field = KafkaSource08.class.getDeclaredField("batchUpperLimit");
field.setAccessible(true);
field.set(mockKafkaSource, 2);

when(mockIt.next()).thenReturn(mockMessageAndMetadata);
when(mockIt.hasNext()).thenReturn(true);
when(mockIt.next()).thenReturn(mockMessageAndMetadata);
when(mockIt.hasNext()).thenReturn(true);
Status status = mockKafkaSource.process();
verify(mockIt, times(2)).hasNext();
verify(mockIt, times(2)).next();
verify(mockChannelProcessor, times(1)).processEventBatch(anyList());
when(mockIt.next()).thenReturn(mockMessageAndMetadata);
assertEquals(Status.READY, status);
}

@SuppressWarnings("unchecked")
@Test
Expand All @@ -120,4 +140,6 @@ public void testProcessException() throws EventDeliveryException, SecurityExcept
verify(mockChannelProcessor, times(0)).processEventBatch(anyList());
assertEquals(Status.BACKOFF, status);
}


}