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.hadoop.hbase.util;
19  import java.util.concurrent.CountDownLatch;
20  
21  import org.apache.hadoop.hbase.testclassification.MediumTests;
22  import org.junit.Assert;
23  import org.junit.Test;
24  import org.junit.experimental.categories.Category;
25  
26  @Category(MediumTests.class)
27  public class TestCounter {
28    private static final int[] THREAD_COUNTS = {1, 10, 100};
29    private static final int DATA_COUNT = 1000000;
30  
31    private interface Operation {
32      void execute();
33    }
34  
35    @Test
36    public void testIncrement() throws Exception {
37      for(int threadCount : THREAD_COUNTS) {
38        final Counter counter = new Counter();
39  
40        execute(new Operation() {
41          @Override
42          public void execute() {
43            counter.increment();
44          }
45        }, threadCount);
46  
47        Assert.assertEquals(threadCount * (long)DATA_COUNT, counter.get());
48      }
49    }
50  
51    @Test
52    public void testIncrementAndGet() throws Exception {
53      for(int threadCount: THREAD_COUNTS) {
54        final Counter counter = new Counter();
55  
56        execute(new Operation() {
57          @Override
58          public void execute() {
59            counter.increment();
60            counter.get();
61          }
62        }, threadCount);
63  
64        Assert.assertEquals(threadCount * (long)DATA_COUNT, counter.get());
65      }
66    }
67  
68    private static void execute(final Operation op, int threadCount)
69        throws InterruptedException {
70  
71      final CountDownLatch prepareLatch = new CountDownLatch(threadCount);
72      final CountDownLatch startLatch = new CountDownLatch(1);
73      final CountDownLatch endLatch = new CountDownLatch(threadCount);
74  
75      class OperationThread extends Thread {
76        @Override
77        public void run() {
78          try {
79            prepareLatch.countDown();
80            startLatch.await();
81  
82            for(int i=0; i<DATA_COUNT; i++) {
83              op.execute();
84            }
85  
86            endLatch.countDown();
87  
88          } catch(Exception e) {}
89        }
90      }
91  
92      for(int j=0; j<threadCount; j++) {
93        new OperationThread().start();
94      }
95  
96      prepareLatch.await();
97      startLatch.countDown();
98      endLatch.await();
99    }
100 }