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(
102        Collection<? extends Callable<T>> tasks) throws InterruptedException {
103      return null;
104    }
105
106    @Override
107    public <T> List<Future<T>> invokeAll(
108        Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
109        throws InterruptedException {
110      return null;
111    }
112
113    @Override
114    public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
115        throws InterruptedException, ExecutionException {
116      return null;
117    }
118
119    @Override
120    public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
121        long timeout, TimeUnit unit)
122        throws InterruptedException, ExecutionException, TimeoutException {
123      return null;
124    }
125  }
126
127  /**
128   * Just to create an instance, this doesn't actually function.
129   */
130  private static class MockExceptionListener implements BufferedMutator.ExceptionListener {
131    @Override
132    public void onException(RetriesExhaustedWithDetailsException exception,
133        BufferedMutator mutator) throws RetriesExhaustedWithDetailsException {
134    }
135  }
136
137  @Test
138  public void testClone() {
139    ExecutorService pool = new MockExecutorService();
140    final String tableName = name.getMethodName();
141    BufferedMutatorParams bmp = new BufferedMutatorParams(TableName.valueOf(tableName));
142
143    BufferedMutator.ExceptionListener listener = new MockExceptionListener();
144    bmp
145            .writeBufferSize(17)
146            .setWriteBufferPeriodicFlushTimeoutMs(123)
147            .setWriteBufferPeriodicFlushTimerTickMs(456)
148            .maxKeyValueSize(13)
149            .pool(pool)
150            .listener(listener);
151    bmp.implementationClassName("someClassName");
152    BufferedMutatorParams clone = bmp.clone();
153
154    // Confirm some literals
155    assertEquals(tableName, clone.getTableName().toString());
156    assertEquals(17, clone.getWriteBufferSize());
157    assertEquals(123, clone.getWriteBufferPeriodicFlushTimeoutMs());
158    assertEquals(456, clone.getWriteBufferPeriodicFlushTimerTickMs());
159    assertEquals(13, clone.getMaxKeyValueSize());
160    assertEquals("someClassName", clone.getImplementationClassName());
161
162    cloneTest(bmp, clone);
163
164    BufferedMutatorParams cloneWars = clone.clone();
165    cloneTest(clone, cloneWars);
166    cloneTest(bmp, cloneWars);
167
168    // Mocking with clone leave original unaffected.
169    clone.implementationClassName(null);
170    assertEquals("someClassName", bmp.getImplementationClassName());
171  }
172
173  /**
174   * Confirm all fields are equal.
175   * @param some some instance
176   * @param clone a clone of that instance, but not the same instance.
177   */
178  private void cloneTest(BufferedMutatorParams some,
179      BufferedMutatorParams clone) {
180    assertFalse(some == clone);
181    assertEquals(some.getTableName().toString(),
182        clone.getTableName().toString());
183    assertEquals(some.getWriteBufferSize(), clone.getWriteBufferSize());
184    assertEquals(some.getWriteBufferPeriodicFlushTimeoutMs(),
185        clone.getWriteBufferPeriodicFlushTimeoutMs());
186    assertEquals(some.getWriteBufferPeriodicFlushTimerTickMs(),
187        clone.getWriteBufferPeriodicFlushTimerTickMs());
188    assertEquals(some.getMaxKeyValueSize(), clone.getMaxKeyValueSize());
189    assertTrue(some.getListener() == clone.getListener());
190    assertTrue(some.getPool() == clone.getPool());
191    assertEquals(some.getImplementationClassName(), clone.getImplementationClassName());
192  }
193
194}