From f55435faff04a425868aefcb5767c028df84a411 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 18 Feb 2025 14:17:01 +0800 Subject: [PATCH] add codecov --- .github/workflows/ci.yml | 20 ++ .travis.yml | 1 - .../motan-benchmark-client/pom.xml | 4 +- .../motan-benchmark-server/pom.xml | 4 +- .../serialize/protobuf/TestProtoBuf.java | 184 +++++++++--------- .../registry/weibomesh/MeshRegistryTest.java | 18 +- .../transport/netty/NettyClientTest.java | 10 +- .../transport/netty4/NettyClientTest.java | 9 +- .../netty4/admin/AdminRpcServerTest.java | 2 + pom.xml | 20 ++ 10 files changed, 164 insertions(+), 108 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..97d3e0018 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,20 @@ +name: Workflow for Codecov +on: [ push, pull_request ] +jobs: + run: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up JDK 8 + uses: actions/setup-java@v1 + with: + java-version: 8 + - name: Install dependencies + run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V + - name: Run tests and collect coverage + run: mvn -B test + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v5 + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index dff5f3a5d..000000000 --- a/.travis.yml +++ /dev/null @@ -1 +0,0 @@ -language: java diff --git a/motan-benchmark/motan-benchmark-client/pom.xml b/motan-benchmark/motan-benchmark-client/pom.xml index 4e5dd545c..e99a1d265 100644 --- a/motan-benchmark/motan-benchmark-client/pom.xml +++ b/motan-benchmark/motan-benchmark-client/pom.xml @@ -40,7 +40,9 @@ maven-assembly-plugin - src/main/assembly/assembly.xml + + src/main/assembly/assembly.xml + diff --git a/motan-benchmark/motan-benchmark-server/pom.xml b/motan-benchmark/motan-benchmark-server/pom.xml index 5f5f6c1fb..9a1f381eb 100644 --- a/motan-benchmark/motan-benchmark-server/pom.xml +++ b/motan-benchmark/motan-benchmark-server/pom.xml @@ -40,7 +40,9 @@ maven-assembly-plugin - src/main/assembly/assembly.xml + + src/main/assembly/assembly.xml + diff --git a/motan-extension/serialization-extension/src/test/java/com/weibo/api/motan/serialize/protobuf/TestProtoBuf.java b/motan-extension/serialization-extension/src/test/java/com/weibo/api/motan/serialize/protobuf/TestProtoBuf.java index dd368c934..4207ee463 100644 --- a/motan-extension/serialization-extension/src/test/java/com/weibo/api/motan/serialize/protobuf/TestProtoBuf.java +++ b/motan-extension/serialization-extension/src/test/java/com/weibo/api/motan/serialize/protobuf/TestProtoBuf.java @@ -15,105 +15,103 @@ */ package com.weibo.api.motan.serialize.protobuf; -import com.weibo.api.motan.exception.MotanBizException; -import com.weibo.api.motan.exception.MotanServiceException; -import com.weibo.api.motan.serialize.ProtobufSerialization; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - import com.weibo.api.motan.config.ProtocolConfig; import com.weibo.api.motan.config.RefererConfig; import com.weibo.api.motan.config.RegistryConfig; import com.weibo.api.motan.config.ServiceConfig; +import com.weibo.api.motan.exception.MotanBizException; +import com.weibo.api.motan.exception.MotanServiceException; import com.weibo.api.motan.serialize.protobuf.gen.UserProto.Address; import com.weibo.api.motan.serialize.protobuf.gen.UserProto.User; - -import java.io.IOException; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; public class TestProtoBuf { - private ServiceConfig serviceConfig; - private RefererConfig refererConfig; - private HelloService service; - - @Before - public void setUp() throws InterruptedException { - ProtocolConfig protocolConfig = new ProtocolConfig(); - protocolConfig.setId("testMotan"); - protocolConfig.setName("motan"); - protocolConfig.setSerialization("protobuf"); - protocolConfig.setCodec("motan"); - - RegistryConfig registryConfig = new RegistryConfig(); - registryConfig.setAddress("127.0.0.1"); - registryConfig.setPort(8002); - - serviceConfig = new ServiceConfig<>(); - serviceConfig.setRef(new HelloServiceImpl()); - serviceConfig.setInterface(HelloService.class); - serviceConfig.setProtocol(protocolConfig); - serviceConfig.setExport("testMotan:18002"); - serviceConfig.setRegistry(registryConfig); - serviceConfig.setShareChannel(true); - serviceConfig.export(); - - refererConfig = new RefererConfig<>(); - refererConfig.setDirectUrl("127.0.0.1:18002"); - refererConfig.setProtocol(protocolConfig); - refererConfig.setInterface(HelloService.class); - - service = refererConfig.getRef(); - Thread.sleep(20L); - } - - @Test - public void testPrimitiveType() { - Assert.assertEquals("-1", service.sumAsString(Integer.MAX_VALUE, Integer.MIN_VALUE)); - Assert.assertEquals("-1", service.sumAsString(-2, 1)); - Assert.assertEquals((Long) 100L, service.boxIfNotZero(100)); - Assert.assertNull(service.boxIfNotZero(0)); - } - - @Test - public void testException() { - try { - service.testException(); - Assert.fail("should throw MotanServiceException"); - } catch (MotanServiceException mse){ - Assert.assertTrue(mse.getMessage().contains(MotanBizException.class.getName())); - Assert.assertTrue(mse.getMessage().contains("provider call process error")); - } - } - - @Test - public void testNull() { - Assert.assertTrue(service.isNull(null)); - - User user = User.newBuilder().setId(120).setName("zhou").build(); - - Assert.assertFalse(service.isNull(user)); - } - - @Test - public void testProtobuf() { - Address address = service.queryByUid(1); - Assert.assertEquals(1, address.getId()); - - User user = User.newBuilder().setId(120).setName("zhou").setGender(false).addAddress(address).build(); - - Assert.assertTrue(service.isUserAddress(user, address)); - - User newOne = service.copy(user); - Assert.assertEquals(user.getId(), newOne.getId()); - Assert.assertEquals(user.getName(), newOne.getName()); - Assert.assertEquals(user.getGender(), newOne.getGender()); - Assert.assertEquals(user.getAddress(0).getId(), newOne.getAddress(0).getId()); - } - - @After - public void tearDown() { - refererConfig.destroy(); - serviceConfig.unexport(); - } + private ServiceConfig serviceConfig; + private RefererConfig refererConfig; + private HelloService service; + + @Before + public void setUp() throws InterruptedException { + ProtocolConfig protocolConfig = new ProtocolConfig(); + protocolConfig.setId("testMotan"); + protocolConfig.setName("motan"); + protocolConfig.setSerialization("protobuf"); + protocolConfig.setCodec("motan"); + + RegistryConfig registryConfig = new RegistryConfig(); + registryConfig.setAddress("127.0.0.1"); + registryConfig.setPort(8002); + + serviceConfig = new ServiceConfig<>(); + serviceConfig.setRef(new HelloServiceImpl()); + serviceConfig.setInterface(HelloService.class); + serviceConfig.setProtocol(protocolConfig); + serviceConfig.setExport("testMotan:18002"); + serviceConfig.setRegistry(registryConfig); + serviceConfig.setShareChannel(true); + serviceConfig.export(); + + refererConfig = new RefererConfig<>(); + refererConfig.setDirectUrl("127.0.0.1:18002"); + refererConfig.setProtocol(protocolConfig); + refererConfig.setInterface(HelloService.class); + // 设置超时时间为1秒 + refererConfig.setRequestTimeout(1000); + + service = refererConfig.getRef(); + Thread.sleep(20L); + } + + @Test + public void testPrimitiveType() { + Assert.assertEquals("-1", service.sumAsString(Integer.MAX_VALUE, Integer.MIN_VALUE)); + Assert.assertEquals("-1", service.sumAsString(-2, 1)); + Assert.assertEquals((Long) 100L, service.boxIfNotZero(100)); + Assert.assertNull(service.boxIfNotZero(0)); + } + + @Test + public void testException() { + try { + service.testException(); + Assert.fail("should throw MotanServiceException"); + } catch (MotanServiceException mse) { + Assert.assertTrue(mse.getMessage().contains(MotanBizException.class.getName())); + Assert.assertTrue(mse.getMessage().contains("provider call process error")); + } + } + + @Test + public void testNull() { + Assert.assertTrue(service.isNull(null)); + + User user = User.newBuilder().setId(120).setName("zhou").build(); + + Assert.assertFalse(service.isNull(user)); + } + + @Test + public void testProtobuf() { + Address address = service.queryByUid(1); + Assert.assertEquals(1, address.getId()); + + User user = User.newBuilder().setId(120).setName("zhou").setGender(false).addAddress(address).build(); + + Assert.assertTrue(service.isUserAddress(user, address)); + + User newOne = service.copy(user); + Assert.assertEquals(user.getId(), newOne.getId()); + Assert.assertEquals(user.getName(), newOne.getName()); + Assert.assertEquals(user.getGender(), newOne.getGender()); + Assert.assertEquals(user.getAddress(0).getId(), newOne.getAddress(0).getId()); + } + + @After + public void tearDown() { + refererConfig.destroy(); + serviceConfig.unexport(); + } } diff --git a/motan-registry-weibomesh/src/test/java/com/weibo/api/motan/registry/weibomesh/MeshRegistryTest.java b/motan-registry-weibomesh/src/test/java/com/weibo/api/motan/registry/weibomesh/MeshRegistryTest.java index d266b0e1f..0a76001f6 100644 --- a/motan-registry-weibomesh/src/test/java/com/weibo/api/motan/registry/weibomesh/MeshRegistryTest.java +++ b/motan-registry-weibomesh/src/test/java/com/weibo/api/motan/registry/weibomesh/MeshRegistryTest.java @@ -57,6 +57,10 @@ public class MeshRegistryTest { @Before public void setUp() throws Exception { + //开关默认值 + MotanSwitcherUtil.setSwitcherValue(MeshRegistry.MESH_REGISTRY_SWITCHER_NAME, true); + MotanSwitcherUtil.setSwitcherValue(MeshRegistry.MESH_REGISTRY_HEALTH_CHECK_SWITCHER_NAME, true); + copy = 3; //默认副本数 requestTimeout = 100; URL agentMockUrl = new URL("motan2", "localhost", 0, "testpath", new HashMap<>()); @@ -83,9 +87,7 @@ public void setUp() throws Exception { //因为测试流程原因,单测时需要手动触发健康检测 registry.initHealthCheck(); - //开关默认值 - MotanSwitcherUtil.setSwitcherValue(MeshRegistry.MESH_REGISTRY_SWITCHER_NAME, true); - MotanSwitcherUtil.setSwitcherValue(MeshRegistry.MESH_REGISTRY_HEALTH_CHECK_SWITCHER_NAME, true); + } @Test @@ -120,12 +122,13 @@ public void testDoSubscribe() throws Exception { assertEquals(notifyListener.urls.get(0).getGroup(), subUrl.getGroup()); // 验证降级开关 + Thread.sleep(50l); // 等待proxyRegistry的notify MotanSwitcherUtil.setSwitcherValue(MeshRegistry.MESH_REGISTRY_SWITCHER_NAME, false); - Thread.sleep(100l); + Thread.sleep(50l); assertEquals(notifyListener.urls, mockProxyRegistry.discover(subUrl)); MotanSwitcherUtil.setSwitcherValue(MeshRegistry.MESH_REGISTRY_SWITCHER_NAME, true); - Thread.sleep(100l); + Thread.sleep(50l); assertEquals(notifyListener.urls.size(), copy); // health check @@ -177,9 +180,12 @@ public void testDoDiscover() throws Exception { result = registry.doDiscover(subUrl); assertEquals(copy, result.size()); + registry.setUseMesh(true); // 确保订阅节点(有backup节点) TestNotifyListener notifyListener = new TestNotifyListener(); registry.doSubscribe(subUrl, notifyListener); - Thread.sleep(50l); + Thread.sleep(100L); + + registry.setUseMesh(false); result = registry.doDiscover(subUrl); assertEquals(mockProxyRegistry.discover(subUrl), result); } diff --git a/motan-transport-netty/src/test/java/com/weibo/api/motan/transport/netty/NettyClientTest.java b/motan-transport-netty/src/test/java/com/weibo/api/motan/transport/netty/NettyClientTest.java index df076ec1a..be62c360d 100644 --- a/motan-transport-netty/src/test/java/com/weibo/api/motan/transport/netty/NettyClientTest.java +++ b/motan-transport-netty/src/test/java/com/weibo/api/motan/transport/netty/NettyClientTest.java @@ -133,14 +133,17 @@ public void testAbNormal() throws TransportException { @SuppressWarnings("all") public void testForceClose() throws Exception { nettyServer.close(); + Thread.sleep(50l); URL providerUrl = new URL("motan", "localhost", 0, Codec.class.getName()); // any interface just for test provider runtime info nettyServer = new NettyServer(url, new ProviderMessageRouter(new DefaultProvider(new DefaultRpcCodec(), providerUrl, Codec.class))); nettyServer.open(); + Thread.sleep(50l); NettyTestClient nettyClient = new NettyTestClient(url); this.nettyClient = nettyClient; nettyClient.open(); assertTrue(nettyClient.isAvailable()); assertFalse(nettyClient.forceClosed); + Thread.sleep(50l); // provider not exist request.setInterfaceName("unknownService"); @@ -150,12 +153,13 @@ public void testForceClose() throws Exception { nettyClient.request(request); fail(); } catch (MotanServiceException e) { - if (i < forceCloseTimes) { + if (i < forceCloseTimes - 1) { // check provide not exist exception assertTrue(nettyClient.isAvailable()); assertEquals(e.getErrorCode(), MotanErrorMsgConstant.PROVIDER_NOT_EXIST.getErrorCode()); assertTrue(e.getOriginMessage().contains(MotanErrorMsgConstant.PROVIDER_NOT_EXIST_EXCEPTION_PREFIX)); - } else { + } + if (i == forceCloseTimes) { assertTrue(e.getErrorCode() != MotanErrorMsgConstant.PROVIDER_NOT_EXIST.getErrorCode()); } } @@ -170,7 +174,7 @@ public void testForceClose() throws Exception { assertFalse(((Map) serverInfos.get(RuntimeInfoKeys.PROTECT_STRATEGY_KEY)).isEmpty()); // check client force closed info assertTrue((Boolean) clientInfos.get(RuntimeInfoKeys.FORCE_CLOSED_KEY)); - assertTrue((Long) clientInfos.get(RuntimeInfoKeys.ERROR_COUNT_KEY) > (Integer) clientInfos.get(RuntimeInfoKeys.FUSING_THRESHOLD_KEY)); + assertTrue((Long) clientInfos.get(RuntimeInfoKeys.ERROR_COUNT_KEY) >= (Integer) clientInfos.get(RuntimeInfoKeys.FUSING_THRESHOLD_KEY)); // check force close assertFalse(nettyClient.isAvailable()); diff --git a/motan-transport-netty4/src/test/java/com/weibo/api/motan/transport/netty4/NettyClientTest.java b/motan-transport-netty4/src/test/java/com/weibo/api/motan/transport/netty4/NettyClientTest.java index 954c5374d..e33912a3d 100644 --- a/motan-transport-netty4/src/test/java/com/weibo/api/motan/transport/netty4/NettyClientTest.java +++ b/motan-transport-netty4/src/test/java/com/weibo/api/motan/transport/netty4/NettyClientTest.java @@ -165,9 +165,11 @@ public void testAbNormal2() throws Exception { @Test public void testForceClose() throws Exception { nettyServer.close(); + Thread.sleep(50l); URL providerUrl = new URL("motan", "localhost", 0, Codec.class.getName()); // any interface just for test provider runtime info nettyServer = new NettyServer(url, new ProviderMessageRouter(new DefaultProvider(new DefaultRpcCodec(), providerUrl, Codec.class))); nettyServer.open(); + Thread.sleep(50l); NettyTestClient nettyClient = new NettyTestClient(url); this.nettyClient = nettyClient; nettyClient.open(); @@ -182,12 +184,13 @@ public void testForceClose() throws Exception { nettyClient.request(request); fail(); } catch (MotanServiceException e) { - if (i < forceCloseTimes) { + if (i < forceCloseTimes - 1) { // check provide not exist exception assertTrue(nettyClient.isAvailable()); assertEquals(e.getErrorCode(), MotanErrorMsgConstant.PROVIDER_NOT_EXIST.getErrorCode()); assertTrue(e.getOriginMessage().contains(MotanErrorMsgConstant.PROVIDER_NOT_EXIST_EXCEPTION_PREFIX)); - } else { + } + if (i == forceCloseTimes) { assertTrue(e.getErrorCode() != MotanErrorMsgConstant.PROVIDER_NOT_EXIST.getErrorCode()); } } @@ -202,7 +205,7 @@ public void testForceClose() throws Exception { assertFalse(((Map) serverInfos.get(RuntimeInfoKeys.PROTECT_STRATEGY_KEY)).isEmpty()); // check client force closed info assertTrue((Boolean) clientInfos.get(RuntimeInfoKeys.FORCE_CLOSED_KEY)); - assertTrue((Long) clientInfos.get(RuntimeInfoKeys.ERROR_COUNT_KEY) > (Integer) clientInfos.get(RuntimeInfoKeys.FUSING_THRESHOLD_KEY)); + assertTrue((Long) clientInfos.get(RuntimeInfoKeys.ERROR_COUNT_KEY) >= (Integer) clientInfos.get(RuntimeInfoKeys.FUSING_THRESHOLD_KEY)); // check force close assertFalse(nettyClient.isAvailable()); diff --git a/motan-transport-netty4/src/test/java/com/weibo/api/motan/transport/netty4/admin/AdminRpcServerTest.java b/motan-transport-netty4/src/test/java/com/weibo/api/motan/transport/netty4/admin/AdminRpcServerTest.java index 44a7b3ec6..789f45e23 100644 --- a/motan-transport-netty4/src/test/java/com/weibo/api/motan/transport/netty4/admin/AdminRpcServerTest.java +++ b/motan-transport-netty4/src/test/java/com/weibo/api/motan/transport/netty4/admin/AdminRpcServerTest.java @@ -43,6 +43,8 @@ public class AdminRpcServerTest extends TestCase { public void testAdminRpcServer() throws Exception { final Request[] requests = new Request[1]; URL url = new URL("motan2", "127.0.0.1", 0, "tempPath"); + // 设置超时时间为1s + url.addParameter(URLParamType.requestTimeout.getName(), "1000"); AdminRpcServer server = new AdminRpcServer(url, new DefaultAdminHandler() { @Override public Response handle(Request request) { diff --git a/pom.xml b/pom.xml index 494ab03a8..2e4113162 100644 --- a/pom.xml +++ b/pom.xml @@ -159,6 +159,26 @@ ${project.build.sourceEncoding} + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + prepare-agent + + prepare-agent + + + + report + test + + report + + + +