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