You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2023/08/01 15:15:32 UTC
[sling-org-apache-sling-tracer] branch SLING-11980 updated: Fix bounded cache and add tests
This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch SLING-11980
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-tracer.git
The following commit(s) were added to refs/heads/SLING-11980 by this push:
new dc1d7cc Fix bounded cache and add tests
dc1d7cc is described below
commit dc1d7cc9f04e4495e3f010352c9317e67d81af2d
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Aug 1 17:15:26 2023 +0200
Fix bounded cache and add tests
---
.../sling/tracer/internal/TracerLogServlet.java | 5 +-
.../sling/tracer/internal/BoundedCacheTest.java | 72 ++++++++++++++++++++++
2 files changed, 76 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java b/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
index 09de28d..b7a674f 100644
--- a/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
+++ b/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
@@ -144,7 +144,7 @@ class TracerLogServlet extends SimpleWebConsolePlugin implements TraceLogRecorde
Collections.sort(list);
if ( this.currentSize > this.maxSize ) {
while (this.currentSize > this.maxSize && !list.isEmpty()) {
- final Entry entry = list.remove(list.size() - 1);
+ final Entry entry = list.remove(0);
toRemove.add(entry.recording.getRequestId());
currentSize -= entry.recording.size();
}
@@ -162,10 +162,12 @@ class TracerLogServlet extends SimpleWebConsolePlugin implements TraceLogRecorde
}
public synchronized int size() {
+ checkCache();
return this.cache.size();
}
public synchronized long memorySize() {
+ checkCache();
return this.currentSize;
}
@@ -175,6 +177,7 @@ class TracerLogServlet extends SimpleWebConsolePlugin implements TraceLogRecorde
}
public synchronized List<JSONRecording> asList() {
+ checkCache();
final List<JSONRecording> result = new ArrayList<>();
for(final Entry entry : this.cache.values()) {
result.add(entry.recording);
diff --git a/src/test/java/org/apache/sling/tracer/internal/BoundedCacheTest.java b/src/test/java/org/apache/sling/tracer/internal/BoundedCacheTest.java
new file mode 100644
index 0000000..8fdb8e2
--- /dev/null
+++ b/src/test/java/org/apache/sling/tracer/internal/BoundedCacheTest.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.tracer.internal;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class BoundedCacheTest {
+
+ @Test public void testMemoryBoundary() {
+ final TracerLogServlet.BoundedCache cache = new TracerLogServlet.BoundedCache(2, 10);
+ JSONRecording recordingA = Mockito.mock(JSONRecording.class);
+ Mockito.when(recordingA.size()).thenReturn(1*1024*1024);
+ Mockito.when(recordingA.getRequestId()).thenReturn("a");
+ JSONRecording recordingB = Mockito.mock(JSONRecording.class);
+ Mockito.when(recordingB.size()).thenReturn(1*1000*1000);
+ Mockito.when(recordingB.getRequestId()).thenReturn("b");
+
+ cache.put("a", recordingA);
+ cache.put("b", recordingB);
+
+ Assert.assertEquals(recordingA.size() + recordingB.size(), cache.memorySize());
+ Assert.assertNotNull(cache.get("a"));
+ Assert.assertNotNull(cache.get("b"));
+
+ JSONRecording recordingC = Mockito.mock(JSONRecording.class);
+ Mockito.when(recordingC.size()).thenReturn(1*1024*1024);
+ Mockito.when(recordingC.getRequestId()).thenReturn("c");
+ cache.put("c", recordingC);
+
+ Assert.assertNull(cache.get("a"));
+ Assert.assertNotNull(cache.get("b"));
+ Assert.assertNotNull(cache.get("c"));
+ Assert.assertEquals(recordingB.size() + recordingC.size(), cache.memorySize());
+ }
+
+ @Test public void testTimeBoundary() throws InterruptedException {
+ final TracerLogServlet.BoundedCache cache = new TracerLogServlet.BoundedCache(2, 1);
+ JSONRecording recordingA = Mockito.mock(JSONRecording.class);
+ Mockito.when(recordingA.size()).thenReturn(1*1024*1024);
+ Mockito.when(recordingA.getRequestId()).thenReturn("a");
+ JSONRecording recordingB = Mockito.mock(JSONRecording.class);
+ Mockito.when(recordingB.size()).thenReturn(1*1000*1000);
+ Mockito.when(recordingB.getRequestId()).thenReturn("b");
+
+ cache.put("a", recordingA);
+ cache.put("b", recordingB);
+
+ Thread.sleep(2000); // cache should be empty now
+ Assert.assertEquals(0L, cache.memorySize());
+ Assert.assertNull(cache.get("a"));
+ Assert.assertNull(cache.get("b"));
+ Assert.assertEquals(0, cache.size());
+ }
+}