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.net.SocketException;
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_counter_increment_to_statsd() throws Exception {
        final DummyStatsDServer server = new DummyStatsDServer(STATSD_SERVER_PORT);
        
        client.incrementCounter("blah");
        server.waitForMessage();
        
        assertThat(server.messagesReceived(), contains("my.prefix.blah:1|c"));
    }

    private static final class DummyStatsDServer {
        private final List<String> messagesReceived = new ArrayList<String>();
        private final DatagramSocket server;

        public DummyStatsDServer(int port) throws SocketException {
            server = new DatagramSocket(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();
        }
        
        public void waitForMessage() {
            while (messagesReceived.isEmpty()) {
                try {
                    Thread.sleep(50L);
                } catch (InterruptedException e) {}}
        }
        
        public List<String> messagesReceived() {
            return new ArrayList<String>(messagesReceived);
        }
    }
}
@@ -820,20 +820,21 @@
 ounter(%22
-blah
+myinc
 %22);%0A    
@@ -941,16 +941,737 @@
 fix.
-blah:1%7Cc
+myinc:1%7Cc%22));%0A    %7D%0A%0A    @Test(timeout=5000L) public void%0A    sends_gauge_to_statsd() throws Exception %7B%0A        final DummyStatsDServer server = new DummyStatsDServer(STATSD_SERVER_PORT);%0A        %0A        client.recordGaugeValue(%22mygauge%22, 423);%0A        server.waitForMessage();%0A        %0A        assertThat(server.messagesReceived(), contains(%22my.prefix.mygauge:423%7Cg%22));%0A    %7D%0A%0A    @Test(timeout=5000L) public void%0A    sends_timer_to_statsd() throws Exception %7B%0A        final DummyStatsDServer server = new DummyStatsDServer(STATSD_SERVER_PORT);%0A        %0A        client.recordExecutionTime(%22mytime%22, 123);%0A        server.waitForMessage();%0A        %0A        assertThat(server.messagesReceived(), contains(%22my.prefix.mytime:123%7Cms
 %22));

add tests for timers and gauges