|
17 | 17 | */ |
18 | 18 | package org.apache.hadoop.hbase.monitoring; |
19 | 19 |
|
20 | | -import static org.junit.Assert.*; |
21 | | - |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertNotEquals; |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | +import static org.junit.Assert.fail; |
22 | 24 | import java.util.ArrayList; |
23 | 25 | import java.util.List; |
| 26 | +import java.util.Map; |
24 | 27 | import java.util.concurrent.atomic.AtomicBoolean; |
25 | 28 | import org.apache.hadoop.conf.Configuration; |
26 | 29 | import org.apache.hadoop.hbase.HBaseClassTestRule; |
|
34 | 37 | import org.junit.ClassRule; |
35 | 38 | import org.junit.Test; |
36 | 39 | import org.junit.experimental.categories.Category; |
| 40 | +import org.slf4j.Logger; |
| 41 | +import org.slf4j.LoggerFactory; |
37 | 42 |
|
38 | 43 | @Category({MiscTests.class, SmallTests.class}) |
39 | 44 | public class TestTaskMonitor { |
| 45 | + private static final Logger LOG = LoggerFactory.getLogger(TestTaskMonitor.class); |
40 | 46 |
|
41 | 47 | @ClassRule |
42 | 48 | public static final HBaseClassTestRule CLASS_RULE = |
@@ -226,5 +232,66 @@ public void testStatusJournal() { |
226 | 232 | assertEquals("status3", task.getStatusJournal().get(1).getStatus()); |
227 | 233 | tm.shutdown(); |
228 | 234 | } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void testClone() throws Exception { |
| 238 | + MonitoredRPCHandlerImpl monitor = new MonitoredRPCHandlerImpl(); |
| 239 | + monitor.abort("abort RPC"); |
| 240 | + TestParam testParam = new TestParam("param1"); |
| 241 | + monitor.setRPC("method1", new Object[]{ testParam }, 0); |
| 242 | + MonitoredRPCHandlerImpl clone = monitor.clone(); |
| 243 | + assertEquals(clone.getDescription(), monitor.getDescription()); |
| 244 | + assertEquals(clone.getState(), monitor.getState()); |
| 245 | + assertEquals(clone.getStatus(), monitor.getStatus()); |
| 246 | + assertEquals(clone.toString(), monitor.toString()); |
| 247 | + assertEquals(clone.toMap(), monitor.toMap()); |
| 248 | + assertEquals(clone.toJSON(), monitor.toJSON()); |
| 249 | + |
| 250 | + // mark complete and make param dirty |
| 251 | + monitor.markComplete("complete RPC"); |
| 252 | + testParam.setParam("dirtyParam"); |
| 253 | + assertEquals(clone.getDescription(), monitor.getDescription()); |
| 254 | + assertNotEquals(clone.getState(), monitor.getState()); |
| 255 | + assertNotEquals(clone.getStatus(), monitor.getStatus()); |
| 256 | + monitor.setState(MonitoredTask.State.RUNNING); |
| 257 | + try { |
| 258 | + // when markComplete, the param in monitor is set null, so toMap should fail here |
| 259 | + monitor.toMap(); |
| 260 | + fail("Should not call toMap successfully, because param=null"); |
| 261 | + } catch (Exception e) { |
| 262 | + } |
| 263 | + // the param of clone monitor should not be dirty |
| 264 | + assertNotEquals("[dirtyString]", |
| 265 | + String.valueOf(((Map<String, Object>) clone.toMap().get("rpcCall")).get("params"))); |
| 266 | + |
| 267 | + monitor.resume("resume"); |
| 268 | + monitor.setRPC("method2", new Object[]{new TestParam("param2")}, 1); |
| 269 | + assertNotEquals(((Map<String, Object>) clone.toMap().get("rpcCall")).get("params"), |
| 270 | + ((Map<String, Object>) monitor.toMap().get("rpcCall")).get( |
| 271 | + "params")); |
| 272 | + LOG.info(String.valueOf(clone.toMap())); |
| 273 | + LOG.info(String.valueOf(monitor.toMap())); |
| 274 | + assertNotEquals(clone.toString(), monitor.toString()); |
| 275 | + assertNotEquals(clone.getRPCQueueTime(), monitor.getRPCQueueTime()); |
| 276 | + assertNotEquals(clone.toMap(), monitor.toMap()); |
| 277 | + assertNotEquals(clone.toJSON(), monitor.toJSON()); |
| 278 | + } |
| 279 | + |
| 280 | + private class TestParam { |
| 281 | + public String param = null; |
| 282 | + |
| 283 | + public TestParam(String param) { |
| 284 | + this.param = param; |
| 285 | + } |
| 286 | + |
| 287 | + public void setParam(String param) { |
| 288 | + this.param = param; |
| 289 | + } |
| 290 | + |
| 291 | + @Override |
| 292 | + public String toString() { |
| 293 | + return param; |
| 294 | + } |
| 295 | + } |
229 | 296 | } |
230 | 297 |
|
0 commit comments