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.master.procedure;
019
020import static org.junit.Assert.assertTrue;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.client.TableDescriptor;
025import org.apache.hadoop.hbase.master.HMaster;
026import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
027import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
028import org.junit.After;
029import org.junit.AfterClass;
030import org.junit.Before;
031import org.junit.BeforeClass;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035public abstract class TestTableDDLProcedureBase {
036  private static final Logger LOG = LoggerFactory.getLogger(TestTableDDLProcedureBase.class);
037  protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
038
039  private static void setupConf(Configuration conf) {
040    conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
041  }
042
043  @BeforeClass
044  public static void setupCluster() throws Exception {
045    setupConf(UTIL.getConfiguration());
046    UTIL.startMiniCluster(1);
047  }
048
049  @AfterClass
050  public static void cleanupTest() throws Exception {
051    try {
052      UTIL.shutdownMiniCluster();
053    } catch (Exception e) {
054      LOG.warn("failure shutting down cluster", e);
055    }
056  }
057
058  @Before
059  public void setup() throws Exception {
060    resetProcExecutorTestingKillFlag();
061  }
062
063  @After
064  public void tearDown() throws Exception {
065    resetProcExecutorTestingKillFlag();
066    for (TableDescriptor htd : UTIL.getAdmin().listTableDescriptors()) {
067      LOG.info("Tear down, remove table=" + htd.getTableName());
068      UTIL.deleteTable(htd.getTableName());
069    }
070  }
071
072  protected void resetProcExecutorTestingKillFlag() {
073    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
074    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
075    assertTrue("expected executor to be running", procExec.isRunning());
076  }
077
078  protected ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
079    return getMaster().getMasterProcedureExecutor();
080  }
081
082  protected HMaster getMaster() {
083    return UTIL.getHBaseCluster().getMaster();
084  }
085}