Skip to content

Commit 1163ec7

Browse files
author
chengyitian
committed
Merge remote-tracking branch 'origin/dev_conn_pool_Prj2' into dev_conn_pool_Prj2
2 parents a4d7884 + f5f3468 commit 1163ec7

File tree

1 file changed

+323
-0
lines changed

1 file changed

+323
-0
lines changed

test/com/xxdb/SimpleDBConnectionPoolTest.java

Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.time.LocalTime;
1616
import java.time.Month;
1717
import java.util.*;
18+
import java.util.concurrent.atomic.AtomicReference;
1819

1920
import static org.junit.Assert.assertEquals;
2021

@@ -53,6 +54,71 @@ public void tearDown() throws Exception {
5354
System.out.println(site);
5455
}
5556
}
57+
@Test
58+
public void test_SimpleDBConnectionPool_config_oldversion(){
59+
config.setInitialPoolSize(5);
60+
SimpleDBConnectionPool pool=new SimpleDBConnectionPool(config);
61+
assertEquals(5,config.getMinimumPoolSize());
62+
assertEquals(5,config.getMaximumPoolSize());
63+
}
64+
@Test
65+
public void test_SimpleDBConnectionPool_config_newversion(){
66+
SimpleDBConnectionPool pool=new SimpleDBConnectionPool(config);
67+
assertEquals(5,config.getMinimumPoolSize());
68+
assertEquals(5,config.getMaximumPoolSize());
69+
}
70+
@Test
71+
public void test_SimpleDBConnectionPool_config_MinimumandPoolSize_error() {
72+
config.setMinimumPoolSize(-1);
73+
pool = new SimpleDBConnectionPool(config);
74+
assertEquals(5, config.getMinimumPoolSize());
75+
76+
config.setMinimumPoolSize(0);
77+
pool = new SimpleDBConnectionPool(config);
78+
assertEquals(5, config.getMinimumPoolSize());
79+
80+
config.setMinimumPoolSize(6);
81+
pool = new SimpleDBConnectionPool(config);
82+
assertEquals(6, config.getMinimumPoolSize());
83+
assertEquals(6, config.getMaximumPoolSize());
84+
85+
}
86+
@Test
87+
public void test_SimpleDBConnectionPool_config_MinimumandPoolSize_null() {
88+
pool = new SimpleDBConnectionPool(config);
89+
assertEquals(5, config.getMinimumPoolSize());
90+
}
91+
@Test
92+
public void test_SimpleDBConnectionPool_config_MaximumPoolSize_error() {
93+
config.setMaximumPoolSize(0);
94+
pool = new SimpleDBConnectionPool(config);
95+
assertEquals(5, config.getMaximumPoolSize());
96+
97+
config.setMaximumPoolSize(-5);
98+
pool = new SimpleDBConnectionPool(config);
99+
assertEquals(5, config.getMaximumPoolSize());
100+
101+
}
102+
@Test
103+
public void test_SimpleDBConnectionPool_config_MaximumPoolSize_null() {
104+
pool = new SimpleDBConnectionPool(config);
105+
assertEquals(5, config.getMaximumPoolSize());
106+
}
107+
@Test
108+
public void test_SimpleDBConnectionPool_config_InvalididleTimeout_error() {
109+
config.setIdleTimeout(-50);
110+
pool = new SimpleDBConnectionPool(config);
111+
assertEquals(600000, config.getIdleTimeout());
112+
config.setIdleTimeout(0);
113+
pool = new SimpleDBConnectionPool(config);
114+
assertEquals(600000, config.getIdleTimeout());
115+
116+
}
117+
@Test
118+
public void test_SimpleDBConnectionPool_config_InvalididleTimeout_null() {
119+
pool = new SimpleDBConnectionPool(config);
120+
assertEquals(600000, config.getIdleTimeout());
121+
}
56122

