Search in sources :

Example 1 with DatagramSender

use of org.apache.nifi.processors.standard.TestListenSyslog.DatagramSender in project nifi by apache.

the class ITListenSyslog method testUDP.

@Test
public void testUDP() throws IOException, InterruptedException {
    final ListenSyslog proc = new ListenSyslog();
    final TestRunner runner = TestRunners.newTestRunner(proc);
    runner.setProperty(ListenSyslog.PROTOCOL, ListenSyslog.UDP_VALUE.getValue());
    runner.setProperty(ListenSyslog.PORT, "0");
    // schedule to start listening on a random port
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    final ProcessContext context = runner.getProcessContext();
    proc.onScheduled(context);
    final int numMessages = 20;
    final int port = proc.getPort();
    Assert.assertTrue(port > 0);
    // write some UDP messages to the port in the background
    final Thread sender = new Thread(new DatagramSender(port, numMessages, 10, VALID_MESSAGE));
    sender.setDaemon(true);
    sender.start();
    // call onTrigger until we read all datagrams, or 30 seconds passed
    try {
        int numTransferred = 0;
        long timeout = System.currentTimeMillis() + 30000;
        while (numTransferred < numMessages && System.currentTimeMillis() < timeout) {
            Thread.sleep(10);
            proc.onTrigger(context, processSessionFactory);
            numTransferred = runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS).size();
        }
        Assert.assertEquals("Did not process all the datagrams", numMessages, numTransferred);
        MockFlowFile flowFile = runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS).get(0);
        checkFlowFile(flowFile, 0, ListenSyslog.UDP_VALUE.getValue());
        final List<ProvenanceEventRecord> events = runner.getProvenanceEvents();
        Assert.assertNotNull(events);
        Assert.assertEquals(numMessages, events.size());
        final ProvenanceEventRecord event = events.get(0);
        Assert.assertEquals(ProvenanceEventType.RECEIVE, event.getEventType());
        Assert.assertTrue("transit uri must be set and start with proper protocol", event.getTransitUri().toLowerCase().startsWith("udp"));
    } finally {
        // unschedule to close connections
        proc.onUnscheduled();
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) ProcessContext(org.apache.nifi.processor.ProcessContext) DatagramSender(org.apache.nifi.processors.standard.TestListenSyslog.DatagramSender) Test(org.junit.Test)

Aggregations

ProcessContext (org.apache.nifi.processor.ProcessContext)1 ProcessSessionFactory (org.apache.nifi.processor.ProcessSessionFactory)1 DatagramSender (org.apache.nifi.processors.standard.TestListenSyslog.DatagramSender)1 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)1 MockFlowFile (org.apache.nifi.util.MockFlowFile)1 TestRunner (org.apache.nifi.util.TestRunner)1 Test (org.junit.Test)1