1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.blocking_cache;
17
18 import java.io.Reader;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.TimeUnit;
22
23 import org.apache.ibatis.BaseDataTest;
24 import org.apache.ibatis.io.Resources;
25 import org.apache.ibatis.session.SqlSession;
26 import org.apache.ibatis.session.SqlSessionFactory;
27 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28 import org.assertj.core.api.Assertions;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31
32
33 class BlockingCacheTest {
34
35 private static SqlSessionFactory sqlSessionFactory;
36
37 @BeforeEach
38 void setUp() throws Exception {
39
40 try (Reader reader = Resources
41 .getResourceAsReader("org/apache/ibatis/submitted/blocking_cache/mybatis-config.xml")) {
42 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43 }
44
45
46 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
47 "org/apache/ibatis/submitted/blocking_cache/CreateDB.sql");
48 }
49
50 @Test
51 void testBlockingCache() throws InterruptedException {
52 ExecutorService defaultThreadPool = Executors.newFixedThreadPool(2);
53
54 long init = System.currentTimeMillis();
55
56 for (int i = 0; i < 2; i++) {
57 defaultThreadPool.execute(this::accessDB);
58 }
59
60 defaultThreadPool.shutdown();
61 if (!defaultThreadPool.awaitTermination(5, TimeUnit.SECONDS)) {
62 defaultThreadPool.shutdownNow();
63 }
64
65 long totalTime = System.currentTimeMillis() - init;
66 Assertions.assertThat(totalTime).isGreaterThanOrEqualTo(1000);
67 }
68
69 private void accessDB() {
70 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
71 PersonMapper pm = sqlSession.getMapper(PersonMapper.class);
72 pm.findAll();
73 try {
74 Thread.sleep(500);
75 } catch (InterruptedException e) {
76 Assertions.fail(e.getMessage());
77 }
78 }
79 }
80
81 @Test
82 void ensureLockIsAcquiredBeforePut() {
83 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
84 PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
85 mapper.delete(-1);
86 mapper.findAll();
87 sqlSession.commit();
88 }
89 }
90
91 @Test
92 void ensureLockIsReleasedOnRollback() {
93 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
94 PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
95 mapper.delete(-1);
96 mapper.findAll();
97 sqlSession.rollback();
98 }
99 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
100 PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
101 mapper.findAll();
102 }
103 }
104 }