|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shenyu.client.core.register.registrar; |
| 19 | + |
| 20 | +import org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher; |
| 21 | +import org.apache.shenyu.client.core.register.ApiBean; |
| 22 | +import org.apache.shenyu.client.core.register.ClientRegisterConfig; |
| 23 | +import org.apache.shenyu.register.common.dto.URIRegisterDTO; |
| 24 | +import org.apache.shenyu.register.common.enums.EventType; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.mockito.ArgumentCaptor; |
| 28 | +import org.springframework.mock.env.MockEnvironment; |
| 29 | +import org.springframework.test.util.ReflectionTestUtils; |
| 30 | + |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 35 | +import static org.mockito.ArgumentMatchers.any; |
| 36 | +import static org.mockito.Mockito.doNothing; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | +import static org.mockito.Mockito.times; |
| 39 | +import static org.mockito.Mockito.verify; |
| 40 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 41 | +import static org.mockito.Mockito.when; |
| 42 | + |
| 43 | +/** URI instance publication and retry ownership. */ |
| 44 | +class UriApiRegistrarTest { |
| 45 | + |
| 46 | + private final ClientRegisterConfig config = mock(ClientRegisterConfig.class); |
| 47 | + |
| 48 | + private final ShenyuClientRegisterEventPublisher publisher = mock(ShenyuClientRegisterEventPublisher.class); |
| 49 | + |
| 50 | + private UriApiRegistrarImplImpl registrar; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + void setUp() { |
| 54 | + when(config.getNamespace()).thenReturn(List.of("one", "two")); |
| 55 | + when(config.getHost()).thenReturn("127.0.0.1"); |
| 56 | + when(config.getPort()).thenReturn(8080); |
| 57 | + when(config.getAppName()).thenReturn("app"); |
| 58 | + when(config.getContextPath()).thenReturn("/app"); |
| 59 | + registrar = new UriApiRegistrarImplImpl(config); |
| 60 | + ReflectionTestUtils.setField(registrar, "publisher", publisher); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testBeanAndApisShareIdentityEvenAfterHeartbeatMutation() throws Exception { |
| 65 | + registrar.register(bean()); |
| 66 | + ArgumentCaptor<URIRegisterDTO> events = ArgumentCaptor.forClass(URIRegisterDTO.class); |
| 67 | + verify(publisher, times(2)).publishEvent(events.capture()); |
| 68 | + events.getAllValues().forEach(uri -> uri.setEventType(EventType.HEARTBEAT)); |
| 69 | + registrar.register(bean()); |
| 70 | + verify(publisher, times(2)).publishEvent(any()); |
| 71 | + assertEquals(URIRegisterDTO.class, registrar.registerDataType()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testEnqueueFailureCanRetryWithoutRepeatingPublishedNamespaces() throws Exception { |
| 76 | + doNothing().doThrow(new IllegalStateException("queue unavailable")).doNothing().when(publisher).publishEvent(any()); |
| 77 | + assertThrows(IllegalStateException.class, () -> registrar.register(bean())); |
| 78 | + registrar.register(bean()); |
| 79 | + ArgumentCaptor<URIRegisterDTO> events = ArgumentCaptor.forClass(URIRegisterDTO.class); |
| 80 | + verify(publisher, times(3)).publishEvent(events.capture()); |
| 81 | + assertEquals(List.of("one", "two", "two"), events.getAllValues().stream().map(URIRegisterDTO::getNamespaceId).toList()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testChangedInstanceIdentityIsNotSuppressed() throws Exception { |
| 86 | + registrar.register(bean()); |
| 87 | + when(config.getAppName()).thenReturn("other-app"); |
| 88 | + registrar.register(bean()); |
| 89 | + when(config.getContextPath()).thenReturn("/other"); |
| 90 | + registrar.register(bean()); |
| 91 | + when(config.getPort()).thenReturn(8081); |
| 92 | + registrar.register(bean()); |
| 93 | + when(config.getHost()).thenReturn("127.0.0.2"); |
| 94 | + registrar.register(bean()); |
| 95 | + verify(publisher, times(10)).publishEvent(any()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testRpcTypesHaveIndependentIdentities() throws Exception { |
| 100 | + registrar.register(bean()); |
| 101 | + ApiBean other = bean(); |
| 102 | + other.addProperties("rpcType", "grpc"); |
| 103 | + other.getApiDefinitions().forEach(api -> api.addProperties("rpcType", "grpc")); |
| 104 | + registrar.register(other); |
| 105 | + verify(publisher, times(4)).publishEvent(any()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testInvalidPortIsNotPublished() { |
| 110 | + for (int port : List.of(-1, 0, 65536)) { |
| 111 | + when(config.getPort()).thenReturn(port); |
| 112 | + assertThrows(IllegalArgumentException.class, () -> registrar.register(bean())); |
| 113 | + } |
| 114 | + verifyNoInteractions(publisher); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void testNonLocalDiscoveryDoesNotReadLocalAddress() throws Exception { |
| 119 | + for (String mode : List.of("nacos", "LOCAL", "")) { |
| 120 | + registrar.setEnvironment(new MockEnvironment().withProperty("shenyu.discovery.type", mode)); |
| 121 | + registrar.register(bean()); |
| 122 | + } |
| 123 | + verifyNoInteractions(publisher); |
| 124 | + } |
| 125 | + |
| 126 | + private ApiBean bean() throws Exception { |
| 127 | + ApiBean bean = new ApiBean("http", "endpoints", this, "/api"); |
| 128 | + bean.addApiDefinition(Object.class.getMethod("toString"), "/one"); |
| 129 | + bean.addApiDefinition(Object.class.getMethod("hashCode"), "/two"); |
| 130 | + bean.setStatus(ApiBean.Status.REGISTRABLE); |
| 131 | + bean.getApiDefinitions().forEach(api -> api.setStatus(ApiBean.Status.REGISTRABLE)); |
| 132 | + return bean; |
| 133 | + } |
| 134 | +} |
0 commit comments