package com.librato.metrics;

import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

public class DeltaTrackerTest {
    DeltaTracker converter;

    @Before
    public void setUp() throws Exception {
        converter = new DeltaTracker();
    }

    @Test
    public void testAssumesInitialValueOfZero() throws Exception {
        assertThat(converter.getDelta("foo", 5), is(5L));
    }

    @Test
    public void testIncrementsReported() throws Exception {
        converter.getDelta("foo", 1);
        assertThat(converter.getDelta("foo", 2), equalTo(1L));
        assertThat(converter.getDelta("foo", 3), equalTo(1L));
        assertThat(converter.getDelta("foo", 4), equalTo(1L));
    }

    @Test
    public void testReportsZeroWhenNoChanges() throws Exception {
        converter.getDelta("foo", 1);
        assertThat(converter.getDelta("foo", 1), equalTo(0L));
        assertThat(converter.getDelta("foo", 1), equalTo(0L));
    }

    @Test
    public void testDoesNotReportNegativeIncrements() throws Exception {
        assertThat(converter.getDelta("foo", 1), is(1L));
        assertThat(converter.getDelta("foo", 0), is(0L));
        assertThat(converter.getDelta("foo", 5), equalTo(5L));
    }

    @Test
    public void testTracksMultipleCounters() throws Exception {
        converter.getDelta("foo", 1);
        assertThat(converter.getDelta("foo", 2), equalTo(1L));
        converter.getDelta("bar", 5);
        assertThat(converter.getDelta("bar", 10), equalTo(5L));
    }
}

@@ -1566,11 +1566,847 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testPeekWorks() throws Exception %7B%0A        converter.getDelta(%22foo%22, 1); // sets the value of 1 for the metric count%0A        assertThat(converter.peekDelta(%22foo%22, 1), equalTo(0L));%0A        assertThat(converter.peekDelta(%22foo%22, 1), equalTo(0L));%0A        assertThat(converter.peekDelta(%22foo%22, 2), equalTo(1L));%0A        assertThat(converter.peekDelta(%22foo%22, 2), equalTo(1L));%0A        // now update it%0A        assertThat(converter.getDelta(%22foo%22, 2), equalTo(1L));%0A        assertThat(converter.getDelta(%22foo%22, 2), equalTo(0L));%0A        // now peek it%0A        assertThat(converter.peekDelta(%22foo%22, 2), equalTo(0L));%0A        assertThat(converter.peekDelta(%22foo%22, 2), equalTo(0L));%0A        assertThat(converter.peekDelta(%22foo%22, 3), equalTo(1L));%0A        assertThat(converter.peekDelta(%22foo%22, 3), equalTo(1L));%0A    %7D
 %0A%7D%0A

Add test for delta tracker / peek / idle detection