001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import java.util.concurrent.CountDownLatch;
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.testclassification.MiscTests;
023import org.apache.hadoop.hbase.testclassification.SmallTests;
024import org.junit.Assert;
025import org.junit.ClassRule;
026import org.junit.Test;
027import org.junit.experimental.categories.Category;
028
029@Category({ MiscTests.class, SmallTests.class })
030public class TestCounter {
031
032  @ClassRule
033  public static final HBaseClassTestRule CLASS_RULE =
034    HBaseClassTestRule.forClass(TestCounter.class);
035
036  private static final int[] THREAD_COUNTS = { 1, 10, 100 };
037  private static final int DATA_COUNT = 1000000;
038
039  private interface Operation {
040    void execute();
041  }
042
043  @Test
044  public void testIncrement() throws Exception {
045    for (int threadCount : THREAD_COUNTS) {
046      final Counter counter = new Counter();
047
048      execute(new Operation() {
049        @Override
050        public void execute() {
051          counter.increment();
052        }
053      }, threadCount);
054
055      Assert.assertEquals(threadCount * (long) DATA_COUNT, counter.get());
056    }
057  }
058
059  @Test
060  public void testIncrementAndGet() throws Exception {
061    for (int threadCount : THREAD_COUNTS) {
062      final Counter counter = new Counter();
063
064      execute(new Operation() {
065        @Override
066        public void execute() {
067          counter.increment();
068          counter.get();
069        }
070      }, threadCount);
071
072      Assert.assertEquals(threadCount * (long) DATA_COUNT, counter.get());
073    }
074  }
075
076  private static void execute(final Operation op, int threadCount) throws InterruptedException {
077
078    final CountDownLatch prepareLatch = new CountDownLatch(threadCount);
079    final CountDownLatch startLatch = new CountDownLatch(1);
080    final CountDownLatch endLatch = new CountDownLatch(threadCount);
081
082    class OperationThread extends Thread {
083      @Override
084      public void run() {
085        try {
086          prepareLatch.countDown();
087          startLatch.await();
088
089          for (int i = 0; i < DATA_COUNT; i++) {
090            op.execute();
091          }
092
093          endLatch.countDown();
094
095        } catch (Exception e) {
096        }
097      }
098    }
099
100    for (int j = 0; j < threadCount; j++) {
101      new OperationThread().start();
102    }
103
104    prepareLatch.await();
105    startLatch.countDown();
106    endLatch.await();
107  }
108}