-
Notifications
You must be signed in to change notification settings - Fork 90
/
TxTest.java
208 lines (171 loc) · 7.54 KB
/
TxTest.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.table;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import org.apache.ignite.internal.schema.Column;
import org.apache.ignite.internal.schema.NativeTypes;
import org.apache.ignite.internal.schema.SchemaDescriptor;
import org.apache.ignite.internal.table.impl.DummyInternalTableImpl;
import org.apache.ignite.internal.table.impl.DummySchemaManagerImpl;
import org.apache.ignite.internal.util.Pair;
import org.apache.ignite.table.KeyValueBinaryView;
import org.apache.ignite.table.Table;
import org.apache.ignite.table.Tuple;
import org.apache.ignite.tx.IgniteTransactions;
import org.apache.ignite.tx.Transaction;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import static java.util.concurrent.CompletableFuture.allOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** */
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class TxTest {
/** Table ID test value. */
public final java.util.UUID tableId = java.util.UUID.randomUUID();
/** Accounts table. */
private Table accounts;
/** */
public static final double BALANCE_1 = 500;
/** */
public static final double BALANCE_2 = 500;
/** */
public static final double DELTA = 500;
/** */
@Mock
private IgniteTransactions igniteTransactions;
/** */
@Mock
private Transaction tx;
/**
* Initialize the test state.
*/
@BeforeEach
public void before() {
SchemaDescriptor schema = new SchemaDescriptor(
tableId,
1,
new Column[]{new Column("accountNumber", NativeTypes.INT64, false)},
new Column[]{new Column("balance", NativeTypes.DOUBLE, false)}
);
accounts = new TableImpl(new DummyInternalTableImpl(), new DummySchemaManagerImpl(schema), null);
Tuple r1 = accounts.tupleBuilder().set("accountNumber", 1L).set("balance", BALANCE_1).build();
Tuple r2 = accounts.tupleBuilder().set("accountNumber", 2L).set("balance", BALANCE_2).build();
accounts.insert(r1);
accounts.insert(r2);
Mockito.doAnswer(invocation -> {
Consumer<Transaction> argument = invocation.getArgument(0);
argument.accept(tx);
return null;
}).when(igniteTransactions).runInTransaction(Mockito.any());
Mockito.when(igniteTransactions.beginAsync()).thenReturn(CompletableFuture.completedFuture(tx));
}
/**
* Tests a synchronous transaction.
*/
@Test
public void testTxSync() {
igniteTransactions.runInTransaction(tx -> {
Table txAcc = accounts.withTx(tx);
CompletableFuture<Tuple> read1 = txAcc.getAsync(makeKey(1));
CompletableFuture<Tuple> read2 = txAcc.getAsync(makeKey(2));
txAcc.upsertAsync(makeValue(1, read1.join().doubleValue("balance") - DELTA));
txAcc.upsertAsync(makeValue(2, read2.join().doubleValue("balance") + DELTA));
tx.commit(); // Not necessary to wait for async ops expicitly before the commit.
});
Mockito.verify(tx).commit();
assertEquals(BALANCE_1 - DELTA, accounts.get(makeKey(1)).doubleValue("balance"));
assertEquals(BALANCE_2 + DELTA, accounts.get(makeKey(2)).doubleValue("balance"));
}
/**
* Tests a synchronous transaction over key-value view.
*/
@Test
public void testTxSyncKeyValue() {
igniteTransactions.runInTransaction(tx -> {
KeyValueBinaryView txAcc = accounts.withTx(tx).kvView();
CompletableFuture<Tuple> read1 = txAcc.getAsync(makeKey(1));
CompletableFuture<Tuple> read2 = txAcc.getAsync(makeKey(2));
txAcc.putAsync(makeKey(1), makeValue(1, read1.join().doubleValue("balance") - DELTA));
txAcc.putAsync(makeKey(2), makeValue(2, read2.join().doubleValue("balance") + DELTA));
tx.commit(); // Not necessary to wait for async ops expicitly before the commit.
});
Mockito.verify(tx).commit();
assertEquals(BALANCE_1 - DELTA, accounts.get(makeKey(1)).doubleValue("balance"));
assertEquals(BALANCE_2 + DELTA, accounts.get(makeKey(2)).doubleValue("balance"));
}
/**
* Tests an asynchronous transaction.
*/
@Test
public void testTxAsync() {
igniteTransactions.beginAsync().thenApply(tx -> accounts.withTx(tx)).
thenCompose(txAcc -> txAcc.getAsync(makeKey(1))
.thenCombine(txAcc.getAsync(makeKey(2)), (v1, v2) -> new Pair<>(v1, v2))
.thenCompose(pair -> allOf(
txAcc.upsertAsync(makeValue(1, pair.getFirst().doubleValue("balance") - DELTA)),
txAcc.upsertAsync(makeValue(2, pair.getSecond().doubleValue("balance") + DELTA))
)
)
.thenApply(ignore -> txAcc.transaction())
).thenCompose(Transaction::commitAsync);
Mockito.verify(tx).commitAsync();
assertEquals(BALANCE_1 - DELTA, accounts.get(makeKey(1)).doubleValue("balance"));
assertEquals(BALANCE_2 + DELTA, accounts.get(makeKey(2)).doubleValue("balance"));
}
/**
* Tests an asynchronous transaction over key-value view.
*/
@Test
public void testTxAsyncKeyValue() {
igniteTransactions.beginAsync().thenApply(tx -> accounts.withTx(tx).kvView()).
thenCompose(txAcc -> txAcc.getAsync(makeKey(1))
.thenCombine(txAcc.getAsync(makeKey(2)), (v1, v2) -> new Pair<>(v1, v2))
.thenCompose(pair -> allOf(
txAcc.putAsync(makeKey(1), makeValue(1, pair.getFirst().doubleValue("balance") - DELTA)),
txAcc.putAsync(makeKey(2), makeValue(2, pair.getSecond().doubleValue("balance") + DELTA))
)
)
.thenApply(ignore -> txAcc.transaction())
).thenCompose(Transaction::commitAsync);
Mockito.verify(tx).commitAsync();
assertEquals(BALANCE_1 - DELTA, accounts.get(makeKey(1)).doubleValue("balance"));
assertEquals(BALANCE_2 + DELTA, accounts.get(makeKey(2)).doubleValue("balance"));
}
/**
* @param id The id.
* @return The key tuple.
*/
private Tuple makeKey(long id) {
return accounts.tupleBuilder().set("accountNumber", id).build();
}
/**
* @param id The id.
* @param balance The balance.
* @return The value tuple.
*/
private Tuple makeValue(long id, double balance) {
return accounts.tupleBuilder().set("accountNumber", id).set("balance", balance).build();
}
}