View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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  }