refactor: make command gateway opt-in

This commit is contained in:
lingniu
2026-07-01 04:22:10 +08:00
parent 2e54607bd6
commit 02504de021
3 changed files with 68 additions and 1 deletions

View File

@@ -49,5 +49,10 @@
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -8,7 +8,7 @@ import org.springframework.web.servlet.DispatcherServlet;
@AutoConfiguration @AutoConfiguration
@ConditionalOnClass(DispatcherServlet.class) @ConditionalOnClass(DispatcherServlet.class)
@ConditionalOnProperty(prefix = "lingniu.ingest.command-gateway", name = "enabled", havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(prefix = "lingniu.ingest.command-gateway", name = "enabled", havingValue = "true")
@ComponentScan(basePackageClasses = TerminalCommandController.class) @ComponentScan(basePackageClasses = TerminalCommandController.class)
public class CommandGatewayAutoConfiguration { public class CommandGatewayAutoConfiguration {
// 只扫描 REST 下行命令网关;不参与 32960 接收、RAW 冷存或历史查询链路。 // 只扫描 REST 下行命令网关;不参与 32960 接收、RAW 冷存或历史查询链路。

View File

@@ -0,0 +1,62 @@
package com.lingniu.ingest.gateway;
import com.lingniu.ingest.session.CommandDispatcher;
import com.lingniu.ingest.session.DeviceSession;
import com.lingniu.ingest.session.SessionStore;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.UnaryOperator;
import static org.assertj.core.api.Assertions.assertThat;
class CommandGatewayAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(CommandGatewayAutoConfiguration.class))
.withBean(SessionStore.class, EmptySessionStore::new)
.withBean(CommandDispatcher.class, NoopCommandDispatcher::new);
@Test
void backsOffByDefault() {
contextRunner.run(context -> assertThat(context).doesNotHaveBean(TerminalCommandController.class));
}
@Test
void createsControllerWhenExplicitlyEnabled() {
contextRunner
.withPropertyValues("lingniu.ingest.command-gateway.enabled=true")
.run(context -> assertThat(context).hasSingleBean(TerminalCommandController.class));
}
private static final class NoopCommandDispatcher implements CommandDispatcher {
@Override
public CompletableFuture<Void> notify(String sessionId, Object command) {
return CompletableFuture.completedFuture(null);
}
@Override
public <T> CompletableFuture<T> request(String sessionId,
Object command,
Class<T> responseType,
Duration timeout) {
return CompletableFuture.failedFuture(new UnsupportedOperationException("not used"));
}
}
private static final class EmptySessionStore implements SessionStore {
@Override public void put(DeviceSession session) {}
@Override public Optional<DeviceSession> findBySessionId(String sessionId) { return Optional.empty(); }
@Override public Optional<DeviceSession> findByVin(String vin) { return Optional.empty(); }
@Override public Optional<DeviceSession> findByPhone(String phone) { return Optional.empty(); }
@Override public Optional<DeviceSession> update(String sessionId, UnaryOperator<DeviceSession> updater) {
return Optional.empty();
}
@Override public void remove(String sessionId) {}
@Override public int size() { return 0; }
}
}