|
6 | 6 |
|
7 | 7 | import java.util.Comparator;
|
8 | 8 | import java.util.List;
|
| 9 | +import java.util.concurrent.CountDownLatch; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.concurrent.atomic.AtomicReference; |
9 | 12 |
|
10 | 13 | import static org.junit.Assert.*;
|
11 | 14 |
|
@@ -79,4 +82,35 @@ private void assertTestEntityEquals(TestEntity expected, TestEntity actual) {
|
79 | 82 | assertEquals(expected.getId(), actual.getId());
|
80 | 83 | assertEquals(expected.getSimpleString(), actual.getSimpleString());
|
81 | 84 | }
|
| 85 | + |
| 86 | + @Test |
| 87 | + public void queryThreadLocal() throws InterruptedException { |
| 88 | + Query<TestEntity> queryOriginal = box.query().build(); |
| 89 | + QueryThreadLocal<TestEntity> threadLocal = new QueryThreadLocal<>(queryOriginal); |
| 90 | + |
| 91 | + AtomicReference<Query<TestEntity>> queryThreadAtomic = new AtomicReference<>(); |
| 92 | + CountDownLatch latch = new CountDownLatch(1); |
| 93 | + new Thread(() -> { |
| 94 | + queryThreadAtomic.set(threadLocal.get()); |
| 95 | + latch.countDown(); |
| 96 | + }).start(); |
| 97 | + |
| 98 | + assertTrue(latch.await(1, TimeUnit.SECONDS)); |
| 99 | + |
| 100 | + Query<TestEntity> queryThread = queryThreadAtomic.get(); |
| 101 | + Query<TestEntity> queryMain = threadLocal.get(); |
| 102 | + |
| 103 | + // Assert that initialValue returns something. |
| 104 | + assertNotNull(queryThread); |
| 105 | + assertNotNull(queryMain); |
| 106 | + |
| 107 | + // Assert that initialValue returns clones. |
| 108 | + assertNotEquals(queryThread.handle, queryOriginal.handle); |
| 109 | + assertNotEquals(queryMain.handle, queryOriginal.handle); |
| 110 | + assertNotEquals(queryThread.handle, queryMain.handle); |
| 111 | + |
| 112 | + queryOriginal.close(); |
| 113 | + queryMain.close(); |
| 114 | + queryThread.close(); |
| 115 | + } |
82 | 116 | }
|
0 commit comments