Add unit test for GRPCExporter and GRPCExporterProvider (#2498)
* Add unit test for GRPCExporter and GRPCExporterProvider * MockLongValueIndicator impl the LongValueHolder and MockIntValueIndicator impl the IntValueHolder * formate the code
This commit is contained in:
parent
9c61bedc20
commit
ec7dba3cab
|
|
@ -35,6 +35,10 @@
|
|||
<artifactId>server-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-testing</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<ExportMetricValue> export(StreamObserver<ExportResponse> responseObserver) {
|
||||
@Override
|
||||
public StreamObserver<ExportMetricValue> export(StreamObserver<ExportResponse> responseObserver) {
|
||||
return new StreamObserver<ExportMetricValue>() {
|
||||
@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<SubscriptionsResp> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<ModuleProvider> serviceLoader = ServiceLoader.load(ModuleProvider.class);
|
||||
private ModuleProvider grpcExporterProvider;
|
||||
|
||||
@Before
|
||||
public void setUp() throws ModuleStartException {
|
||||
Iterator<ModuleProvider> 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]);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<GRPCExporter.ExportData> dataList() {
|
||||
List<GRPCExporter.ExportData> 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<SubscriptionsResp> responseObserver) {
|
||||
SubscriptionsResp resp = SubscriptionsResp.newBuilder()
|
||||
.addMetricNames("first")
|
||||
.addMetricNames("second")
|
||||
.build();
|
||||
responseObserver.onNext(resp);
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue