Home | History | Annotate | Download | only in scheduler
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package android.platform.longevity.scheduler;
     17 
     18 import android.os.Bundle;
     19 
     20 import java.util.Collections;
     21 import java.util.List;
     22 import java.util.Random;
     23 
     24 import org.junit.runner.Runner;
     25 
     26 /**
     27  * A {@link Scheduler} for shuffling the order of test execution.
     28  */
     29 public class Shuffle implements Scheduler {
     30     static final String SHUFFLE_OPTION_NAME = "shuffle";
     31     private static final boolean SHUFFLE_DEFAULT_VALUE = false;
     32     static final String SEED_OPTION_NAME = "seed";
     33 
     34     @Override
     35     public List<Runner> apply(Bundle args, List<Runner> input) {
     36         boolean shuffle = Boolean.parseBoolean(
     37                 args.getString(SHUFFLE_OPTION_NAME, String.valueOf(SHUFFLE_DEFAULT_VALUE)));
     38         long seed = Long.parseLong(
     39                 args.getString(SEED_OPTION_NAME, String.valueOf(new Random().nextLong())));
     40         // TODO: Log the options selected.
     41         if (shuffle) {
     42             Collections.shuffle(input, new Random(seed));
     43         }
     44         return input;
     45     }
     46 }
     47