Make MobileSuspendResume more 'stressful'

Instead of using autotest 'iterations', move the loop
into the scenario to speed up the process
but to also allow for scenarios where device suspends
while it is in the process of connecting to wimax.

TEST=run the test with both wimax and cellular modems
BUG=None

Change-Id: I7e7d7c56035c85c9169419af0b15d14eefc50059
Previous-Reviewed-on: https://gerrit.chromium.org/gerrit/38169
(cherry picked from commit fdd7b3cf21b507b2f373a92e6ac6f49ba480bad4)
Reviewed-on: https://gerrit.chromium.org/gerrit/39909
Reviewed-by: Stanley Wong <stanleyw@chromium.org>
Tested-by: Stanley Wong <stanleyw@chromium.org>
diff --git a/client/site_tests/network_MobileSuspendResume/control.stress b/client/site_tests/network_MobileSuspendResume/control.stress
index 96b7e52..2cfcf3f 100644
--- a/client/site_tests/network_MobileSuspendResume/control.stress
+++ b/client/site_tests/network_MobileSuspendResume/control.stress
@@ -23,7 +23,7 @@
 # Run all scenarios twice, first with autoconnect off, then with it on
 job.run_test('network_MobileSuspendResume',
              autoconnect=False, tag='autoconnect_off-stress',
-             scenario_group='stress', iterations=1000)
+             scenario_group='stress', stress_iterations=100)
 job.run_test('network_MobileSuspendResume',
              autoconnect=True, tag='autoconnect_on-stress',
-             scenario_group='stress', iterations=1000)
+             scenario_group='stress', stress_iterations=100)
diff --git a/client/site_tests/network_MobileSuspendResume/control.wimax_stress b/client/site_tests/network_MobileSuspendResume/control.wimax_stress_autoconnect_off
similarity index 74%
copy from client/site_tests/network_MobileSuspendResume/control.wimax_stress
copy to client/site_tests/network_MobileSuspendResume/control.wimax_stress_autoconnect_off
index 521884f..7c39a68 100644
--- a/client/site_tests/network_MobileSuspendResume/control.wimax_stress
+++ b/client/site_tests/network_MobileSuspendResume/control.wimax_stress_autoconnect_off
@@ -21,10 +21,6 @@
     situations.
 """
 
-# Run all scenarios twice, first with autoconnect off, then with it on
 job.run_test('network_MobileSuspendResume', autoconnect=False,
              tag='autoconnect_off-stress_wimax', scenario_group='stress',
-             iterations=1000, device_type='wimax')
-job.run_test('network_MobileSuspendResume', autoconnect=True,
-             tag='autoconnect_on-stress_wimax', scenario_group='stress',
-             iterations=1000, device_type='wimax')
+             stress_iterations=1000, device_type='wimax')
diff --git a/client/site_tests/network_MobileSuspendResume/control.wimax_stress b/client/site_tests/network_MobileSuspendResume/control.wimax_stress_autoconnect_on
similarity index 74%
rename from client/site_tests/network_MobileSuspendResume/control.wimax_stress
rename to client/site_tests/network_MobileSuspendResume/control.wimax_stress_autoconnect_on
index 521884f..f0b222a 100644
--- a/client/site_tests/network_MobileSuspendResume/control.wimax_stress
+++ b/client/site_tests/network_MobileSuspendResume/control.wimax_stress_autoconnect_on
@@ -21,10 +21,6 @@
     situations.
 """
 
-# Run all scenarios twice, first with autoconnect off, then with it on
-job.run_test('network_MobileSuspendResume', autoconnect=False,
-             tag='autoconnect_off-stress_wimax', scenario_group='stress',
-             iterations=1000, device_type='wimax')
 job.run_test('network_MobileSuspendResume', autoconnect=True,
              tag='autoconnect_on-stress_wimax', scenario_group='stress',
-             iterations=1000, device_type='wimax')
+             stress_iterations=1000, device_type='wimax')
diff --git a/client/site_tests/network_MobileSuspendResume/network_MobileSuspendResume.py b/client/site_tests/network_MobileSuspendResume/network_MobileSuspendResume.py
index 972a796..3118682 100644
--- a/client/site_tests/network_MobileSuspendResume/network_MobileSuspendResume.py
+++ b/client/site_tests/network_MobileSuspendResume/network_MobileSuspendResume.py
@@ -138,7 +138,7 @@
 
     # The suspend_mobile_enabled test suspends, then resumes the machine while
     # mobile is enabled.
