| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Single threaded tests of UrlInfo functionality. | 5 // Single threaded tests of UrlInfo functionality. |
| 6 | 6 |
| 7 #include <time.h> | 7 #include <time.h> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 info.SetFoundState(); | 50 info.SetFoundState(); |
| 51 // "Immediately" check to see if we need an update yet (we shouldn't). | 51 // "Immediately" check to see if we need an update yet (we shouldn't). |
| 52 if (info.NeedsDnsUpdate()) { | 52 if (info.NeedsDnsUpdate()) { |
| 53 // The test bot must be really slow, so we can verify that. | 53 // The test bot must be really slow, so we can verify that. |
| 54 EXPECT_GT((TimeTicks::Now() - before_resolution_complete).InMilliseconds(), | 54 EXPECT_GT((TimeTicks::Now() - before_resolution_complete).InMilliseconds(), |
| 55 UrlInfo::get_cache_expiration().InMilliseconds()); | 55 UrlInfo::get_cache_expiration().InMilliseconds()); |
| 56 return; // Lets punt here, the test bot is too slow. | 56 return; // Lets punt here, the test bot is too slow. |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Run similar test with a shortened expiration, so we can trigger it. | 59 // Run similar test with a shortened expiration, so we can trigger it. |
| 60 const int kMockExpirationTime(300); // Vastly reduced expiration time. | 60 const TimeDelta kMockExpirationTime = TimeDelta::FromMilliseconds(300); |
| 61 info.set_cache_expiration(TimeDelta::FromMilliseconds(kMockExpirationTime)); | 61 info.set_cache_expiration(kMockExpirationTime); |
| 62 | 62 |
| 63 // That was a nice life when the object was found.... but next time it won't | 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. | 64 // be found. We'll sleep for a while, and then come back with not-found. |
| 65 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); | 65 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); |
| 66 EXPECT_FALSE(info.NeedsDnsUpdate()); | 66 EXPECT_FALSE(info.NeedsDnsUpdate()); |
| 67 info.SetAssignedState(); | 67 info.SetAssignedState(); |
| 68 EXPECT_FALSE(info.NeedsDnsUpdate()); | 68 EXPECT_FALSE(info.NeedsDnsUpdate()); |
| 69 // Greater than minimal expected network latency on DNS lookup. | 69 // Greater than minimal expected network latency on DNS lookup. |
| 70 base::PlatformThread::Sleep(25); | 70 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(25)); |
| 71 before_resolution_complete = TimeTicks::Now(); | 71 before_resolution_complete = TimeTicks::Now(); |
| 72 info.SetNoSuchNameState(); | 72 info.SetNoSuchNameState(); |
| 73 // "Immediately" check to see if we need an update yet (we shouldn't). | 73 // "Immediately" check to see if we need an update yet (we shouldn't). |
| 74 if (info.NeedsDnsUpdate()) { | 74 if (info.NeedsDnsUpdate()) { |
| 75 // The test bot must be really slow, so we can verify that. | 75 // The test bot must be really slow, so we can verify that. |
| 76 EXPECT_GT((TimeTicks::Now() - before_resolution_complete).InMilliseconds(), | 76 EXPECT_GT((TimeTicks::Now() - before_resolution_complete), |
| 77 kMockExpirationTime); | 77 kMockExpirationTime); |
| 78 return; | 78 return; |
| 79 } | 79 } |
| 80 // Wait over 300ms, so it should definately be considered out of cache. | 80 // Wait over 300ms, so it should definately be considered out of cache. |
| 81 base::PlatformThread::Sleep(kMockExpirationTime + 20); | 81 base::PlatformThread::Sleep(kMockExpirationTime + |
| 82 TimeDelta::FromMilliseconds(20)); |
| 82 EXPECT_TRUE(info.NeedsDnsUpdate()) << "expiration time not honored"; | 83 EXPECT_TRUE(info.NeedsDnsUpdate()) << "expiration time not honored"; |
| 83 } | 84 } |
| 84 | 85 |
| 85 // When a system gets "congested" relative to DNS, it means it is doing too many | 86 // When a system gets "congested" relative to DNS, it means it is doing too many |
| 86 // DNS resolutions, and bogging down the system. When we detect such a | 87 // DNS resolutions, and bogging down the system. When we detect such a |
| 87 // situation, we divert the sequence of states a UrlInfo instance moves | 88 // situation, we divert the sequence of states a UrlInfo instance moves |
| 88 // through. Rather than proceeding from QUEUED (waiting in a name queue for a | 89 // through. Rather than proceeding from QUEUED (waiting in a name queue for a |
| 89 // worker thread that can resolve the name) to ASSIGNED (where a worker thread | 90 // worker thread that can resolve the name) to ASSIGNED (where a worker thread |
| 90 // actively resolves the name), we enter the ASSIGNED state (without actually | 91 // actively resolves the name), we enter the ASSIGNED state (without actually |
| 91 // getting sent to a resolver thread) and reset our state to what it was before | 92 // getting sent to a resolver thread) and reset our state to what it was before |
| (...skipping 28 matching lines...) Expand all Loading... |
| 120 info.RemoveFromQueue(); // Do the reset. | 121 info.RemoveFromQueue(); // Do the reset. |
| 121 EXPECT_FALSE(info.is_assigned()); | 122 EXPECT_FALSE(info.is_assigned()); |
| 122 EXPECT_TRUE(info.was_found()); // Back to what it was before being queued. | 123 EXPECT_TRUE(info.was_found()); // Back to what it was before being queued. |
| 123 } | 124 } |
| 124 | 125 |
| 125 | 126 |
| 126 // TODO(jar): Add death test for illegal state changes, and also for setting | 127 // TODO(jar): Add death test for illegal state changes, and also for setting |
| 127 // hostname when already set. | 128 // hostname when already set. |
| 128 | 129 |
| 129 } // namespace | 130 } // namespace |
| OLD | NEW |