|
50 | 50 | import static org.mockito.Mockito.when;
|
51 | 51 |
|
52 | 52 | public final class ExecuteEventListenerTest {
|
53 |
| - |
54 |
| - private static final MockTracer TRACER = new MockTracer(new ThreadLocalActiveSpanSource(), |
55 |
| - MockTracer.Propagator.TEXT_MAP); |
56 |
| - |
57 |
| - private final ExecutorEngine executorEngine = new ExecutorEngine(5); |
58 |
| - |
59 |
| - @BeforeClass |
60 |
| - public static void init() { |
61 |
| - ShardingJDBCTracer.init(TRACER); |
62 |
| - } |
63 |
| - |
64 |
| - @AfterClass |
65 |
| - public static void tearDown() throws Exception { |
66 |
| - releaseTracer(); |
67 |
| - } |
68 |
| - |
69 |
| - @Before |
70 |
| - public void before() { |
71 |
| - TRACER.reset(); |
72 |
| - } |
73 |
| - |
74 |
| - @Test |
75 |
| - public void assertSingleStatement() throws Exception { |
76 |
| - Statement statement = mock(Statement.class); |
77 |
| - when(statement.getConnection()).thenReturn(mock(Connection.class)); |
78 |
| - executorEngine.execute(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", |
79 |
| - new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() { |
80 |
| - |
81 |
| - @Override |
82 |
| - public Integer execute(final BaseStatementUnit baseStatementUnit) { |
83 |
| - return 0; |
84 |
| - } |
85 |
| - }); |
86 |
| - assertThat(TRACER.finishedSpans().size(), is(2)); |
87 |
| - } |
88 |
| - |
89 |
| - @Test |
90 |
| - public void assertMultiStatement() throws Exception { |
91 |
| - List<StatementUnit> statementUnitList = new ArrayList<>(2); |
92 |
| - Statement stm1 = mock(Statement.class); |
93 |
| - when(stm1.getConnection()).thenReturn(mock(Connection.class)); |
94 |
| - statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm1)); |
95 |
| - Statement stm2 = mock(Statement.class); |
96 |
| - when(stm2.getConnection()).thenReturn(mock(Connection.class)); |
97 |
| - statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm2)); |
98 |
| - executorEngine.execute(SQLType.DML, statementUnitList, new ExecuteCallback<Integer>() { |
99 |
| - @Override |
100 |
| - public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception { |
101 |
| - return 0; |
102 |
| - } |
103 |
| - }); |
104 |
| - assertThat(TRACER.finishedSpans().size(), is(3)); |
105 |
| - } |
106 |
| - |
107 |
| - @Test(expected = SQLException.class) |
108 |
| - public void assertSQLException() throws Exception { |
109 |
| - Statement statement = mock(Statement.class); |
110 |
| - when(statement.getConnection()).thenReturn(mock(Connection.class)); |
111 |
| - executorEngine.execute(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", |
112 |
| - new SQLUnit("select ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() { |
113 |
| - |
114 |
| - @Override |
115 |
| - public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception { |
116 |
| - throw new SQLException(); |
117 |
| - } |
118 |
| - }); |
119 |
| - } |
120 |
| - |
121 |
| - private static void releaseTracer() throws NoSuchFieldException, IllegalAccessException { |
122 |
| - Field tracerField = GlobalTracer.class.getDeclaredField("tracer"); |
123 |
| - tracerField.setAccessible(true); |
124 |
| - tracerField.set(GlobalTracer.class, NoopTracerFactory.create()); |
125 |
| - Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType"); |
126 |
| - subscribersByTypeField.setAccessible(true); |
127 |
| - subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create()); |
128 |
| - } |
| 53 | + |
| 54 | + private static final MockTracer TRACER = new MockTracer(new ThreadLocalActiveSpanSource(), MockTracer.Propagator.TEXT_MAP); |
| 55 | + |
| 56 | + private final ExecutorEngine executorEngine = new ExecutorEngine(5); |
| 57 | + |
| 58 | + @BeforeClass |
| 59 | + public static void init() { |
| 60 | + ShardingJDBCTracer.init(TRACER); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterClass |
| 64 | + public static void tearDown() throws Exception { |
| 65 | + releaseTracer(); |
| 66 | + } |
| 67 | + |
| 68 | + @Before |
| 69 | + public void before() { |
| 70 | + TRACER.reset(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void assertSingleStatement() throws Exception { |
| 75 | + Statement statement = mock(Statement.class); |
| 76 | + when(statement.getConnection()).thenReturn(mock(Connection.class)); |
| 77 | + executorEngine.execute(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", |
| 78 | + new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() { |
| 79 | + |
| 80 | + @Override |
| 81 | + public Integer execute(final BaseStatementUnit baseStatementUnit) { |
| 82 | + return 0; |
| 83 | + } |
| 84 | + }); |
| 85 | + assertThat(TRACER.finishedSpans().size(), is(2)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void assertMultiStatement() throws Exception { |
| 90 | + List<StatementUnit> statementUnitList = new ArrayList<>(2); |
| 91 | + Statement stm1 = mock(Statement.class); |
| 92 | + when(stm1.getConnection()).thenReturn(mock(Connection.class)); |
| 93 | + statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm1)); |
| 94 | + Statement stm2 = mock(Statement.class); |
| 95 | + when(stm2.getConnection()).thenReturn(mock(Connection.class)); |
| 96 | + statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm2)); |
| 97 | + executorEngine.execute(SQLType.DML, statementUnitList, new ExecuteCallback<Integer>() { |
| 98 | + |
| 99 | + @Override |
| 100 | + public Integer execute(final BaseStatementUnit baseStatementUnit) { |
| 101 | + return 0; |
| 102 | + } |
| 103 | + }); |
| 104 | + assertThat(TRACER.finishedSpans().size(), is(3)); |
| 105 | + } |
| 106 | + |
| 107 | + @Test(expected = SQLException.class) |
| 108 | + public void assertSQLException() throws Exception { |
| 109 | + Statement statement = mock(Statement.class); |
| 110 | + when(statement.getConnection()).thenReturn(mock(Connection.class)); |
| 111 | + executorEngine.execute(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", |
| 112 | + new SQLUnit("select ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() { |
| 113 | + |
| 114 | + @Override |
| 115 | + public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception { |
| 116 | + throw new SQLException(); |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + private static void releaseTracer() throws NoSuchFieldException, IllegalAccessException { |
| 122 | + Field tracerField = GlobalTracer.class.getDeclaredField("tracer"); |
| 123 | + tracerField.setAccessible(true); |
| 124 | + tracerField.set(GlobalTracer.class, NoopTracerFactory.create()); |
| 125 | + Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType"); |
| 126 | + subscribersByTypeField.setAccessible(true); |
| 127 | + subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create()); |
| 128 | + } |
129 | 129 | }
|
0 commit comments