-    def scenario_suspend_mobile_enabled(self):
+    def scenario_suspend_mobile_enabled(self, **kwargs):
         device = self.__get_mobile_device()
         self.enable_device(device, True)
         if not self.mobile_service_available():
@@ -147,7 +147,7 @@
 
     # The suspend_mobile_disabled test suspends, then resumes the machine
     # while mobile is disabled.
-    def scenario_suspend_mobile_disabled(self):
+    def scenario_suspend_mobile_disabled(self, **kwargs):
         device = self.__get_mobile_device()
         self.enable_device(device, False)
         self.suspend_resume(20)
@@ -166,7 +166,7 @@
     # of bug 9405.  The test will suspend/resume the device twice
     # while mobile is disabled.  We will then verify that mobile can be
     # enabled thereafter.
-    def scenario_suspend_mobile_disabled_twice(self):
+    def scenario_suspend_mobile_disabled_twice(self, **kwargs):
         device = self.__get_mobile_device()
         self.enable_device(device, False)
 
@@ -194,15 +194,27 @@
     # This test randomly enables or disables the modem.  This
     # is mainly used for stress tests as it does not check the power state of
     # the modem before and after suspend/resume.
-    def scenario_suspend_mobile_random(self):
+    def scenario_suspend_mobile_random(self, stress_iterations=10, **kwargs):
+        logging.debug('Running suspend_mobile_random %d times' %
+                      stress_iterations)
         device = self.__get_mobile_device()
         self.enable_device(device, choice([True, False]))
-        self.suspend_resume(randint(10, 40))
-        device = self.__get_mobile_device()
-        self.enable_device(device, True)
+
+        # Suspend the device for a random duration, wake it,
+        # wait for the service to appear, then wait for
+        # some random duration before suspending again.
+        for i in range(stress_iterations):
+            logging.debug('Running iteration %d' % (i+1))
+            self.suspend_resume(randint(10, 40))
+            device = self.__get_mobile_device()
+            self.enable_device(device, True)
+            if not self.FindMobileService(self.TIMEOUT*2):
+                raise error.TestError('Unable to find mobile service')
+            time.sleep(randint(1, 30))
+
 
     # This verifies that autoconnect works.
-    def scenario_autoconnect(self):
+    def scenario_autoconnect(self, **kwargs):
         device = self.__get_mobile_device()
         self.enable_device(device, True)
         service = self.FindMobileService(self.TIMEOUT)
@@ -275,7 +287,7 @@
 
     # This is the wrapper around the running of each scenario with
     # initialization steps and final checks.
-    def run_scenario(self, function_name):
+    def run_scenario(self, function_name, **kwargs):
         device = self.__get_mobile_device()
 
         # Initialize all tests with the power off.
@@ -283,7 +295,7 @@
 
         function = getattr(self, function_name)
         logging.info('Running %s' % function_name)
-        function()
+        function(**kwargs)
 
         # By the end of each test, the mobile device should be up.
         # Here we verify that the power state of the device is up, and
@@ -324,7 +336,7 @@
                                   device_type)
 
     def run_once(self, scenario_group='all', autoconnect=False,
-                 device_type=flimflam.FlimFlam.DEVICE_CELLULAR):
+                 device_type=flimflam.FlimFlam.DEVICE_CELLULAR, **kwargs):
 
         # Replace the test type with the list of tests
         if scenario_group not in network_MobileSuspendResume.scenarios.keys():
@@ -348,4 +360,4 @@
         logging.info('Running scenarios with autoconnect %s.' % autoconnect)
 
         for t in scenarios:
-            self.run_scenario(t)
+            self.run_scenario(t, **kwargs)