/* 
 * Copyright 1999,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class TokenBucketFilterTest {
  private static Logger logger = Logger.getLogger(TokenBucketFilterTest.class);

  public static void main(String argv[]) {

    if (argv.length == 1) {
      init(argv[0]);
    } else {
      usage("Wrong number of arguments.");
    }

    test();
  }

  static void usage(String msg) {
    System.err.println(msg);
    System.err.println("Usage: java " + TokenBucketFilterTest.class.getName() + " configFile");
    System.exit(1);
  }

  static void init(String configFile) {
    DOMConfigurator.configure(configFile);
  }

  static void test() {
    // empty the bucket and make sure no more than 100 errors get logged
    for (int i = 0; i < 110; i++) {
      logger.info("Logging 110 messages, should only see 100 logs # "  + (i + 1));
    }

    // the bucket should be empty, now wait for 12 seconds and see if we can log
    // more messages
    try {
      Thread.sleep(12000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    for (int i = 0; i < 100; i++) {
      logger.info("Waited 12 seconds and trying to log again, should only see 20 logs #" + (i + 1));
    }
  }
}
