Skip to content

Commit

Permalink
Merge pull request #856 from beckhampu/dev
Browse files Browse the repository at this point in the history
 fix opentracing test sequence bug
  • Loading branch information
terrymanu authored May 21, 2018
2 parents f93ef1e + d3a8b9c commit dbe89f6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

package io.shardingsphere.opentracing;

import com.google.common.collect.HashMultimap;
import com.google.common.eventbus.EventBus;
import io.opentracing.NoopTracerFactory;
import io.opentracing.mock.MockTracer;
import io.opentracing.util.GlobalTracer;
import io.opentracing.util.ThreadLocalActiveSpanSource;
import io.shardingsphere.core.constant.SQLType;
import io.shardingsphere.core.executor.BaseStatementUnit;
Expand All @@ -26,10 +30,13 @@
import io.shardingsphere.core.executor.type.statement.StatementUnit;
import io.shardingsphere.core.routing.SQLExecutionUnit;
import io.shardingsphere.core.routing.SQLUnit;
import io.shardingsphere.core.util.EventBusInstance;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
Expand All @@ -43,66 +50,80 @@
import static org.mockito.Mockito.when;

public final class ExecuteEventListenerTest {

private static final MockTracer TRACER = new MockTracer(new ThreadLocalActiveSpanSource(),
MockTracer.Propagator.TEXT_MAP);

private final ExecutorEngine executorEngine = new ExecutorEngine(5);

@BeforeClass
public static void init() {
ShardingJDBCTracer.init(TRACER);
}

@Before
public void before() {
TRACER.reset();
}

@Test
public void assertSingleStatement() throws Exception {
Statement statement = mock(Statement.class);
when(statement.getConnection()).thenReturn(mock(Connection.class));
executorEngine.execute(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0",
new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {

@Override
public Integer execute(final BaseStatementUnit baseStatementUnit) {
return 0;
}
});
assertThat(TRACER.finishedSpans().size(), is(2));
}

@Test
public void assertMultiStatement() throws Exception {
List<StatementUnit> statementUnitList = new ArrayList<>(2);
Statement stm1 = mock(Statement.class);
when(stm1.getConnection()).thenReturn(mock(Connection.class));
statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm1));
Statement stm2 = mock(Statement.class);
when(stm2.getConnection()).thenReturn(mock(Connection.class));
statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm2));
executorEngine.execute(SQLType.DML, statementUnitList, new ExecuteCallback<Integer>() {
@Override
public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception {
return 0;
}
});
assertThat(TRACER.finishedSpans().size(), is(3));
}

@Test(expected = SQLException.class)
public void assertSQLException() throws Exception {
Statement statement = mock(Statement.class);
when(statement.getConnection()).thenReturn(mock(Connection.class));
executorEngine.execute(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0",
new SQLUnit("select ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {

@Override
public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception {
throw new SQLException();
}
});
}

private static final MockTracer TRACER = new MockTracer(new ThreadLocalActiveSpanSource(),
MockTracer.Propagator.TEXT_MAP);

private final ExecutorEngine executorEngine = new ExecutorEngine(5);

@BeforeClass
public static void init() {
ShardingJDBCTracer.init(TRACER);
}

@AfterClass
public static void tearDown() throws Exception {
releaseTracer();
}

@Before
public void before() {
TRACER.reset();
}

@Test
public void assertSingleStatement() throws Exception {
Statement statement = mock(Statement.class);
when(statement.getConnection()).thenReturn(mock(Connection.class));
executorEngine.execute(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0",
new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {

@Override
public Integer execute(final BaseStatementUnit baseStatementUnit) {
return 0;
}
});
assertThat(TRACER.finishedSpans().size(), is(2));
}

@Test
public void assertMultiStatement() throws Exception {
List<StatementUnit> statementUnitList = new ArrayList<>(2);
Statement stm1 = mock(Statement.class);
when(stm1.getConnection()).thenReturn(mock(Connection.class));
statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm1));
Statement stm2 = mock(Statement.class);
when(stm2.getConnection()).thenReturn(mock(Connection.class));
statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm2));
executorEngine.execute(SQLType.DML, statementUnitList, new ExecuteCallback<Integer>() {
@Override
public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception {
return 0;
}
});
assertThat(TRACER.finishedSpans().size(), is(3));
}

@Test(expected = SQLException.class)
public void assertSQLException() throws Exception {
Statement statement = mock(Statement.class);
when(statement.getConnection()).thenReturn(mock(Connection.class));
executorEngine.execute(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0",
new SQLUnit("select ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {

@Override
public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception {
throw new SQLException();
}
});
}

private static void releaseTracer() throws NoSuchFieldException, IllegalAccessException {
Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
tracerField.setAccessible(true);
tracerField.set(GlobalTracer.class, NoopTracerFactory.create());
Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType");
subscribersByTypeField.setAccessible(true);
subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package io.shardingsphere.opentracing;

import com.google.common.collect.HashMultimap;
import com.google.common.eventbus.EventBus;
import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.opentracing.fixture.FooTracer;

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -41,6 +44,7 @@ public final class ShardingJDBCTracerTest {
public void setUp() throws Exception {
System.setProperty("shardingjdbc.opentracing.tracer.class", FooTracer.class.getName());
clearGlobalTracer();
unregisterEventBus();
}

@After
Expand Down Expand Up @@ -76,5 +80,13 @@ private static void clearGlobalTracer() throws NoSuchFieldException, IllegalAcce
Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
tracerField.setAccessible(true);
tracerField.set(GlobalTracer.class, NoopTracerFactory.create());

}

private static void unregisterEventBus() throws NoSuchFieldException, IllegalAccessException {
Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType");
subscribersByTypeField.setAccessible(true);
subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create());
}

}

0 comments on commit dbe89f6

Please sign in to comment.