21
21
import static org .junit .Assert .assertEquals ;
22
22
import static org .junit .Assert .assertFalse ;
23
23
import static org .junit .Assert .assertTrue ;
24
+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
24
25
25
26
import org .apache .jena .query .ReadWrite ;
26
27
import org .apache .jena .query .TxnType ;
31
32
32
33
public class TestTxn {
33
34
34
- TxnCounter counter = new TxnCounter (0 ) ;
35
+ TxnCounter counter = new TxnCounter (0 ) ;
35
36
36
37
@ Test public void txn_basic_01 () {
37
38
long v1 = counter .get () ;
@@ -42,7 +43,7 @@ public class TestTxn {
42
43
}
43
44
44
45
@ Test public void txn_basic_02 () {
45
- long x =
46
+ long x =
46
47
Txn .calculateRead (counter , () -> {
47
48
assertEquals ("In R, value()" , 0 , counter .value ()) ;
48
49
assertEquals ("In R, get()" , 0 , counter .get ()) ;
@@ -53,7 +54,7 @@ public class TestTxn {
53
54
54
55
@ Test public void txn_basic_03 () {
55
56
Txn .executeWrite (counter , counter ::inc ) ;
56
- long x =
57
+ long x =
57
58
Txn .calculateRead (counter , () -> {
58
59
assertEquals ("In R, value()" , 1 , counter .value ()) ;
59
60
assertEquals ("In R, get()" , 1 , counter .get ()) ;
@@ -63,7 +64,7 @@ public class TestTxn {
63
64
}
64
65
65
66
@ Test public void txn_basic_05 () {
66
- long x =
67
+ long x =
67
68
Txn .calculateWrite (counter , () -> {
68
69
counter .inc () ;
69
70
assertEquals ("In W, value()" , 0 , counter .value ()) ;
@@ -74,7 +75,7 @@ public class TestTxn {
74
75
}
75
76
76
77
@ Test public void txn_write_01 () {
77
- long x =
78
+ long x =
78
79
Txn .calculateWrite (counter , () -> {
79
80
counter .inc () ;
80
81
assertEquals ("In W, value()" , 0 , counter .value ()) ;
@@ -87,7 +88,7 @@ public class TestTxn {
87
88
}
88
89
89
90
@ Test public void txn_write_02 () {
90
- long x =
91
+ long x =
91
92
Txn .calculateWrite (counter , () -> {
92
93
counter .inc () ;
93
94
assertEquals ("In W, value()" , 0 , counter .value ()) ;
@@ -121,13 +122,13 @@ public class TestTxn {
121
122
122
123
@ Test public void txn_rw_1 () {
123
124
assertEquals (0 , counter .get ()) ;
124
-
125
+
125
126
Txn .executeWrite (counter , () -> {
126
127
counter .inc () ;
127
128
assertEquals ("In W, value()" , 0 , counter .value ()) ;
128
129
assertEquals ("In W, get()" ,1 , counter .get ()) ;
129
130
}) ;
130
-
131
+
131
132
assertEquals ("Direct value()" , 1 , counter .value ()) ;
132
133
assertEquals ("Direct get()" , 1 , counter .get ()) ;
133
134
@@ -148,7 +149,7 @@ public class TestTxn {
148
149
assertEquals ("In W, value()" , 0 , counter .value ()) ;
149
150
assertEquals ("In W, get()" ,1 , counter .get ()) ;
150
151
}) ;
151
-
152
+
152
153
assertEquals ("Direct value()" , 1 , counter .get ()) ;
153
154
assertEquals ("Direct get()" , 1 , counter .get ()) ;
154
155
@@ -160,11 +161,11 @@ public class TestTxn {
160
161
161
162
@ Test public void txn_continue_1 () {
162
163
Txn .executeWrite (counter , ()->counter .set (91 )) ;
163
-
164
+
164
165
Txn .executeWrite (counter , ()-> {
165
166
assertEquals ("In txn, value()" , 91 , counter .value ()) ;
166
167
assertEquals ("In txn, read()" , 91 , counter .read ()) ;
167
- counter .inc ();
168
+ counter .inc ();
168
169
Txn .executeWrite (counter , ()->{
169
170
assertEquals ("In txn, value()" , 91 , counter .value ()) ;
170
171
assertEquals ("In txn, get()" , 92 , counter .read ()) ;
@@ -175,11 +176,11 @@ public class TestTxn {
175
176
176
177
@ Test public void txn_continue_2 () {
177
178
Txn .executeWrite (counter , ()->counter .set (91 )) ;
178
-
179
+
179
180
Txn .executeWrite (counter , ()-> {
180
181
assertEquals ("In txn, value()" , 91 , counter .value ()) ;
181
182
assertEquals ("In txn, read()" , 91 , counter .read ()) ;
182
- counter .inc ();
183
+ counter .inc ();
183
184
Txn .executeWrite (counter , ()->{
184
185
assertEquals ("In txn, value()" , 91 , counter .value ()) ;
185
186
assertEquals ("In txn, get()" , 92 , counter .read ()) ;
@@ -195,7 +196,7 @@ public class TestTxn {
195
196
@ Test (expected =ExceptionFromTest .class )
196
197
public void txn_exception_01 () {
197
198
Txn .executeWrite (counter , counter ::inc ) ;
198
-
199
+
199
200
Txn .executeWrite (counter , () -> {
200
201
counter .inc () ;
201
202
assertEquals ("In W, value()" , 1 , counter .value ()) ;
@@ -207,7 +208,7 @@ public void txn_exception_01() {
207
208
@ Test
208
209
public void txn_exception_02 () {
209
210
Txn .executeWrite (counter , ()->counter .set (8 )) ;
210
-
211
+
211
212
try {
212
213
Txn .executeWrite (counter , () -> {
213
214
counter .inc ();
@@ -327,7 +328,7 @@ public void txn_nested_11() {
327
328
Txn .exec (counter , TxnType .WRITE , ()->{});
328
329
});
329
330
}
330
-
331
+
331
332
@ Test (expected =JenaTransactionException .class )
332
333
public void txn_nested_12 () {
333
334
Txn .exec (counter , TxnType .READ_PROMOTE , ()->{
@@ -339,7 +340,7 @@ public void txn_nested_12() {
339
340
Txn .exec (counter , TxnType .WRITE , ()->{});
340
341
});
341
342
}
342
-
343
+
343
344
@ Test
344
345
public void txn_nested_13 () {
345
346
Txn .exec (counter , TxnType .READ_COMMITTED_PROMOTE , ()->{
@@ -378,41 +379,67 @@ public void txn_threaded_01() {
378
379
@ Test
379
380
public void txn_threaded_02 () {
380
381
//Transactional tx = DatasetGraphFactory.createTxnMem();
381
- Transactional tx = counter ;
382
-
382
+ Transactional tx = counter ;
383
+
383
384
// Start and enter the W transaction.
384
385
ThreadAction a = ThreadTxn .threadTxnWrite (tx , ()->{});
385
386
386
387
// ThreadAction started ... in W transaction.
387
388
Txn .exec (tx , TxnType .READ_PROMOTE , ()->{
388
389
// ... have the thread action complete.
389
- a .run ();
390
+ a .run ();
390
391
// Blocks promotion.
391
392
boolean b = tx .promote ();
392
393
assertFalse (b );
393
394
assertEquals (ReadWrite .READ , tx .transactionMode ());
394
395
});
395
396
}
396
-
397
+
397
398
@ Test
398
399
public void txn_threaded_03 () {
399
400
Transactional tx = DatasetGraphFactory .createTxnMem ();
400
- //Transactional tx = counter;
401
-
401
+ //Transactional tx = counter;
402
+
402
403
// Start and enter the W transaction.
403
404
ThreadAction a = ThreadTxn .threadTxnWriteAbort (tx , ()->{});
404
405
405
406
// ThreadAction started ... in W transaction.
406
407
Txn .exec (tx , TxnType .READ_PROMOTE , ()->{
407
408
// ... have the thread action abort..
408
- a .run ();
409
+ a .run ();
409
410
// Does not block promotion.
410
411
boolean b = tx .promote ();
411
412
assertTrue (b );
412
413
assertEquals (ReadWrite .WRITE , tx .transactionMode ());
413
414
});
414
415
}
415
416
417
+ @ Test public void txn_ctl_write_01 () {
418
+ long actualValue ;
419
+ try (TxnCtl txn = Txn .begin (counter , TxnType .WRITE )) {
420
+ counter .inc () ;
421
+ assertEquals ("In W, value()" , 0 , counter .value ()) ;
422
+ assertEquals ("In W, get()" ,1 , counter .get ()) ;
423
+ actualValue = counter .get () ;
424
+ counter .commit () ;
425
+ }
426
+ long expectedValue = counter .get ();
427
+ assertEquals ("Outside W" , expectedValue , actualValue ) ;
428
+ }
429
+
430
+ @ Test public void txn_ctl_write_02 () {
431
+ long expectedValue = counter .get ();
432
+ try (TxnCtl txn = Txn .begin (counter , TxnType .WRITE )) {
433
+ counter .inc () ;
434
+ assertEquals ("In W, value()" , 0 , counter .value ()) ;
435
+ assertEquals ("In W, get()" ,1 , counter .get ()) ;
436
+ // Intermediate value will be reverted.
437
+ long intermediateValue = counter .get () ;
438
+ assertNotEquals (expectedValue , intermediateValue );
439
+ // no commit - auto-close is expected to abort.
440
+ }
441
+ long actualValue = counter .get ();
442
+ assertEquals ("Outside W" , expectedValue , actualValue ) ;
443
+ }
416
444
}
417
445
418
-
0 commit comments