1+ package com .iluwatar .object .pool ;
2+
3+ import org .junit .Test ;
4+
5+ import java .util .Arrays ;
6+ import java .util .List ;
7+
8+ import static org .junit .Assert .assertEquals ;
9+ import static org .junit .Assert .assertNotSame ;
10+ import static org .junit .Assert .assertSame ;
11+ import static org .junit .Assert .assertTrue ;
12+
13+ /**
14+ * Date: 12/27/15 - 1:05 AM
15+ *
16+ * @author Jeroen Meulemeester
17+ */
18+ public class OliphauntPoolTest {
19+
20+ /**
21+ * Use the same object 100 times subsequently. This should not take much time since the heavy
22+ * object instantiation is done only once. Verify if we get the same object each time.
23+ */
24+ @ Test (timeout = 5000 )
25+ public void testSubsequentCheckinCheckout () {
26+ final OliphauntPool pool = new OliphauntPool ();
27+ assertEquals (pool .toString (), "Pool available=0 inUse=0" );
28+
29+ final Oliphaunt expectedOliphaunt = pool .checkOut ();
30+ assertEquals (pool .toString (), "Pool available=0 inUse=1" );
31+
32+ pool .checkIn (expectedOliphaunt );
33+ assertEquals (pool .toString (), "Pool available=1 inUse=0" );
34+
35+ for (int i = 0 ; i < 100 ; i ++) {
36+ final Oliphaunt oliphaunt = pool .checkOut ();
37+ assertEquals (pool .toString (), "Pool available=0 inUse=1" );
38+ assertSame (expectedOliphaunt , oliphaunt );
39+ assertEquals (expectedOliphaunt .getId (), oliphaunt .getId ());
40+ assertEquals (expectedOliphaunt .toString (), oliphaunt .toString ());
41+
42+ pool .checkIn (oliphaunt );
43+ assertEquals (pool .toString (), "Pool available=1 inUse=0" );
44+ }
45+
46+ }
47+
48+ /**
49+ * Use the same object 100 times subsequently. This should not take much time since the heavy
50+ * object instantiation is done only once. Verify if we get the same object each time.
51+ */
52+ @ Test (timeout = 5000 )
53+ public void testConcurrentCheckinCheckout () {
54+ final OliphauntPool pool = new OliphauntPool ();
55+ assertEquals (pool .toString (), "Pool available=0 inUse=0" );
56+
57+ final Oliphaunt firstOliphaunt = pool .checkOut ();
58+ assertEquals (pool .toString (), "Pool available=0 inUse=1" );
59+
60+ final Oliphaunt secondOliphaunt = pool .checkOut ();
61+ assertEquals (pool .toString (), "Pool available=0 inUse=2" );
62+
63+ assertNotSame (firstOliphaunt , secondOliphaunt );
64+ assertEquals (firstOliphaunt .getId () + 1 , secondOliphaunt .getId ());
65+
66+ // After checking in the second, we should get the same when checking out a new oliphaunt ...
67+ pool .checkIn (secondOliphaunt );
68+ assertEquals (pool .toString (), "Pool available=1 inUse=1" );
69+
70+ final Oliphaunt oliphaunt3 = pool .checkOut ();
71+ assertEquals (pool .toString (), "Pool available=0 inUse=2" );
72+ assertSame (secondOliphaunt , oliphaunt3 );
73+
74+ // ... and the same applies for the first one
75+ pool .checkIn (firstOliphaunt );
76+ assertEquals (pool .toString (), "Pool available=1 inUse=1" );
77+
78+ final Oliphaunt oliphaunt4 = pool .checkOut ();
79+ assertEquals (pool .toString (), "Pool available=0 inUse=2" );
80+ assertSame (firstOliphaunt , oliphaunt4 );
81+
82+ // When both oliphaunt return to the pool, we should still get the same instances
83+ pool .checkIn (firstOliphaunt );
84+ assertEquals (pool .toString (), "Pool available=1 inUse=1" );
85+
86+ pool .checkIn (secondOliphaunt );
87+ assertEquals (pool .toString (), "Pool available=2 inUse=0" );
88+
89+ // The order of the returned instances is not determined, so just put them in a list
90+ // and verify if both expected instances are in there.
91+ final List <Oliphaunt > oliphaunts = Arrays .asList (pool .checkOut (), pool .checkOut ());
92+ assertEquals (pool .toString (), "Pool available=0 inUse=2" );
93+ assertTrue (oliphaunts .contains (firstOliphaunt ));
94+ assertTrue (oliphaunts .contains (secondOliphaunt ));
95+
96+ }
97+
98+
99+ }
0 commit comments