AUTHOR = 'Ashwin Ganti (aganti@google.com)'
TIME ='SHORT'
DEPENDENCIES = 'STANDARD'
NAME = 'Parallel Sleeptest'
TEST_TYPE = 'server'
TEST_CATEGORY = 'Functional'
DOC = """\
This control file executes multiple sleeptest tests on a machine
starting at the same time. Multiple machines can also be specified
wherein the specified set of tests are executed in parallel
on each of the machines.

To run a different set of tests do the following:
1. Replace the sleeptest control files with the required test control files
2. Give tag names for each of the tests
3. Run the control file through autoserv using
   server/autoserv <this control file path> -m <machines> -r <subdir to contain the run results>
"""

from autotest.client.shared import utils

# Specify the path to the client control files and the tag names
# for the respective jobs here.
tests = [("client/tests/sleeptest/control", "sleeptag0"),
         ("client/tests/sleeptest/control", "sleeptag1"),
         ]

def run_client(at, machine_name, machine_num, instance):
    control = open(os.path.join(job.autodir,tests[instance][0])).read()
    '''
    The get_sync_control_file method basically does the setup of the barriers
    required to start these multiple tests at the same time and returns the
    modified control file (that contains the barrier code added to it)
    Check client/shared/utils.py for detailed documentation of how this
    method sets up the barriers.
    '''
    control_new = utils.get_sync_control_file(control, machine_name,
                                    machine_num, instance, len(tests))
    '''
    This control file is now simply passed in to the run method along with
    a tag name of the test and a 'parallel_flag' that identifies this scenario
    of running multiple tests on the same machine at the same time.
    '''
    at.run(control_new, tag='%s' % tests[instance][1], parallel_flag=True)

def main(machine_name, machine_num):
    host = hosts.create_host(machine_name)
    at = autotest_remote.Autotest(host)
    at.install()

    parallel([subcommand(run_client, [at, machine_name, machine_num, i])
              for i in range(len(tests))])

parallel([subcommand(main, [machines[i], i], machines[i])
          for i in range(len(machines))])
