refactor: remove production in-memory session store
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package com.lingniu.ingest.protocol.jt808;
|
||||
|
||||
import com.lingniu.ingest.session.DeviceSession;
|
||||
import com.lingniu.ingest.session.SessionStore;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
public final class TestSessionStore implements SessionStore {
|
||||
|
||||
private final ConcurrentMap<String, DeviceSession> byId = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<String, String> vinToId = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<String, String> phoneToId = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void put(DeviceSession session) {
|
||||
byId.put(session.sessionId(), session);
|
||||
if (session.vin() != null) {
|
||||
vinToId.put(session.vin(), session.sessionId());
|
||||
}
|
||||
if (session.phone() != null) {
|
||||
phoneToId.put(session.phone(), session.sessionId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DeviceSession> findBySessionId(String sessionId) {
|
||||
return Optional.ofNullable(byId.get(sessionId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DeviceSession> findByVin(String vin) {
|
||||
String sessionId = vinToId.get(vin);
|
||||
return sessionId == null ? Optional.empty() : findBySessionId(sessionId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DeviceSession> findByPhone(String phone) {
|
||||
String sessionId = phoneToId.get(phone);
|
||||
return sessionId == null ? Optional.empty() : findBySessionId(sessionId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Optional<DeviceSession> update(String sessionId, UnaryOperator<DeviceSession> updater) {
|
||||
DeviceSession current = byId.get(sessionId);
|
||||
if (current == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
DeviceSession next = updater.apply(current);
|
||||
put(next);
|
||||
return Optional.of(next);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(String sessionId) {
|
||||
DeviceSession removed = byId.remove(sessionId);
|
||||
if (removed == null) {
|
||||
return;
|
||||
}
|
||||
if (removed.vin() != null) {
|
||||
vinToId.remove(removed.vin(), sessionId);
|
||||
}
|
||||
if (removed.phone() != null) {
|
||||
phoneToId.remove(removed.phone(), sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return byId.size();
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import com.lingniu.ingest.protocol.jt808.model.Jt808MessageId;
|
||||
import com.lingniu.ingest.protocol.jt808.session.Jt808ChannelRegistry;
|
||||
import com.lingniu.ingest.protocol.jt808.session.Jt808PendingRequests;
|
||||
import com.lingniu.ingest.session.DeviceSession;
|
||||
import com.lingniu.ingest.session.InMemorySessionStore;
|
||||
import com.lingniu.ingest.protocol.jt808.TestSessionStore;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -26,7 +26,7 @@ class Jt808CommandDispatcherTest {
|
||||
|
||||
@Test
|
||||
void notifyWritesSubpackageFramesForLargeBody() {
|
||||
InMemorySessionStore sessions = new InMemorySessionStore();
|
||||
TestSessionStore sessions = new TestSessionStore();
|
||||
sessions.put(new DeviceSession("sid-1", ProtocolId.JT808, "LNVIN000000000303",
|
||||
"123456789012", "", "127.0.0.1:10000", Instant.now(), Instant.now(), Map.of()));
|
||||
Jt808ChannelRegistry channels = new Jt808ChannelRegistry();
|
||||
|
||||
@@ -28,7 +28,7 @@ import com.lingniu.ingest.protocol.jt808.model.Jt808Message;
|
||||
import com.lingniu.ingest.protocol.jt808.model.Jt808MessageId;
|
||||
import com.lingniu.ingest.protocol.jt808.session.Jt808ChannelRegistry;
|
||||
import com.lingniu.ingest.protocol.jt808.session.Jt808PendingRequests;
|
||||
import com.lingniu.ingest.session.InMemorySessionStore;
|
||||
import com.lingniu.ingest.protocol.jt808.TestSessionStore;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -47,7 +47,7 @@ class Jt808ChannelHandlerTest {
|
||||
|
||||
@Test
|
||||
void registerSessionUsesResolvedInternalVinInsteadOfDeviceIdPlaceholder() {
|
||||
InMemorySessionStore sessions = new InMemorySessionStore();
|
||||
TestSessionStore sessions = new TestSessionStore();
|
||||
InMemoryVehicleIdentityService identity = new InMemoryVehicleIdentityService();
|
||||
identity.bind(new VehicleIdentityBinding(
|
||||
ProtocolId.JT808, "LNVIN000000000808", "123456789012", "DEV808", "B80808"));
|
||||
@@ -88,7 +88,7 @@ class Jt808ChannelHandlerTest {
|
||||
|
||||
@Test
|
||||
void unresolvedRegisterBindsGeneratedInternalVinToExternalIdentifiers() {
|
||||
InMemorySessionStore sessions = new InMemorySessionStore();
|
||||
TestSessionStore sessions = new TestSessionStore();
|
||||
InMemoryVehicleIdentityService identity = new InMemoryVehicleIdentityService();
|
||||
DisruptorEventBus eventBus = new DisruptorEventBus(1024, "blocking", List.of(new ImmediateKafkaSink()));
|
||||
AsyncBatchExecutor batchExecutor = new AsyncBatchExecutor(eventBus::publish);
|
||||
@@ -127,7 +127,7 @@ class Jt808ChannelHandlerTest {
|
||||
|
||||
@Test
|
||||
void authSessionUsesResolvedInternalVinFromImei() {
|
||||
InMemorySessionStore sessions = new InMemorySessionStore();
|
||||
TestSessionStore sessions = new TestSessionStore();
|
||||
InMemoryVehicleIdentityService identity = new InMemoryVehicleIdentityService();
|
||||
identity.bind(new VehicleIdentityBinding(
|
||||
ProtocolId.JT808, "LNVIN000000AUTH01", "123456789012", "123456789012345", ""));
|
||||
@@ -177,7 +177,7 @@ class Jt808ChannelHandlerTest {
|
||||
Jt808ChannelHandler handler = new Jt808ChannelHandler(
|
||||
new Jt808MessageDecoder(new BodyParserRegistry(List.of(new RegisterBodyParser()))),
|
||||
dispatcher,
|
||||
new InMemorySessionStore(),
|
||||
new TestSessionStore(),
|
||||
new InMemoryVehicleIdentityService(),
|
||||
new Jt808ChannelRegistry(),
|
||||
new Jt808PendingRequests());
|
||||
@@ -203,7 +203,7 @@ class Jt808ChannelHandlerTest {
|
||||
|
||||
@Test
|
||||
void inactiveChannelRemovesSessionStoreEntry() {
|
||||
InMemorySessionStore sessions = new InMemorySessionStore();
|
||||
TestSessionStore sessions = new TestSessionStore();
|
||||
DisruptorEventBus eventBus = new DisruptorEventBus(1024, "blocking", List.of(new ImmediateKafkaSink()));
|
||||
AsyncBatchExecutor batchExecutor = new AsyncBatchExecutor(eventBus::publish);
|
||||
Dispatcher dispatcher = new Dispatcher(
|
||||
@@ -253,7 +253,7 @@ class Jt808ChannelHandlerTest {
|
||||
Jt808ChannelHandler handler = new Jt808ChannelHandler(
|
||||
new Jt808MessageDecoder(new BodyParserRegistry(List.of(new LocationBodyParser()))),
|
||||
dispatcher,
|
||||
new InMemorySessionStore(),
|
||||
new TestSessionStore(),
|
||||
identity,
|
||||
new Jt808ChannelRegistry(),
|
||||
new Jt808PendingRequests());
|
||||
@@ -315,7 +315,7 @@ class Jt808ChannelHandlerTest {
|
||||
Jt808ChannelHandler handler = new Jt808ChannelHandler(
|
||||
new Jt808MessageDecoder(new BodyParserRegistry(List.of(new RegisterBodyParser()))),
|
||||
dispatcher,
|
||||
new InMemorySessionStore(),
|
||||
new TestSessionStore(),
|
||||
identity,
|
||||
new Jt808ChannelRegistry(),
|
||||
new Jt808PendingRequests());
|
||||
@@ -364,7 +364,7 @@ class Jt808ChannelHandlerTest {
|
||||
Jt808ChannelHandler handler = new Jt808ChannelHandler(
|
||||
new Jt808MessageDecoder(new BodyParserRegistry(List.of(new RegisterBodyParser(), new LocationBodyParser()))),
|
||||
dispatcher,
|
||||
new InMemorySessionStore(),
|
||||
new TestSessionStore(),
|
||||
identity,
|
||||
new Jt808ChannelRegistry(),
|
||||
new Jt808PendingRequests());
|
||||
@@ -422,7 +422,7 @@ class Jt808ChannelHandlerTest {
|
||||
Jt808ChannelHandler handler = new Jt808ChannelHandler(
|
||||
new Jt808MessageDecoder(new BodyParserRegistry(List.of(new LocationBodyParser()))),
|
||||
dispatcher,
|
||||
new InMemorySessionStore(),
|
||||
new TestSessionStore(),
|
||||
identity,
|
||||
new Jt808ChannelRegistry(),
|
||||
new Jt808PendingRequests());
|
||||
@@ -465,7 +465,7 @@ class Jt808ChannelHandlerTest {
|
||||
Jt808ChannelHandler handler = new Jt808ChannelHandler(
|
||||
new Jt808MessageDecoder(new BodyParserRegistry(List.of(new RegisterBodyParser()))),
|
||||
dispatcher,
|
||||
new InMemorySessionStore(),
|
||||
new TestSessionStore(),
|
||||
identity,
|
||||
new Jt808ChannelRegistry(),
|
||||
new Jt808PendingRequests());
|
||||
@@ -511,7 +511,7 @@ class Jt808ChannelHandlerTest {
|
||||
Jt808ChannelHandler handler = new Jt808ChannelHandler(
|
||||
new Jt808MessageDecoder(new BodyParserRegistry(List.of())),
|
||||
dispatcher,
|
||||
new InMemorySessionStore(),
|
||||
new TestSessionStore(),
|
||||
new InMemoryVehicleIdentityService(),
|
||||
new Jt808ChannelRegistry(),
|
||||
new Jt808PendingRequests());
|
||||
@@ -562,7 +562,7 @@ class Jt808ChannelHandlerTest {
|
||||
Jt808ChannelHandler handler = new Jt808ChannelHandler(
|
||||
new Jt808MessageDecoder(new BodyParserRegistry(List.of())),
|
||||
dispatcher,
|
||||
new InMemorySessionStore(),
|
||||
new TestSessionStore(),
|
||||
new InMemoryVehicleIdentityService(),
|
||||
new Jt808ChannelRegistry(),
|
||||
new Jt808PendingRequests());
|
||||
@@ -614,7 +614,7 @@ class Jt808ChannelHandlerTest {
|
||||
Jt808ChannelHandler handler = new Jt808ChannelHandler(
|
||||
new Jt808MessageDecoder(new BodyParserRegistry(List.of(new LocationBodyParser()))),
|
||||
dispatcher,
|
||||
new InMemorySessionStore(),
|
||||
new TestSessionStore(),
|
||||
new InMemoryVehicleIdentityService(),
|
||||
failingIdentity,
|
||||
new Jt808ChannelRegistry(),
|
||||
|
||||
Reference in New Issue
Block a user