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.client;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.nio.charset.StandardCharsets;
025import java.util.Collection;
026import java.util.List;
027import java.util.concurrent.Callable;
028import java.util.concurrent.ExecutionException;
029import java.util.concurrent.ExecutorService;
030import java.util.concurrent.Future;
031import java.util.concurrent.TimeUnit;
032import java.util.concurrent.TimeoutException;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.testclassification.ClientTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.Test;
038import org.junit.jupiter.api.TestInfo;
039
040@Tag(ClientTests.TAG)
041@Tag(SmallTests.TAG)
042public class TestBufferedMutatorParams {
043
044  /**
045   * Just to create in instance, this doesn't actually function.
046   */
047  private class MockExecutorService implements ExecutorService {
048
049    @Override
050    public void execute(Runnable command) {
051    }
052
053    @Override
054    public void shutdown() {
055    }
056
057    @Override
058    public List<Runnable> shutdownNow() {
059      return null;
060    }
061
062    @Override
063    public boolean isShutdown() {
064      return false;
065    }
066
067    @Override
068    public boolean isTerminated() {
069      return false;
070    }
071
072    @Override
073    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
074      return false;
075    }
076
077    @Override
078    public <T> Future<T> submit(Callable<T> task) {
079      return null;
080    }
081
082    @Override
083    public <T> Future<T> submit(Runnable task, T result) {
084      return null;
085    }
086
087    @Override
088    public Future<?> submit(Runnable task) {
089      return null;
090    }
091
092    @Override
093    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
094      throws InterruptedException {
095      return null;
096    }
097
098    @Override
099    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout,
100      TimeUnit unit) throws InterruptedException {
101      return null;
102    }
103
104    @Override
105    public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
106      throws InterruptedException, ExecutionException {
107      return null;
108    }
109
110    @Override
111    public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
112      throws InterruptedException, ExecutionException, TimeoutException {
113      return null;
114    }
115  }
116
117  /**
118   * Just to create an instance, this doesn't actually function.
119   */
120  private static class MockExceptionListener implements BufferedMutator.ExceptionListener {
121    @Override
122    public void onException(RetriesExhaustedWithDetailsException exception, BufferedMutator mutator)
123      throws RetriesExhaustedWithDetailsException {
124    }
125  }
126
127  @Test
128  public void testClone(TestInfo testInfo) {
129    ExecutorService pool = new MockExecutorService();
130    final String tableName = testInfo.getTestMethod().get().getName();
131    ;
132    BufferedMutatorParams bmp = new BufferedMutatorParams(TableName.valueOf(tableName));
133
134    BufferedMutator.ExceptionListener listener = new MockExceptionListener();
135    bmp.writeBufferSize(17).setWriteBufferPeriodicFlushTimeoutMs(123)
136      .setWriteBufferPeriodicFlushTimerTickMs(456).maxKeyValueSize(13).setMaxMutations(3737)
137      .setRequestAttribute("foo", "bar".getBytes(StandardCharsets.UTF_8)).pool(pool)
138      .listener(listener);
139    bmp.implementationClassName("someClassName");
140    BufferedMutatorParams clone = bmp.clone();
141
142    // Confirm some literals
143    assertEquals(tableName, clone.getTableName().toString());
144    assertEquals(17, clone.getWriteBufferSize());
145    assertEquals(123, clone.getWriteBufferPeriodicFlushTimeoutMs());
146    assertEquals(456, clone.getWriteBufferPeriodicFlushTimerTickMs());
147    assertEquals(13, clone.getMaxKeyValueSize());
148    assertEquals(3737, clone.getMaxMutations());
149    assertEquals("someClassName", clone.getImplementationClassName());
150
151    cloneTest(bmp, clone);
152
153    BufferedMutatorParams cloneWars = clone.clone();
154    cloneTest(clone, cloneWars);
155    cloneTest(bmp, cloneWars);
156
157    // Mocking with clone leave original unaffected.
158    clone.implementationClassName(null);
159    assertEquals("someClassName", bmp.getImplementationClassName());
160  }
161
162  /**
163   * Confirm all fields are equal.
164   * @param some  some instance
165   * @param clone a clone of that instance, but not the same instance.
166   */
167  private void cloneTest(BufferedMutatorParams some, BufferedMutatorParams clone) {
168    assertFalse(some == clone);
169    assertEquals(some.getTableName().toString(), clone.getTableName().toString());
170    assertEquals(some.getWriteBufferSize(), clone.getWriteBufferSize());
171    assertEquals(some.getWriteBufferPeriodicFlushTimeoutMs(),
172      clone.getWriteBufferPeriodicFlushTimeoutMs());
173    assertEquals(some.getWriteBufferPeriodicFlushTimerTickMs(),
174      clone.getWriteBufferPeriodicFlushTimerTickMs());
175    assertEquals(some.getMaxKeyValueSize(), clone.getMaxKeyValueSize());
176    assertTrue(some.getMaxMutations() == clone.getMaxMutations());
177    assertEquals(some.requestAttributes, clone.requestAttributes);
178    assertTrue(some.getListener() == clone.getListener());
179    assertTrue(some.getPool() == clone.getPool());
180    assertEquals(some.getImplementationClassName(), clone.getImplementationClassName());
181  }
182
183}