Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
97af6d5
Test: a cache neither stores nor serves a cloud response with a top-l…
jwrosewell Nov 15, 2026
d26dd96
Test: only the result with a 51Did found in the cache is kept out of it
jwrosewell Nov 15, 2026
277e08d
Fix: keep a cloud response with a top-level fodid out of the result c…
jwrosewell Nov 15, 2026
2acc1b9
Test: the Pipeline interface exposes isSuppressProcessExceptions
jwrosewell Nov 15, 2026
7d91183
Fix: expose isSuppressProcessExceptions on the Pipeline interface
jwrosewell Nov 15, 2026
4486b4f
Test: only one thread at a time reads the Device Detection native res…
jwrosewell Nov 15, 2026
1366d15
Fix: hold the results lock for every read of the Device Detection nat…
jwrosewell Nov 15, 2026
7802440
Test: Latitude and Longitude are given rounded to 3 decimal places
jwrosewell Nov 15, 2026
1a4236f
Fix: move cxx to 178371ac, which rounds Azimuth and Declination numbers
jwrosewell Nov 15, 2026
bec88e9
Test: a built Pipeline's elements cannot be changed, even through its…
jwrosewell Nov 15, 2026
e72bbb7
Fix: give each Pipeline its own copy of the builder's elements
jwrosewell Nov 15, 2026
99763c0
Test: cloud and on-premise engines give results under one key and one…
jwrosewell Nov 15, 2026
f7bb902
Test: one JSON Builder in two Pipelines uses each Pipeline's own conf…
jwrosewell Nov 15, 2026
075ea83
Test: Flow Data takes writes from several threads, and locks only whe…
jwrosewell Nov 15, 2026
bed28c7
Test: FlowElement requires the Evidence keys, the Properties and the …
jwrosewell Nov 15, 2026
8a8f512
Test: cache keys come from the advertised Evidence, with values compa…
jwrosewell Nov 15, 2026
f9b0f3b
Test: a cached engine takes Evidence keys in any case as the same key
jwrosewell Nov 15, 2026
b4419d0
Test: an error in the web filter does not stop the request when excep…
jwrosewell Nov 15, 2026
d4e4bdd
Fix: let the web filter suppress its own errors as the Pipeline is se…
jwrosewell Nov 15, 2026
0425ec7
Test: an unexpected error in the background update check is logged an…
jwrosewell Nov 15, 2026
6dc6f2c
Fix: log an unexpected error in a background data update task
jwrosewell Nov 15, 2026
83eea04
Test: a Pipeline set not to close its elements leaves a shared elemen…
jwrosewell Nov 15, 2026
c334cd8
Test: closing Flow Data leaves Element Data that needs no cleanup alone
jwrosewell Nov 15, 2026
616eb15
Test: FlowData declares the exceptions its methods throw, and throws …
jwrosewell Nov 15, 2026
9497185
Fix: declare on FlowData the exceptions its methods throw
jwrosewell Nov 15, 2026
e52c0ad
Test: getWhere leaves out data that is not there rather than throwing
jwrosewell Nov 15, 2026
cab1d74
Fix: leave out of getWhere the data that is not there, and say what g…
jwrosewell Nov 15, 2026
2317375
Test: every builder setter in the pipeline packages gives its default…
jwrosewell Nov 15, 2026
5f5c8d2
Fix: give the default value beside every builder setter in the pipeli…
jwrosewell Nov 15, 2026
44eebc4
Test: only one thread at a time reads the IP Intelligence native results
jwrosewell Nov 15, 2026
3e88f26
Fix: hold the results lock for every read of the IP Intelligence nati…
jwrosewell Nov 15, 2026
b4b0df1
Test: every Device Detection and IP Intelligence builder setter gives…
jwrosewell Nov 15, 2026
55661b7
Fix: give the default beside every Device Detection and IP Intelligen…
jwrosewell Nov 15, 2026
8ad48e8
Test: a cached lazy cloud engine returns without waiting for the clou…
jwrosewell Nov 15, 2026
089dfbb
Fix: decide whether a lazy result may be cached without waiting for it
jwrosewell Nov 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cxx
Submodule cxx updated 157 files
Original file line number Diff line number Diff line change
Expand Up @@ -259,33 +259,45 @@ private AspectPropertyValue<List<String>> getUserAgentsInternal() {
@Override
protected boolean propertyIsAvailable(String propertyName) {
checkState();
for (ResultsHashSwig results : resultsList) {
if (results.containsProperty(Swig.asBytes(propertyName))) {
return true;
synchronized (getResultsLock()) {
for (ResultsHashSwig results : resultsList) {
if (results.containsProperty(Swig.asBytes(propertyName))) {
return true;
}
}
return false;
}
return false;
}

/**
* Get the values of a property as a list. This is public, so it holds
* the results lock itself, as every read from the native results does.
* See the
* <a href="https://github.com/51Degrees/specifications/blob/706de1f0cb3edd560eb7fd7ef3bb19b93a4883dc/pipeline-specification/features/thread-safety.md#L61">thread safety specification</a>.
* @param propertyName name of the property to get values for
* @return values as a list
*/
@Override
public AspectPropertyValue<List<String>> getValues(String propertyName) {
checkState();
AspectPropertyValue<List<String>> result =
new AspectPropertyValueDefault<>();
ResultsHashSwig results = getSingleResults(propertyName);
if (results != null) {
Integer index = indexOf(propertyName);
try (VectorStringValuesSwig value = index == null ?
results.getValues(Swig.asBytes(propertyName)) :
results.getValues(index)) {
if (value.hasValue()) {
try (VectorStringSwig vector = value.getValue()) {
result.setValue(Collections.unmodifiableList(
Swig.asList(vector)));
synchronized (getResultsLock()) {
ResultsHashSwig results = getSingleResults(propertyName);
if (results != null) {
Integer index = indexOf(propertyName);
try (VectorStringValuesSwig value = index == null ?
results.getValues(Swig.asBytes(propertyName)) :
results.getValues(index)) {
if (value.hasValue()) {
try (VectorStringSwig vector = value.getValue()) {
result.setValue(Collections.unmodifiableList(
Swig.asList(vector)));
}
}
else {
result.setNoValueMessage(value.getNoValueMessage());
}
}
else {
result.setNoValueMessage(value.getNoValueMessage());
}
}
}
Expand Down Expand Up @@ -407,29 +419,33 @@ protected <T> TryGetResult<T> tryGetValue(
if (result.hasValue() == false) {
boolean objSet = false;
Object obj = null;
if (key.equalsIgnoreCase("DeviceId")) {
obj = getDeviceIdInternal();
objSet = true;
} else if (key.equalsIgnoreCase("Difference")) {
obj = getDifferenceInternal();
objSet = true;
} else if (key.equalsIgnoreCase("UserAgents")) {
obj = getUserAgentsInternal();
objSet = true;
} else if (key.equalsIgnoreCase("Drift")) {
obj = getDriftInternal();
objSet = true;
} else if (key.equalsIgnoreCase("Iterations")) {
obj = getIterationsInternal();
objSet = true;
}
else if (key.equalsIgnoreCase("MatchedNodes")) {
obj = getMatchedNodesInternal();
objSet = true;
}
else if (key.equalsIgnoreCase("Method")){
obj = getMethodInternal();
objSet = true;
// The match metrics read the native results, so they hold the
// results lock as every other read does.
synchronized (getResultsLock()) {
if (key.equalsIgnoreCase("DeviceId")) {
obj = getDeviceIdInternal();
objSet = true;
} else if (key.equalsIgnoreCase("Difference")) {
obj = getDifferenceInternal();
objSet = true;
} else if (key.equalsIgnoreCase("UserAgents")) {
obj = getUserAgentsInternal();
objSet = true;
} else if (key.equalsIgnoreCase("Drift")) {
obj = getDriftInternal();
objSet = true;
} else if (key.equalsIgnoreCase("Iterations")) {
obj = getIterationsInternal();
objSet = true;
}
else if (key.equalsIgnoreCase("MatchedNodes")) {
obj = getMatchedNodesInternal();
objSet = true;
}
else if (key.equalsIgnoreCase("Method")){
obj = getMethodInternal();
objSet = true;
}
}
if (objSet == true) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public DeviceDetectionHashEngineBuilder setPerformanceProfile(
}
}

@DefaultValue("Balanced")
@Override
public DeviceDetectionHashEngineBuilder setPerformanceProfile(
PerformanceProfiles profile) {
Expand Down Expand Up @@ -285,6 +286,7 @@ public DeviceDetectionHashEngineBuilder setUsePredictiveGraph(boolean use) {
return this;
}

@DefaultValue("No results cache, and none can be set for this engine")
@Override
public DeviceDetectionHashEngineBuilder setCache(
CacheConfiguration cacheConfiguration) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */

package fiftyone.devicedetection.hash.engine.onpremise.flowelements;

import fiftyone.devicedetection.hash.engine.onpremise.interop.swig.ResultsHashSwig;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.engines.services.MissingPropertyService;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Reads of finished Device Detection on-premise results are safe from
* several threads, because only one thread at a time reads from the native
* results. See
* <a href="https://github.com/51Degrees/specifications/blob/706de1f0cb3edd560eb7fd7ef3bb19b93a4883dc/pipeline-specification/features/thread-safety.md#L57">thread-safety.md line 57</a>:
* "Reading an Element Data instance once the Flow Element that writes it
* has finished MUST therefore be safe from several threads at once." and
* <a href="https://github.com/51Degrees/specifications/blob/706de1f0cb3edd560eb7fd7ef3bb19b93a4883dc/pipeline-specification/features/thread-safety.md#L61">line 61</a>:
* "Such Element Data MUST let only one thread at a time read from its
* results".
* <p>
* The native results are a mock. The first thread's read is held inside a
* native call until the test lets it go. The second thread must then wait
* to enter the lock, which the test sees from the thread's state, and must
* not reach the native results. Nothing here depends on how long anything
* takes; the deadline only stops a broken run from hanging.
*/
public class DeviceDataHashThreadSafetyTests {

private static final long DEADLINE_MILLIS = 60_000;

/**
* Native results whose every read counts the threads inside it, and
* holds the first thread's first read until released.
*/
private static class Gate implements Answer<Object> {
final AtomicInteger inside = new AtomicInteger();
final AtomicInteger most = new AtomicInteger();
final CountDownLatch firstInside = new CountDownLatch(1);
final CountDownLatch release = new CountDownLatch(1);
volatile Thread first;

@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
String name = invocation.getMethod().getName();
if (name.equals("close") || name.equals("delete") ||
name.equals("finalize")) {
return null;
}
int now = inside.incrementAndGet();
most.accumulateAndGet(now, Math::max);
try {
if (Thread.currentThread() == first &&
firstInside.getCount() > 0) {
firstInside.countDown();
assertTrue(release.await(
DEADLINE_MILLIS, TimeUnit.MILLISECONDS));
}
return valueFor(invocation);
} finally {
inside.decrementAndGet();
}
}

private Object valueFor(InvocationOnMock invocation) {
Class<?> type = invocation.getMethod().getReturnType();
if (type == boolean.class) {
Object arg = invocation.getArguments().length > 0 ?
invocation.getArgument(0) : null;
String property = arg instanceof byte[] ?
new String((byte[]) arg, StandardCharsets.UTF_8) :
String.valueOf(arg);
return property.toLowerCase().contains("ismobile");
}
if (type == int.class) {
return 0;
}
if (type == String.class) {
return "0-0-0-0";
}
if (type == void.class) {
return null;
}
// A native value holder, such as VectorStringValuesSwig, with
// no value.
return mock(type);
}
}

private DeviceDataHashDefault newData(Gate gate) {
FlowData flowData = mock(FlowData.class);
when(flowData.getPipeline()).thenReturn(mock(Pipeline.class));
DeviceDataHashDefault data = new DeviceDataHashDefault(
mock(Logger.class),
flowData,
mock(DeviceDetectionHashEngine.class),
mock(MissingPropertyService.class));
data.setResults(mock(ResultsHashSwig.class, gate));
return data;
}

/**
* Start the first read and hold it inside the native results, then start
* the second read and wait until it either waits for the lock or reaches
* the native results. Then let both finish.
*/
private void assertOneReaderAtATime(
Consumer<DeviceDataHashDefault> firstRead,
Consumer<DeviceDataHashDefault> secondRead) throws Exception {
Gate gate = new Gate();
DeviceDataHashDefault data = newData(gate);
AtomicReference<Throwable> failure = new AtomicReference<>();

Thread first = new Thread(() -> run(firstRead, data, failure));
gate.first = first;
first.start();
assertTrue("The first read did not reach the native results.",
gate.firstInside.await(DEADLINE_MILLIS, TimeUnit.MILLISECONDS));

Thread second = new Thread(() -> run(secondRead, data, failure));
second.start();
long deadline = System.currentTimeMillis() + DEADLINE_MILLIS;
while (gate.most.get() < 2 &&
second.isAlive() &&
waitingForTheLock(second) == false) {
assertTrue("The second read neither waited nor read.",
System.currentTimeMillis() < deadline);
Thread.sleep(1);
}

gate.release.countDown();
first.join(DEADLINE_MILLIS);
second.join(DEADLINE_MILLIS);
assertNull("A read failed: " + failure.get(), failure.get());
assertEquals(
"Two threads read from the native results at once.",
1,
gate.most.get());
}

private static void run(
Consumer<DeviceDataHashDefault> read,
DeviceDataHashDefault data,
AtomicReference<Throwable> failure) {
try {
read.accept(data);
} catch (Throwable t) {
failure.compareAndSet(null, t);
}
}

/**
* True if the thread is blocked entering a monitor in the Device
* Detection Element Data, rather than anywhere else.
*/
private static boolean waitingForTheLock(Thread thread) {
if (thread.getState() != Thread.State.BLOCKED) {
return false;
}
StackTraceElement[] stack = thread.getStackTrace();
return stack.length > 0 &&
stack[0].getClassName().startsWith("fiftyone.devicedetection.");
}

@Test
public void getValues_WaitsForAnotherGetValues() throws Exception {
assertOneReaderAtATime(
d -> d.getValues("IsMobile"),
d -> d.getValues("IsMobile"));
}

@Test
public void getValues_WaitsForAPropertyGetter() throws Exception {
assertOneReaderAtATime(
d -> d.getIsMobile(),
d -> d.getValues("IsMobile"));
}

@Test
public void propertyGetter_WaitsForGetValues() throws Exception {
assertOneReaderAtATime(
d -> d.getValues("IsMobile"),
d -> d.getIsMobile());
}

@Test
public void matchMetric_WaitsForGetValues() throws Exception {
assertOneReaderAtATime(
d -> d.getValues("IsMobile"),
d -> d.getDifference());
}

@Test
public void deviceId_WaitsForAPropertyGetter() throws Exception {
assertOneReaderAtATime(
d -> d.getIsMobile(),
d -> d.getDeviceId());
}

@Test
public void getValues_WaitsForAMatchMetric() throws Exception {
assertOneReaderAtATime(
d -> d.getDrift(),
d -> d.getValues("IsMobile"));
}
}
Loading
Loading