Skip to content

Commit 9436e06

Browse files
committed
Fix tests
1 parent 830b966 commit 9436e06

File tree

4 files changed

+45
-114
lines changed

4 files changed

+45
-114
lines changed

java/src/org/openqa/selenium/grid/data/SessionStatus.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

java/src/org/openqa/selenium/grid/node/Node.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.IOException;
3030
import java.net.URI;
3131
import java.time.Duration;
32+
import java.util.ArrayList;
3233
import java.util.List;
3334
import java.util.Map;
3435
import java.util.ServiceLoader;
@@ -271,10 +272,12 @@ public TemporaryFilesystem getDownloadsFilesystem(SessionId id) throws IOExcepti
271272

272273
public abstract NodeStatus getStatus();
273274

274-
public abstract List<SessionHistoryEntry> getSessionHistory();
275-
276275
public abstract HealthCheck getHealthCheck();
277276

277+
public List<SessionHistoryEntry> getSessionHistory() {
278+
return new ArrayList<>();
279+
}
280+
278281
public Duration getSessionTimeout() {
279282
return sessionTimeout;
280283
}

java/src/org/openqa/selenium/grid/node/local/LocalNode.java

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import java.util.Queue;
6464
import java.util.Set;
6565
import java.util.UUID;
66-
import java.util.concurrent.ConcurrentHashMap;
6766
import java.util.concurrent.ConcurrentLinkedQueue;
6867
import java.util.concurrent.ExecutionException;
6968
import java.util.concurrent.Executors;
@@ -160,7 +159,6 @@ public class LocalNode extends Node implements Closeable {
160159
private final Optional<Path> statusFilePath;
161160
private final Optional<Path> sessionHistoryFilePath;
162161
private final Queue<SessionHistoryEntry> sessionHistory = new ConcurrentLinkedQueue<>();
163-
private final Map<SessionId, Instant> sessionStartTimes = new ConcurrentHashMap<>();
164162

165163
protected LocalNode(
166164
Tracer tracer,
@@ -308,6 +306,7 @@ protected LocalNode(
308306

309307
bus.addListener(SessionStartedEvent.listener(this::recordSessionStart));
310308
bus.addListener(SessionClosedEvent.listener(this::recordSessionStop));
309+
bus.addListener(NodeHeartBeatEvent.listener(this::cleanupSessionHistory));
311310

312311
shutdown =
313312
() -> {
@@ -1138,33 +1137,58 @@ private void recordSessionStart(SessionId sessionId) {
11381137
return;
11391138
}
11401139
Instant startTime = Instant.now();
1141-
sessionStartTimes.put(sessionId, startTime);
11421140
sessionHistory.add(new SessionHistoryEntry(sessionId, startTime, null));
11431141
writeSessionHistoryToFile();
11441142
}
11451143

11461144
private void recordSessionStop(SessionId sessionId) {
1147-
if (!isSessionOwner(sessionId)) {
1148-
return;
1149-
}
11501145
Instant stopTime = Instant.now();
1151-
Instant startTime = sessionStartTimes.remove(sessionId);
1152-
if (startTime != null) {
1153-
// Find and update the existing history entry
1154-
sessionHistory.stream()
1155-
.filter(entry -> entry.getSessionId().equals(sessionId))
1156-
.findFirst()
1157-
.ifPresent(entry -> entry.setStopTime(stopTime));
1158-
writeSessionHistoryToFile();
1146+
// Find and update the existing history entry
1147+
sessionHistory.stream()
1148+
.filter(entry -> entry.getSessionId().equals(sessionId))
1149+
.findFirst()
1150+
.ifPresent(
1151+
entry -> {
1152+
entry.setStopTime(stopTime);
1153+
writeSessionHistoryToFile();
1154+
});
1155+
}
1156+
1157+
private void cleanupSessionHistory(NodeStatus status) {
1158+
int maxHistorySize = 100;
1159+
if (!status.getNodeId().equals(getId()) || sessionHistory.size() < maxHistorySize) {
1160+
return;
11591161
}
1162+
1163+
// Keep only the last 100 completed sessions
1164+
List<SessionHistoryEntry> completedSessions =
1165+
sessionHistory.stream()
1166+
.filter(entry -> entry.getStopTime() != null)
1167+
.sorted(
1168+
(a, b) ->
1169+
b.getStartTime().compareTo(a.getStartTime())) // Sort by start time descending
1170+
.limit(100)
1171+
.collect(Collectors.toList());
1172+
1173+
// Keep all ongoing sessions
1174+
List<SessionHistoryEntry> ongoingSessions =
1175+
sessionHistory.stream()
1176+
.filter(entry -> entry.getStopTime() == null)
1177+
.collect(Collectors.toList());
1178+
1179+
// Clear and rebuild the history queue
1180+
sessionHistory.clear();
1181+
sessionHistory.addAll(completedSessions);
1182+
sessionHistory.addAll(ongoingSessions);
1183+
1184+
// Write the cleaned history to file
1185+
writeSessionHistoryToFile();
11601186
}
11611187

11621188
private void writeSessionHistoryToFile() {
11631189
if (sessionHistoryFilePath.isPresent()) {
11641190
try {
11651191
List<SessionHistoryEntry> sortedHistory = new ArrayList<>(sessionHistory);
1166-
sortedHistory.sort((a, b) -> a.getStartTime().compareTo(b.getStartTime()));
1167-
11681192
String historyJson = JSON.toJson(sortedHistory);
11691193
Files.write(
11701194
sessionHistoryFilePath.get(),

java/test/org/openqa/selenium/grid/data/SessionClosedEventTest.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)