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.procedure2.util;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import org.apache.hadoop.hbase.testclassification.MasterTests;
023import org.apache.hadoop.hbase.testclassification.SmallTests;
024import org.junit.jupiter.api.Tag;
025import org.junit.jupiter.api.Test;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029@Tag(MasterTests.TAG)
030@Tag(SmallTests.TAG)
031public class TestDelayedUtil {
032
033  private static final Logger LOG = LoggerFactory.getLogger(TestDelayedUtil.class);
034
035  @Test
036  public void testDelayedContainerEquals() {
037    Object o1 = new Object();
038    Object o2 = new Object();
039    ZeroDelayContainer<Long> lnull = new ZeroDelayContainer(null);
040    ZeroDelayContainer<Long> l10a = new ZeroDelayContainer<>(10L);
041    ZeroDelayContainer<Long> l10b = new ZeroDelayContainer(10L);
042    ZeroDelayContainer<Long> l15 = new ZeroDelayContainer(15L);
043    ZeroDelayContainer<Object> onull = new ZeroDelayContainer<>(null);
044    ZeroDelayContainer<Object> o1ca = new ZeroDelayContainer<>(o1);
045    ZeroDelayContainer<Object> o1cb = new ZeroDelayContainer<>(o1);
046    ZeroDelayContainer<Object> o2c = new ZeroDelayContainer<>(o2);
047
048    ZeroDelayContainer[] items =
049      new ZeroDelayContainer[] { lnull, l10a, l10b, l15, onull, o1ca, o1cb, o2c, };
050
051    assertContainersEquals(lnull, items, lnull, onull);
052    assertContainersEquals(l10a, items, l10a, l10b);
053    assertContainersEquals(l10b, items, l10a, l10b);
054    assertContainersEquals(l15, items, l15);
055    assertContainersEquals(onull, items, lnull, onull);
056    assertContainersEquals(o1ca, items, o1ca, o1cb);
057    assertContainersEquals(o1cb, items, o1ca, o1cb);
058    assertContainersEquals(o2c, items, o2c);
059  }
060
061  private void assertContainersEquals(final ZeroDelayContainer src,
062    final ZeroDelayContainer[] items, final ZeroDelayContainer... matches) {
063    for (int i = 0; i < items.length; ++i) {
064      boolean shouldMatch = false;
065      for (int j = 0; j < matches.length; ++j) {
066        if (items[i] == matches[j]) {
067          shouldMatch = true;
068          break;
069        }
070      }
071      boolean isMatching = src.equals(items[i]);
072      assertEquals(shouldMatch, isMatching,
073        src.getObject() + " unexpectedly match " + items[i].getObject());
074    }
075  }
076
077  private static class ZeroDelayContainer<T> extends DelayedUtil.DelayedContainer<T> {
078    public ZeroDelayContainer(final T object) {
079      super(object);
080    }
081
082    @Override
083    public long getTimeout() {
084      return 0;
085    }
086  }
087}