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 */
018
019package org.apache.hadoop.hbase.test;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HBaseConfiguration;
023import org.apache.hadoop.hbase.IntegrationTestingUtility;
024import org.apache.hadoop.hbase.chaos.util.ChaosMonkeyRunner;
025import org.apache.hadoop.hbase.chaos.util.Monkeys;
026import org.apache.hadoop.hbase.testclassification.IntegrationTests;
027import org.apache.hadoop.util.ToolRunner;
028import org.junit.Test;
029import org.junit.experimental.categories.Category;
030
031/**
032 * This is an integration test for showing a simple usage of how to use {@link Monkeys}
033 * to control {@link ChaosMonkeyRunner}.
034 */
035@Category(IntegrationTests.class)
036public class IntegrationTestMonkeys extends ChaosMonkeyRunner {
037  private static final int RUN_SECS = 15 * 1000;
038  private static final int WAIT_SECS = 10 * 1000;
039
040  @Override
041  protected int doWork() throws Exception {
042    super.setUpCluster();
043    runMonkeys();
044    return 0;
045  }
046
047  @Test
048  public void runMonkeys() throws Exception {
049    try (Monkeys monkeys = new Monkeys()) {
050      for (int i = 0; i < 2; i++) {
051        monkeys.startChaos();
052        Thread.sleep(RUN_SECS);
053        monkeys.stopChaos();
054        Thread.sleep(WAIT_SECS);
055      }
056    }
057  }
058
059  public static void main(String[] args) throws Exception {
060    // Run chaos monkeys 15 seconds, then stop them.
061    // After 10 seconds, run chaos monkeys again.
062    Configuration conf = HBaseConfiguration.create();
063    IntegrationTestingUtility.setUseDistributedCluster(conf);
064    int exitCode = ToolRunner.run(conf, new IntegrationTestMonkeys(), args);
065    System.exit(exitCode);
066  }
067}