<commit_before>/*
 * Copyright 2019 <NAME>, <NAME>
 *
 * 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.github.resilience4j.micrometer.tagged;

import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

import static io.github.resilience4j.micrometer.tagged.MetricsTestHelper.findMeterByNamesTag;
import static io.github.resilience4j.micrometer.tagged.RateLimiterMetricNames.DEFAULT_AVAILABLE_PERMISSIONS_METRIC_NAME;
import static io.github.resilience4j.micrometer.tagged.RateLimiterMetricNames.DEFAULT_WAITING_THREADS_METRIC_NAME;
import static org.assertj.core.api.Assertions.assertThat;

public class TaggedRateLimiterMetricsTest {

    private MeterRegistry meterRegistry;
    private RateLimiter rateLimiter;
    private RateLimiterRegistry rateLimiterRegistry;
    private TaggedRateLimiterMetrics taggedRateLimiterMetrics;

    @Before
    public void setUp() {
        meterRegistry = new SimpleMeterRegistry();
        rateLimiterRegistry = RateLimiterRegistry.ofDefaults();

        rateLimiter = rateLimiterRegistry.rateLimiter("backendA");
        taggedRateLimiterMetrics = TaggedRateLimiterMetrics
            .ofRateLimiterRegistry(rateLimiterRegistry);
        taggedRateLimiterMetrics.bindTo(meterRegistry);
    }

    @Test
    public void shouldAddMetricsForANewlyCreatedRateLimiter() {
        RateLimiter newRateLimiter = rateLimiterRegistry.rateLimiter("backendB");

        assertThat(taggedRateLimiterMetrics.meterIdMap).containsKeys("backendA", "backendB");
        assertThat(taggedRateLimiterMetrics.meterIdMap.get("backendA")).hasSize(2);
        assertThat(taggedRateLimiterMetrics.meterIdMap.get("backendB")).hasSize(2);

        List<Meter> meters = meterRegistry.getMeters();
        assertThat(meters).hasSize(4);

        Collection<Gauge> gauges = meterRegistry.get(DEFAULT_AVAILABLE_PERMISSIONS_METRIC_NAME)
            .gauges();

        Optional<Gauge> successful = findMeterByNamesTag(gauges, newRateLimiter.getName());
        assertThat(successful).isPresent();
        assertThat(successful.get().value())
            .isEqualTo(newRateLimiter.getMetrics().getAvailablePermissions());
    }

    @Test
    public void shouldAddCustomTags() {
        assertThat(taggedRateLimiterMetrics.meterIdMap).containsKeys("backendA", "backendF");
        assertThat(taggedRateLimiterMetrics.meterIdMap.get("backendA")).hasSize(2);
        assertThat(taggedRateLimiterMetrics.meterIdMap.get("backendF")).hasSize(2);
        List<Meter> meters = meterRegistry.getMeters();
        assertThat(meters).hasSize(4);
        assertThat(meterRegistry.get(DEFAULT_AVAILABLE_PERMISSIONS_METRIC_NAME).tag("key1", "value1")).isNotNull();
    }

    @Test
    public void shouldRemovedMetricsForRemovedRetry() {
        List<Meter> meters = meterRegistry.getMeters();
        assertThat(meters).hasSize(2);

        assertThat(taggedRateLimiterMetrics.meterIdMap).containsKeys("backendA");
        rateLimiterRegistry.remove("backendA");

        assertThat(taggedRateLimiterMetrics.meterIdMap).isEmpty();

        meters = meterRegistry.getMeters();
        assertThat(meters).isEmpty();<commit_msg>Fixed TaggedRateLimiterMetricsTest<commit_after>/*
 * Copyright 2019 <NAME>, <NAME>
 *
 * 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.github.resilience4j.micrometer.tagged;

import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

import static io.github.resilience4j.micrometer.tagged.MetricsTestHelper.findMeterByNamesTag;
import static io.github.resilience4j.micrometer.tagged.RateLimiterMetricNames.DEFAULT_AVAILABLE_PERMISSIONS_METRIC_NAME;
import static io.github.resilience4j.micrometer.tagged.RateLimiterMetricNames.DEFAULT_WAITING_THREADS_METRIC_NAME;
import static org.assertj.core.api.Assertions.assertThat;

public class TaggedRateLimiterMetricsTest {

    private MeterRegistry meterRegistry;
    private RateLimiter rateLimiter;
    private RateLimiterRegistry rateLimiterRegistry;
    private TaggedRateLimiterMetrics taggedRateLimiterMetrics;

    @Before
    public void setUp() {
        meterRegistry = new SimpleMeterRegistry();
        rateLimiterRegistry = RateLimiterRegistry.ofDefaults();

        rateLimiter = rateLimiterRegistry.rateLimiter("backendA");
        taggedRateLimiterMetrics = TaggedRateLimiterMetrics
            .ofRateLimiterRegistry(rateLimiterRegistry);
        taggedRateLimiterMetrics.bindTo(meterRegistry);
    }

    @Test
    public void shouldAddMetricsForANewlyCreatedRateLimiter() {
        RateLimiter newRateLimiter = rateLimiterRegistry.rateLimiter("backendB");

        assertThat(taggedRateLimiterMetrics.meterIdMap).containsKeys("backendA", "backendB");
        assertThat(taggedRateLimiterMetrics.meterIdMap.get("backendA")).hasSize(2);
        assertThat(taggedRateLimiterMetrics.meterIdMap.get("backendB")).hasSize(2);

        List<Meter> meters = meterRegistry.getMeters();
        assertThat(meters).hasSize(4);

        Collection<Gauge> gauges = meterRegistry.get(DEFAULT_AVAILABLE_PERMISSIONS_METRIC_NAME)
            .gauges();

        Optional<Gauge> successful = findMeterByNamesTag(gauges, newRateLimiter.getName());
        assertThat(successful).isPresent();
        assertThat(successful.get().value())
            .isEqualTo(newRateLimiter.getMetrics().getAvailablePermissions());
    }

    @Test
    public void shouldAddCustomTags() {
        RateLimiter newRateLimiter = rateLimiterRegistry.rateLimiter("backendF", Map.of("key1", "value1"));
        assertThat(taggedRateLimiterMetrics.meterIdMap).containsKeys("backendA", "backendF");
        assertThat(taggedRateLimiterMetrics.meterIdMap.get("backendA")).hasSize(2);
        assertThat(taggedRateLimiterMetrics.meterIdMap.get("backendF")).hasSize(2);
        List<Meter> meters = meterRegistry.getMeters();
        assertThat(meters).hasSize(4);
        assertThat(meterRegistry.get(DEFAULT_AVAILABLE_PERMISSIONS_METRIC_NAME).tag("key1", "value1")).isNotNull();
    }

    @Test
    public void shouldRemovedMetricsForRemovedRetry() {
        List<Meter> meters = meterRegistry.getMeters();
        assertThat(meters).hasSize(2);

        assertThat(taggedRateLimiterMetrics.meterIdMap).containsKeys("backendA");
        rateLimiterRegistry.remove("backendA");

        assertThat(taggedRateLimiterMetrics.meterIdMap).isEmpty();

        meters = meterRegistry.getMeters();
        assertThat(meters).isEmpty();
{'fraction_non_alphanumeric': 0.09665337682735291, 'fraction_numerical': 0.006040836051709557, 'mean_word_length': 4.693259972489684, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2283307', 'n_tokens_mistral': 2423, 'n_tokens_neox': 2362, 'n_words': 411}