View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.client;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  import java.util.Collection;
25  import java.util.List;
26  import java.util.concurrent.Callable;
27  import java.util.concurrent.ExecutorService;
28  import java.util.concurrent.Future;
29  import java.util.concurrent.TimeUnit;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.testclassification.ClientTests;
32  import org.apache.hadoop.hbase.testclassification.SmallTests;
33  import org.junit.Rule;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  import org.junit.rules.TestName;
37  
38  @Category({ ClientTests.class, SmallTests.class })
39  public class TestBufferedMutatorParams {
40    @Rule
41    public TestName name = new TestName();
42  
43    /**
44     * Just to create in instance, this doesn't actually function.
45     */
46    private class MockExecutorService implements ExecutorService {
47  
48      @Override
49      public void execute(Runnable command) {
50      }
51  
52      @Override
53      public void shutdown() {
54      }
55  
56      @Override
57      public List<Runnable> shutdownNow() {
58        return null;
59      }
60  
61      @Override
62      public boolean isShutdown() {
63        return false;
64      }
65  
66      @Override
67      public boolean isTerminated() {
68        return false;
69      }
70  
71      @Override
72      public boolean awaitTermination(long timeout, TimeUnit unit) {
73        return false;
74      }
75  
76      @Override
77      public <T> Future<T> submit(Callable<T> task) {
78        return null;
79      }
80  
81      @Override
82      public <T> Future<T> submit(Runnable task, T result) {
83        return null;
84      }
85  
86      @Override
87      public Future<?> submit(Runnable task) {
88        return null;
89      }
90  
91      @Override
92      public <T> List<Future<T>> invokeAll(
93          Collection<? extends Callable<T>> tasks) {
94        return null;
95      }
96  
97      @Override
98      public <T> List<Future<T>> invokeAll(
99          Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) {
100       return null;
101     }
102 
103     @Override
104     public <T> T invokeAny(Collection<? extends Callable<T>> tasks) {
105       return null;
106     }
107 
108     @Override
109     public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
110         long timeout, TimeUnit unit) {
111       return null;
112     }
113   }
114 
115   /**
116    * Just to create an instance, this doesn't actually function.
117    */
118   private static class MockExceptionListener implements BufferedMutator.ExceptionListener {
119     @Override
120     public void onException(RetriesExhaustedWithDetailsException exception,
121         BufferedMutator mutator) {
122     }
123   }
124 
125   @Test
126   public void testClone() {
127     ExecutorService pool = new MockExecutorService();
128     final String tableName = name.getMethodName();
129     BufferedMutatorParams bmp = new BufferedMutatorParams(TableName.valueOf(tableName));
130 
131     BufferedMutator.ExceptionListener listener = new MockExceptionListener();
132     bmp
133             .writeBufferSize(17)
134             .setWriteBufferPeriodicFlushTimeoutMs(123)
135             .setWriteBufferPeriodicFlushTimerTickMs(456)
136             .maxKeyValueSize(13)
137             .pool(pool)
138             .listener(listener);
139     BufferedMutatorParams clone = bmp.clone();
140 
141     // Confirm some literals
142     assertEquals(tableName, clone.getTableName().toString());
143     assertEquals(17, clone.getWriteBufferSize());
144     assertEquals(123, clone.getWriteBufferPeriodicFlushTimeoutMs());
145     assertEquals(456, clone.getWriteBufferPeriodicFlushTimerTickMs());
146     assertEquals(13, clone.getMaxKeyValueSize());
147 
148     cloneTest(bmp, clone);
149 
150     BufferedMutatorParams cloneWars = clone.clone();
151     cloneTest(clone, cloneWars);
152     cloneTest(bmp, cloneWars);
153   }
154 
155   /**
156    * Confirm all fields are equal.
157    * @param some some instance
158    * @param clone a clone of that instance, but not the same instance.
159    */
160   private void cloneTest(BufferedMutatorParams some,
161       BufferedMutatorParams clone) {
162     assertFalse(some == clone);
163     assertEquals(some.getTableName().toString(),
164         clone.getTableName().toString());
165     assertEquals(some.getWriteBufferSize(), clone.getWriteBufferSize());
166     assertEquals(some.getWriteBufferPeriodicFlushTimeoutMs(),
167         clone.getWriteBufferPeriodicFlushTimeoutMs());
168     assertEquals(some.getWriteBufferPeriodicFlushTimerTickMs(),
169         clone.getWriteBufferPeriodicFlushTimerTickMs());
170     assertEquals(some.getMaxKeyValueSize(), clone.getMaxKeyValueSize());
171     assertTrue(some.getListener() == clone.getListener());
172     assertTrue(some.getPool() == clone.getPool());
173   }
174 
175 }