Home | History | Annotate | Download | only in net
      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 // Single threaded tests of UrlInfo functionality.
      6 
      7 #include <time.h>
      8 #include <string>
      9 
     10 #include "base/threading/platform_thread.h"
     11 #include "base/time/time.h"
     12 #include "chrome/browser/net/url_info.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 using base::TimeDelta;
     16 using base::TimeTicks;
     17 
     18 namespace {
     19 
     20 class UrlHostInfoTest : public testing::Test {
     21 };
     22 
     23 typedef chrome_browser_net::UrlInfo UrlInfo;
     24 
     25 // Cycle throught the states held by a UrlInfo instance, and check to see that
     26 // states look reasonable as time ticks away.  If the test bots are too slow,
     27 // we'll just give up on this test and exit from it.
     28 TEST(UrlHostInfoTest, StateChangeTest) {
     29   UrlInfo info_practice, info;
     30   GURL url1("http://domain1.com:80"), url2("https://domain2.com:443");
     31 
     32   // First load DLL, so that their load time won't interfere with tests.
     33   // Some tests involve timing function performance, and DLL time can overwhelm
     34   // test durations (which are considering network vs cache response times).
     35   info_practice.SetUrl(url2);
     36   info_practice.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
     37   info_practice.SetAssignedState();
     38   info_practice.SetFoundState();
     39 
     40   // Start test with actual (long/default) expiration time intact.
     41 
     42   // Complete the construction of real test object.
     43   info.SetUrl(url1);
     44   EXPECT_TRUE(info.NeedsDnsUpdate()) << "error in construction state";
     45   info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
     46   EXPECT_FALSE(info.NeedsDnsUpdate()) << "update needed after being queued";
     47   info.SetAssignedState();
     48   EXPECT_FALSE(info.NeedsDnsUpdate())  << "update needed during resolution";
     49   base::TimeTicks before_resolution_complete = TimeTicks::Now();
     50   info.SetFoundState();
     51   // "Immediately" check to see if we need an update yet (we shouldn't).
     52   if (info.NeedsDnsUpdate()) {
     53     // The test bot must be really slow, so we can verify that.
     54     EXPECT_GT((TimeTicks::Now() - before_resolution_complete).InMilliseconds(),
     55               UrlInfo::get_cache_expiration().InMilliseconds());
     56     return;  // Lets punt here, the test bot is too slow.
     57   }
     58 
     59   // Run similar test with a shortened expiration, so we can trigger it.
     60   const TimeDelta kMockExpirationTime = TimeDelta::FromMilliseconds(300);
     61   info.set_cache_expiration(kMockExpirationTime);
     62 
     63   // That was a nice life when the object was found.... but next time it won't
     64   // be found.  We'll sleep for a while, and then come back with not-found.
     65   info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
     66   EXPECT_FALSE(info.NeedsDnsUpdate());
     67   info.SetAssignedState();
     68   EXPECT_FALSE(info.NeedsDnsUpdate());
     69   // Greater than minimal expected network latency on DNS lookup.
     70   base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(25));
     71   before_resolution_complete = TimeTicks::Now();
     72   info.SetNoSuchNameState();
     73   // "Immediately" check to see if we need an update yet (we shouldn't).
     74   if (info.NeedsDnsUpdate()) {
     75     // The test bot must be really slow, so we can verify that.
     76     EXPECT_GT((TimeTicks::Now() - before_resolution_complete),
     77               kMockExpirationTime);
     78     return;
     79   }
     80   // Wait over 300ms, so it should definately be considered out of cache.
     81   base::PlatformThread::Sleep(kMockExpirationTime +
     82                               TimeDelta::FromMilliseconds(20));
     83   EXPECT_TRUE(info.NeedsDnsUpdate()) << "expiration time not honored";
     84 }
     85 
     86 // When a system gets "congested" relative to DNS, it means it is doing too many
     87 // DNS resolutions, and bogging down the system.  When we detect such a
     88 // situation, we divert the sequence of states a UrlInfo instance moves
     89 // through.  Rather than proceeding from QUEUED (waiting in a name queue for a
     90 // worker thread that can resolve the name) to ASSIGNED (where a worker thread
     91 // actively resolves the name), we enter the ASSIGNED state (without actually
     92 // getting sent to a resolver thread) and reset our state to what it was before
     93 // the corresponding name was put in the work_queue_.  This test drives through
     94 // the state transitions used in such congestion handling.
     95 TEST(UrlHostInfoTest, CongestionResetStateTest) {
     96   UrlInfo info;
     97   GURL url("http://domain1.com:80");
     98 
     99   info.SetUrl(url);
    100   info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
    101   info.SetAssignedState();
    102   EXPECT_TRUE(info.is_assigned());
    103 
    104   info.RemoveFromQueue();  // Do the reset.
    105   EXPECT_FALSE(info.is_assigned());
    106 
    107   // Since this was a new info instance, and it never got resolved, we land back
    108   // in a PENDING state rather than FOUND or NO_SUCH_NAME.
    109   EXPECT_FALSE(info.was_found());
    110   EXPECT_FALSE(info.was_nonexistent());
    111 
    112   // Make sure we're completely re-usable, by going throug a normal flow.
    113   info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
    114   info.SetAssignedState();
    115   info.SetFoundState();
    116   EXPECT_TRUE(info.was_found());
    117 
    118   // Use the congestion flow, and check that we end up in the found state.
    119   info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
    120   info.SetAssignedState();
    121   info.RemoveFromQueue();  // Do the reset.
    122   EXPECT_FALSE(info.is_assigned());
    123   EXPECT_TRUE(info.was_found());  // Back to what it was before being queued.
    124 }
    125 
    126 
    127 // TODO(jar): Add death test for illegal state changes, and also for setting
    128 // hostname when already set.
    129 
    130 }  // namespace
    131