1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.procedure2;
20
21 import java.io.IOException;
22 import java.util.concurrent.CountDownLatch;
23 import java.util.concurrent.TimeUnit;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
28 import org.apache.hadoop.hbase.procedure2.store.NoopProcedureStore;
29 import org.apache.hadoop.hbase.testclassification.SmallTests;
30 import org.apache.hadoop.hbase.testclassification.MasterTests;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35
36 import static org.junit.Assert.assertFalse;
37 import static org.junit.Assert.assertTrue;
38
39 @Category({MasterTests.class, SmallTests.class})
40 public class TestProcedureInMemoryChore {
41 private static final Log LOG = LogFactory.getLog(TestProcedureInMemoryChore.class);
42
43 private static final int PROCEDURE_EXECUTOR_SLOTS = 1;
44
45 private TestProcEnv procEnv;
46 private NoopProcedureStore procStore;
47 private ProcedureExecutor<TestProcEnv> procExecutor;
48
49 private HBaseCommonTestingUtility htu;
50
51 @SuppressWarnings("rawtypes")
52 @Before
53 public void setUp() throws IOException {
54 htu = new HBaseCommonTestingUtility();
55
56 procEnv = new TestProcEnv();
57 procStore = new NoopProcedureStore();
58 procExecutor = new ProcedureExecutor(htu.getConfiguration(), procEnv, procStore);
59 procExecutor.testing = new ProcedureExecutor.Testing();
60 procStore.start(PROCEDURE_EXECUTOR_SLOTS);
61 procExecutor.start(PROCEDURE_EXECUTOR_SLOTS, true);
62 }
63
64 @After
65 public void tearDown() throws IOException {
66 procExecutor.stop();
67 procStore.stop(false);
68 }
69
70 @Test
71 public void testChoreAddAndRemove() throws Exception {
72 final int timeoutMSec = 50;
73 final int nCountDown = 5;
74
75
76 CountDownLatch latch = new CountDownLatch(nCountDown);
77 TestLatchChore chore = new TestLatchChore(timeoutMSec, latch);
78 procExecutor.addChore(chore);
79 assertTrue(chore.isRunnable());
80 latch.await();
81
82
83 assertTrue(chore.isRunnable());
84 procExecutor.removeChore(chore);
85 latch = new CountDownLatch(nCountDown);
86 chore.setLatch(latch);
87 latch.await(timeoutMSec * nCountDown, TimeUnit.MILLISECONDS);
88 LOG.info("chore latch count=" + latch.getCount());
89 assertFalse(chore.isRunnable());
90 assertTrue("latchCount=" + latch.getCount(), latch.getCount() > 0);
91 }
92
93 public static class TestLatchChore extends ProcedureInMemoryChore<TestProcEnv> {
94 private CountDownLatch latch;
95
96 public TestLatchChore(final int timeoutMSec, final CountDownLatch latch) {
97 super(timeoutMSec);
98 setLatch(latch);
99 }
100
101 public void setLatch(final CountDownLatch latch) {
102 this.latch = latch;
103 }
104
105 @Override
106 protected void periodicExecute(final TestProcEnv env) {
107 latch.countDown();
108 }
109 }
110
111 private static class TestProcEnv {
112 }
113 }