From 1ba2d24568e9b5af40d1996062ea87dbde224d54 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Sun, 15 Nov 2026 23:34:07 +0000 Subject: [PATCH 1/6] Test: close usage sharing within its 5 second wait when the service stops answering The usage sharing element page, at 51Degrees/specifications 706de1f0, says in pipeline-specification/pipeline-elements/usage-sharing-element.md line 387: "Cleanup SHOULD complete within 5 seconds, so that closing the element does not hold up the process that closes it." https://github.com/51Degrees/specifications/blob/706de1f0cb3edd560eb7fd7ef3bb19b93a4883dc/pipeline-specification/pipeline-elements/usage-sharing-element.md#L387 Closing already waits at most SHARE_USAGE_DEFAULT_HTTP_POST_TIMEOUT, 5000 milliseconds, and ShareUsageCloseTimeoutTests checked what is sent and logged after that wait, not how long closing takes. The new test starts a send to a service that does not answer, then closes the element, and checks that closing returns within the 5 second wait and a 5 second margin for discarding the rest, while the send is still waiting for its answer. The element setup the two tests share moves into one helper. The code is unchanged. Specification conformance item US-30. --- .../ShareUsageCloseTimeoutTests.java | 95 +++++++++++++++---- 1 file changed, 78 insertions(+), 17 deletions(-) diff --git a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java index cf6507034..1075df8e2 100644 --- a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java +++ b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java @@ -32,6 +32,7 @@ import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.lang.reflect.Proxy; +import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -45,6 +46,7 @@ import static fiftyone.pipeline.core.Constants.EVIDENCE_CLIENTIP_KEY; import static fiftyone.pipeline.engines.Constants.DEFAULT_SESSION_COOKIE_NAME; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -65,6 +67,12 @@ public class ShareUsageCloseTimeoutTests { */ private static final int ENTRIES = 10; + /** + * The most closing may take beyond its wait for the data to be sent, to + * discard the rest and stop the sending thread. + */ + private static final long CLOSE_MARGIN_MILLIS = 5000; + /** * An upload whose first answer does not come until the test releases * it, as with a remote service that has stopped answering. Interrupting @@ -107,23 +115,7 @@ public void ShareUsageElement_CloseTimesOut_StopsSendingAndDiscards() List warnings = new CopyOnWriteArrayList<>(); StalledUploader uploader = new StalledUploader(); Pipeline pipeline = new PipelineBuilder().build(); - ShareUsageElement element = new ShareUsageElement( - recordingLogger(warnings), - 1, - 1, - 100, - 100, - 100, - 1, - true, - "http://localhost/usage", - new ArrayList<>(), - new ArrayList<>(), - new ArrayList<>(), - DEFAULT_SESSION_COOKIE_NAME, - flowData -> true); - element.addPipeline(pipeline); - element.dataUploader = uploader; + ShareUsageElement element = element(warnings, pipeline, uploader); try { for (int i = 1; i <= ENTRIES; i++) { process(pipeline, element, "10.0.0." + i); @@ -158,6 +150,75 @@ public void ShareUsageElement_CloseTimesOut_StopsSendingAndDiscards() } } + /** + * Closing while the remote service has stopped answering does not wait + * for the service. It returns within the 5 seconds it waits for the data + * to be sent and a margin for discarding the rest, while the upload in + * progress is still waiting for its answer. + */ + @Test + public void ShareUsageElement_ServiceStopsAnswering_CloseDoesNotWait() + throws Exception { + StalledUploader uploader = new StalledUploader(); + Pipeline pipeline = new PipelineBuilder().build(); + ShareUsageElement element = element( + new CopyOnWriteArrayList<>(), + pipeline, + uploader); + try { + for (int i = 1; i <= ENTRIES; i++) { + process(pipeline, element, "10.0.0." + i); + } + assertTrue( + uploader.sending.await(WAIT_LIMIT_MILLIS, TimeUnit.MILLISECONDS), + "No send was started."); + + assertTimeoutPreemptively( + Duration.ofMillis( + Constants.SHARE_USAGE_DEFAULT_HTTP_POST_TIMEOUT + + CLOSE_MARGIN_MILLIS), + element::close, + "Closing waited for the remote service to answer."); + assertEquals(1, uploader.release.getCount(), + "The upload in progress was answered before closing ended."); + } + finally { + uploader.release.countDown(); + if (element.executor.isShutdown() == false) { + element.close(); + } + pipeline.close(); + } + } + + /** + * An element that sends each entry on its own, through the uploader + * given, logging its warnings to the list given. + */ + private static ShareUsageElement element( + List warnings, + Pipeline pipeline, + DataUploader uploader) { + ShareUsageElement element = new ShareUsageElement( + recordingLogger(warnings), + 1, + 1, + 100, + 100, + 100, + 1, + true, + "http://localhost/usage", + new ArrayList<>(), + new ArrayList<>(), + new ArrayList<>(), + DEFAULT_SESSION_COOKIE_NAME, + flowData -> true); + element.addPipeline(pipeline); + element.dataUploader = uploader; + return element; + } + private static void process( Pipeline pipeline, ShareUsageElement element, From bbef6e431ff8036b5de128549b8cb07849d5a728 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Sun, 15 Nov 2026 23:42:46 +0000 Subject: [PATCH 2/6] Test: finish closing usage sharing within 5 seconds of its start The usage sharing element page, at 51Degrees/specifications dbaf099c, lists cleanup as three steps in pipeline-specification/pipeline-elements/usage-sharing-element.md lines 418 to 421 (wait while the consumer sends the data in the queue, send a final message, free the resources), and says at line 423: "Cleanup SHOULD complete within 5 seconds, so that closing the element does not hold up the process that closes it." https://github.com/51Degrees/specifications/blob/dbaf099c/pipeline-specification/pipeline-elements/usage-sharing-element.md#L413-L426 So the 5 seconds bound the whole of closing. The test of a service that stops answering now measures close from its start and fails when it takes more than 5000 milliseconds, the bound the specification sets, rather than allowing 5 seconds of margin beyond the wait. A guard of 30 seconds still stops the test if closing waits for the service. Specification conformance item US-30. --- .../ShareUsageCloseTimeoutTests.java | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java index 1075df8e2..6d4f31d70 100644 --- a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java +++ b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java @@ -50,8 +50,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** - * Closing waits up to 5 seconds for the data waiting to be sent. If it has - * not all been sent by then, sending stops, the rest is discarded and a + * Closing finishes within 5 seconds of its start. If the data waiting has + * not all been sent in time, sending stops, the rest is discarded and a * warning gives the number discarded. See Cleanup in the usage sharing * element specification. */ @@ -68,10 +68,11 @@ public class ShareUsageCloseTimeoutTests { private static final int ENTRIES = 10; /** - * The most closing may take beyond its wait for the data to be sent, to - * discard the rest and stop the sending thread. + * The most closing may take from its start, as the specification's + * Cleanup section sets it, holding the wait for the data to be sent, + * discarding the rest, the warning and freeing the resources. */ - private static final long CLOSE_MARGIN_MILLIS = 5000; + private static final long CLOSE_LIMIT_MILLIS = 5000; /** * An upload whose first answer does not come until the test releases @@ -151,13 +152,12 @@ public void ShareUsageElement_CloseTimesOut_StopsSendingAndDiscards() } /** - * Closing while the remote service has stopped answering does not wait - * for the service. It returns within the 5 seconds it waits for the data - * to be sent and a margin for discarding the rest, while the upload in - * progress is still waiting for its answer. + * Closing while the remote service has stopped answering finishes within + * 5 seconds of its start, the whole of cleanup included, while the + * upload in progress is still waiting for its answer. */ @Test - public void ShareUsageElement_ServiceStopsAnswering_CloseDoesNotWait() + public void ShareUsageElement_ServiceStopsAnswering_CloseWithin5Seconds() throws Exception { StalledUploader uploader = new StalledUploader(); Pipeline pipeline = new PipelineBuilder().build(); @@ -173,12 +173,19 @@ public void ShareUsageElement_ServiceStopsAnswering_CloseDoesNotWait() uploader.sending.await(WAIT_LIMIT_MILLIS, TimeUnit.MILLISECONDS), "No send was started."); + long[] tookMillis = new long[1]; assertTimeoutPreemptively( - Duration.ofMillis( - Constants.SHARE_USAGE_DEFAULT_HTTP_POST_TIMEOUT + - CLOSE_MARGIN_MILLIS), - element::close, + Duration.ofMillis(WAIT_LIMIT_MILLIS), + () -> { + long start = System.nanoTime(); + element.close(); + tookMillis[0] = TimeUnit.NANOSECONDS.toMillis( + System.nanoTime() - start); + }, "Closing waited for the remote service to answer."); + assertTrue(tookMillis[0] <= CLOSE_LIMIT_MILLIS, + "Closing took " + tookMillis[0] + " ms, more than the " + + CLOSE_LIMIT_MILLIS + " ms cleanup may take."); assertEquals(1, uploader.release.getCount(), "The upload in progress was answered before closing ended."); } From bcdb06b38696793e751901c2367ad6b8055dce79 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Sun, 15 Nov 2026 23:44:02 +0000 Subject: [PATCH 3/6] Fix: finish closing usage sharing within 5 seconds of its start Closing waited the full 5000 milliseconds for the data in the queue to be sent, then discarded the rest, logged the warning and stopped the sending thread, so closing took just over 5 seconds. Closing now measures from its start and stops waiting at 4500 milliseconds, keeping 500 milliseconds of the 5 second limit for discarding the rest, the warning and freeing the resources. No second wait is added. The two values are named constants, SHARE_USAGE_CLOSE_LIMIT_MILLIS (5000) and SHARE_USAGE_CLOSE_RESERVE_MILLIS (500), and the warning gives the time waited, 4500 milliseconds. The HTTP timeout, SHARE_USAGE_DEFAULT_HTTP_POST_TIMEOUT, which closing used for its wait before, is unchanged. Measured on an Apple M1 (macOS 14.4.1, OpenJDK 17.0.1), the only runner measured: before this change closing took 5010 milliseconds, so the work after the wait took 10 milliseconds. After it the test of a service that stops answering took 4.675, 4.667 and 4.665 seconds over three runs, setup of about 0.17 seconds included, so closing took about 4.5 seconds. The 500 millisecond reserve is 50 times what the work after the wait took there. The CI runners (Ubuntu x64 and arm64, Windows, macOS) were not measured. Answers pipeline-specification/pipeline-elements/usage-sharing-element.md line 423 at 51Degrees/specifications dbaf099c, "Cleanup SHOULD complete within 5 seconds, so that closing the element does not hold up the process that closes it." Specification conformance item US-30. --- .../fiftyone/flowelements/Constants.java | 13 ++++++++ .../fiftyone/flowelements/ShareUsageBase.java | 32 +++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/Constants.java b/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/Constants.java index 0641be77e..49d74b363 100644 --- a/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/Constants.java +++ b/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/Constants.java @@ -96,6 +96,19 @@ public class Constants { */ static final int SHARE_USAGE_DEFAULT_HTTP_POST_TIMEOUT = 5000; + /** + * The most time closing the Usage Sharing Element may take, from its + * start, in milliseconds. + */ + static final int SHARE_USAGE_CLOSE_LIMIT_MILLIS = 5000; + + /** + * The part of the close limit, in milliseconds, kept for what closing + * does after it stops waiting for the data to be sent: discarding the + * rest, logging the warning and freeing the resources. + */ + static final int SHARE_USAGE_CLOSE_RESERVE_MILLIS = 500; + /** * Default lost data count for share usage before it wraps */ diff --git a/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageBase.java b/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageBase.java index 48d28f657..4c13ed897 100644 --- a/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageBase.java +++ b/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageBase.java @@ -789,8 +789,31 @@ public DataFactory getDataFactory() { return null; } + /** + * The time closing may still wait for the data to be sent, so that the + * whole of closing, measured from its start, keeps within the close + * limit with the reserve left for what follows the wait. + * @param closeStartNanos {@link System#nanoTime()} when closing started + * @return milliseconds left to wait, never less than 0 + */ + private static long closeWaitLeftMillis(long closeStartNanos) { + long elapsedMillis = TimeUnit.NANOSECONDS.toMillis( + System.nanoTime() - closeStartNanos); + return Math.max(0, closeWaitMillis() - elapsedMillis); + } + + /** + * The longest closing waits for the data to be sent, from its start. + * @return milliseconds + */ + private static long closeWaitMillis() { + return Constants.SHARE_USAGE_CLOSE_LIMIT_MILLIS - + Constants.SHARE_USAGE_CLOSE_RESERVE_MILLIS; + } + @Override protected void managedResourcesCleanup() { + long closeStartNanos = System.nanoTime(); // Tell any consumer - one that is already running as well as the final // one queued below - that the queue has to be drained completely rather // than stopping once fewer than minEntriesPerMessage entries are left. @@ -809,9 +832,12 @@ protected void managedResourcesCleanup() { submitSendData(); try { executor.shutdown(); - if (!executor.awaitTermination(Constants.SHARE_USAGE_DEFAULT_HTTP_POST_TIMEOUT, TimeUnit.MILLISECONDS)){ + if (!executor.awaitTermination( + closeWaitLeftMillis(closeStartNanos), + TimeUnit.MILLISECONDS)) { // Sending stops rather than hold up the process that closes - // the element, and the data left is discarded. + // the element, and the data left is discarded, all within + // the close limit. discarding = true; int discarded = evidenceCollection.drainTo(new ArrayList()); @@ -820,7 +846,7 @@ protected void managedResourcesCleanup() { "not finished {} milliseconds after usage sharing " + "started closing.", discarded, - Constants.SHARE_USAGE_DEFAULT_HTTP_POST_TIMEOUT); + closeWaitMillis()); executor.shutdownNow(); } } catch (InterruptedException interruptedException) { From 8a48c33f572876e1ac3dabe6cf7aae426f0cce8d Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Mon, 16 Nov 2026 11:18:36 +0000 Subject: [PATCH 4/6] Set the close time limit from every runner's measurements --- .../flowelements/ShareUsageCloseTimeoutTests.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java index 6d4f31d70..4ecaeaf0c 100644 --- a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java +++ b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java @@ -68,11 +68,13 @@ public class ShareUsageCloseTimeoutTests { private static final int ENTRIES = 10; /** - * The most closing may take from its start, as the specification's - * Cleanup section sets it, holding the wait for the data to be sent, - * discarding the rest, the warning and freeing the resources. + * The most closing may take from its start, holding the wait for the + * data to be sent, discarding the rest, the warning and freeing the + * resources. It sits below the 5 seconds the specification bounds + * cleanup at, above the longest any runner takes with the wait + * bounded, and below the shortest any runner takes without it. */ - private static final long CLOSE_LIMIT_MILLIS = 5000; + private static final long CLOSE_LIMIT_MILLIS = 4800; /** * An upload whose first answer does not come until the test releases @@ -185,7 +187,8 @@ public void ShareUsageElement_ServiceStopsAnswering_CloseWithin5Seconds() "Closing waited for the remote service to answer."); assertTrue(tookMillis[0] <= CLOSE_LIMIT_MILLIS, "Closing took " + tookMillis[0] + " ms, more than the " + - CLOSE_LIMIT_MILLIS + " ms cleanup may take."); + CLOSE_LIMIT_MILLIS + " ms allowed inside the 5 second " + + "bound the specification puts on cleanup."); assertEquals(1, uploader.release.getCount(), "The upload in progress was answered before closing ended."); } From 77a0e5e464e670e33de93dd7cbba8db1d546ef57 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Mon, 16 Nov 2026 15:33:47 +0000 Subject: [PATCH 5/6] Assert the specification's 5 second bound on closing --- .../ShareUsageCloseTimeoutTests.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java index 4ecaeaf0c..0387e738a 100644 --- a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java +++ b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java @@ -68,13 +68,13 @@ public class ShareUsageCloseTimeoutTests { private static final int ENTRIES = 10; /** - * The most closing may take from its start, holding the wait for the - * data to be sent, discarding the rest, the warning and freeing the - * resources. It sits below the 5 seconds the specification bounds - * cleanup at, above the longest any runner takes with the wait - * bounded, and below the shortest any runner takes without it. + * The 5 seconds the specification bounds the whole of cleanup at. + * Closing must take less, holding the wait for the data to be sent, + * discarding the rest, the warning and freeing the resources. Code that + * waits the full 5 seconds before doing the rest always takes at least + * this long, so it fails. */ - private static final long CLOSE_LIMIT_MILLIS = 4800; + private static final long CLOSE_LIMIT_MILLIS = 5000; /** * An upload whose first answer does not come until the test releases @@ -185,10 +185,10 @@ public void ShareUsageElement_ServiceStopsAnswering_CloseWithin5Seconds() System.nanoTime() - start); }, "Closing waited for the remote service to answer."); - assertTrue(tookMillis[0] <= CLOSE_LIMIT_MILLIS, - "Closing took " + tookMillis[0] + " ms, more than the " + - CLOSE_LIMIT_MILLIS + " ms allowed inside the 5 second " + - "bound the specification puts on cleanup."); + assertTrue(tookMillis[0] < CLOSE_LIMIT_MILLIS, + "Closing took " + tookMillis[0] + " ms, not within the " + + CLOSE_LIMIT_MILLIS + " ms the specification puts on " + + "cleanup."); assertEquals(1, uploader.release.getCount(), "The upload in progress was answered before closing ended."); } From 3276aab48ffd9cc4e9cdae75c369a9c7e9566b44 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Mon, 16 Nov 2026 19:56:20 +0000 Subject: [PATCH 6/6] Quote the specification's cleanup sentence in the close limit test The close test asserts the 5,000 ms the usage sharing element specification puts on cleanup. Quote its sentence, at the fixed commit 6570727d, beside the limit, and say why the element closes in a little over 4,500 ms. Comment only, so there is no failing test to add. --- .../ShareUsageCloseTimeoutTests.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java index 0387e738a..a232bcd25 100644 --- a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java +++ b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageCloseTimeoutTests.java @@ -68,11 +68,18 @@ public class ShareUsageCloseTimeoutTests { private static final int ENTRIES = 10; /** - * The 5 seconds the specification bounds the whole of cleanup at. - * Closing must take less, holding the wait for the data to be sent, - * discarding the rest, the warning and freeing the resources. Code that - * waits the full 5 seconds before doing the rest always takes at least - * this long, so it fails. + * The 5 seconds the specification bounds the whole of cleanup at. The + * usage + * sharing element specification says "Cleanup SHOULD complete within + * 5 seconds, so that closing the element does not hold up the process + * that closes it." + *

+ * The limit is the specification's, not a measurement. Closing must take + * less, holding the wait for the data to be sent, discarding the rest, + * the warning and freeing the resources. The element waits 4,500 ms, its + * 5,000 ms budget less a 500 ms reserve, then does the rest, so it + * closes in a little over 4,500 ms. Code that waits the full 5 seconds + * before doing the rest always takes at least this long, so it fails. */ private static final long CLOSE_LIMIT_MILLIS = 5000;