Skip to content

Commit 5f8a3c0

Browse files
committed
Adjust TestQueryForUpdate for Yugabyte - check repeatable read isolation level
1 parent 0ae6956 commit 5f8a3c0

File tree

1 file changed

+62
-10
lines changed

1 file changed

+62
-10
lines changed

ebean-test/src/test/java/org/tests/basic/TestQueryForUpdate.java

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import io.ebean.*;
44
import io.ebean.annotation.Platform;
5+
import io.ebean.annotation.TxIsolation;
56
import io.ebean.test.LoggedSql;
67
import io.ebean.xtest.BaseTestCase;
78
import io.ebean.xtest.ForPlatform;
89
import org.junit.jupiter.api.Test;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
912
import org.tests.model.basic.Customer;
1013
import org.tests.model.basic.EBasic;
1114
import org.tests.model.basic.Order;
@@ -18,6 +21,8 @@
1821

1922
public class TestQueryForUpdate extends BaseTestCase {
2023

24+
private static final Logger log = LoggerFactory.getLogger(TestQueryForUpdate.class);
25+
2126
@Test
2227
public void testForUpdate() {
2328

@@ -41,6 +46,52 @@ public void testForUpdate() {
4146
}
4247
}
4348

49+
50+
@ForPlatform(Platform.YUGABYTE)
51+
@Test
52+
void concurrentForUpdate() throws InterruptedException {
53+
ResetBasicData.reset();
54+
55+
Database db = DB.getDefault();
56+
Thread t1 = new Thread() {
57+
@Override
58+
public void run() {
59+
try (final Transaction transaction = db.createTransaction(TxIsolation.REPEATABLE_READ)) {
60+
log.info("(Y)Thread: before find");
61+
DB.find(Customer.class)
62+
.usingTransaction(transaction)
63+
.forUpdate()
64+
.findList();
65+
66+
log.info("(Y)Thread: after find");
67+
try {
68+
Thread.sleep(3000);
69+
} catch (InterruptedException e) {
70+
throw new RuntimeException(e);
71+
}
72+
log.info("(Y)Thread: done");
73+
}
74+
}
75+
};
76+
77+
t1.start();
78+
Thread.sleep(300);
79+
80+
long start = System.currentTimeMillis();
81+
try (final Transaction transaction = db.createTransaction(TxIsolation.REPEATABLE_READ)) {
82+
log.info("(Y)Main: before find");
83+
DB.find(Customer.class)
84+
.usingTransaction(transaction)
85+
.forUpdate()
86+
.findList();
87+
88+
log.info("(Y)Main: after find");
89+
}
90+
91+
long exeMillis = System.currentTimeMillis() - start;
92+
assertThat(exeMillis).isGreaterThan(2500);
93+
}
94+
4495
@Test
4596
public void testConcurrentForUpdate() throws InterruptedException {
4697

@@ -49,45 +100,46 @@ public void testConcurrentForUpdate() throws InterruptedException {
49100
Thread t1 = new Thread() {
50101
@Override
51102
public void run() {
52-
try (final Transaction transaction = DB.beginTransaction()) {
53-
System.out.println("Thread: before find");
103+
try (final Transaction transaction = DB.createTransaction()) {
104+
log.info("Thread: before find");
54105
DB.find(Customer.class)
106+
.usingTransaction(transaction)
55107
.forUpdate()
56108
// .orderBy().desc("1") // this would help by the locks in DB2
57109
.findList();
58110

59-
System.out.println("Thread: after find");
111+
log.info("Thread: after find");
60112
try {
61113
Thread.sleep(3000);
62114
} catch (InterruptedException e) {
63115
throw new RuntimeException(e);
64116
}
65-
System.out.println("Thread: done");
117+
log.info("Thread: done");
66118
}
67119
}
68120
};
69121

70122
t1.start();
71123

72-
Thread.sleep(100);
124+
Thread.sleep(300);
73125

74126
long start = System.currentTimeMillis();
75127
try (final Transaction transaction = DB.beginTransaction()) {
76128
if (isH2()) {
77129
DB.sqlUpdate("SET LOCK_TIMEOUT 5000").execute();
78130
}
79-
System.out.println("Main: before find");
131+
log.info("Main: before find");
80132
DB.find(Customer.class)
133+
.usingTransaction(transaction)
81134
.forUpdate()
82135
//.orderBy().desc("1") // this would help by the locks in DB2
83136
.findList();
84137

85-
System.out.println("Main: after find");
138+
log.info("Main: after find");
86139
}
87140

88-
start = System.currentTimeMillis() - start;
89-
90-
assertThat(start).isGreaterThan(2800);
141+
long exeMillis = System.currentTimeMillis() - start;
142+
assertThat(exeMillis).isGreaterThan(2500);
91143

92144
}
93145

0 commit comments

Comments
 (0)