mock servlet worker finish

This commit is contained in:
pengys5 2017-04-07 21:34:13 +08:00
parent a2924fad04
commit 98a2157c29
15 changed files with 889 additions and 15 deletions

View File

@ -24,7 +24,7 @@ public class GlobalTraceGetWithGlobalId extends AbstractGet {
private Logger logger = LogManager.getFormatterLogger(GlobalTraceGetWithGlobalId.class);
private GlobalTraceGetWithGlobalId(Role role, ClusterWorkerContext clusterContext, LocalWorkerContext selfContext) {
GlobalTraceGetWithGlobalId(Role role, ClusterWorkerContext clusterContext, LocalWorkerContext selfContext) {
super(role, clusterContext, selfContext);
}
@ -46,6 +46,7 @@ public class GlobalTraceGetWithGlobalId extends AbstractGet {
}
public static class Factory extends AbstractGetProvider<GlobalTraceGetWithGlobalId> {
public static Factory INSTANCE = new Factory();
@Override
public Role role() {

View File

@ -24,7 +24,7 @@ public class NodeRefResSumGetGroupWithTimeSlice extends AbstractGet {
private Logger logger = LogManager.getFormatterLogger(NodeRefResSumGetGroupWithTimeSlice.class);
private NodeRefResSumGetGroupWithTimeSlice(Role role, ClusterWorkerContext clusterContext, LocalWorkerContext selfContext) {
NodeRefResSumGetGroupWithTimeSlice(Role role, ClusterWorkerContext clusterContext, LocalWorkerContext selfContext) {
super(role, clusterContext, selfContext);
}
@ -61,6 +61,7 @@ public class NodeRefResSumGetGroupWithTimeSlice extends AbstractGet {
}
public static class Factory extends AbstractGetProvider<NodeRefResSumGetGroupWithTimeSlice> {
public static Factory INSTANCE = new Factory();
@Override
public Role role() {

View File

@ -24,7 +24,7 @@ public class SegmentTopGetWithGlobalTraceId extends AbstractGet {
private Logger logger = LogManager.getFormatterLogger(SegmentTopGetWithGlobalTraceId.class);
private SegmentTopGetWithGlobalTraceId(Role role, ClusterWorkerContext clusterContext, LocalWorkerContext selfContext) {
SegmentTopGetWithGlobalTraceId(Role role, ClusterWorkerContext clusterContext, LocalWorkerContext selfContext) {
super(role, clusterContext, selfContext);
}
@ -41,14 +41,14 @@ public class SegmentTopGetWithGlobalTraceId extends AbstractGet {
logger.debug("globalTraceId: %s, from: %s, limit: %s", Arrays.toString(request.get("globalTraceId")),
Arrays.toString(request.get("from")), Arrays.toString(request.get("limit")));
int from = 0;
int from;
try {
from = Integer.valueOf(ParameterTools.INSTANCE.toString(request, "from"));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("the request parameter from must numeric with int type");
}
int limit = 0;
int limit;
try {
limit = Integer.valueOf(ParameterTools.INSTANCE.toString(request, "limit"));
} catch (NumberFormatException e) {
@ -62,6 +62,7 @@ public class SegmentTopGetWithGlobalTraceId extends AbstractGet {
}
public static class Factory extends AbstractGetProvider<SegmentTopGetWithGlobalTraceId> {
public static Factory INSTANCE = new Factory();
@Override
public Role role() {

View File

@ -24,7 +24,7 @@ public class SegmentTopGetWithTimeSlice extends AbstractGet {
private Logger logger = LogManager.getFormatterLogger(SegmentTopGetWithTimeSlice.class);
private SegmentTopGetWithTimeSlice(Role role, ClusterWorkerContext clusterContext, LocalWorkerContext selfContext) {
SegmentTopGetWithTimeSlice(Role role, ClusterWorkerContext clusterContext, LocalWorkerContext selfContext) {
super(role, clusterContext, selfContext);
}
@ -55,14 +55,14 @@ public class SegmentTopGetWithTimeSlice extends AbstractGet {
throw new IllegalArgumentException("the request parameter endTime must numeric with long type");
}
int from = 0;
int from;
try {
from = Integer.valueOf(ParameterTools.INSTANCE.toString(request, "from"));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("the request parameter from must numeric with int type");
}
int limit = 0;
int limit;
try {
limit = Integer.valueOf(ParameterTools.INSTANCE.toString(request, "limit"));
} catch (NumberFormatException e) {
@ -86,6 +86,7 @@ public class SegmentTopGetWithTimeSlice extends AbstractGet {
}
public static class Factory extends AbstractGetProvider<SegmentTopGetWithTimeSlice> {
public static Factory INSTANCE = new Factory();
@Override
public Role role() {

View File

@ -123,6 +123,18 @@ public class SegmentTopSearchWithGlobalTraceId extends AbstractLocalSyncWorker {
this.limit = limit;
this.globalTraceId = globalTraceId;
}
public int getFrom() {
return from;
}
public int getLimit() {
return limit;
}
public String getGlobalTraceId() {
return globalTraceId;
}
}
public static class Factory extends AbstractLocalSyncWorkerProvider<SegmentTopSearchWithGlobalTraceId> {

View File

@ -140,6 +140,30 @@ public class SegmentTopSearchWithTimeSlice extends AbstractLocalSyncWorker {
public void setMaxCost(int maxCost) {
this.maxCost = maxCost;
}
public int getFrom() {
return from;
}
public int getLimit() {
return limit;
}
public long getStartTime() {
return startTime;
}
public long getEndTime() {
return endTime;
}
public int getMinCost() {
return minCost;
}
public int getMaxCost() {
return maxCost;
}
}
public static class Factory extends AbstractLocalSyncWorkerProvider<SegmentTopSearchWithTimeSlice> {

View File

@ -24,7 +24,7 @@ public class SpanGetWithId extends AbstractGet {
private Logger logger = LogManager.getFormatterLogger(SpanGetWithId.class);
private SpanGetWithId(Role role, ClusterWorkerContext clusterContext, LocalWorkerContext selfContext) {
SpanGetWithId(Role role, ClusterWorkerContext clusterContext, LocalWorkerContext selfContext) {
super(role, clusterContext, selfContext);
}
@ -48,6 +48,7 @@ public class SpanGetWithId extends AbstractGet {
}
public static class Factory extends AbstractGetProvider<SpanGetWithId> {
public static Factory INSTANCE = new Factory();
@Override
public Role role() {

View File

@ -57,6 +57,14 @@ public class SpanSearchWithId extends AbstractLocalSyncWorker {
this.segId = segId;
this.spanId = spanId;
}
public String getSegId() {
return segId;
}
public String getSpanId() {
return spanId;
}
}
public static class Factory extends AbstractLocalSyncWorkerProvider<SpanSearchWithId> {

View File

@ -64,30 +64,41 @@ public class TraceDagGetWithTimeSlice extends AbstractGet {
String timeSliceType = ParameterTools.INSTANCE.toString(request, "timeSliceType");
JsonObject compResponse = new JsonObject();
JsonObject compResponse = getNewResponse();
getSelfContext().lookup(NodeCompLoad.WorkerRole.INSTANCE).ask(null, compResponse);
JsonObject nodeMappingResponse = new JsonObject();
JsonObject nodeMappingResponse = getNewResponse();
NodeMappingSearchWithTimeSlice.RequestEntity nodeMappingEntity = new NodeMappingSearchWithTimeSlice.RequestEntity(timeSliceType, startTime, endTime);
getSelfContext().lookup(NodeMappingSearchWithTimeSlice.WorkerRole.INSTANCE).ask(nodeMappingEntity, nodeMappingResponse);
JsonObject nodeRefResponse = new JsonObject();
JsonObject nodeRefResponse = getNewResponse();
NodeRefSearchWithTimeSlice.RequestEntity nodeReftEntity = new NodeRefSearchWithTimeSlice.RequestEntity(timeSliceType, startTime, endTime);
getSelfContext().lookup(NodeRefSearchWithTimeSlice.WorkerRole.INSTANCE).ask(nodeReftEntity, nodeRefResponse);
JsonObject resSumResponse = new JsonObject();
JsonObject resSumResponse = getNewResponse();
NodeRefResSumSearchWithTimeSlice.RequestEntity resSumEntity = new NodeRefResSumSearchWithTimeSlice.RequestEntity(timeSliceType, startTime, endTime);
getSelfContext().lookup(NodeRefResSumSearchWithTimeSlice.WorkerRole.INSTANCE).ask(resSumEntity, resSumResponse);
TraceDagDataBuilder builder = new TraceDagDataBuilder();
JsonObject result = builder.build(compResponse.get(Const.RESULT).getAsJsonArray(), nodeMappingResponse.get(Const.RESULT).getAsJsonArray(),
JsonObject result = getBuilder().build(compResponse.get(Const.RESULT).getAsJsonArray(), nodeMappingResponse.get(Const.RESULT).getAsJsonArray(),
nodeRefResponse.get(Const.RESULT).getAsJsonArray(), resSumResponse.get(Const.RESULT).getAsJsonArray());
response.add(Const.RESULT, result);
}
private JsonObject getNewResponse() {
JsonObject response = new JsonObject();
return response;
}
private TraceDagDataBuilder getBuilder() {
TraceDagDataBuilder builder = new TraceDagDataBuilder();
return builder;
}
public static class Factory extends AbstractGetProvider<TraceDagGetWithTimeSlice> {
public static Factory INSTANCE = new Factory();
@Override
public Role role() {
return WorkerRole.INSTANCE;

View File

@ -0,0 +1,103 @@
package com.a.eye.skywalking.collector.worker.globaltrace;
import com.a.eye.skywalking.collector.actor.ClusterWorkerContext;
import com.a.eye.skywalking.collector.actor.LocalWorkerContext;
import com.a.eye.skywalking.collector.actor.ProviderNotFoundException;
import com.a.eye.skywalking.collector.actor.WorkerRefs;
import com.a.eye.skywalking.collector.actor.selector.RollingSelector;
import com.a.eye.skywalking.collector.worker.globaltrace.persistence.GlobalTraceSearchWithGlobalId;
import com.google.gson.JsonObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.*;
/**
* @author pengys5
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ClusterWorkerContext.class})
@PowerMockIgnore({"javax.management.*"})
public class GlobalTraceGetWithGlobalIdTestCase {
private GlobalTraceGetWithGlobalId getObj;
private GlobalTraceAnswerGet answer;
private ClusterWorkerContext clusterWorkerContext;
@Before
public void init() throws Exception {
clusterWorkerContext = PowerMockito.mock(ClusterWorkerContext.class);
LocalWorkerContext localWorkerContext = PowerMockito.mock(LocalWorkerContext.class);
WorkerRefs workerRefs = mock(WorkerRefs.class);
answer = new GlobalTraceAnswerGet();
doAnswer(answer).when(workerRefs).ask(Mockito.anyString(), Mockito.any(JsonObject.class));
when(localWorkerContext.lookup(GlobalTraceSearchWithGlobalId.WorkerRole.INSTANCE)).thenReturn(workerRefs);
getObj = new GlobalTraceGetWithGlobalId(GlobalTraceGetWithGlobalId.WorkerRole.INSTANCE, clusterWorkerContext, localWorkerContext);
}
@Test
public void testRole() {
Assert.assertEquals(GlobalTraceGetWithGlobalId.class.getSimpleName(), GlobalTraceGetWithGlobalId.WorkerRole.INSTANCE.roleName());
Assert.assertEquals(RollingSelector.class.getSimpleName(), GlobalTraceGetWithGlobalId.WorkerRole.INSTANCE.workerSelector().getClass().getSimpleName());
}
@Test
public void testFactory() {
Assert.assertEquals(GlobalTraceGetWithGlobalId.class.getSimpleName(), GlobalTraceGetWithGlobalId.Factory.INSTANCE.role().roleName());
Assert.assertEquals(GlobalTraceGetWithGlobalId.class.getSimpleName(), GlobalTraceGetWithGlobalId.Factory.INSTANCE.workerInstance(null).getClass().getSimpleName());
Assert.assertEquals("/globalTrace/globalId", GlobalTraceGetWithGlobalId.Factory.INSTANCE.servletPath());
}
@Test
public void testPreStart() throws ProviderNotFoundException {
when(clusterWorkerContext.findProvider(GlobalTraceSearchWithGlobalId.WorkerRole.INSTANCE)).thenReturn(GlobalTraceSearchWithGlobalId.Factory.INSTANCE);
ArgumentCaptor<GlobalTraceSearchWithGlobalId.WorkerRole> argumentCaptor = ArgumentCaptor.forClass(GlobalTraceSearchWithGlobalId.WorkerRole.class);
getObj.preStart();
verify(clusterWorkerContext).findProvider(argumentCaptor.capture());
}
@Test
public void testOnSearch() throws Exception {
Map<String, String[]> request = new HashMap<>();
String[] globalId = {"Test"};
request.put("globalId", globalId);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchError() throws Exception {
Map<String, String[]> request = new HashMap<>();
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
class GlobalTraceAnswerGet implements Answer {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
String globalId = (String) invocation.getArguments()[0];
System.out.println(globalId);
Assert.assertEquals("Test", globalId);
return null;
}
}
}

View File

@ -0,0 +1,134 @@
package com.a.eye.skywalking.collector.worker.noderef;
import com.a.eye.skywalking.collector.actor.ClusterWorkerContext;
import com.a.eye.skywalking.collector.actor.LocalWorkerContext;
import com.a.eye.skywalking.collector.actor.ProviderNotFoundException;
import com.a.eye.skywalking.collector.actor.WorkerRefs;
import com.a.eye.skywalking.collector.actor.selector.RollingSelector;
import com.a.eye.skywalking.collector.worker.noderef.persistence.NodeRefResSumGroupWithTimeSlice;
import com.google.gson.JsonObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.*;
/**
* @author pengys5
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ClusterWorkerContext.class})
@PowerMockIgnore({"javax.management.*"})
public class NodeRefResSumGetGroupWithTimeSliceTestCase {
private NodeRefResSumGetGroupWithTimeSlice getObj;
private NodeRefResSumGetAnswerGet answer;
private ClusterWorkerContext clusterWorkerContext;
@Before
public void init() throws Exception {
clusterWorkerContext = PowerMockito.mock(ClusterWorkerContext.class);
LocalWorkerContext localWorkerContext = PowerMockito.mock(LocalWorkerContext.class);
WorkerRefs workerRefs = mock(WorkerRefs.class);
answer = new NodeRefResSumGetAnswerGet();
doAnswer(answer).when(workerRefs).ask(Mockito.any(NodeRefResSumGroupWithTimeSlice.RequestEntity.class), Mockito.any(JsonObject.class));
when(localWorkerContext.lookup(NodeRefResSumGroupWithTimeSlice.WorkerRole.INSTANCE)).thenReturn(workerRefs);
getObj = new NodeRefResSumGetGroupWithTimeSlice(NodeRefResSumGetGroupWithTimeSlice.WorkerRole.INSTANCE, clusterWorkerContext, localWorkerContext);
}
@Test
public void testRole() {
Assert.assertEquals(NodeRefResSumGetGroupWithTimeSlice.class.getSimpleName(), NodeRefResSumGetGroupWithTimeSlice.WorkerRole.INSTANCE.roleName());
Assert.assertEquals(RollingSelector.class.getSimpleName(), NodeRefResSumGetGroupWithTimeSlice.WorkerRole.INSTANCE.workerSelector().getClass().getSimpleName());
}
@Test
public void testFactory() {
Assert.assertEquals(NodeRefResSumGetGroupWithTimeSlice.class.getSimpleName(), NodeRefResSumGetGroupWithTimeSlice.Factory.INSTANCE.role().roleName());
Assert.assertEquals(NodeRefResSumGetGroupWithTimeSlice.class.getSimpleName(), NodeRefResSumGetGroupWithTimeSlice.Factory.INSTANCE.workerInstance(null).getClass().getSimpleName());
Assert.assertEquals("/nodeRef/resSum/groupTimeSlice", NodeRefResSumGetGroupWithTimeSlice.Factory.INSTANCE.servletPath());
}
@Test
public void testPreStart() throws ProviderNotFoundException {
when(clusterWorkerContext.findProvider(NodeRefResSumGroupWithTimeSlice.WorkerRole.INSTANCE)).thenReturn(NodeRefResSumGroupWithTimeSlice.Factory.INSTANCE);
ArgumentCaptor<NodeRefResSumGroupWithTimeSlice.WorkerRole> argumentCaptor = ArgumentCaptor.forClass(NodeRefResSumGroupWithTimeSlice.WorkerRole.class);
getObj.preStart();
verify(clusterWorkerContext).findProvider(argumentCaptor.capture());
}
@Test
public void testOnSearch() throws Exception {
Map<String, String[]> request = new HashMap<>();
String[] startTime = {"100"};
request.put("startTime", startTime);
String[] endTime = {"200"};
request.put("endTime", endTime);
String[] timeSliceType = {"minute"};
request.put("timeSliceType", timeSliceType);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchError() throws Exception {
Map<String, String[]> request = new HashMap<>();
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchStartTimeError() throws Exception {
Map<String, String[]> request = new HashMap<>();
String[] startTime = {"x"};
request.put("startTime", startTime);
String[] endTime = {"200"};
request.put("endTime", endTime);
String[] timeSliceType = {"minute"};
request.put("timeSliceType", timeSliceType);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchEndTimeError() throws Exception {
Map<String, String[]> request = new HashMap<>();
String[] startTime = {"100"};
request.put("startTime", startTime);
String[] endTime = {"x"};
request.put("endTime", endTime);
String[] timeSliceType = {"minute"};
request.put("timeSliceType", timeSliceType);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
class NodeRefResSumGetAnswerGet implements Answer {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
NodeRefResSumGroupWithTimeSlice.RequestEntity requestEntity = (NodeRefResSumGroupWithTimeSlice.RequestEntity) invocation.getArguments()[0];
Assert.assertEquals(100L, requestEntity.getStartTime());
Assert.assertEquals(200L, requestEntity.getEndTime());
Assert.assertEquals("minute", requestEntity.getSliceType());
return null;
}
}
}

View File

@ -0,0 +1,141 @@
package com.a.eye.skywalking.collector.worker.segment;
import com.a.eye.skywalking.collector.actor.ClusterWorkerContext;
import com.a.eye.skywalking.collector.actor.LocalWorkerContext;
import com.a.eye.skywalking.collector.actor.ProviderNotFoundException;
import com.a.eye.skywalking.collector.actor.WorkerRefs;
import com.a.eye.skywalking.collector.actor.selector.RollingSelector;
import com.a.eye.skywalking.collector.worker.segment.persistence.SegmentExceptionWithSegId;
import com.a.eye.skywalking.collector.worker.segment.persistence.SegmentTopSearchWithGlobalTraceId;
import com.google.gson.JsonObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.*;
/**
* @author pengys5
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ClusterWorkerContext.class})
@PowerMockIgnore({"javax.management.*"})
public class SegmentTopGetWithGlobalTraceIdTestCase {
private SegmentTopGetWithGlobalTraceId getObj;
private SegmentTopGetAnswerGet answer;
private ClusterWorkerContext clusterWorkerContext;
@Before
public void init() throws Exception {
clusterWorkerContext = PowerMockito.mock(ClusterWorkerContext.class);
LocalWorkerContext localWorkerContext = PowerMockito.mock(LocalWorkerContext.class);
WorkerRefs workerRefs = mock(WorkerRefs.class);
answer = new SegmentTopGetAnswerGet();
doAnswer(answer).when(workerRefs).ask(Mockito.any(SegmentTopSearchWithGlobalTraceId.RequestEntity.class), Mockito.any(JsonObject.class));
when(localWorkerContext.lookup(SegmentTopSearchWithGlobalTraceId.WorkerRole.INSTANCE)).thenReturn(workerRefs);
getObj = new SegmentTopGetWithGlobalTraceId(SegmentTopGetWithGlobalTraceId.WorkerRole.INSTANCE, clusterWorkerContext, localWorkerContext);
}
@Test
public void testRole() {
Assert.assertEquals(SegmentTopGetWithGlobalTraceId.class.getSimpleName(), SegmentTopGetWithGlobalTraceId.WorkerRole.INSTANCE.roleName());
Assert.assertEquals(RollingSelector.class.getSimpleName(), SegmentTopGetWithGlobalTraceId.WorkerRole.INSTANCE.workerSelector().getClass().getSimpleName());
}
@Test
public void testFactory() {
Assert.assertEquals(SegmentTopGetWithGlobalTraceId.class.getSimpleName(), SegmentTopGetWithGlobalTraceId.Factory.INSTANCE.role().roleName());
Assert.assertEquals(SegmentTopGetWithGlobalTraceId.class.getSimpleName(), SegmentTopGetWithGlobalTraceId.Factory.INSTANCE.workerInstance(null).getClass().getSimpleName());
Assert.assertEquals("/segments/top/globalTraceId", SegmentTopGetWithGlobalTraceId.Factory.INSTANCE.servletPath());
}
@Test
public void testPreStart() throws ProviderNotFoundException {
ClusterWorkerContext exceptionContext = PowerMockito.mock(ClusterWorkerContext.class);
when(exceptionContext.findProvider(SegmentExceptionWithSegId.WorkerRole.INSTANCE)).thenReturn(SegmentExceptionWithSegId.Factory.INSTANCE);
SegmentTopSearchWithGlobalTraceId.Factory.INSTANCE.setClusterContext(exceptionContext);
when(clusterWorkerContext.findProvider(SegmentTopSearchWithGlobalTraceId.WorkerRole.INSTANCE)).thenReturn(SegmentTopSearchWithGlobalTraceId.Factory.INSTANCE);
ArgumentCaptor<SegmentTopSearchWithGlobalTraceId.WorkerRole> argumentCaptor = ArgumentCaptor.forClass(SegmentTopSearchWithGlobalTraceId.WorkerRole.class);
getObj.preStart();
verify(clusterWorkerContext).findProvider(argumentCaptor.capture());
}
@Test
public void testOnSearch() throws Exception {
Map<String, String[]> request = new HashMap<>();
String[] globalTraceId = {"TestId"};
request.put("globalTraceId", globalTraceId);
String[] from = {"20"};
request.put("from", from);
String[] limit = {"50"};
request.put("limit", limit);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchError() throws Exception {
Map<String, String[]> request = new HashMap<>();
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchErrorFrom() throws Exception {
Map<String, String[]> request = new HashMap<>();
String[] globalTraceId = {"TestId"};
request.put("globalTraceId", globalTraceId);
String[] from = {"x"};
request.put("from", from);
String[] limit = {"50"};
request.put("limit", limit);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchErrorLimit() throws Exception {
Map<String, String[]> request = new HashMap<>();
String[] globalTraceId = {"TestId"};
request.put("globalTraceId", globalTraceId);
String[] from = {"20"};
request.put("from", from);
String[] limit = {"x"};
request.put("limit", limit);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
class SegmentTopGetAnswerGet implements Answer {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
SegmentTopSearchWithGlobalTraceId.RequestEntity requestEntity = (SegmentTopSearchWithGlobalTraceId.RequestEntity) invocation.getArguments()[0];
Assert.assertEquals("TestId", requestEntity.getGlobalTraceId());
Assert.assertEquals(20, requestEntity.getFrom());
Assert.assertEquals(50, requestEntity.getLimit());
return null;
}
}
}

View File

@ -0,0 +1,159 @@
package com.a.eye.skywalking.collector.worker.segment;
import com.a.eye.skywalking.collector.actor.ClusterWorkerContext;
import com.a.eye.skywalking.collector.actor.LocalWorkerContext;
import com.a.eye.skywalking.collector.actor.ProviderNotFoundException;
import com.a.eye.skywalking.collector.actor.WorkerRefs;
import com.a.eye.skywalking.collector.actor.selector.RollingSelector;
import com.a.eye.skywalking.collector.worker.segment.persistence.SegmentExceptionWithSegId;
import com.a.eye.skywalking.collector.worker.segment.persistence.SegmentTopSearchWithTimeSlice;
import com.google.gson.JsonObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.*;
/**
* @author pengys5
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ClusterWorkerContext.class})
@PowerMockIgnore({"javax.management.*"})
public class SegmentTopGetWithTimeSliceTestCase {
private SegmentTopGetWithTimeSlice getObj;
private SegmentTopGetAnswerGet answer;
private ClusterWorkerContext clusterWorkerContext;
@Before
public void init() throws Exception {
clusterWorkerContext = PowerMockito.mock(ClusterWorkerContext.class);
LocalWorkerContext localWorkerContext = PowerMockito.mock(LocalWorkerContext.class);
WorkerRefs workerRefs = mock(WorkerRefs.class);
answer = new SegmentTopGetAnswerGet();
doAnswer(answer).when(workerRefs).ask(Mockito.any(SegmentTopSearchWithTimeSlice.RequestEntity.class), Mockito.any(JsonObject.class));
when(localWorkerContext.lookup(SegmentTopSearchWithTimeSlice.WorkerRole.INSTANCE)).thenReturn(workerRefs);
getObj = new SegmentTopGetWithTimeSlice(SegmentTopGetWithTimeSlice.WorkerRole.INSTANCE, clusterWorkerContext, localWorkerContext);
}
@Test
public void testRole() {
Assert.assertEquals(SegmentTopGetWithTimeSlice.class.getSimpleName(), SegmentTopGetWithTimeSlice.WorkerRole.INSTANCE.roleName());
Assert.assertEquals(RollingSelector.class.getSimpleName(), SegmentTopGetWithTimeSlice.WorkerRole.INSTANCE.workerSelector().getClass().getSimpleName());
}
@Test
public void testFactory() {
Assert.assertEquals(SegmentTopGetWithTimeSlice.class.getSimpleName(), SegmentTopGetWithTimeSlice.Factory.INSTANCE.role().roleName());
Assert.assertEquals(SegmentTopGetWithTimeSlice.class.getSimpleName(), SegmentTopGetWithTimeSlice.Factory.INSTANCE.workerInstance(null).getClass().getSimpleName());
Assert.assertEquals("/segments/top/timeSlice", SegmentTopGetWithTimeSlice.Factory.INSTANCE.servletPath());
}
@Test
public void testPreStart() throws ProviderNotFoundException {
ClusterWorkerContext exceptionContext = PowerMockito.mock(ClusterWorkerContext.class);
when(exceptionContext.findProvider(SegmentExceptionWithSegId.WorkerRole.INSTANCE)).thenReturn(SegmentExceptionWithSegId.Factory.INSTANCE);
SegmentTopSearchWithTimeSlice.Factory.INSTANCE.setClusterContext(exceptionContext);
when(clusterWorkerContext.findProvider(SegmentTopSearchWithTimeSlice.WorkerRole.INSTANCE)).thenReturn(SegmentTopSearchWithTimeSlice.Factory.INSTANCE);
ArgumentCaptor<SegmentTopSearchWithTimeSlice.WorkerRole> argumentCaptor = ArgumentCaptor.forClass(SegmentTopSearchWithTimeSlice.WorkerRole.class);
getObj.preStart();
verify(clusterWorkerContext).findProvider(argumentCaptor.capture());
}
@Test
public void testOnSearch() throws Exception {
Map<String, String[]> request = createRequest();
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchErrorStartTime() throws Exception {
Map<String, String[]> request = createRequest();
String[] startTime = {"x"};
request.put("startTime", startTime);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchErrorEndTime() throws Exception {
Map<String, String[]> request = createRequest();
String[] endTime = {"x"};
request.put("endTime", endTime);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchErrorFrom() throws Exception {
Map<String, String[]> request = createRequest();
String[] from = {"x"};
request.put("from", from);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchErrorLimit() throws Exception {
Map<String, String[]> request = createRequest();
String[] limit = {"x"};
request.put("limit", limit);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
private Map<String, String[]> createRequest(){
Map<String, String[]> request = new HashMap<>();
String[] startTime = {"10"};
request.put("startTime", startTime);
String[] endTime = {"20"};
request.put("endTime", endTime);
String[] from = {"30"};
request.put("from", from);
String[] limit = {"40"};
request.put("limit", limit);
String[] minCost = {"50"};
request.put("minCost", minCost);
String[] maxCost = {"60"};
request.put("maxCost", maxCost);
return request;
}
class SegmentTopGetAnswerGet implements Answer {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
SegmentTopSearchWithTimeSlice.RequestEntity requestEntity = (SegmentTopSearchWithTimeSlice.RequestEntity) invocation.getArguments()[0];
Assert.assertEquals(10, requestEntity.getStartTime());
Assert.assertEquals(20, requestEntity.getEndTime());
Assert.assertEquals(30, requestEntity.getFrom());
Assert.assertEquals(40, requestEntity.getLimit());
Assert.assertEquals(50, requestEntity.getMinCost());
Assert.assertEquals(60, requestEntity.getMaxCost());
return null;
}
}
}

View File

@ -1,17 +1,104 @@
package com.a.eye.skywalking.collector.worker.span;
import com.a.eye.skywalking.collector.actor.ClusterWorkerContext;
import com.a.eye.skywalking.collector.actor.LocalWorkerContext;
import com.a.eye.skywalking.collector.actor.ProviderNotFoundException;
import com.a.eye.skywalking.collector.actor.WorkerRefs;
import com.a.eye.skywalking.collector.actor.selector.RollingSelector;
import com.a.eye.skywalking.collector.worker.span.persistence.SpanSearchWithId;
import com.google.gson.JsonObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.*;
/**
* @author pengys5
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ClusterWorkerContext.class})
@PowerMockIgnore({"javax.management.*"})
public class SpanGetWithIdTestCase {
private SpanGetWithId getObj;
private SpanGetAnswerGet answer;
private ClusterWorkerContext clusterWorkerContext;
@Before
public void init() throws Exception {
clusterWorkerContext = PowerMockito.mock(ClusterWorkerContext.class);
LocalWorkerContext localWorkerContext = PowerMockito.mock(LocalWorkerContext.class);
WorkerRefs workerRefs = mock(WorkerRefs.class);
answer = new SpanGetAnswerGet();
doAnswer(answer).when(workerRefs).ask(Mockito.any(SpanSearchWithId.RequestEntity.class), Mockito.any(JsonObject.class));
when(localWorkerContext.lookup(SpanSearchWithId.WorkerRole.INSTANCE)).thenReturn(workerRefs);
getObj = new SpanGetWithId(SpanGetWithId.WorkerRole.INSTANCE, clusterWorkerContext, localWorkerContext);
}
@Test
public void testRole() {
Assert.assertEquals(SpanGetWithId.class.getSimpleName(), SpanGetWithId.WorkerRole.INSTANCE.roleName());
Assert.assertEquals(RollingSelector.class.getSimpleName(), SpanGetWithId.WorkerRole.INSTANCE.workerSelector().getClass().getSimpleName());
}
@Test
public void testFactory() {
Assert.assertEquals(SpanGetWithId.class.getSimpleName(), SpanGetWithId.Factory.INSTANCE.role().roleName());
Assert.assertEquals(SpanGetWithId.class.getSimpleName(), SpanGetWithId.Factory.INSTANCE.workerInstance(null).getClass().getSimpleName());
Assert.assertEquals("/span/spanId", SpanGetWithId.Factory.INSTANCE.servletPath());
}
@Test
public void testPreStart() throws ProviderNotFoundException {
when(clusterWorkerContext.findProvider(SpanSearchWithId.WorkerRole.INSTANCE)).thenReturn(SpanSearchWithId.Factory.INSTANCE);
ArgumentCaptor<SpanSearchWithId.WorkerRole> argumentCaptor = ArgumentCaptor.forClass(SpanSearchWithId.WorkerRole.class);
getObj.preStart();
verify(clusterWorkerContext).findProvider(argumentCaptor.capture());
}
@Test
public void testOnSearch() throws Exception {
Map<String, String[]> request = new HashMap<>();
String[] segId = {"10"};
request.put("segId", segId);
String[] spanId = {"20"};
request.put("spanId", spanId);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchError() throws Exception {
Map<String, String[]> request = new HashMap<>();
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
class SpanGetAnswerGet implements Answer {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
SpanSearchWithId.RequestEntity requestEntity = (SpanSearchWithId.RequestEntity) invocation.getArguments()[0];
Assert.assertEquals("10", requestEntity.getSegId());
Assert.assertEquals("20", requestEntity.getSpanId());
return null;
}
}
}

View File

@ -0,0 +1,190 @@
package com.a.eye.skywalking.collector.worker.tracedag;
import com.a.eye.skywalking.collector.actor.*;
import com.a.eye.skywalking.collector.actor.selector.RollingSelector;
import com.a.eye.skywalking.collector.worker.Const;
import com.a.eye.skywalking.collector.worker.node.persistence.NodeCompLoad;
import com.a.eye.skywalking.collector.worker.node.persistence.NodeMappingSearchWithTimeSlice;
import com.a.eye.skywalking.collector.worker.noderef.persistence.NodeRefResSumSearchWithTimeSlice;
import com.a.eye.skywalking.collector.worker.noderef.persistence.NodeRefSearchWithTimeSlice;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.*;
/**
* @author pengys5
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ClusterWorkerContext.class, TraceDagGetWithTimeSlice.class})
@PowerMockIgnore({"javax.management.*"})
public class TraceDagGetWithTimeSliceTestCase {
private TraceDagGetWithTimeSlice getObj;
private ClusterWorkerContext clusterWorkerContext;
@Before
public void init() throws Exception {
clusterWorkerContext = PowerMockito.mock(ClusterWorkerContext.class);
LocalWorkerContext localWorkerContext = PowerMockito.mock(LocalWorkerContext.class);
WorkerRefs workerRefs_1 = mock(WorkerRefs.class);
TraceDagGetAnswerGet_1 answer_1 = new TraceDagGetAnswerGet_1();
doAnswer(answer_1).when(workerRefs_1).ask(Mockito.any(), Mockito.any(JsonObject.class));
when(localWorkerContext.lookup(NodeCompLoad.WorkerRole.INSTANCE)).thenReturn(workerRefs_1);
WorkerRefs workerRefs_2 = mock(WorkerRefs.class);
TraceDagGetAnswerGet_2 answer_2 = new TraceDagGetAnswerGet_2();
doAnswer(answer_2).when(workerRefs_2).ask(Mockito.any(NodeMappingSearchWithTimeSlice.RequestEntity.class), Mockito.any(JsonObject.class));
when(localWorkerContext.lookup(NodeMappingSearchWithTimeSlice.WorkerRole.INSTANCE)).thenReturn(workerRefs_2);
WorkerRefs workerRefs_3 = mock(WorkerRefs.class);
TraceDagGetAnswerGet_3 answer_3 = new TraceDagGetAnswerGet_3();
doAnswer(answer_3).when(workerRefs_3).ask(Mockito.any(NodeRefSearchWithTimeSlice.RequestEntity.class), Mockito.any(JsonObject.class));
when(localWorkerContext.lookup(NodeRefSearchWithTimeSlice.WorkerRole.INSTANCE)).thenReturn(workerRefs_3);
WorkerRefs workerRefs_4 = mock(WorkerRefs.class);
TraceDagGetAnswerGet_4 answer_4 = new TraceDagGetAnswerGet_4();
doAnswer(answer_4).when(workerRefs_4).ask(Mockito.any(NodeRefResSumSearchWithTimeSlice.RequestEntity.class), Mockito.any(JsonObject.class));
when(localWorkerContext.lookup(NodeRefResSumSearchWithTimeSlice.WorkerRole.INSTANCE)).thenReturn(workerRefs_4);
getObj = PowerMockito.spy(new TraceDagGetWithTimeSlice(TraceDagGetWithTimeSlice.WorkerRole.INSTANCE, clusterWorkerContext, localWorkerContext));
}
@Test
public void testRole() {
Assert.assertEquals(TraceDagGetWithTimeSlice.class.getSimpleName(), TraceDagGetWithTimeSlice.WorkerRole.INSTANCE.roleName());
Assert.assertEquals(RollingSelector.class.getSimpleName(), TraceDagGetWithTimeSlice.WorkerRole.INSTANCE.workerSelector().getClass().getSimpleName());
}
@Test
public void testFactory() {
Assert.assertEquals(TraceDagGetWithTimeSlice.class.getSimpleName(), TraceDagGetWithTimeSlice.Factory.INSTANCE.role().roleName());
Assert.assertEquals(TraceDagGetWithTimeSlice.class.getSimpleName(), TraceDagGetWithTimeSlice.Factory.INSTANCE.workerInstance(null).getClass().getSimpleName());
Assert.assertEquals("/traceDag/timeSlice", TraceDagGetWithTimeSlice.Factory.INSTANCE.servletPath());
}
@Test
public void testPreStart() throws ProviderNotFoundException {
when(clusterWorkerContext.findProvider(NodeCompLoad.WorkerRole.INSTANCE)).thenReturn(NodeCompLoad.Factory.INSTANCE);
when(clusterWorkerContext.findProvider(NodeMappingSearchWithTimeSlice.WorkerRole.INSTANCE)).thenReturn(NodeMappingSearchWithTimeSlice.Factory.INSTANCE);
when(clusterWorkerContext.findProvider(NodeRefSearchWithTimeSlice.WorkerRole.INSTANCE)).thenReturn(NodeRefSearchWithTimeSlice.Factory.INSTANCE);
when(clusterWorkerContext.findProvider(NodeRefResSumSearchWithTimeSlice.WorkerRole.INSTANCE)).thenReturn(NodeRefResSumSearchWithTimeSlice.Factory.INSTANCE);
ArgumentCaptor<Role> argumentCaptor = ArgumentCaptor.forClass(Role.class);
getObj.preStart();
verify(clusterWorkerContext, times(4)).findProvider(argumentCaptor.capture());
Assert.assertEquals("NodeCompLoad", argumentCaptor.getAllValues().get(0).roleName());
Assert.assertEquals("NodeMappingSearchWithTimeSlice", argumentCaptor.getAllValues().get(1).roleName());
Assert.assertEquals("NodeRefSearchWithTimeSlice", argumentCaptor.getAllValues().get(2).roleName());
Assert.assertEquals("NodeRefResSumSearchWithTimeSlice", argumentCaptor.getAllValues().get(3).roleName());
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchError() throws Exception {
Map<String, String[]> request = new HashMap<>();
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchErrorStartTime() throws Exception {
Map<String, String[]> request = createRequest();
String[] startTime = {"xx"};
request.put("startTime", startTime);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
@Test(expected = IllegalArgumentException.class)
public void testOnSearchErrorEndTime() throws Exception {
Map<String, String[]> request = createRequest();
String[] endTime = {"xx"};
request.put("endTime", endTime);
JsonObject response = new JsonObject();
getObj.onSearch(request, response);
}
private Map<String, String[]> createRequest() {
Map<String, String[]> request = new HashMap<>();
String[] startTime = {"10"};
request.put("startTime", startTime);
String[] endTime = {"20"};
request.put("endTime", endTime);
String[] timeSliceType = {"minute"};
request.put("timeSliceType", timeSliceType);
return request;
}
@Test
public void testOnSearch() throws Exception {
TraceDagDataBuilder builder = mock(TraceDagDataBuilder.class);
PowerMockito.when(getObj, "getBuilder").thenReturn(builder);
JsonObject response = new JsonObject();
response.add(Const.RESULT, new JsonArray());
PowerMockito.when(getObj, "getNewResponse").thenReturn(response);
Map<String, String[]> request = createRequest();
getObj.onSearch(request, response);
}
class TraceDagGetAnswerGet_1 implements Answer {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}
class TraceDagGetAnswerGet_2 implements Answer {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
NodeMappingSearchWithTimeSlice.RequestEntity entity = (NodeMappingSearchWithTimeSlice.RequestEntity) invocation.getArguments()[0];
Assert.assertEquals(10, entity.getStartTime());
Assert.assertEquals(20, entity.getEndTime());
Assert.assertEquals("minute", entity.getSliceType());
return null;
}
}
class TraceDagGetAnswerGet_3 implements Answer {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
NodeRefSearchWithTimeSlice.RequestEntity entity = (NodeRefSearchWithTimeSlice.RequestEntity) invocation.getArguments()[0];
Assert.assertEquals(10, entity.getStartTime());
Assert.assertEquals(20, entity.getEndTime());
Assert.assertEquals("minute", entity.getSliceType());
return null;
}
}
class TraceDagGetAnswerGet_4 implements Answer {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
NodeRefResSumSearchWithTimeSlice.RequestEntity entity = (NodeRefResSumSearchWithTimeSlice.RequestEntity) invocation.getArguments()[0];
Assert.assertEquals(10, entity.getStartTime());
Assert.assertEquals(20, entity.getEndTime());
Assert.assertEquals("minute", entity.getSliceType());
return null;
}
}
}