package com.timgroup.statsd;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Test;

public class StatsDClientTest {

    private static final int STATSD_SERVER_PORT = 17254;
    private final StatsDClient client = new StatsDClient("my.prefix", "localhost", STATSD_SERVER_PORT);

    @After
    public void stop() throws Exception {
        client.stop();
    }

    @Test(timeout=5000L) public void
    sends_records_to_statsd() throws Exception {
        final List<String> messagesReceived = new ArrayList<String>();
        final DatagramSocket server = new DatagramSocket(STATSD_SERVER_PORT);
        
        new Thread(new Runnable() {
            @Override public void run() {
                try {
                    final DatagramPacket packet = new DatagramPacket(new byte[256], 256);
                    server.receive(packet);
                    messagesReceived.add(new String(packet.getData()).trim());
                    server.close();
                } catch (Exception e) { }
            }
        }).start();
        
        client.incrementCounter("blah");
        while (messagesReceived.isEmpty()) { Thread.sleep(50L); }
        
        assertThat(messagesReceived, contains("my.prefix.blah:1|c"));
    }
}
@@ -183,24 +183,57 @@
 gramSocket;%0A
+import java.net.SocketException;%0A
 import java.
@@ -649,15 +649,25 @@
 nds_
-records
+counter_increment
 _to_
@@ -708,16 +708,337 @@
   final 
+DummyStatsDServer server = new DummyStatsDServer(STATSD_SERVER_PORT);%0A        %0A        client.incrementCounter(%22blah%22);%0A        server.waitForMessage();%0A        %0A        assertThat(server.messagesReceived(), contains(%22my.prefix.blah:1%7Cc%22));%0A    %7D%0A%0A    private static final class DummyStatsDServer %7B%0A        private final 
 List%3CStr
@@ -1086,32 +1086,40 @@
 ing%3E();%0A        
+private 
 final DatagramSo
@@ -1123,16 +1123,105 @@
 mSocket 
+server;%0A%0A        public DummyStatsDServer(int port) throws SocketException %7B%0A            
 server =
@@ -1240,34 +1240,20 @@
 mSocket(
-STATSD_SERVER_PORT
+port
 );%0A     
@@ -1251,29 +1251,24 @@
 t);%0A        
-%0A    
     new Thre
@@ -1283,24 +1283,28 @@
 unnable() %7B%0A
+    
             
@@ -1329,24 +1329,28 @@
 oid run() %7B%0A
+    
             
@@ -1375,24 +1375,28 @@
             
+    
 final Datagr
@@ -1469,24 +1469,28 @@
             
+    
 server.recei
@@ -1497,24 +1497,28 @@
 ve(packet);%0A
+    
             
@@ -1600,24 +1600,28 @@
             
+    
 server.close
@@ -1620,24 +1620,28 @@
 er.close();%0A
+    
             
@@ -1670,16 +1670,20 @@
  e) %7B %7D%0A
+    
         
@@ -1696,16 +1696,20 @@
         
+    
 %7D).start
@@ -1712,32 +1712,33 @@
 tart();%0A        
+%7D
 %0A        client.
@@ -1734,40 +1734,47 @@
     
-client.incrementCounter(%22blah%22);
+%0A        public void waitForMessage() %7B
 %0A   
@@ -1778,16 +1778,20 @@
         
+    
 while (m
@@ -1822,108 +1822,260 @@
 )) %7B
- Thread.sleep(50L); %7D%0A        %0A        assertThat(messagesReceived, contains(%22my.prefix.blah:1%7Cc%22));
+%0A                try %7B%0A                    Thread.sleep(50L);%0A                %7D catch (InterruptedException e) %7B%7D%7D%0A        %7D%0A        %0A        public List%3CString%3E messagesReceived() %7B%0A            return new ArrayList%3CString%3E(messagesReceived);%0A        %7D
 %0A   

refactor test