Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void onApplicationEvent(@NonNull final ContextRefreshedEvent event) {
if (MapUtils.isEmpty(beans)) {
return;
}
if (!registered.compareAndSet(false, true)) {
if (!markRegistered()) {
return;
}
if (isDiscoveryLocalMode) {
Expand Down Expand Up @@ -372,6 +372,15 @@ public ShenyuClientRegisterEventPublisher getPublisher() {
return publisher;
}

/**
* Mark the listener as registered once.
*
* @return true when this invocation acquires the registration gate
*/
protected boolean markRegistered() {
return registered.compareAndSet(false, true);
}

/**
* Get the metadata map.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.client.core.client.AbstractContextRefreshedEventListener;
import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
import org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
import org.apache.shenyu.client.core.utils.PortUtils;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.apache.shenyu.common.enums.ApiHttpMethodEnum;
Expand Down Expand Up @@ -68,8 +67,6 @@ public class SpringMvcClientEventListener extends AbstractContextRefreshedEventL

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

private final ShenyuClientRegisterEventPublisher publisher = ShenyuClientRegisterEventPublisher.getInstance();

private final List<Class<? extends Annotation>> mappingAnnotation = new ArrayList<>(3);

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

publisher.publishEvent(buildURIRegisterDTO(context, Collections.emptyMap(), namespaceId));
getPublisher().publishEvent(buildURIRegisterDTO(context, Collections.emptyMap(), namespaceId));
});
return Collections.emptyMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shenyu.client.springmvc.init;

import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
import org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
import org.apache.shenyu.client.core.exception.ShenyuClientIllegalArgumentException;
import org.apache.shenyu.client.core.register.ShenyuClientRegisterRepositoryFactory;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
Expand Down Expand Up @@ -178,6 +179,21 @@ public void testOnApplicationEventError() {
registerUtilsMockedStatic.close();
}

@Test
public void testOnApplicationEventFullModeShouldRegisterOnce() {
try (MockedStatic<ShenyuClientRegisterEventPublisher> publisherMockedStatic = mockStatic(ShenyuClientRegisterEventPublisher.class)) {
ShenyuClientRegisterEventPublisher publisher = mock(ShenyuClientRegisterEventPublisher.class);
publisherMockedStatic.when(ShenyuClientRegisterEventPublisher::getInstance).thenReturn(publisher);
SpringMvcClientEventListener springMvcClientEventListener = buildSpringMvcClientEventListener(true, true);
ContextRefreshedEvent event = new ContextRefreshedEvent(applicationContext);
springMvcClientEventListener.onApplicationEvent(event);
springMvcClientEventListener.onApplicationEvent(event);
verify(publisher, times(1)).start(any());
verify(publisher, times(2)).publishEvent(any());
}
registerUtilsMockedStatic.close();
}

@Test
public void testOnBuildApiSuperPath() {
SpringMvcClientEventListener springMvcClientEventListener = buildSpringMvcClientEventListener(false, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.client.core.client.AbstractContextRefreshedEventListener;
import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
import org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
import org.apache.shenyu.client.core.utils.PortUtils;
import org.apache.shenyu.client.spring.websocket.annotation.ShenyuServerEndpoint;
import org.apache.shenyu.client.spring.websocket.annotation.ShenyuSpringWebSocketClient;
Expand Down Expand Up @@ -62,8 +61,6 @@
*/
public class SpringWebSocketClientEventListener extends AbstractContextRefreshedEventListener<Object, ShenyuSpringWebSocketClient> {

private final ShenyuClientRegisterEventPublisher publisher = ShenyuClientRegisterEventPublisher.getInstance();

private final String[] pathAttributeNames = new String[] {"path", "value"};

private final List<Class<? extends Annotation>> mappingAnnotation = new ArrayList<>(7);
Expand Down Expand Up @@ -105,9 +102,12 @@ protected Sextet<String[], String, String, ApiHttpMethodEnum[], RpcTypeEnum, Str
protected Map<String, Object> getBeans(final ApplicationContext context) {
// Filter out is not controller out
if (Boolean.TRUE.equals(isFull)) {
if (!markRegistered()) {
return Collections.emptyMap();
}
LOG.info("init spring websocket client success with isFull mode");
List<String> namespaceIds = super.getNamespace();
namespaceIds.forEach(namespaceId -> publisher.publishEvent(buildURIRegisterDTO(context, Collections.emptyMap(), namespaceId)));
namespaceIds.forEach(namespaceId -> getPublisher().publishEvent(buildURIRegisterDTO(context, Collections.emptyMap(), namespaceId)));
return Collections.emptyMap();
}
Map<String, Object> endpointBeans = context.getBeansWithAnnotation(ShenyuServerEndpoint.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.util.ReflectionUtils;

import java.lang.annotation.Annotation;
Expand All @@ -49,7 +50,9 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -84,21 +87,7 @@ public class SpringWebSocketClientEventListenerTest {

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
Properties properties = mock(Properties.class);
when(properties.getProperty("appName")).thenReturn("appName");
when(properties.getProperty("contextPath")).thenReturn("contextPath");
when(properties.getProperty(ShenyuClientConstants.PORT)).thenReturn("8080");
when(properties.getProperty(ShenyuClientConstants.HOST)).thenReturn("127.0.0.1");
when(properties.getProperty(ShenyuClientConstants.IP_PORT)).thenReturn("127.0.0.1:8080");

ShenyuClientConfig clientConfig = mock(ShenyuClientConfig.class);
Map<String, ClientPropertiesConfig> client = new HashMap<>();
ClientPropertiesConfig clientPropertiesConfig = new ClientPropertiesConfig();
clientPropertiesConfig.setProps(properties);
client.put(RpcTypeEnum.WEB_SOCKET.getName(), clientPropertiesConfig);
when(clientConfig.getClient()).thenReturn(client);
eventListener = new SpringWebSocketClientEventListener(clientConfig, registerRepository);
eventListener = buildEventListener(false);
Comment on lines 88 to +90
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed MockitoAnnotations.openMocks(this) and the unused import. @ExtendWith(MockitoExtension.class) now owns mock initialization for this test.

}

@Test
Expand Down Expand Up @@ -178,6 +167,39 @@ public void testGetPort() {
assertEquals(port, "8080");
}

@Test
public void testOnApplicationEventFullModeShouldRegisterOnce() {
try (MockedStatic<ShenyuClientRegisterEventPublisher> publisherMockedStatic = mockStatic(ShenyuClientRegisterEventPublisher.class)) {
ShenyuClientRegisterEventPublisher mockPublisher = mock(ShenyuClientRegisterEventPublisher.class);
publisherMockedStatic.when(ShenyuClientRegisterEventPublisher::getInstance).thenReturn(mockPublisher);
SpringWebSocketClientEventListener fullModeEventListener = buildEventListener(true);
ContextRefreshedEvent event = new ContextRefreshedEvent(applicationContext);
fullModeEventListener.onApplicationEvent(event);
fullModeEventListener.onApplicationEvent(event);
verify(mockPublisher, times(1)).start(any());
verify(mockPublisher, times(1)).publishEvent(any());
}
}

private SpringWebSocketClientEventListener buildEventListener(final boolean full) {
Properties properties = new Properties();
properties.setProperty("appName", "appName");
properties.setProperty("contextPath", "contextPath");
properties.setProperty(ShenyuClientConstants.PORT, "8080");
properties.setProperty(ShenyuClientConstants.HOST, "127.0.0.1");
properties.setProperty(ShenyuClientConstants.IP_PORT, "127.0.0.1:8080");
properties.setProperty(ShenyuClientConstants.IS_FULL, String.valueOf(full));
properties.setProperty(ShenyuClientConstants.DISCOVERY_LOCAL_MODE_KEY, Boolean.TRUE.toString());

ShenyuClientConfig clientConfig = mock(ShenyuClientConfig.class);
Map<String, ClientPropertiesConfig> client = new HashMap<>();
ClientPropertiesConfig clientPropertiesConfig = new ClientPropertiesConfig();
clientPropertiesConfig.setProps(properties);
client.put(RpcTypeEnum.WEB_SOCKET.getName(), clientPropertiesConfig);
when(clientConfig.getClient()).thenReturn(client);
return new SpringWebSocketClientEventListener(clientConfig, registerRepository);
}

/**
* class for mock.
*/
Expand Down
Loading