Home | History | Annotate | Download | only in integration
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/sync/test/integration/retry_verifier.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "base/logging.h"
     10 #include "sync/internal_api/public/engine/polling_constants.h"
     11 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
     12 
     13 namespace {
     14 // Given the current delay calculate the minimum and maximum wait times for
     15 // the next retry.
     16 DelayInfo CalculateDelay(int64 current_delay) {
     17   int64 backoff_s = std::max(static_cast<int64>(1), current_delay *
     18                              syncer::kBackoffRandomizationFactor);
     19 
     20   DelayInfo delay_info;
     21   delay_info.min_delay = backoff_s + (-1 * current_delay/
     22                              syncer::kBackoffRandomizationFactor);
     23   delay_info.max_delay = backoff_s + current_delay/2;
     24 
     25   delay_info.min_delay = std::max(static_cast<int64>(1),
     26       std::min(delay_info.min_delay, syncer::kMaxBackoffSeconds));
     27 
     28   delay_info.max_delay = std::max(static_cast<int64>(1),
     29       std::min(delay_info.max_delay, syncer::kMaxBackoffSeconds));
     30 
     31   return delay_info;
     32 }
     33 
     34 // Fills the table with the maximum and minimum values for each retry, upto
     35 // |count| number of retries.
     36 void FillDelayTable(DelayInfo* delay_table, int count) {
     37   DCHECK(count > 1);
     38 
     39   // We start off with the minimum value of 2 seconds.
     40   delay_table[0].min_delay = static_cast<int64>(2);
     41   delay_table[0].max_delay = static_cast<int64>(2);
     42 
     43   for (int i = 1 ; i < count ; ++i) {
     44     delay_table[i].min_delay = CalculateDelay(delay_table[i-1].min_delay).
     45                                min_delay;
     46     delay_table[i].max_delay = CalculateDelay(delay_table[i-1].max_delay).
     47                                max_delay;
     48   }
     49 }
     50 }  // namespace
     51 
     52 // Verifies if the current retry is on time. Note that we dont use the
     53 // maximum value of the retry range in verifying, only the minimum. Reason
     54 // being there is no guarantee that the retry will be on the dot. However in
     55 // practice it is on the dot. But making that assumption for all the platforms
     56 // would make the test flaky. However we have the global timeout for the
     57 // verification which would make sure all retries take place in a reasonable
     58 // amount of time. The global timeout is defined in profile sync service
     59 // harness as |kExponentialBackoffVerificationTimeoutMs|.
     60 bool IsRetryOnTime(DelayInfo* delay_table, int retry_count,
     61                    const base::TimeDelta& time_elapsed) {
     62   DVLOG(1) << "Retry Count : " << retry_count
     63            << " Time elapsed : " << time_elapsed.InSeconds()
     64            << " Retry table min: " << delay_table[retry_count].min_delay
     65            << " Retry table max: " << delay_table[retry_count].max_delay;
     66   return ((time_elapsed.InSeconds() >= delay_table[retry_count].min_delay));
     67 }
     68 
     69 RetryVerifier::RetryVerifier() : retry_count_(0),
     70                                  success_(false),
     71                                  done_(false) {
     72   memset(&delay_table_, 0, sizeof(delay_table_));
     73 }
     74 
     75 RetryVerifier::~RetryVerifier() {
     76 }
     77 
     78 // Initializes the state for verification.
     79 void RetryVerifier::Initialize(
     80     const syncer::sessions::SyncSessionSnapshot& snap) {
     81   retry_count_ = 0;
     82   last_sync_time_ = snap.sync_start_time();
     83   FillDelayTable(delay_table_, kMaxRetry);
     84   done_ = false;
     85   success_ = false;
     86 }
     87 
     88 void RetryVerifier::VerifyRetryInterval(
     89     const syncer::sessions::SyncSessionSnapshot& snap) {
     90   DCHECK(retry_count_ < kMaxRetry);
     91   if (retry_count_ == 0) {
     92     if (snap.sync_start_time() != last_sync_time_) {
     93       retry_count_++;
     94       last_sync_time_ = snap.sync_start_time();
     95     }
     96     success_ = true;
     97     return;
     98   }
     99 
    100   // Check if the sync start time has changed. If so indicates a new sync
    101   // has taken place.
    102   if (snap.sync_start_time() != last_sync_time_) {
    103     base::TimeDelta delta = snap.sync_start_time() - last_sync_time_;
    104     success_ = IsRetryOnTime(delay_table_,retry_count_ -1, delta);
    105     last_sync_time_ = snap.sync_start_time();
    106     ++retry_count_;
    107     done_ = (retry_count_ >= kMaxRetry);
    108     return;
    109   }
    110 }
    111 
    112