This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch experimental/WTES-69-diagnosis in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit 019f9b4962281710ea24fddb47d1485bf8e7563e Author: Stefan Seifert <[email protected]> AuthorDate: Tue Dec 7 12:20:35 2021 +0100 ensure generated service and bundle IDs are unique accross threads --- .../src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java | 5 +++-- .../org/apache/sling/testing/mock/osgi/MockServiceRegistration.java | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java index 5e36191..f53abd4 100644 --- a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java +++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java @@ -29,6 +29,7 @@ import java.util.Enumeration; import java.util.List; import java.util.Map; import java.util.Vector; +import java.util.concurrent.atomic.AtomicLong; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -41,7 +42,7 @@ import org.osgi.framework.Version; */ public final class MockBundle implements Bundle { - private static volatile long bundleCounter; + private static final AtomicLong BUNDLE_ID_COUNTER = new AtomicLong(); private final long bundleId; private final BundleContext bundleContext; @@ -65,7 +66,7 @@ public final class MockBundle implements Bundle { * @param bundleContext Bundle context */ public MockBundle(BundleContext bundleContext) { - this(bundleContext, ++bundleCounter); + this(bundleContext, BUNDLE_ID_COUNTER.incrementAndGet()); } @Override diff --git a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java index 2294611..ad4a824 100644 --- a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java +++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java @@ -25,6 +25,7 @@ import java.util.HashSet; import java.util.Hashtable; import java.util.Map; import java.util.Set; +import java.util.concurrent.atomic.AtomicLong; import org.apache.commons.lang3.StringUtils; import org.apache.felix.framework.FilterImpl; @@ -42,7 +43,7 @@ import org.osgi.framework.ServiceRegistration; */ class MockServiceRegistration<T> implements ServiceRegistration<T>, Comparable<MockServiceRegistration<T>> { - private static volatile long serviceCounter; + private static final AtomicLong SERVICE_ID_COUNTER = new AtomicLong(); private final Long serviceId; private final Set<String> clazzes; @@ -54,7 +55,7 @@ class MockServiceRegistration<T> implements ServiceRegistration<T>, Comparable<M @SuppressWarnings("unchecked") public MockServiceRegistration(final Bundle bundle, final String[] clazzes, final T service, final Dictionary<String, Object> properties, MockBundleContext bundleContext) { - this.serviceId = ++serviceCounter; + this.serviceId = SERVICE_ID_COUNTER.incrementAndGet(); this.clazzes = new HashSet<String>(Arrays.asList(clazzes)); if (service instanceof ServiceFactory) {
