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