package com.cloudera.flume.handlers.avro;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import com.cloudera.flume.ExampleData;
import com.cloudera.flume.conf.FlumeConfiguration;
import com.cloudera.flume.core.EventImpl;
import com.cloudera.flume.core.EventSource;
import com.cloudera.flume.core.EventUtil;
import com.cloudera.flume.handlers.debug.MemorySinkSource;
import com.cloudera.flume.handlers.debug.NoNlASCIISynthSource;
import com.cloudera.flume.reporter.ReportEvent;
import com.cloudera.flume.reporter.aggregator.CounterSink;
import com.cloudera.util.NetUtils;
public static Logger LOG = Logger.getLogger(TestAvroSinks.class);
@Before
Logger.getLogger(TestAvroSinks.class).setLevel(Level.DEBUG);
Logger.getLogger(AvroEventSource.class).setLevel(Level.DEBUG);
}
@Before
NetUtils.setLocalhost("host");
}
@Test
EventSource txt = new NoNlASCIISynthSource(25, 100);
txt.open();
MemorySinkSource mem = new MemorySinkSource();
mem.open();
EventUtil.dumpAll(txt, mem);
txt.close();
FlumeConfiguration conf = FlumeConfiguration.get();
final AvroEventSource tes = new AvroEventSource(conf.getCollectorPort() + 1);
tes.open();
final CounterSink cnt = new CounterSink("count");
cnt.open();
Thread t = new Thread("drain") {
try {
EventUtil.dumpAll(tes, cnt);
} catch (IOException e) {
}
}
};
t.start();
AvroEventSink snk = new AvroEventSink("0.0.0.0",
conf.getCollectorPort() + 1);
snk.open();
EventUtil.dumpAll(mem, snk);
mem.close();
snk.close();
try {
Thread.sleep(1000);
t.interrupt();
} catch (InterruptedException e) {
}
tes.close();
assertEquals(25, cnt.getCount());
ReportEvent rpt = tes.getReport();
assertEquals(2500, rpt.getLongMetric(AvroEventSource.A_BYTES_IN)
.longValue());
assertEquals(25, rpt.getLongMetric(AvroEventSource.A_DEQUEUED).longValue());
assertEquals(25, rpt.getLongMetric(AvroEventSource.A_ENQUEUED).longValue());
assertEquals(0, rpt.getLongMetric(AvroEventSource.A_QUEUE_CAPACITY)
.intValue());
assertEquals(1000, rpt.getLongMetric(AvroEventSource.A_QUEUE_FREE)
.intValue());
}
@Test
InterruptedException {
final int threads = 10;
final FlumeConfiguration conf = FlumeConfiguration.get();
final AvroEventSource tes = new AvroEventSource(conf.getCollectorPort() + 1);
tes.open();
final CounterSink cnt = new CounterSink("count");
cnt.open();
Thread t = new Thread("drain") {
try {
EventUtil.dumpAll(tes, cnt);
} catch (IOException e) {
}
}
};
t.start();
final CountDownLatch sendStarted = new CountDownLatch(threads);
final CountDownLatch sendDone = new CountDownLatch(threads);
final AtomicLong sendByteSum = new AtomicLong(0);
for (int i = 0; i < threads; i++) {
final int id = i;
Thread th = new Thread() {
try {
EventSource txt = new NoNlASCIISynthSource(25, 100);
txt.open();
MemorySinkSource mem = new MemorySinkSource();
mem.open();
EventUtil.dumpAll(txt, mem);
txt.close();
AvroEventSink snk = new AvroEventSink("0.0.0.0", conf
.getCollectorPort() + 1);
snk.open();
sendStarted.countDown();
sendStarted.await();
EventUtil.dumpAll(mem, snk);
mem.close();
snk.close();
sendByteSum.addAndGet(snk.sentBytes.get());
LOG.info("sink " + id + " sent " + snk.sentBytes + " bytes");
sendDone.countDown();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
th.start();
}
sendDone.await();
Thread.sleep(1000);
tes.close();
assertEquals(25 * threads, cnt.getCount());
ReportEvent rpt = tes.getReport();
assertEquals(2500 * threads, sendByteSum.get());
assertEquals(2500 * threads, rpt.getLongMetric(AvroEventSource.A_BYTES_IN)
.longValue());
assertEquals(25 * threads, rpt.getLongMetric(AvroEventSource.A_DEQUEUED)
.longValue());
assertEquals(25 * threads, rpt.getLongMetric(AvroEventSource.A_ENQUEUED)
.longValue());
assertEquals(0, rpt.getLongMetric(AvroEventSource.A_QUEUE_CAPACITY)
.longValue());
assertEquals(1000, rpt.getLongMetric(AvroEventSource.A_QUEUE_FREE)
.longValue());
}
@Test
final FlumeConfiguration conf = FlumeConfiguration.get();
final AvroEventSource tes = new AvroEventSource(conf.getCollectorPort() + 1);
tes.open();
tes.enqueue(new EventImpl(new byte[0]));
tes.close();
}
}