1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.datacollection.adaptor;
19
20 import junit.framework.TestCase;
21 import org.apache.hadoop.chukwa.datacollection.ChunkReceiver;
22 import org.apache.hadoop.chukwa.datacollection.agent.AdaptorManager;
23 import org.apache.hadoop.chukwa.*;
24 import java.net.*;
25
26
27 public class TestUDPAdaptor extends TestCase implements ChunkReceiver {
28 volatile boolean receivedOK = false;
29 String STR = "a short string";
30
31 public void testUDP() throws Exception {
32 UDPAdaptor u = new UDPAdaptor();
33 u.parseArgs("Test", "0", AdaptorManager.NULL);
34 u.start("id", "Test", 0, this);
35
36 DatagramSocket send = new DatagramSocket();
37 byte[] buf = STR.getBytes();
38 DatagramPacket p = new DatagramPacket(buf, buf.length);
39 p.setSocketAddress(new InetSocketAddress("127.0.0.1",u.portno));
40 send.send(p);
41
42 synchronized(this) {
43 wait(1000);
44 }
45 assertTrue(receivedOK);
46 }
47
48 public void add(Chunk c) {
49 assertTrue(c.getDataType().equals("Test"));
50 assertEquals(c.getSeqID(), c.getData().length);
51 assertTrue(STR.equals(new String(c.getData())));
52 receivedOK= true;
53 synchronized(this) {
54 notify();
55 }
56 }
57
58 }