View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.omid.tso;
19  
20  import com.google.inject.Guice;
21  import com.google.inject.Injector;
22  import com.google.inject.Key;
23  import com.google.inject.TypeLiteral;
24  import org.apache.commons.pool2.ObjectPool;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.testng.annotations.BeforeMethod;
28  import org.testng.annotations.Test;
29  
30  import java.util.concurrent.Callable;
31  import java.util.concurrent.ExecutorService;
32  import java.util.concurrent.Executors;
33  import java.util.concurrent.Future;
34  import java.util.concurrent.TimeUnit;
35  import java.util.concurrent.TimeoutException;
36  
37  import static org.testng.Assert.assertEquals;
38  import static org.testng.Assert.fail;
39  
40  public class TestBatchPool {
41  
42      private static final Logger LOG = LoggerFactory.getLogger(TestBatchPool.class);
43  
44      private static final int CONCURRENT_WRITERS = 16;
45      private static final int BATCH_SIZE = 1000;
46  
47  
48      private Injector injector;
49  
50      @BeforeMethod
51      void setup() {
52  
53          TSOServerConfig tsoServerConfig = new TSOServerConfig();
54          tsoServerConfig.setNumConcurrentCTWriters(CONCURRENT_WRITERS);
55          tsoServerConfig.setBatchSizePerCTWriter(BATCH_SIZE);
56  
57          // Injector to get the element under test: the ObjectPool<Batch> returned by Guice's BatchPoolModule
58          injector = Guice.createInjector(new BatchPoolModule(tsoServerConfig));
59  
60      }
61  
62      @Test(timeOut = 10_000)
63      public void testBatchPoolObtainedIsSingleton() {
64  
65          final ObjectPool<Batch> instance1 = injector.getInstance(Key.get(new TypeLiteral<ObjectPool<Batch>>() {}));
66          final ObjectPool<Batch> instance2 = injector.getInstance(Key.get(new TypeLiteral<ObjectPool<Batch>>() {}));
67  
68          assertEquals(instance1, instance2, "Objects are NOT equal !");
69  
70      }
71  
72      @Test(timeOut = 10_000)
73      public void testBatchPoolInitializesAllBatchObjectsAsIdle() throws Exception {
74  
75          final ObjectPool<Batch> batchPool = injector.getInstance(Key.get(new TypeLiteral<ObjectPool<Batch>>() {}));
76  
77          assertEquals(batchPool.getNumActive(), 0);
78          assertEquals(batchPool.getNumIdle(), CONCURRENT_WRITERS);
79  
80          // Now make all of them active and check it below
81          for (int i = 0; i < CONCURRENT_WRITERS; i++) {
82              batchPool.borrowObject();
83          }
84  
85          assertEquals(batchPool.getNumActive(), CONCURRENT_WRITERS);
86          assertEquals(batchPool.getNumIdle(), 0);
87  
88      }
89  
90      @Test(timeOut = 10_000)
91      public void testBatchPoolBlocksWhenAllObjectsAreActive() throws Exception {
92  
93          ExecutorService executor = Executors.newCachedThreadPool();
94  
95          final ObjectPool<Batch> batchPool = injector.getInstance(Key.get(new TypeLiteral<ObjectPool<Batch>>() {}));
96  
97          // Try to get one more batch than the number of concurrent writers set
98          for (int i = 0; i < CONCURRENT_WRITERS + 1; i++) {
99  
100             // Wrap the call to batchPool.borrowObject() in a task to detect when is blocked by the ObjectPool
101             Callable<Batch> task = new Callable<Batch>() {
102                 public Batch call() throws Exception {
103                     return batchPool.borrowObject();
104                 }
105             };
106 
107             Future<Batch> future = executor.submit(task);
108 
109             try {
110                 /** The future below should return immediately except for the last one, which should be blocked by
111                     the ObjectPool as per the configuration setup in the {@link BatchPoolModule} */
112                 Batch batch = future.get(1, TimeUnit.SECONDS);
113                 LOG.info("Batch {} returned with success", batch.toString());
114             } catch (TimeoutException ex) {
115                 if (i < CONCURRENT_WRITERS) {
116                     fail();
117                 } else {
118                     LOG.info("Yaaaayyyyy! This is the blocked call!");
119                 }
120             } finally {
121                 future.cancel(true); // may or may not desire this
122             }
123 
124         }
125 
126     }
127 
128 }