diff --git a/oap-server/exporter/pom.xml b/oap-server/exporter/pom.xml index 73a0caf46..d7a13acc4 100644 --- a/oap-server/exporter/pom.xml +++ b/oap-server/exporter/pom.xml @@ -35,6 +35,10 @@ server-core ${project.version} + + io.grpc + grpc-testing + diff --git a/oap-server/exporter/src/main/java/org/apache/skywalking/oap/server/exporter/provider/grpc/GRPCExporterProvider.java b/oap-server/exporter/src/main/java/org/apache/skywalking/oap/server/exporter/provider/grpc/GRPCExporterProvider.java index 84de71932..c0fa7a0e8 100644 --- a/oap-server/exporter/src/main/java/org/apache/skywalking/oap/server/exporter/provider/grpc/GRPCExporterProvider.java +++ b/oap-server/exporter/src/main/java/org/apache/skywalking/oap/server/exporter/provider/grpc/GRPCExporterProvider.java @@ -53,9 +53,10 @@ public class GRPCExporterProvider extends ModuleProvider { } @Override public void notifyAfterCompleted() throws ServiceNotProvidedException, ModuleStartException { - exporter.setServiceInventoryCache(getManager().find(CoreModule.NAME).provider().getService(ServiceInventoryCache.class)); - exporter.setServiceInstanceInventoryCache(getManager().find(CoreModule.NAME).provider().getService(ServiceInstanceInventoryCache.class)); - exporter.setEndpointInventoryCache(getManager().find(CoreModule.NAME).provider().getService(EndpointInventoryCache.class)); + ModuleServiceHolder serviceHolder = getManager().find(CoreModule.NAME).provider(); + exporter.setServiceInventoryCache(serviceHolder.getService(ServiceInventoryCache.class)); + exporter.setServiceInstanceInventoryCache(serviceHolder.getService(ServiceInstanceInventoryCache.class)); + exporter.setEndpointInventoryCache(serviceHolder.getService(EndpointInventoryCache.class)); exporter.initSubscriptionList(); } diff --git a/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/ExporterMockReceiver.java b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/ExporterMockReceiver.java index 57da559fd..e0b307cca 100644 --- a/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/ExporterMockReceiver.java +++ b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/ExporterMockReceiver.java @@ -21,7 +21,8 @@ package org.apache.skywalking.oap.server.exporter.provider.grpc; import io.grpc.stub.StreamObserver; import org.apache.skywalking.oap.server.exporter.grpc.*; import org.apache.skywalking.oap.server.library.server.ServerException; -import org.apache.skywalking.oap.server.library.server.grpc.*; +import org.apache.skywalking.oap.server.library.server.grpc.GRPCHandler; +import org.apache.skywalking.oap.server.library.server.grpc.GRPCServer; public class ExporterMockReceiver { public static void main(String[] args) throws ServerException, InterruptedException { @@ -36,16 +37,20 @@ public class ExporterMockReceiver { } public static class MockHandler extends MetricExportServiceGrpc.MetricExportServiceImplBase implements GRPCHandler { - @Override public StreamObserver export(StreamObserver responseObserver) { + @Override + public StreamObserver export(StreamObserver responseObserver) { return new StreamObserver() { - @Override public void onNext(ExportMetricValue value) { + @Override + public void onNext(ExportMetricValue value) { } - @Override public void onError(Throwable throwable) { + @Override + public void onError(Throwable throwable) { responseObserver.onError(throwable); } - @Override public void onCompleted() { + @Override + public void onCompleted() { responseObserver.onCompleted(); } }; @@ -54,7 +59,7 @@ public class ExporterMockReceiver { @Override public void subscription(SubscriptionReq request, StreamObserver responseObserver) { responseObserver.onNext(SubscriptionsResp.newBuilder() - .addMetricNames("all_p99").addMetricNames("service_cpm").addMetricNames("endpoint_sla").build()); + .addMetricNames("all_p99").addMetricNames("service_cpm").addMetricNames("endpoint_sla").build()); responseObserver.onCompleted(); } } diff --git a/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/GRPCExporterProviderTest.java b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/GRPCExporterProviderTest.java new file mode 100644 index 000000000..97f0741ea --- /dev/null +++ b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/GRPCExporterProviderTest.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.exporter.provider.grpc; + +import org.apache.skywalking.oap.server.core.CoreModule; +import org.apache.skywalking.oap.server.core.cache.EndpointInventoryCache; +import org.apache.skywalking.oap.server.core.cache.ServiceInstanceInventoryCache; +import org.apache.skywalking.oap.server.core.cache.ServiceInventoryCache; +import org.apache.skywalking.oap.server.core.exporter.ExporterModule; +import org.apache.skywalking.oap.server.library.module.*; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +import java.util.Iterator; +import java.util.ServiceLoader; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +/** + * Created by dengming, 2019.04.20 + */ +@Ignore +public class GRPCExporterProviderTest { + + private ServiceLoader serviceLoader = ServiceLoader.load(ModuleProvider.class); + private ModuleProvider grpcExporterProvider; + + @Before + public void setUp() throws ModuleStartException { + Iterator moduleProviderIterator = serviceLoader.iterator(); + assertTrue(moduleProviderIterator.hasNext()); + + grpcExporterProvider = moduleProviderIterator.next(); + assertTrue(grpcExporterProvider instanceof GRPCExporterProvider); + + GRPCExporterSetting config = (GRPCExporterSetting) grpcExporterProvider.createConfigBeanIfAbsent(); + assertNotNull(config); + assertNull(config.getTargetHost()); + assertEquals(0, config.getTargetPort()); + assertEquals(20000, config.getBufferChannelSize()); + assertEquals(2, config.getBufferChannelNum()); + + //for test + config.setTargetHost("localhost"); + + grpcExporterProvider.prepare(); + + grpcExporterProvider.start(); + } + + @Test + public void name() { + assertEquals("grpc", grpcExporterProvider.name()); + } + + @Test + public void module() { + assertEquals(ExporterModule.class, grpcExporterProvider.module()); + } + + + @Test + public void notifyAfterCompleted() throws ServiceNotProvidedException, ModuleStartException { + GRPCExporter exporter = mock(GRPCExporter.class); + + ModuleManager manager = mock(ModuleManager.class); + ModuleProviderHolder providerHolder = mock(ModuleProviderHolder.class); + + + ModuleServiceHolder serviceHolder = mock(ModuleServiceHolder.class); + + when(manager.find(CoreModule.NAME)).thenReturn(providerHolder); + when(providerHolder.provider()).thenReturn(serviceHolder); + + when(serviceHolder.getService(ServiceInventoryCache.class)).thenReturn(null); + when(serviceHolder.getService(ServiceInstanceInventoryCache.class)).thenReturn(null); + when(serviceHolder.getService(EndpointInventoryCache.class)).thenReturn(null); + + doNothing().when(exporter).initSubscriptionList(); + + grpcExporterProvider.setManager(manager); + Whitebox.setInternalState(grpcExporterProvider, "exporter", exporter); + grpcExporterProvider.notifyAfterCompleted(); + } + + @Test + public void requiredModules() { + String[] requireModules = grpcExporterProvider.requiredModules(); + assertNotNull(requireModules); + assertEquals(1, requireModules.length); + assertEquals("core", requireModules[0]); + } +} \ No newline at end of file diff --git a/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/GRPCExporterTest.java b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/GRPCExporterTest.java new file mode 100644 index 000000000..815875629 --- /dev/null +++ b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/GRPCExporterTest.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.exporter.provider.grpc; + +import io.grpc.testing.GrpcServerRule; +import org.apache.skywalking.oap.server.core.analysis.indicator.IndicatorMetaInfo; +import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; +import org.apache.skywalking.oap.server.exporter.grpc.MetricExportServiceGrpc; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +/** + * Created by dengming, 2019.04.20 + */ +public class GRPCExporterTest { + + private GRPCExporter exporter; + + @Rule + public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor(); + + private MetricExportServiceGrpc.MetricExportServiceImplBase server = new MockMetricExportServiceImpl(); + private IndicatorMetaInfo metaInfo = new IndicatorMetaInfo("mock-indicator", DefaultScopeDefine.ALL); + + private MetricExportServiceGrpc.MetricExportServiceBlockingStub stub; + + @Before + public void setUp() throws Exception { + GRPCExporterSetting setting = new GRPCExporterSetting(); + setting.setTargetHost("localhost"); + setting.setTargetPort(9870); + exporter = new GRPCExporter(setting); + grpcServerRule.getServiceRegistry().addService(server); + stub = MetricExportServiceGrpc.newBlockingStub(grpcServerRule.getChannel()); + } + + @Test + public void export() { + exporter.export(metaInfo, new MockIndicator()); + } + + @Test + public void initSubscriptionList() { + Whitebox.setInternalState(exporter, "blockingStub", stub); + exporter.initSubscriptionList(); + } + + @Test + public void init() { + exporter.init(); + } + + @Test + public void consume() { + + exporter.consume(dataList()); + exporter.consume(Collections.emptyList()); + } + + + @Test + public void onError() { + Exception e = new IllegalArgumentException("some something wrong"); + exporter.onError(Collections.emptyList(), e); + exporter.onError(dataList(), e); + } + + @Test + public void onExit() { + exporter.onExit(); + } + + private List dataList() { + List dataList = new LinkedList<>(); + dataList.add(exporter.new ExportData(metaInfo, new MockIndicator())); + dataList.add(exporter.new ExportData(metaInfo, new MockIntValueIndicatior())); + dataList.add(exporter.new ExportData(metaInfo, new MockLongValueIndicator())); + dataList.add(exporter.new ExportData(metaInfo, new MockDoubleValueIndicator())); + return dataList; + } +} \ No newline at end of file diff --git a/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockDoubleValueIndicator.java b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockDoubleValueIndicator.java new file mode 100644 index 000000000..6b4fc9c15 --- /dev/null +++ b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockDoubleValueIndicator.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.exporter.provider.grpc; + +import org.apache.skywalking.oap.server.core.analysis.indicator.DoubleValueHolder; + +/** + * Created by dengming, 2019.04.20 + */ +public class MockDoubleValueIndicator extends MockIndicator implements DoubleValueHolder { + @Override + public double getValue() { + return 2.3; + } +} diff --git a/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockIndicator.java b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockIndicator.java new file mode 100644 index 000000000..f1a74c47d --- /dev/null +++ b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockIndicator.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.exporter.provider.grpc; + +import org.apache.skywalking.oap.server.core.analysis.indicator.Indicator; +import org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteData; + +/** + * Created by dengming, 2019.04.20 + */ +public class MockIndicator extends Indicator { + + @Override + public String id() { + return "mock-indicator"; + } + + @Override + public void combine(Indicator indicator) { + + } + + @Override + public void calculate() { + + } + + @Override + public Indicator toHour() { + return this; + } + + @Override + public Indicator toDay() { + return this; + } + + @Override + public Indicator toMonth() { + return this; + } + + @Override + public int remoteHashCode() { + return 1; + } + + @Override + public void deserialize(RemoteData remoteData) { + + } + + @Override + public RemoteData.Builder serialize() { + return null; + } +} diff --git a/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockIntValueIndicatior.java b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockIntValueIndicatior.java new file mode 100644 index 000000000..cd3b9277e --- /dev/null +++ b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockIntValueIndicatior.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.exporter.provider.grpc; + +import org.apache.skywalking.oap.server.core.analysis.indicator.IntValueHolder; + +/** + * Created by dengming, 2019.04.20 + */ +public class MockIntValueIndicatior extends MockIndicator implements IntValueHolder { + @Override + public int getValue() { + return 12; + } +} diff --git a/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockLongValueIndicator.java b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockLongValueIndicator.java new file mode 100644 index 000000000..a8ee9157d --- /dev/null +++ b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockLongValueIndicator.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.exporter.provider.grpc; + +import org.apache.skywalking.oap.server.core.analysis.indicator.LongValueHolder; + +/** + * Created by dengming, 2019.04.20 + */ +public class MockLongValueIndicator extends MockIndicator implements LongValueHolder { + @Override + public long getValue() { + return 1234567891234563312L; + } +} diff --git a/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockMetricExportServiceImpl.java b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockMetricExportServiceImpl.java new file mode 100644 index 000000000..b4d64c81f --- /dev/null +++ b/oap-server/exporter/src/test/java/org/apache/skywalking/oap/server/exporter/provider/grpc/MockMetricExportServiceImpl.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.exporter.provider.grpc; + +import io.grpc.stub.StreamObserver; +import org.apache.skywalking.oap.server.exporter.grpc.MetricExportServiceGrpc; +import org.apache.skywalking.oap.server.exporter.grpc.SubscriptionReq; +import org.apache.skywalking.oap.server.exporter.grpc.SubscriptionsResp; + +/** + * Created by dengming, 2019.04.20 + */ +public class MockMetricExportServiceImpl extends MetricExportServiceGrpc.MetricExportServiceImplBase { + @Override + public void subscription(SubscriptionReq request, StreamObserver responseObserver) { + SubscriptionsResp resp = SubscriptionsResp.newBuilder() + .addMetricNames("first") + .addMetricNames("second") + .build(); + responseObserver.onNext(resp); + responseObserver.onCompleted(); + } +}