fix(bugcollect): wire ingestion limiter constructor
这个提交包含在:
父节点
331ea03bda
当前提交
edb1324026
@ -2,6 +2,7 @@ package com.xuqm.bugcollect.service;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||||
@ -39,6 +40,7 @@ public class IngestionRateLimiter {
|
|||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
private final Map<String, LocalWindow> localWindows = new ConcurrentHashMap<>();
|
private final Map<String, LocalWindow> localWindows = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
public IngestionRateLimiter(
|
public IngestionRateLimiter(
|
||||||
StringRedisTemplate redis,
|
StringRedisTemplate redis,
|
||||||
@Value("${bugcollect-service.ingestion.max-events-per-minute:6000}") long maxEventsPerMinute,
|
@Value("${bugcollect-service.ingestion.max-events-per-minute:6000}") long maxEventsPerMinute,
|
||||||
|
|||||||
@ -1,6 +1,10 @@
|
|||||||
package com.xuqm.bugcollect.service;
|
package com.xuqm.bugcollect.service;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
@ -14,6 +18,22 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
class IngestionRateLimiterTest {
|
class IngestionRateLimiterTest {
|
||||||
|
|
||||||
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withUserConfiguration(TestConfiguration.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void startsAsSpringBeanWithConfiguredConstructor() {
|
||||||
|
contextRunner
|
||||||
|
.withPropertyValues(
|
||||||
|
"bugcollect-service.ingestion.max-events-per-minute=120",
|
||||||
|
"bugcollect-service.ingestion.window-seconds=30")
|
||||||
|
.run(context -> {
|
||||||
|
org.assertj.core.api.Assertions.assertThat(context).hasNotFailed();
|
||||||
|
org.assertj.core.api.Assertions.assertThat(context)
|
||||||
|
.hasSingleBean(IngestionRateLimiter.class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void usesDistributedRetryWindowReturnedByRedis() {
|
void usesDistributedRetryWindowReturnedByRedis() {
|
||||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||||
@ -43,4 +63,13 @@ class IngestionRateLimiterTest {
|
|||||||
.satisfies(error -> org.assertj.core.api.Assertions.assertThat(
|
.satisfies(error -> org.assertj.core.api.Assertions.assertThat(
|
||||||
((IngestionRateLimitedException) error).retryAfterSeconds()).isEqualTo(60));
|
((IngestionRateLimitedException) error).retryAfterSeconds()).isEqualTo(60));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@Import(IngestionRateLimiter.class)
|
||||||
|
static class TestConfiguration {
|
||||||
|
@Bean
|
||||||
|
StringRedisTemplate stringRedisTemplate() {
|
||||||
|
return mock(StringRedisTemplate.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户