-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeataATShardingSphereTransactionManagerTest.java
179 lines (153 loc) · 7.24 KB
/
SeataATShardingSphereTransactionManagerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.lingh;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.sql.DataSource;
import com.lingh.fixture.MockSeataServer;
import io.seata.core.context.RootContext;
import io.seata.core.protocol.MergeResultMessage;
import io.seata.core.protocol.MergedWarpMessage;
import io.seata.core.protocol.RegisterRMRequest;
import io.seata.core.protocol.RegisterRMResponse;
import io.seata.core.protocol.RegisterTMRequest;
import io.seata.core.protocol.RegisterTMResponse;
import io.seata.core.protocol.transaction.GlobalBeginRequest;
import io.seata.core.protocol.transaction.GlobalBeginResponse;
import io.seata.core.protocol.transaction.GlobalCommitRequest;
import io.seata.core.protocol.transaction.GlobalCommitResponse;
import io.seata.core.protocol.transaction.GlobalRollbackRequest;
import io.seata.core.protocol.transaction.GlobalRollbackResponse;
import io.seata.core.rpc.netty.RmNettyRemotingClient;
import io.seata.core.rpc.netty.TmNettyRemotingClient;
import io.seata.rm.datasource.ConnectionProxy;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.SneakyThrows;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.internal.configuration.plugins.Plugins;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
@SuppressWarnings("SameParameterValue")
public final class SeataATShardingSphereTransactionManagerTest {
private static final MockSeataServer MOCK_SEATA_SERVER = new MockSeataServer();
private static final String DATA_SOURCE_UNIQUE_NAME = "sharding_db.ds_0";
private final SeataATShardingSphereTransactionManager seataTransactionManager = new SeataATShardingSphereTransactionManager();
private final Queue<Object> requestQueue = MOCK_SEATA_SERVER.getMessageHandler().getRequestQueue();
private final Queue<Object> responseQueue = MOCK_SEATA_SERVER.getMessageHandler().getResponseQueue();
@BeforeClass
public static void before() {
Executors.newSingleThreadExecutor().submit(MOCK_SEATA_SERVER::start);
while (true) {
if (MOCK_SEATA_SERVER.getInitialized().get()) {
return;
}
}
}
@AfterClass
public static void after() {
MOCK_SEATA_SERVER.shutdown();
}
@Before
public void setUp() {
seataTransactionManager.init(Collections.singletonMap(DATA_SOURCE_UNIQUE_NAME, new MockedDataSource()));
}
@After
public void tearDown() {
SeataXIDContext.remove();
RootContext.unbind();
SeataTransactionHolder.clear();
seataTransactionManager.close();
releaseRpcClient();
requestQueue.clear();
responseQueue.clear();
}
@Test
public void assertInit() {
Map<String, DataSource> actual = getDataSourceMap();
assertThat(actual.size(), is(1));
assertThat(actual.get(DATA_SOURCE_UNIQUE_NAME), instanceOf(DataSourceProxy.class));
assertThat(seataTransactionManager.getTransactionType(), is(TransactionType.BASE));
}
@Test
public void assertGetConnection() throws SQLException {
Connection actual = seataTransactionManager.getConnection("sharding_db", "ds_0");
assertThat(actual, instanceOf(ConnectionProxy.class));
}
@Test
public void assertBegin() {
seataTransactionManager.begin();
assertTrue(seataTransactionManager.isInTransaction());
assertResult(GlobalBeginRequest.class, GlobalBeginResponse.class);
}
@Test
public void assertBeginTimeout() {
seataTransactionManager.begin(30);
assertTrue(seataTransactionManager.isInTransaction());
assertResult(GlobalBeginRequest.class, GlobalBeginResponse.class);
}
@Test
public void assertCommit() {
SeataTransactionHolder.set(GlobalTransactionContext.getCurrentOrCreate());
setXID("testXID");
seataTransactionManager.commit(false);
assertResult(GlobalCommitRequest.class, GlobalCommitResponse.class);
}
@Test(expected = IllegalStateException.class)
public void assertCommitWithoutBegin() {
SeataTransactionHolder.set(GlobalTransactionContext.getCurrentOrCreate());
seataTransactionManager.commit(false);
}
@Test
public void assertRollback() {
SeataTransactionHolder.set(GlobalTransactionContext.getCurrentOrCreate());
setXID("testXID");
seataTransactionManager.rollback();
assertResult(GlobalRollbackRequest.class, GlobalRollbackResponse.class);
}
@Test(expected = IllegalStateException.class)
public void assertRollbackWithoutBegin() {
SeataTransactionHolder.set(GlobalTransactionContext.getCurrentOrCreate());
seataTransactionManager.rollback();
}
private void assertResult(Class<?> requestClazz, Class<?> responseClazz) {
int requestQueueSize = requestQueue.size();
if (3 == requestQueueSize) {
assertThat(requestQueue.poll(), instanceOf(RegisterTMRequest.class));
assertThat(requestQueue.poll(), instanceOf(RegisterRMRequest.class));
assertThat(requestQueue.poll(), instanceOf(requestClazz));
}
int responseQueueSize = responseQueue.size();
if (3 == responseQueueSize) {
assertThat(responseQueue.poll(), instanceOf(RegisterTMResponse.class));
assertThat(responseQueue.poll(), instanceOf(RegisterRMResponse.class));
assertThat(responseQueue.poll(), instanceOf(responseClazz));
}
}
@SneakyThrows(ReflectiveOperationException.class)
@SuppressWarnings("unchecked")
private Map<String, DataSource> getDataSourceMap() {
return (Map<String, DataSource>) Plugins.getMemberAccessor().get(seataTransactionManager.getClass().getDeclaredField("dataSourceMap"), seataTransactionManager);
}
@SneakyThrows(ReflectiveOperationException.class)
private void setXID(final String xid) {
Plugins.getMemberAccessor().set(SeataTransactionHolder.get().getClass().getDeclaredField("xid"), SeataTransactionHolder.get(), xid);
RootContext.bind(xid);
}
@SneakyThrows(ReflectiveOperationException.class)
private void releaseRpcClient() {
Plugins.getMemberAccessor().set(TmNettyRemotingClient.getInstance().getClass().getDeclaredField("initialized"), TmNettyRemotingClient.getInstance(), new AtomicBoolean(false));
Plugins.getMemberAccessor().set(TmNettyRemotingClient.getInstance().getClass().getDeclaredField("instance"), TmNettyRemotingClient.getInstance(), null);
Plugins.getMemberAccessor().set(RmNettyRemotingClient.getInstance().getClass().getDeclaredField("initialized"), RmNettyRemotingClient.getInstance(), new AtomicBoolean(false));
Plugins.getMemberAccessor().set(RmNettyRemotingClient.getInstance().getClass().getDeclaredField("instance"), RmNettyRemotingClient.getInstance(), null);
}
}