Skip to content

Commit

Permalink
Merge pull request #544 from OpenSkywalking/fix/sjdbcbug
Browse files Browse the repository at this point in the history
Plugin test must compile with jdk 1.6, so not allow to use lambda.
  • Loading branch information
wu-sheng authored Oct 25, 2017
2 parents 0e23b02 + 12c3c4e commit 6543814
Showing 1 changed file with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.dangdang.ddframe.rdb.sharding.util.EventBusInstance;
import com.google.common.base.Optional;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -65,48 +65,48 @@
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class InterceptorTest {

private static ExecutorService ES;

@SegmentStoragePoint
private SegmentStorage segmentStorage;

@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();

private ExecuteInterceptor executeInterceptor;

private AsyncExecuteInterceptor asyncExecuteInterceptor;

private Object[] allArguments;

@BeforeClass
public static void init() {
ExecuteEventListener.init();
new ExecutorEngineConstructorInterceptor().onConstruct(null, null);
ES = Executors.newSingleThreadExecutor();
}

@AfterClass
public static void finish() {
ES.shutdown();
}

@Before
public void setUp() throws SQLException {
executeInterceptor = new ExecuteInterceptor();
asyncExecuteInterceptor = new AsyncExecuteInterceptor();
allArguments = new Object[]{SQLType.DQL, null};
allArguments = new Object[] {SQLType.DQL, null};
}

@Test
public void assertSyncExecute() throws Throwable {
executeInterceptor.beforeMethod(null, null, allArguments, null, null);
sendEvent("ds_0", "select * from t_order_0");
executeInterceptor.afterMethod(null, null, allArguments, null, null);
assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment segment = segmentStorage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(segment);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(segment);
assertNotNull(spans);
assertThat(spans.size(), is(2));
assertSpan(spans.get(0), 0);
Expand All @@ -118,9 +118,11 @@ public void assertAsyncExecute() throws Throwable {
executeInterceptor.beforeMethod(null, null, allArguments, null, null);
asyncExecuteInterceptor.beforeMethod(null, null, null, null, null);
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
ES.submit(() -> {
ExecutorDataMap.setDataMap(dataMap);
sendEvent("ds_1", "select * from t_order_1");
ES.submit(new Runnable() {
@Override public void run() {
ExecutorDataMap.setDataMap(dataMap);
sendEvent("ds_1", "select * from t_order_1");
}
}).get();
asyncExecuteInterceptor.afterMethod(null, null, null, null, null);
sendEvent("ds_0", "select * from t_order_0");
Expand All @@ -140,15 +142,17 @@ public void assertAsyncExecute() throws Throwable {
assertSpan(spans1.get(0), 0);
assertThat(spans1.get(1).getOperationName(), is("/SJDBC/TRUNK/DQL"));
}

@Test
public void assertExecuteError() throws Throwable {
executeInterceptor.beforeMethod(null, null, allArguments, null, null);
asyncExecuteInterceptor.beforeMethod(null, null, null, null, null);
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
ES.submit(() -> {
ExecutorDataMap.setDataMap(dataMap);
sendError();
ES.submit(new Runnable() {
@Override public void run() {
ExecutorDataMap.setDataMap(dataMap);
sendError();
}
}).get();
asyncExecuteInterceptor.handleMethodException(null, null, null, null, new SQLException("test"));
asyncExecuteInterceptor.afterMethod(null, null, null, null, null);
Expand Down Expand Up @@ -178,18 +182,22 @@ private void assertSpan(AbstractTracingSpan span, int index) {
assertThat(span.isExit(), is(true));
assertThat(span.getOperationName(), is("/SJDBC/BRANCH/QUERY"));
}

private void assertErrorSpan(AbstractTracingSpan span) {
assertOccurException(span, true);
}

private void sendEvent(String datasource, String sql) {
DQLExecutionEvent event = new DQLExecutionEvent(datasource, sql, Arrays.asList("1", 100));
List<Object> parameters = new ArrayList<Object>();
parameters.add("1");
parameters.add(100);

DQLExecutionEvent event = new DQLExecutionEvent(datasource, sql, parameters);
EventBusInstance.getInstance().post(event);
event.setEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
EventBusInstance.getInstance().post(event);
}

private void sendError() {
DMLExecutionEvent event = new DMLExecutionEvent("", "", Collections.emptyList());
EventBusInstance.getInstance().post(event);
Expand Down

0 comments on commit 6543814

Please sign in to comment.