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.quotas;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.Map;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.testclassification.SmallTests;
025import org.junit.ClassRule;
026import org.junit.Test;
027import org.junit.experimental.categories.Category;
028
029import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap;
030
031@Category({ SmallTests.class })
032public class TestRpcThrottlingException {
033
034  @ClassRule
035  public static final HBaseClassTestRule CLASS_RULE =
036    HBaseClassTestRule.forClass(TestRpcThrottlingException.class);
037
038  private static final Map<String, Long> STR_TO_MS_NEW_FORMAT =
039    ImmutableMap.<String, Long> builder().put("0ms", 0L).put("50ms", 50L).put("1sec, 1ms", 1001L)
040      .put("1min, 5sec, 15ms", 65_015L).put("5mins, 2sec, 0ms", 302000L)
041      .put("1hr, 3mins, 5sec, 1ms", 3785001L).put("1hr, 5sec, 1ms", 3605001L)
042      .put("1hr, 0ms", 3600000L).put("1hr, 1min, 1ms", 3660001L).build();
043  private static final Map<String,
044    Long> STR_TO_MS_LEGACY_FORMAT = ImmutableMap.<String, Long> builder().put("0sec", 0L)
045      .put("1sec", 1000L).put("2sec", 2000L).put("1mins, 5sec", 65_000L).put("5mins, 2sec", 302000L)
046      .put("1hrs, 3mins, 5sec", 3785000L).build();
047
048  @Test
049  public void itConvertsMillisToNewString() {
050    for (Map.Entry<String, Long> strAndMs : STR_TO_MS_NEW_FORMAT.entrySet()) {
051      String output = RpcThrottlingException.stringFromMillis(strAndMs.getValue());
052      assertEquals(strAndMs.getKey(), output);
053    }
054  }
055
056  @Test
057  public void itConvertsNewStringToMillis() {
058    for (Map.Entry<String, Long> strAndMs : STR_TO_MS_NEW_FORMAT.entrySet()) {
059      Long output = RpcThrottlingException.timeFromString(strAndMs.getKey());
060      assertEquals(strAndMs.getValue(), output);
061    }
062  }
063
064  @Test
065  public void itConvertsLegacyStringToMillis() {
066    for (Map.Entry<String, Long> strAndMs : STR_TO_MS_LEGACY_FORMAT.entrySet()) {
067      Long output = RpcThrottlingException.timeFromString(strAndMs.getKey());
068      assertEquals(strAndMs.getValue(), output);
069    }
070  }
071
072}