/*
 * Copyright 2019 The gRPC Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.grpc.services;

import static com.google.common.truth.Truth.assertThat;

import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link BinlogHelper}. */
@RunWith(JUnit4.class)
public class CallMetricRecorderTest {

  private final CallMetricRecorder recorder = new CallMetricRecorder();

  @Test
  public void dumpGivesEmptyResultWhenNoSavedMetricValues() {
    assertThat(recorder.finalizeAndDump()).isEmpty();
  }

  @Test
  public void dumpDumpsAllSavedMetricValues() {
    recorder.recordCallMetric("ssd", 154353.423);
    recorder.recordCallMetric("cpu", 0.1367);
    recorder.recordCallMetric("mem", 1437.34);

    Map<String, Double> dump = recorder.finalizeAndDump();
    assertThat(dump)
        .containsExactly("ssd", 154353.423, "cpu", 0.1367, "mem", 1437.34);
  }

  @Test
  public void noMetricsRecordedAfterSnapshot() {
    Map<String, Double> initDump = recorder.finalizeAndDump();
    recorder.recordCallMetric("cpu", 154353.423);
    assertThat(recorder.finalizeAndDump()).isEqualTo(initDump);
  }
}

@@ -1180,19 +1180,21 @@
 Metric(%22
-ssd
+cost1
 %22, 15435
@@ -1233,18 +1233,20 @@
 etric(%22c
-pu
+ost2
 %22, 0.136
@@ -1280,19 +1280,21 @@
 Metric(%22
-mem
+cost3
 %22, 1437.
@@ -1409,11 +1409,13 @@
 ly(%22
-ssd
+cost1
 %22, 1
@@ -1427,18 +1427,20 @@
 .423, %22c
-pu
+ost2
 %22, 0.136
@@ -1447,11 +1447,13 @@
 7, %22
-mem
+cost3
 %22, 1
@@ -1618,18 +1618,19 @@
 etric(%22c
-pu
+ost
 %22, 15435
@@ -1704,11 +1704,464 @@
 mp);%0A  %7D
+%0A%0A  @Test%0A  public void lastValueWinForMetricsWithSameName() %7B%0A    recorder.recordCallMetric(%22cost1%22, 3412.5435);%0A    recorder.recordCallMetric(%22cost2%22, 6441.341);%0A    recorder.recordCallMetric(%22cost1%22, 6441.341);%0A    recorder.recordCallMetric(%22cost1%22, 4654.67);%0A    recorder.recordCallMetric(%22cost2%22, 75.83);%0A    Map%3CString, Double%3E dump = recorder.finalizeAndDump();%0A    assertThat(dump)%0A        .containsExactly(%22cost1%22, 4654.67, %22cost2%22, 75.83);%0A  %7D
 %0A%7D%0A

add a missing test case for CallMetricRecorder and polish test data. (#6019)