57123
@Test
58124
public void test_SimpleDBConnectionPool_config_hostName_error() throws IOException, InterruptedException {
@@ -563,6 +629,263 @@ public void test_SimpleDBConnectionPool_config_default() throws IOException, Int
563629
pool.close();
564630
}
565631
@Test
632+
public void test_SimpleDBConnectionPool_getConnection_new(){
633+
config.setMaximumPoolSize(10);
634+
config.setMinimumPoolSize(6);
635+
pool=new SimpleDBConnectionPool(config);
636+
DBConnection conn = pool.getConnection();
637+
assertEquals(1, pool.getActiveConnectionsCount());
638+
assertEquals(5, pool.getIdleConnectionsCount());
639+
assertEquals(6, pool.getTotalConnectionsCount());
640+
for (int i = 1; i < 10; i++) {
641+
pool.getConnection();
642+
}
643+
assertEquals(10, pool.getActiveConnectionsCount());
644+
assertEquals(0, pool.getIdleConnectionsCount());
645+
assertEquals(10, pool.getTotalConnectionsCount());
646+
}
647+
@Test(expected = RuntimeException.class)
648+
public void test_SimpleDBConnectionPool_getConnection_new_WhenMaxrReadched() {
649+
for (int i = 0; i < 5; i++) {
650+
pool.getConnection();
651+
}
652+
pool.getConnection();
653+
}
654+
@Test
655+
public void test_SimpleDBConnectionPool_getConnection_new_MultiThread() throws InterruptedException {
656+
int n=9;
657+
config.setMaximumPoolSize(10);
658+
config.setMinimumPoolSize(6);
659+
pool=new SimpleDBConnectionPool(config);
660+
Thread[] threads = new Thread[10];
661+
for (int i = 0; i <10; i++) {
662+
threads[i] = new Thread(() -> {
663+
DBConnection poolEntry = pool.getConnection();
664+
});
665+
}
666+
for (int i = 0; i <9; i++) {
667+
threads[i].start();
668+
}
669+
for (int i = 0; i <9; i++) {
670+
threads[i].join();
671+
}
672+
assertEquals(9, pool.getActiveConnectionsCount());
673+
assertEquals(9, pool.getTotalConnectionsCount());
674+
assertEquals(0, pool.getIdleConnectionsCount());
675+
threads[9].start();
676+
threads[9].join();
677+
assertEquals(10, pool.getActiveConnectionsCount());
678+
assertEquals(10, pool.getTotalConnectionsCount());
679+
assertEquals(0, pool.getIdleConnectionsCount());
680+
}
681+
@Test
682+
public void test_SimpleDBConnectionPool_getConnection_new_WhenMaxrReadched_MultiThread() throws InterruptedException {
683+
int n=11;
684+
config.setMaximumPoolSize(10);
685+
config.setMinimumPoolSize(6);
686+
pool=new SimpleDBConnectionPool(config);
687+
Thread[] threads = new Thread[n];
688+
AtomicReference<Exception> exception = new AtomicReference<>();
689+
for (int i = 0; i < n; i++) {
690+
threads[i] = new Thread(() -> {
691+
try {
692+
DBConnection poolEntry = pool.getConnection();
693+
} catch (Exception e) {
694+
exception.set(e);
695+
}
696+
});
697+
}
698+
for (int i = 0; i <n; i++) {
699+
threads[i].start();
700+
}
701+
for (int i = 0; i < n; i++) {
702+
threads[i].join();
703+
}
704+
assertEquals("java.lang.RuntimeException: No available idle connections.",exception.get().toString());
705+
}
706+
@Test
707+
public void test_SimpleDBConnectionPool_close_new(){
708+
config.setMaximumPoolSize(10);
709+
config.setMinimumPoolSize(6);
710+
pool=new SimpleDBConnectionPool(config);
711+
DBConnection[] a=new DBConnection[10];
712+
for(int i=0;i<10;i++){
713+
a[i]=pool.getConnection();
714+
}
715+
assertEquals(10, pool.getTotalConnectionsCount());
716+
assertEquals(10, pool.getActiveConnectionsCount());
717+
assertEquals(0, pool.getIdleConnectionsCount());
718+
a[0].close();
719+
assertEquals(10, pool.getTotalConnectionsCount());
720+
assertEquals(9, pool.getActiveConnectionsCount());
721+
assertEquals(1, pool.getIdleConnectionsCount());
722+
for(int i=1;i<10;i++){
723+
a[i].close();
724+
}
725+
assertEquals(10, pool.getTotalConnectionsCount());
726+
assertEquals(0, pool.getActiveConnectionsCount());
727+
assertEquals(10, pool.getIdleConnectionsCount());
728+
}
729+
@Test
730+
public void test_SimpleDBConnectionPool_close_new_MultiThread() throws InterruptedException {
731+
int n=10;
732+
config.setMaximumPoolSize(10);
733+
config.setMinimumPoolSize(6);
734+
pool=new SimpleDBConnectionPool(config);
735+
DBConnection[] a=new DBConnection[10];
736+
for(int i=0;i<10;i++){
737+
a[i]=pool.getConnection();
738+
}
739+
Thread[] threads = new Thread[n];
740+
for (int i = 0; i < n; i++) {
741+
final int index = i;
742+
threads[i] = new Thread(() -> {
743+
a[index].close();
744+
});
745+
}
746+
for (int i = 0; i <n-1; i++) {
747+
threads[i].start();
748+
}
749+
for (int i = 0; i < n-1; i++) {
750+
threads[i].join();
751+
}
752+
assertEquals(1, pool.getActiveConnectionsCount());
753+
assertEquals(10, pool.getTotalConnectionsCount());
754+
assertEquals(9, pool.getIdleConnectionsCount());
755+
threads[9].start();
756+
threads[9].join();
757+
assertEquals(0, pool.getActiveConnectionsCount());
758+
assertEquals(10, pool.getTotalConnectionsCount());
759+
assertEquals(10, pool.getIdleConnectionsCount());
760+
}
761+
@Test
762+
public void test_SimpleDBConnectionPool_closeIdleConnections_Auto() throws InterruptedException {
763+
config.setMaximumPoolSize(10);
764+
config.setMinimumPoolSize(6);
765+
config.setIdleTimeout(10000);
766+
pool=new SimpleDBConnectionPool(config);
767+
DBConnection [] A=new DBConnection[10];
768+
for(int i=0;i<10;i++){
769+
A[i]=pool.getConnection();
770+
}
771+
assertEquals(10,pool.getTotalConnectionsCount());
772+
assertEquals(10,pool.getActiveConnectionsCount());
773+
assertEquals(0,pool.getIdleConnectionsCount());
774+
for(int i=0;i<10;i++){
775+
A[i].close();
776+
}
777+
Thread.sleep(9000);
778+
assertEquals(10,pool.getTotalConnectionsCount());
779+
assertEquals(0,pool.getActiveConnectionsCount());
780+
assertEquals(10,pool.getIdleConnectionsCount());
781+
Thread.sleep(1000);
782+
assertEquals(10,pool.getTotalConnectionsCount());
783+
assertEquals(0,pool.getActiveConnectionsCount());
784+
assertEquals(10,pool.getIdleConnectionsCount());
785+
Thread.sleep(972);//972
786+
assertEquals(6,pool.getTotalConnectionsCount());
787+
assertEquals(0,pool.getActiveConnectionsCount());
788+
assertEquals(6,pool.getIdleConnectionsCount());
789+
Thread.sleep(1000);//972
790+
assertEquals(6,pool.getTotalConnectionsCount());
791+
assertEquals(0,pool.getActiveConnectionsCount());
792+
assertEquals(6,pool.getIdleConnectionsCount());
793+
}
794+
@Test
795+
public void test_SimpleDBConnectionPool_closeIdleConnections_Auto_MultiThread() throws InterruptedException {
796+
config.setMaximumPoolSize(10);
797+
config.setMinimumPoolSize(6);
798+
config.setIdleTimeout(10000);
799+
pool=new SimpleDBConnectionPool(config);
800+
DBConnection [] A=new DBConnection[10];
801+
for(int i=0;i<10;i++){
802+
A[i]=pool.getConnection();
803+
}
804+
Thread[] threads = new Thread[10];
805+
for(int i=0;i<10;i++){
806+
int finalI = i;
807+
threads[i]=new Thread(()->{
808+
A[finalI].close();
809+
try {
810+
Thread.sleep(10972);
811+
} catch (InterruptedException e) {
812+
throw new RuntimeException(e);
813+
}
814+
815+
});
816+
}
817+
for (int i=0;i<10;i++){
818+
threads[i].start();
819+
}
820+
for (int i=0;i<10;i++){
821+
threads[i].join();
822+
}
823+
assertEquals(6,pool.getTotalConnectionsCount());
824+
assertEquals(0,pool.getActiveConnectionsCount());
825+
assertEquals(6,pool.getIdleConnectionsCount());
826+
Thread.sleep(972);//972
827+
assertEquals(6,pool.getTotalConnectionsCount());
828+
assertEquals(0,pool.getActiveConnectionsCount());
829+
assertEquals(6,pool.getIdleConnectionsCount());
830+
}
831+
@Test
832+
public void test_SimpleDBConnectionPool_closeIdleConnectionsManual() throws InterruptedException{
833+
config.setMaximumPoolSize(10);
834+
config.setMinimumPoolSize(6);
835+
config.setIdleTimeout(10000);
836+
pool=new SimpleDBConnectionPool(config);
837+
DBConnection [] A=new DBConnection[10];
838+
for(int i=0;i<10;i++){
839+
A[i]=pool.getConnection();
840+
}
841+
assertEquals(10,pool.getTotalConnectionsCount());
842+
assertEquals(10,pool.getActiveConnectionsCount());
843+
assertEquals(0,pool.getIdleConnectionsCount());
844+
for(int i=0;i<10;i++){
845+
A[i].close();
846+
}
847+
Thread.sleep(7000);
848+
assertEquals(10,pool.getTotalConnectionsCount());
849+
assertEquals(0,pool.getActiveConnectionsCount());
850+
assertEquals(10,pool.getIdleConnectionsCount());
851+
852+
pool.manualCleanupIdleConnections();
853+
assertEquals(6,pool.getTotalConnectionsCount());
854+
assertEquals(0,pool.getActiveConnectionsCount());
855+
assertEquals(6,pool.getIdleConnectionsCount());
856+
Thread.sleep(1000);
857+
assertEquals(6,pool.getTotalConnectionsCount());
858+
assertEquals(0,pool.getActiveConnectionsCount());
859+
assertEquals(6,pool.getIdleConnectionsCount());
860+
}
861+
@Test
862+
public void test_SimpleDBConnectionPool_closeIdleConnectionsManual_MultiThread() throws InterruptedException{
863+
config.setMaximumPoolSize(10);
864+
config.setMinimumPoolSize(6);
865+
config.setIdleTimeout(10000);
866+
pool=new SimpleDBConnectionPool(config);
867+
DBConnection [] A=new DBConnection[10];
868+
for(int i=0;i<10;i++){
869+
A[i]=pool.getConnection();
870+
}
871+
for(int i=0;i<10;i++){
872+
A[i].close();
873+
}
874+
Thread thread=new Thread(()->{
875+
try {
876+
Thread.sleep(1000);
877+
} catch (InterruptedException e) {
878+
throw new RuntimeException(e);
879+
}
880+
pool.manualCleanupIdleConnections();
881+
});
882+
thread.start();
883+
thread.join();
884+
assertEquals(6,pool.getTotalConnectionsCount());
885+
assertEquals(0,pool.getActiveConnectionsCount());
886+
assertEquals(6,pool.getIdleConnectionsCount());
887+
}
888+
@Test
566889
public void test_SimpleDBConnectionPool_connectCount_equal_threadCount() throws IOException, InterruptedException {
567890
config.setInitialPoolSize(5);
568891
pool = new SimpleDBConnectionPool(config);

0 commit comments

Comments
 (0)