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.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotEquals;
022
023import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.testclassification.MetricsTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.ClassRule;
028import org.junit.Test;
029import org.junit.experimental.categories.Category;
030
031@Category({MetricsTests.class, SmallTests.class})
032public class TestMetricsRegionSourceImpl {
033  @ClassRule
034  public static final HBaseClassTestRule CLASS_RULE =
035      HBaseClassTestRule.forClass(TestMetricsRegionSourceImpl.class);
036
037  @SuppressWarnings("SelfComparison")
038  @Test
039  public void testCompareToHashCodeEquals() {
040    MetricsRegionServerSourceFactory fact = CompatibilitySingletonFactory.getInstance(
041            MetricsRegionServerSourceFactory.class);
042
043    MetricsRegionSource one = fact.createRegion(new RegionWrapperStub("TEST"));
044    MetricsRegionSource oneClone = fact.createRegion(new RegionWrapperStub("TEST"));
045    MetricsRegionSource two = fact.createRegion(new RegionWrapperStub("TWO"));
046
047    assertEquals(0, one.compareTo(oneClone));
048    assertEquals(one.hashCode(), oneClone.hashCode());
049    assertNotEquals(one, two);
050
051    assertNotEquals(0, one.compareTo(two));
052    assertNotEquals(0, two.compareTo(one));
053    assertNotEquals(one.compareTo(two), two.compareTo(one));
054    assertEquals(0, two.compareTo(two));
055  }
056
057  @Test(expected = RuntimeException.class)
058  public void testNoGetRegionServerMetricsSourceImpl() {
059    // This should throw an exception because MetricsRegionSourceImpl should only
060    // be created by a factory.
061    CompatibilitySingletonFactory.getInstance(MetricsRegionSource.class);
062  }
063
064  static class RegionWrapperStub implements MetricsRegionWrapper {
065
066    private String regionName;
067
068    RegionWrapperStub(String regionName) {
069      this.regionName = regionName;
070    }
071
072    @Override
073    public String getTableName() {
074      return null;
075    }
076
077    @Override
078    public String getNamespace() {
079      return null;
080    }
081
082    @Override
083    public String getRegionName() {
084      return this.regionName;
085    }
086
087    @Override
088    public long getNumStores() {
089      return 0;
090    }
091
092    @Override
093    public long getNumStoreFiles() {
094      return 0;
095    }
096
097    @Override
098    public long getStoreRefCount() {
099      return 0;
100    }
101
102    @Override
103    public long getMaxStoreFileRefCount() {
104      return 0;
105    }
106
107    @Override
108    public long getMemStoreSize() {
109      return 0;
110    }
111
112    @Override
113    public long getStoreFileSize() {
114      return 0;
115    }
116
117    @Override
118    public long getReadRequestCount() {
119      return 0;
120    }
121
122    @Override
123    public long getFilteredReadRequestCount() {
124      return 0;
125    }
126
127    @Override
128    public long getMaxStoreFileAge() {
129      return 0;
130    }
131
132    @Override
133    public long getMinStoreFileAge() {
134      return 0;
135    }
136
137    @Override
138    public long getAvgStoreFileAge() {
139      return 0;
140    }
141
142    @Override
143    public long getNumReferenceFiles() {
144      return 0;
145    }
146
147    @Override
148    public long getCpRequestCount() {
149      return 0;
150    }
151
152    @Override
153    public long getWriteRequestCount() {
154      return 0;
155    }
156
157    @Override
158    public long getNumFilesCompacted() {
159      return 0;
160    }
161
162    @Override
163    public long getNumBytesCompacted() {
164      return 0;
165    }
166
167    @Override
168    public long getLastMajorCompactionAge() {
169      return 0;
170    }
171
172    @Override
173    public long getNumCompactionsCompleted() {
174      return 0;
175    }
176
177    @Override
178    public long getNumCompactionsFailed() {
179      return 0;
180    }
181
182    @Override
183    public int getRegionHashCode() {
184      return regionName.hashCode();
185    }
186
187    /**
188     * Always return 0 for testing
189     */
190    @Override
191    public int getReplicaId() {
192      return 0;
193    }
194
195    @Override
196    public long getNumCompactionsQueued() {
197      return 0;
198    }
199
200    @Override
201    public long getNumFlushesQueued() {
202      return 0;
203    }
204
205    @Override
206    public long getMaxCompactionQueueSize() {
207      return 0;
208    }
209
210    @Override
211    public long getMaxFlushQueueSize() {
212      return 0;
213    }
214
215    @Override
216    public long getTotalRequestCount() {
217      return 0;
218    }
219  }
220}