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