|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +package org.opensearch.dataprepper.plugins.processor.ratelimiter; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.opensearch.dataprepper.model.event.Event; |
| 15 | +import org.opensearch.dataprepper.model.event.EventFactory; |
| 16 | +import org.opensearch.dataprepper.model.event.LogEventBuilder; |
| 17 | +import org.opensearch.dataprepper.model.processor.Processor; |
| 18 | +import org.opensearch.dataprepper.model.record.Record; |
| 19 | +import org.opensearch.dataprepper.test.plugins.DataPrepperPluginTest; |
| 20 | +import org.opensearch.dataprepper.test.plugins.PluginConfigurationFile; |
| 21 | +import org.opensearch.dataprepper.test.plugins.junit.BaseDataPrepperPluginStandardTestSuite; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 29 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 30 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 32 | + |
| 33 | +@DataPrepperPluginTest(pluginName = "rate_limiter", pluginType = Processor.class) |
| 34 | +class RateLimiterProcessorTest extends BaseDataPrepperPluginStandardTestSuite { |
| 35 | + |
| 36 | + @Test |
| 37 | + void test_events_under_limit_all_pass_through( |
| 38 | + @PluginConfigurationFile("rate_limiter_default.yaml") final Processor<Record<Event>, Record<Event>> processor, |
| 39 | + final EventFactory eventFactory) { |
| 40 | + final List<Record<Event>> records = createEvents(eventFactory, 200); |
| 41 | + |
| 42 | + final Collection<Record<Event>> result = processor.execute(records); |
| 43 | + |
| 44 | + assertThat(result, notNullValue()); |
| 45 | + assertThat(result.size(), equalTo(200)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void test_events_over_limit_are_dropped( |
| 50 | + @PluginConfigurationFile("rate_limiter_default.yaml") final Processor<Record<Event>, Record<Event>> processor, |
| 51 | + final EventFactory eventFactory) { |
| 52 | + final List<Record<Event>> records = createEvents(eventFactory, 500); |
| 53 | + |
| 54 | + final Collection<Record<Event>> result = processor.execute(records); |
| 55 | + |
| 56 | + assertThat(result, notNullValue()); |
| 57 | + assertThat(result.size(), lessThanOrEqualTo(400)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void test_counts_accumulate_across_multiple_execute_calls( |
| 62 | + @PluginConfigurationFile("rate_limiter_default.yaml") final Processor<Record<Event>, Record<Event>> processor, |
| 63 | + final EventFactory eventFactory) { |
| 64 | + final Collection<Record<Event>> result1 = processor.execute(createEvents(eventFactory, 300)); |
| 65 | + final Collection<Record<Event>> result2 = processor.execute(createEvents(eventFactory, 200)); |
| 66 | + |
| 67 | + assertThat(result1.size() + result2.size(), lessThanOrEqualTo(400)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void test_limit_when_only_rate_limits_matching_events( |
| 72 | + @PluginConfigurationFile("rate_limiter_with_limit_when.yaml") final Processor<Record<Event>, Record<Event>> processor, |
| 73 | + final EventFactory eventFactory) { |
| 74 | + final List<Record<Event>> records = new ArrayList<>(); |
| 75 | + records.addAll(createEvents(eventFactory, 5, Map.of("message", "test", "level", "DEBUG"))); |
| 76 | + records.addAll(createEvents(eventFactory, 5, Map.of("message", "test", "level", "INFO"))); |
| 77 | + |
| 78 | + final Collection<Record<Event>> result = processor.execute(records); |
| 79 | + |
| 80 | + assertThat(result, notNullValue()); |
| 81 | + assertThat(result.size(), equalTo(7)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void test_hold_mode( |
| 86 | + @PluginConfigurationFile("rate_limiter_hold_mode.yaml") final Processor<Record<Event>, Record<Event>> processor, |
| 87 | + final EventFactory eventFactory) { |
| 88 | + final List<Record<Event>> records = createEvents(eventFactory, 3); |
| 89 | + final Collection<Record<Event>> result = processor.execute(records); |
| 90 | + assertThat(result, notNullValue()); |
| 91 | + assertThat(result.size(), equalTo(3)); |
| 92 | + } |
| 93 | + |
| 94 | + private List<Record<Event>> createEvents(final EventFactory eventFactory, final int count) { |
| 95 | + return createEvents(eventFactory, count, Map.of("message", "test")); |
| 96 | + } |
| 97 | + |
| 98 | + private List<Record<Event>> createEvents(final EventFactory eventFactory, final int count, final Map<String, Object> data) { |
| 99 | + final List<Record<Event>> records = new ArrayList<>(); |
| 100 | + for (int i = 0; i < count; i++) { |
| 101 | + final Event event = eventFactory.eventBuilder(LogEventBuilder.class) |
| 102 | + .withData(data) |
| 103 | + .build(); |
| 104 | + records.add(new Record<>(event)); |
| 105 | + } |
| 106 | + return records; |
| 107 | + } |
| 108 | +} |
0 commit comments