Home | History | Annotate | Download | only in web_request
      1 // Copyright (c) 2012 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 "extensions/browser/api/web_request/web_request_time_tracker.h"
      6 
      7 #include "testing/gmock/include/gmock/gmock.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 
     10 namespace {
     11 const base::TimeDelta kRequestDelta = base::TimeDelta::FromMilliseconds(100);
     12 const base::TimeDelta kTinyDelay = base::TimeDelta::FromMilliseconds(1);
     13 const base::TimeDelta kModerateDelay = base::TimeDelta::FromMilliseconds(25);
     14 const base::TimeDelta kExcessiveDelay = base::TimeDelta::FromMilliseconds(75);
     15 
     16 class ExtensionWebRequestTimeTrackerDelegateMock
     17     : public ExtensionWebRequestTimeTrackerDelegate {
     18  public:
     19   MOCK_METHOD4(NotifyExcessiveDelays,
     20       void (void*, size_t, size_t, const std::set<std::string>&));
     21   MOCK_METHOD4(NotifyModerateDelays,
     22       void (void*, size_t, size_t, const std::set<std::string>&));
     23 };
     24 
     25 }  // namespace
     26 
     27 //class ExtensionWebRequestTimeTrackerTest : public testing::Test {};
     28 
     29 TEST(ExtensionWebRequestTimeTrackerTest, Basic) {
     30   ExtensionWebRequestTimeTracker tracker;
     31   base::Time start;
     32   void* profile = NULL;
     33 
     34   tracker.LogRequestStartTime(42, start, GURL(), profile);
     35   EXPECT_EQ(1u, tracker.request_time_logs_.size());
     36   ASSERT_EQ(1u, tracker.request_ids_.size());
     37   EXPECT_EQ(42, tracker.request_ids_.front());
     38   tracker.LogRequestEndTime(42, start + kRequestDelta);
     39   EXPECT_EQ(1u, tracker.request_time_logs_.size());
     40   EXPECT_EQ(0u, tracker.moderate_delays_.size());
     41   EXPECT_EQ(0u, tracker.excessive_delays_.size());
     42 }
     43 
     44 TEST(ExtensionWebRequestTimeTrackerTest, CancelOrRedirect) {
     45   ExtensionWebRequestTimeTracker tracker;
     46   base::Time start;
     47   void* profile = NULL;
     48 
     49   tracker.LogRequestStartTime(1, start, GURL(), profile);
     50   EXPECT_EQ(1u, tracker.request_time_logs_.size());
     51   tracker.SetRequestCanceled(1);
     52   tracker.LogRequestEndTime(1, start + kRequestDelta);
     53   EXPECT_EQ(0u, tracker.request_time_logs_.size());
     54 
     55   tracker.LogRequestStartTime(2, start, GURL(), profile);
     56   EXPECT_EQ(1u, tracker.request_time_logs_.size());
     57   tracker.SetRequestRedirected(2);
     58   tracker.LogRequestEndTime(2, start + kRequestDelta);
     59   EXPECT_EQ(0u, tracker.request_time_logs_.size());
     60 }
     61 
     62 TEST(ExtensionWebRequestTimeTrackerTest, Delays) {
     63   ExtensionWebRequestTimeTracker tracker;
     64   base::Time start;
     65   std::string extension1_id("1");
     66   std::string extension2_id("2");
     67   void* profile = NULL;
     68 
     69   // Start 3 requests with different amounts of delay from 2 extensions.
     70   tracker.LogRequestStartTime(1, start, GURL(), profile);
     71   tracker.LogRequestStartTime(2, start, GURL(), profile);
     72   tracker.LogRequestStartTime(3, start, GURL(), profile);
     73   tracker.IncrementExtensionBlockTime(extension1_id, 1, kTinyDelay);
     74   tracker.IncrementExtensionBlockTime(extension1_id, 2, kTinyDelay);
     75   tracker.IncrementExtensionBlockTime(extension1_id, 3, kTinyDelay);
     76   tracker.IncrementExtensionBlockTime(extension2_id, 2, kModerateDelay);
     77   tracker.IncrementExtensionBlockTime(extension2_id, 3, kExcessiveDelay);
     78   tracker.IncrementTotalBlockTime(1, kTinyDelay);
     79   tracker.IncrementTotalBlockTime(2, kModerateDelay);
     80   tracker.IncrementTotalBlockTime(3, kExcessiveDelay);
     81   tracker.LogRequestEndTime(1, start + kRequestDelta);
     82   tracker.LogRequestEndTime(2, start + kRequestDelta);
     83   tracker.LogRequestEndTime(3, start + kRequestDelta);
     84   EXPECT_EQ(3u, tracker.request_time_logs_.size());
     85   EXPECT_EQ(1u, tracker.moderate_delays_.size());
     86   EXPECT_EQ(1u, tracker.moderate_delays_.count(2));
     87   EXPECT_EQ(1u, tracker.excessive_delays_.size());
     88   EXPECT_EQ(1u, tracker.excessive_delays_.count(3));
     89 
     90   // Now issue a bunch more requests and ensure that the old delays are
     91   // forgotten.
     92   for (int64 i = 4; i < 500; ++i) {
     93     tracker.LogRequestStartTime(i, start, GURL(), profile);
     94     tracker.LogRequestEndTime(i, start + kRequestDelta);
     95   }
     96   EXPECT_EQ(0u, tracker.moderate_delays_.size());
     97   EXPECT_EQ(0u, tracker.excessive_delays_.size());
     98 }
     99 
    100 TEST(ExtensionWebRequestTimeTrackerTest, Delegate) {
    101   using testing::Mock;
    102 
    103   ExtensionWebRequestTimeTrackerDelegateMock* delegate(
    104       new ExtensionWebRequestTimeTrackerDelegateMock);
    105   ExtensionWebRequestTimeTracker tracker;
    106   tracker.SetDelegate(delegate);
    107   base::Time start;
    108   std::string extension1_id("1");
    109   void* profile = NULL;
    110   // Set of all extensions that blocked network requests.
    111   std::set<std::string> extensions;
    112   extensions.insert(extension1_id);
    113 
    114   const int num_moderate_delays = 51;
    115   const int num_excessive_delays = 11;
    116   int request_nr = 0;
    117 
    118   // Check that (only) the last moderate delay triggers the delegate callback.
    119   for (int64 i = 0; i < num_moderate_delays; ++i) {
    120     request_nr++;
    121     if (i == num_moderate_delays-1) {
    122       EXPECT_CALL(*delegate,
    123                   NotifyModerateDelays(profile , i+1, request_nr, extensions));
    124     }
    125     tracker.LogRequestStartTime(request_nr, start, GURL(), profile);
    126     tracker.IncrementExtensionBlockTime(extension1_id, request_nr,
    127                                         kModerateDelay);
    128     tracker.IncrementTotalBlockTime(request_nr, kModerateDelay);
    129     tracker.LogRequestEndTime(request_nr, start + kRequestDelta);
    130     Mock::VerifyAndClearExpectations(delegate);
    131   }
    132 
    133   // Check that (only) the last excessive delay triggers the delegate callback.
    134   for (int64 i = 0; i < num_excessive_delays; ++i) {
    135     request_nr++;
    136     if (i == num_excessive_delays-1) {
    137       EXPECT_CALL(*delegate,
    138                   NotifyExcessiveDelays(profile, i+1, request_nr, extensions));
    139     }
    140     tracker.LogRequestStartTime(request_nr, start, GURL(), profile);
    141     tracker.IncrementExtensionBlockTime(extension1_id, request_nr,
    142                                         kExcessiveDelay);
    143     tracker.IncrementTotalBlockTime(request_nr, kExcessiveDelay);
    144     tracker.LogRequestEndTime(request_nr, start + kRequestDelta);
    145     Mock::VerifyAndClearExpectations(delegate);
    146   }
    147 }
    148