Skip to content

Commit 08b8997

Browse files
Make AbstractMomSubscriber thread-safe
The list of subscriptions should be thread-safe, so that registrations can be done in parallel. 411785
1 parent 9fd460e commit 08b8997

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2010, 2026 BSI Business Systems Integration AG
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*/
10+
package org.eclipse.scout.rt.mom.api;
11+
12+
import static org.mockito.Mockito.*;
13+
14+
import java.util.List;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.Stream;
17+
18+
import org.junit.Test;
19+
20+
public class MomSubscriberTest {
21+
22+
@Test
23+
public void testDispose() {
24+
ISubscription subscriptionMock = mock(ISubscription.class);
25+
AbstractMomSubscriber subscriber = new AbstractMomSubscriber() {
26+
@Override
27+
public void subscribe() {
28+
registerSubscription(subscriptionMock);
29+
}
30+
};
31+
subscriber.subscribe();
32+
subscriber.dispose();
33+
verify(subscriptionMock, times(1)).dispose();
34+
}
35+
36+
@Test
37+
public void testConcurrentSubscriptions() {
38+
List<ISubscription> subscriptionMocks = Stream.generate(() -> mock(ISubscription.class))
39+
.limit(10000)
40+
.collect(Collectors.toList());
41+
42+
AbstractMomSubscriber subscriber = new AbstractMomSubscriber() {
43+
@Override
44+
public void subscribe() {
45+
subscriptionMocks.stream().parallel().forEach(this::registerSubscription);
46+
}
47+
};
48+
49+
subscriber.subscribe();
50+
subscriber.dispose();
51+
for (ISubscription subscriptionMock : subscriptionMocks) {
52+
verify(subscriptionMock, times(1)).dispose();
53+
}
54+
}
55+
}

org.eclipse.scout.rt.mom.api/src/main/java/org/eclipse/scout/rt/mom/api/AbstractMomSubscriber.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2023 BSI Business Systems Integration AG
2+
* Copyright (c) 2010, 2026 BSI Business Systems Integration AG
33
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
@@ -10,22 +10,21 @@
1010
package org.eclipse.scout.rt.mom.api;
1111

1212
import java.util.ArrayList;
13+
import java.util.Collections;
1314
import java.util.List;
1415

1516
import org.eclipse.scout.rt.platform.ApplicationScoped;
1617

1718
/**
1819
* Keeps a list of {@link ISubscription}s that are registered during {@link #subscribe()}. All registered subscriptions
1920
* are disposed when {@link #dispose()} is called.
20-
* <p>
21-
* This class is not thread-safe.
2221
*
2322
* @since 6.1
2423
*/
2524
@ApplicationScoped
2625
public abstract class AbstractMomSubscriber {
2726

28-
private final List<ISubscription> m_subscriptions = new ArrayList<>();
27+
private final List<ISubscription> m_subscriptions = Collections.synchronizedList(new ArrayList<>());
2928

3029
public abstract void subscribe();
3130

0 commit comments

Comments
 (0)