Skip to content

Commit 48fb9b3

Browse files
committed
Tests Yugabyte - faster test using forUpdate
1 parent f644004 commit 48fb9b3

File tree

1 file changed

+52
-55
lines changed

1 file changed

+52
-55
lines changed

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

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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;
@@ -45,56 +46,52 @@ public void testForUpdate() {
4546
}
4647

4748
// Nah, nothing to do with repeatable read here
48-
// @ForPlatform(Platform.YUGABYTE)
49-
// @Test
50-
// void concurrentForUpdate() throws InterruptedException {
51-
// ResetBasicData.reset();
52-
//
53-
// Database db = DB.getDefault();
54-
// Thread t1 = new Thread() {
55-
// @Override
56-
// public void run() {
57-
// try (final Transaction transaction = db.createTransaction(TxIsolation.REPEATABLE_READ)) {
58-
// log2.info("(REPEATABLE_READ) Thread: before find");
59-
// List<Country> list = DB.find(Country.class)
60-
// .usingTransaction(transaction)
61-
// .forUpdate()
62-
// .findList();
63-
//
64-
// Country first = list.get(0);
65-
// db.markAsDirty(first);
66-
// db.save(first, transaction);
67-
//
68-
// log2.info("(REPEATABLE_READ) Thread: after find - size:{}", list.size());
69-
// try {
70-
// Thread.sleep(3000);
71-
// } catch (InterruptedException e) {
72-
// throw new RuntimeException(e);
73-
// }
74-
// log2.info("(REPEATABLE_READ) Thread: done");
75-
// }
76-
// }
77-
// };
78-
//
79-
// t1.start();
80-
// Thread.sleep(300);
81-
//
82-
// long start = System.currentTimeMillis();
83-
// try (final Transaction transaction = db.createTransaction(TxIsolation.REPEATABLE_READ)) {
84-
// log2.info("(REPEATABLE_READ) Main: before find, should wait ...");
85-
// DB.find(Country.class)
86-
// .usingTransaction(transaction)
87-
// .forUpdate()
88-
// .findList();
89-
//
90-
// log2.info("(REPEATABLE_READ) Main: complete");
91-
// }
92-
//
93-
// long exeMillis = System.currentTimeMillis() - start;
94-
// assertThat(exeMillis).isGreaterThan(2500);
95-
// }
96-
97-
@IgnorePlatform(Platform.YUGABYTE) // ignore this for yugabyte and see if everything else passes
49+
@ForPlatform(Platform.YUGABYTE)
50+
@Test
51+
void concurrentForUpdate() throws InterruptedException {
52+
ResetBasicData.reset();
53+
54+
Database db = DB.getDefault();
55+
Thread t1 = new Thread() {
56+
@Override
57+
public void run() {
58+
try (final Transaction transaction = db.createTransaction(TxIsolation.REPEATABLE_READ)) {
59+
log2.info("(REPEATABLE_READ) Thread: before find");
60+
List<Country> list = DB.find(Country.class)
61+
.usingTransaction(transaction)
62+
.forUpdate()
63+
.findList();
64+
65+
try {
66+
log2.info("(REPEATABLE_READ) Thread: after find - size:{}, hold locks for some time", list.size());
67+
Thread.sleep(500);
68+
} catch (InterruptedException e) {
69+
throw new RuntimeException(e);
70+
}
71+
log2.info("(REPEATABLE_READ) Thread: done, release locks");
72+
}
73+
}
74+
};
75+
76+
t1.start();
77+
Thread.sleep(100);
78+
79+
long start = System.currentTimeMillis();
80+
try (final Transaction transaction = db.createTransaction(TxIsolation.REPEATABLE_READ)) {
81+
log2.info("(REPEATABLE_READ) Main: before find, should wait for locks to be released...");
82+
DB.find(Country.class)
83+
.usingTransaction(transaction)
84+
.forUpdate()
85+
.findList();
86+
87+
log2.info("(REPEATABLE_READ) Main: complete");
88+
}
89+
90+
long exeMillis = System.currentTimeMillis() - start;
91+
assertThat(exeMillis).isGreaterThan(300);
92+
}
93+
94+
// @IgnorePlatform(Platform.YUGABYTE) // ignore this for yugabyte and see if everything else passes
9895
@Test
9996
public void testConcurrentForUpdate() throws InterruptedException {
10097
ResetBasicData.reset();
@@ -115,18 +112,18 @@ public void testConcurrentForUpdate() throws InterruptedException {
115112
db.save(first, transaction);
116113

117114
try {
118-
log.info("Thread: after find - rows locked:{}, hold for 3 seconds", list.size());
119-
Thread.sleep(3000);
115+
log.info("Thread: after find - rows locked:{}, hold for 700ms", list.size());
116+
Thread.sleep(600); // 3000
120117
} catch (InterruptedException e) {
121118
throw new RuntimeException(e);
122119
}
123-
log.info("Thread: done");
120+
log.info("Thread: done, releasing locks");
124121
} // transaction ended here via try-with-resources
125122
});
126123

127124
t1.start();
128125

129-
Thread.sleep(300);
126+
Thread.sleep(100);
130127

131128
long start = System.currentTimeMillis();
132129
try (final Transaction transaction = db.beginTransaction()) { // second transaction
@@ -146,7 +143,7 @@ public void testConcurrentForUpdate() throws InterruptedException {
146143
long exeMillis = System.currentTimeMillis() - start;
147144
assertThat(exeMillis)
148145
.describedAs("select for update expected to wait")
149-
.isGreaterThan(2500);
146+
.isGreaterThan(400); // 2500
150147
}
151148

152149
@Test

0 commit comments

Comments
 (0)