fix(bugcollect): wire ingestion limiter constructor

这个提交包含在:
XuqmGroup 2026-07-28 22:14:14 +08:00
父节点 331ea03bda
当前提交 edb1324026
共有 2 个文件被更改,包括 31 次插入0 次删除

查看文件

@ -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);
}
}
} }