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.procedure2; 019 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertTrue; 022 023import java.io.IOException; 024import java.util.concurrent.CountDownLatch; 025import java.util.concurrent.TimeUnit; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseCommonTestingUtility; 028import org.apache.hadoop.hbase.procedure2.store.NoopProcedureStore; 029import org.apache.hadoop.hbase.testclassification.MasterTests; 030import org.apache.hadoop.hbase.testclassification.SmallTests; 031import org.junit.After; 032import org.junit.Before; 033import org.junit.ClassRule; 034import org.junit.Test; 035import org.junit.experimental.categories.Category; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039@Category({ MasterTests.class, SmallTests.class }) 040public class TestProcedureInMemoryChore { 041 042 @ClassRule 043 public static final HBaseClassTestRule CLASS_RULE = 044 HBaseClassTestRule.forClass(TestProcedureInMemoryChore.class); 045 046 private static final Logger LOG = LoggerFactory.getLogger(TestProcedureInMemoryChore.class); 047 048 private static final int PROCEDURE_EXECUTOR_SLOTS = 1; 049 050 private TestProcEnv procEnv; 051 private NoopProcedureStore procStore; 052 private ProcedureExecutor<TestProcEnv> procExecutor; 053 054 private HBaseCommonTestingUtility htu; 055 056 @Before 057 public void setUp() throws IOException { 058 htu = new HBaseCommonTestingUtility(); 059 060 procEnv = new TestProcEnv(); 061 procStore = new NoopProcedureStore(); 062 procExecutor = new ProcedureExecutor<>(htu.getConfiguration(), procEnv, procStore); 063 procExecutor.testing = new ProcedureExecutor.Testing(); 064 procStore.start(PROCEDURE_EXECUTOR_SLOTS); 065 ProcedureTestingUtility.initAndStartWorkers(procExecutor, PROCEDURE_EXECUTOR_SLOTS, true); 066 } 067 068 @After 069 public void tearDown() throws IOException { 070 procExecutor.stop(); 071 procStore.stop(false); 072 } 073 074 @Test 075 public void testChoreAddAndRemove() throws Exception { 076 final int timeoutMSec = 50; 077 final int nCountDown = 5; 078 079 // submit the chore and wait for execution 080 CountDownLatch latch = new CountDownLatch(nCountDown); 081 TestLatchChore chore = new TestLatchChore(timeoutMSec, latch); 082 procExecutor.addChore(chore); 083 assertTrue(chore.isWaiting()); 084 latch.await(); 085 086 // remove the chore and verify it is no longer executed 087 assertTrue(chore.isWaiting()); 088 procExecutor.removeChore(chore); 089 latch = new CountDownLatch(nCountDown); 090 chore.setLatch(latch); 091 latch.await(timeoutMSec * nCountDown, TimeUnit.MILLISECONDS); 092 LOG.info("chore latch count=" + latch.getCount()); 093 assertFalse(chore.isWaiting()); 094 assertTrue("latchCount=" + latch.getCount(), latch.getCount() > 0); 095 } 096 097 public static class TestLatchChore extends ProcedureInMemoryChore<TestProcEnv> { 098 private CountDownLatch latch; 099 100 public TestLatchChore(final int timeoutMSec, final CountDownLatch latch) { 101 super(timeoutMSec); 102 setLatch(latch); 103 } 104 105 public void setLatch(final CountDownLatch latch) { 106 this.latch = latch; 107 } 108 109 @Override 110 protected void periodicExecute(final TestProcEnv env) { 111 LOG.info("periodic execute " + this); 112 latch.countDown(); 113 } 114 } 115 116 private static class TestProcEnv { 117 } 118}