You are here

function AcquiaLiftWebTestWorkflow::testAutoStop in Acquia Lift Connector 7

Tests the logic of AcquiaLiftAgent's stopNow() implementation in the simplest use case.

File

tests/acquia_lift.test, line 2758
Integration tests for Acquia Lift module.

Class

AcquiaLiftWebTestWorkflow

Code

function testAutoStop() {

  // Create a new agent via the UI.
  $agent = $this
    ->createTestAgent(array(
    'control_rate' => 10,
    'explore_rate' => 30,
    'auto_stop' => 1,
  ), TRUE, FALSE);
  $machine_name = $agent
    ->getMachineName();
  $agent_data = personalize_agent_load($machine_name, TRUE);

  // Set the start time of the agent to 2 minutes ago.
  $agent_data->started = time() - 120;

  // Most basic case - a single option set with 2 options.
  list($option_set, $new_queue_items) = $this
    ->createOptionSet(0, array(
    'agent' => $machine_name,
    'plugin' => 'type1',
    'num_options' => 3,
  ));
  $osid = 'osid-' . $option_set->osid;

  // Generate the reporting data to be returned by the dummy http client for this
  // agent.
  $points = array(
    $osid => array(
      $osid . ':option-A',
      $osid . ':option-B',
      $osid . ':option-C',
    ),
  );
  $test_data = array(
    'reports' => array(
      $machine_name => array(
        // Generate a fake confidence report with no clear winner.
        'confidence' => AcquiaLiftTestReports::generateConfidenceReportWithWinners($machine_name, $points),
      ),
    ),
  );
  variable_set('acquia_lift_web_test_data', $test_data);

  // Ensure no other criteria are used to determine whether we should stop.
  variable_set('acquia_lift_min_decisions', 0);
  variable_set('acquia_lift_min_runtime_num', 0);
  $this
    ->resetAll();

  // Make sure we are using a test instance of the API client.
  AcquiaLiftAPI::setTestInstance();
  $stop = $agent
    ->stopNow();

  // The call should return FALSE as neither minimum runtime nor minimum decisions
  // has been configured as a basis on which to stop.
  $this
    ->assertFalse($stop);

  // No winner should have been set on the option set.
  $option_set = personalize_option_set_load(1, TRUE);
  $this
    ->assertNull($option_set->winner);

  // Set min decisions to 30 and we should get option C set as the winner (just
  // because the test data is set up to give higher values to later options
  // if not otherwise specified.)
  variable_set('acquia_lift_min_decisions', 30);
  $this
    ->resetAll();
  $agent_instance = personalize_agent_load_agent($machine_name, TRUE);
  AcquiaLiftAPI::setTestInstance();
  $stop = $agent_instance
    ->stopNow();
  $this
    ->assertTrue($stop);

  // Confirm the winner has been set on the option set.
  $option_set = personalize_option_set_load($option_set->osid, TRUE);
  $this
    ->assertEqual('option-C', $option_set->winner);

  // Now generate a report where option B has the highest estimated value.
  $test_data = array(
    'reports' => array(
      $machine_name => array(
        'confidence' => AcquiaLiftTestReports::generateConfidenceReportWithWinners($machine_name, $points, array(
          $osid => $osid . ':option-B',
        )),
      ),
    ),
  );
  variable_set('acquia_lift_web_test_data', $test_data);
  $this
    ->resetAll();
  AcquiaLiftAPI::setTestInstance();
  $agent_instance = personalize_agent_load_agent($machine_name, TRUE);
  $stop = $agent_instance
    ->stopNow();
  $this
    ->assertTrue($stop);

  // Confirm the winner has been set on the option set.
  $option_set = personalize_option_set_load($option_set->osid, TRUE);
  $this
    ->assertEqual('option-B', $option_set->winner);

  // Setting the minimum number of decisions to 100 will mean we won't have enough
  // decisions so we should get FALSE.
  variable_set('acquia_lift_min_decisions', 100);
  $this
    ->resetAll();
  $agent_instance = personalize_agent_load_agent($machine_name, TRUE);
  AcquiaLiftAPI::setTestInstance();
  $stop = $agent_instance
    ->stopNow();
  $this
    ->assertFalse($stop);

  // Setting it to 30 should get TRUE again.
  variable_set('acquia_lift_min_decisions', 30);
  $this
    ->resetAll();
  $agent_instance = personalize_agent_load_agent($machine_name, TRUE);
  AcquiaLiftAPI::setTestInstance();
  $stop = $agent_instance
    ->stopNow();
  $this
    ->assertTrue($stop);

  // Setting the minimum runtime to 1 day should give us FALSE.
  variable_set('acquia_lift_min_runtime_num', 1);
  variable_set('acquia_lift_min_runtime_unit', 'day');
  $this
    ->resetAll();
  $agent_instance = personalize_agent_load_agent($machine_name, TRUE);
  AcquiaLiftAPI::setTestInstance();
  $stop = $agent_instance
    ->stopNow();
  $this
    ->assertFalse($stop);

  // Setting the minimum runtime to 1 minute should give us TRUE again.
  variable_set('acquia_lift_min_runtime_unit', 'minute');
  $this
    ->resetAll();
  $agent_instance = personalize_agent_load_agent($machine_name, TRUE);
  AcquiaLiftAPI::setTestInstance();
  $stop = $agent_instance
    ->stopNow();
  $this
    ->assertTrue($stop);
}