Skip to content

Commit f0bb916

Browse files
authored
Merge branch 'master' into codex/remove-sofa
2 parents 00b8e1f + c896152 commit f0bb916

5 files changed

Lines changed: 72 additions & 25 deletions

File tree

‎shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public void onApplicationEvent(@NonNull final ContextRefreshedEvent event) {
150150
if (MapUtils.isEmpty(beans)) {
151151
return;
152152
}
153-
if (!registered.compareAndSet(false, true)) {
153+
if (!markRegistered()) {
154154
return;
155155
}
156156
if (isDiscoveryLocalMode) {
@@ -372,6 +372,15 @@ public ShenyuClientRegisterEventPublisher getPublisher() {
372372
return publisher;
373373
}
374374

375+
/**
376+
* Mark the listener as registered once.
377+
*
378+
* @return true when this invocation acquires the registration gate
379+
*/
380+
protected boolean markRegistered() {
381+
return registered.compareAndSet(false, true);
382+
}
383+
375384
/**
376385
* Get the metadata map.
377386
*

‎shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListener.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.commons.lang3.StringUtils;
2222
import org.apache.shenyu.client.core.client.AbstractContextRefreshedEventListener;
2323
import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
24-
import org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
2524
import org.apache.shenyu.client.core.utils.PortUtils;
2625
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
2726
import org.apache.shenyu.common.enums.ApiHttpMethodEnum;
@@ -68,8 +67,6 @@ public class SpringMvcClientEventListener extends AbstractContextRefreshedEventL
6867

6968
private static final Logger LOG = LoggerFactory.getLogger(SpringMvcClientEventListener.class);
7069

71-
private final ShenyuClientRegisterEventPublisher publisher = ShenyuClientRegisterEventPublisher.getInstance();
72-
7370
private final List<Class<? extends Annotation>> mappingAnnotation = new ArrayList<>(3);
7471

7572
private final Boolean isFull;
@@ -121,6 +118,9 @@ protected Sextet<String[], String, String, ApiHttpMethodEnum[], RpcTypeEnum, Str
121118
protected Map<String, Object> getBeans(final ApplicationContext context) {
122119
// Filter out
123120
if (Boolean.TRUE.equals(isFull)) {
121+
if (!markRegistered()) {
122+
return Collections.emptyMap();
123+
}
124124
LOG.info("init spring mvc client success with isFull mode");
125125
List<String> namespaceIds = super.getNamespace();
126126
namespaceIds.forEach(namespaceId -> {
@@ -135,7 +135,7 @@ protected Map<String, Object> getBeans(final ApplicationContext context) {
135135
.namespaceId(namespaceId)
136136
.build());
137137

138-
publisher.publishEvent(buildURIRegisterDTO(context, Collections.emptyMap(), namespaceId));
138+
getPublisher().publishEvent(buildURIRegisterDTO(context, Collections.emptyMap(), namespaceId));
139139
});
140140
return Collections.emptyMap();
141141
}

‎shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.shenyu.client.springmvc.init;
1919

2020
import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
21+
import org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
2122
import org.apache.shenyu.client.core.exception.ShenyuClientIllegalArgumentException;
2223
import org.apache.shenyu.client.core.register.ShenyuClientRegisterRepositoryFactory;
2324
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
@@ -178,6 +179,21 @@ public void testOnApplicationEventError() {
178179
registerUtilsMockedStatic.close();
179180
}
180181

182+
@Test
183+
public void testOnApplicationEventFullModeShouldRegisterOnce() {
184+
try (MockedStatic<ShenyuClientRegisterEventPublisher> publisherMockedStatic = mockStatic(ShenyuClientRegisterEventPublisher.class)) {
185+
ShenyuClientRegisterEventPublisher publisher = mock(ShenyuClientRegisterEventPublisher.class);
186+
publisherMockedStatic.when(ShenyuClientRegisterEventPublisher::getInstance).thenReturn(publisher);
187+
SpringMvcClientEventListener springMvcClientEventListener = buildSpringMvcClientEventListener(true, true);
188+
ContextRefreshedEvent event = new ContextRefreshedEvent(applicationContext);
189+
springMvcClientEventListener.onApplicationEvent(event);
190+
springMvcClientEventListener.onApplicationEvent(event);
191+
verify(publisher, times(1)).start(any());
192+
verify(publisher, times(2)).publishEvent(any());
193+
}
194+
registerUtilsMockedStatic.close();
195+
}
196+
181197
@Test
182198
public void testOnBuildApiSuperPath() {
183199
SpringMvcClientEventListener springMvcClientEventListener = buildSpringMvcClientEventListener(false, false);

‎shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientEventListener.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.commons.lang3.StringUtils;
2222
import org.apache.shenyu.client.core.client.AbstractContextRefreshedEventListener;
2323
import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
24-
import org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
2524
import org.apache.shenyu.client.core.utils.PortUtils;
2625
import org.apache.shenyu.client.spring.websocket.annotation.ShenyuServerEndpoint;
2726
import org.apache.shenyu.client.spring.websocket.annotation.ShenyuSpringWebSocketClient;
@@ -62,8 +61,6 @@
6261
*/
6362
public class SpringWebSocketClientEventListener extends AbstractContextRefreshedEventListener<Object, ShenyuSpringWebSocketClient> {
6463

65-
private final ShenyuClientRegisterEventPublisher publisher = ShenyuClientRegisterEventPublisher.getInstance();
66-
6764
private final String[] pathAttributeNames = new String[] {"path", "value"};
6865

6966
private final List<Class<? extends Annotation>> mappingAnnotation = new ArrayList<>(7);
@@ -105,9 +102,12 @@ protected Sextet<String[], String, String, ApiHttpMethodEnum[], RpcTypeEnum, Str
105102
protected Map<String, Object> getBeans(final ApplicationContext context) {
106103
// Filter out is not controller out
107104
if (Boolean.TRUE.equals(isFull)) {
105+
if (!markRegistered()) {
106+
return Collections.emptyMap();
107+
}
108108
LOG.info("init spring websocket client success with isFull mode");
109109
List<String> namespaceIds = super.getNamespace();
110-
namespaceIds.forEach(namespaceId -> publisher.publishEvent(buildURIRegisterDTO(context, Collections.emptyMap(), namespaceId)));
110+
namespaceIds.forEach(namespaceId -> getPublisher().publishEvent(buildURIRegisterDTO(context, Collections.emptyMap(), namespaceId)));
111111
return Collections.emptyMap();
112112
}
113113
Map<String, Object> endpointBeans = context.getBeansWithAnnotation(ShenyuServerEndpoint.class);

‎shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/test/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientEventListenerTest.java‎

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
import org.junit.jupiter.api.Test;
3333
import org.junit.jupiter.api.extension.ExtendWith;
3434
import org.mockito.Mock;
35-
import org.mockito.MockitoAnnotations;
35+
import org.mockito.MockedStatic;
3636
import org.mockito.junit.jupiter.MockitoExtension;
3737
import org.springframework.context.ApplicationContext;
38+
import org.springframework.context.event.ContextRefreshedEvent;
3839
import org.springframework.util.ReflectionUtils;
3940

4041
import java.lang.annotation.Annotation;
@@ -49,7 +50,9 @@
4950
import static org.junit.jupiter.api.Assertions.assertNull;
5051
import static org.mockito.ArgumentMatchers.any;
5152
import static org.mockito.Mockito.mock;
53+
import static org.mockito.Mockito.mockStatic;
5254
import static org.mockito.Mockito.never;
55+
import static org.mockito.Mockito.times;
5356
import static org.mockito.Mockito.verify;
5457
import static org.mockito.Mockito.when;
5558

@@ -84,21 +87,7 @@ public class SpringWebSocketClientEventListenerTest {
8487

8588
@BeforeEach
8689
public void setUp() {
87-
MockitoAnnotations.openMocks(this);
88-
Properties properties = mock(Properties.class);
89-
when(properties.getProperty("appName")).thenReturn("appName");
90-
when(properties.getProperty("contextPath")).thenReturn("contextPath");
91-
when(properties.getProperty(ShenyuClientConstants.PORT)).thenReturn("8080");
92-
when(properties.getProperty(ShenyuClientConstants.HOST)).thenReturn("127.0.0.1");
93-
when(properties.getProperty(ShenyuClientConstants.IP_PORT)).thenReturn("127.0.0.1:8080");
94-
95-
ShenyuClientConfig clientConfig = mock(ShenyuClientConfig.class);
96-
Map<String, ClientPropertiesConfig> client = new HashMap<>();
97-
ClientPropertiesConfig clientPropertiesConfig = new ClientPropertiesConfig();
98-
clientPropertiesConfig.setProps(properties);
99-
client.put(RpcTypeEnum.WEB_SOCKET.getName(), clientPropertiesConfig);
100-
when(clientConfig.getClient()).thenReturn(client);
101-
eventListener = new SpringWebSocketClientEventListener(clientConfig, registerRepository);
90+
eventListener = buildEventListener(false);
10291
}
10392

10493
@Test
@@ -178,6 +167,39 @@ public void testGetPort() {
178167
assertEquals(port, "8080");
179168
}
180169

170+
@Test
171+
public void testOnApplicationEventFullModeShouldRegisterOnce() {
172+
try (MockedStatic<ShenyuClientRegisterEventPublisher> publisherMockedStatic = mockStatic(ShenyuClientRegisterEventPublisher.class)) {
173+
ShenyuClientRegisterEventPublisher mockPublisher = mock(ShenyuClientRegisterEventPublisher.class);
174+
publisherMockedStatic.when(ShenyuClientRegisterEventPublisher::getInstance).thenReturn(mockPublisher);
175+
SpringWebSocketClientEventListener fullModeEventListener = buildEventListener(true);
176+
ContextRefreshedEvent event = new ContextRefreshedEvent(applicationContext);
177+
fullModeEventListener.onApplicationEvent(event);
178+
fullModeEventListener.onApplicationEvent(event);
179+
verify(mockPublisher, times(1)).start(any());
180+
verify(mockPublisher, times(1)).publishEvent(any());
181+
}
182+
}
183+
184+
private SpringWebSocketClientEventListener buildEventListener(final boolean full) {
185+
Properties properties = new Properties();
186+
properties.setProperty("appName", "appName");
187+
properties.setProperty("contextPath", "contextPath");
188+
properties.setProperty(ShenyuClientConstants.PORT, "8080");
189+
properties.setProperty(ShenyuClientConstants.HOST, "127.0.0.1");
190+
properties.setProperty(ShenyuClientConstants.IP_PORT, "127.0.0.1:8080");
191+
properties.setProperty(ShenyuClientConstants.IS_FULL, String.valueOf(full));
192+
properties.setProperty(ShenyuClientConstants.DISCOVERY_LOCAL_MODE_KEY, Boolean.TRUE.toString());
193+
194+
ShenyuClientConfig clientConfig = mock(ShenyuClientConfig.class);
195+
Map<String, ClientPropertiesConfig> client = new HashMap<>();
196+
ClientPropertiesConfig clientPropertiesConfig = new ClientPropertiesConfig();
197+
clientPropertiesConfig.setProps(properties);
198+
client.put(RpcTypeEnum.WEB_SOCKET.getName(), clientPropertiesConfig);
199+
when(clientConfig.getClient()).thenReturn(client);
200+
return new SpringWebSocketClientEventListener(clientConfig, registerRepository);
201+
}
202+
181203
/**
182204
* class for mock.
183205
*/

0 commit comments

Comments
 (0)