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.MediumTests;
023import org.apache.hadoop.hbase.testclassification.MiscTests;
024import org.junit.Assert;
025import org.junit.ClassRule;
026import org.junit.Test;
027import org.junit.experimental.categories.Category;
028
029@Category({MiscTests.class, MediumTests.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)
077      throws InterruptedException {
078
079    final CountDownLatch prepareLatch = new CountDownLatch(threadCount);
080    final CountDownLatch startLatch = new CountDownLatch(1);
081    final CountDownLatch endLatch = new CountDownLatch(threadCount);
082
083    class OperationThread extends Thread {
084      @Override
085      public void run() {
086        try {
087          prepareLatch.countDown();
088          startLatch.await();
089
090          for(int i=0; i<DATA_COUNT; i++) {
091            op.execute();
092          }
093
094          endLatch.countDown();
095
096        } catch(Exception e) {}
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}