55import org .slf4j .LoggerFactory ;
66import org .testng .annotations .Test ;
77
8+ import java .sql .Connection ;
89import java .sql .SQLException ;
910
11+ import static org .assertj .core .api .Assertions .assertThat ;
1012import static org .testng .Assert .assertEquals ;
1113
1214public class ConnectionPoolOfflineTest {
@@ -17,7 +19,7 @@ private DataSourceConfig config() {
1719
1820 DataSourceConfig config = new DataSourceConfig ();
1921 config .setDriver ("org.h2.Driver" );
20- config .setUrl ("jdbc:h2:mem:tests " );
22+ config .setUrl ("jdbc:h2:mem:testOffline " );
2123 config .setUsername ("sa" );
2224 config .setPassword ("" );
2325 config .setMinConnections (2 );
@@ -33,7 +35,8 @@ public void testOffline() throws InterruptedException, SQLException {
3335
3436 DataSourceConfig config = config ();
3537
36- ConnectionPool pool = new ConnectionPool ("test" , config );
38+ ConnectionPool pool = new ConnectionPool ("testOffline" , config );
39+ assertThat (pool .isOnline ()).isFalse ();
3740 log .info ("pool created " );
3841 Thread .sleep (3000 );
3942
@@ -42,28 +45,81 @@ public void testOffline() throws InterruptedException, SQLException {
4245
4346 pool .online ();
4447 log .info ("pool online" );
48+ assertThat (pool .isOnline ()).isTrue ();
4549 assertEquals (2 , pool .getStatus (false ).getFree ());
4650 assertEquals (0 , pool .getStatus (false ).getBusy ());
4751
4852 Thread .sleep (3000 );
4953
5054 pool .offline ();
5155 log .info ("pool offline" );
56+ assertThat (pool .isOnline ()).isFalse ();
5257 assertEquals (0 , pool .getStatus (false ).getFree ());
5358 assertEquals (0 , pool .getStatus (false ).getBusy ());
5459
5560 Thread .sleep (3000 );
5661
5762 pool .online ();
5863 log .info ("pool online" );
64+ assertThat (pool .isOnline ()).isTrue ();
5965 assertEquals (2 , pool .getStatus (false ).getFree ());
6066 assertEquals (0 , pool .getStatus (false ).getBusy ());
6167 Thread .sleep (3000 );
6268
63- pool .shutdown (false );
69+ pool .shutdown ();
6470
71+ assertThat (pool .isOnline ()).isFalse ();
6572 assertEquals (0 , pool .getStatus (false ).getFree ());
6673 assertEquals (0 , pool .getStatus (false ).getBusy ());
6774 }
6875
76+ @ Test
77+ public void offlineOffline () {
78+
79+ DataSourceConfig config = config ().setUrl ("jdbc:h2:mem:offlineOffline" );
80+
81+ ConnectionPool pool = new ConnectionPool ("offlineOffline" , config );
82+ assertThat (pool .isOnline ()).isFalse ();
83+
84+ pool .offline ();
85+ assertThat (pool .isOnline ()).isFalse ();
86+
87+ pool .offline ();
88+ pool .offline ();
89+ assertThat (pool .isOnline ()).isFalse ();
90+ }
91+
92+ @ Test
93+ public void offlineGetConnection_expect_goesOnline () throws SQLException {
94+
95+ DataSourceConfig config = config ().setUrl ("jdbc:h2:mem:offlineOffline" );
96+
97+ ConnectionPool pool = new ConnectionPool ("offlineOffline" , config );
98+ pool .offline ();
99+ assertThat (pool .isOnline ()).isFalse ();
100+
101+ try (Connection connection = pool .getConnection ()) {
102+ assertThat (connection ).isNotNull ();
103+ assertThat (pool .isOnline ()).isTrue ();
104+ }
105+
106+ pool .shutdown ();
107+ assertThat (pool .isOnline ()).isFalse ();
108+ }
109+
110+ @ Test
111+ public void onlineOnline () throws SQLException {
112+
113+ DataSourceConfig config = config ().setUrl ("jdbc:h2:mem:onlineOnline" );
114+
115+ ConnectionPool pool = new ConnectionPool ("onlineOnline" , config );
116+ assertThat (pool .isOnline ()).isFalse ();
117+
118+ pool .online ();
119+ assertThat (pool .isOnline ()).isTrue ();
120+
121+ pool .online ();
122+ pool .online ();
123+ assertThat (pool .isOnline ()).isTrue ();
124+ }
69125}
0 commit comments