Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: components/network_time/network_time_tracker_unittest.cc

Issue 2453523002: Add NetworkTimeTracker::StartTimeFetch() for on-demand time queries (Closed)
Patch Set: meacer comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "components/network_time/network_time_tracker.h" 5 #include "components/network_time/network_time_tracker.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
14 #include "base/test/histogram_tester.h" 15 #include "base/test/histogram_tester.h"
15 #include "base/test/simple_test_clock.h" 16 #include "base/test/simple_test_clock.h"
16 #include "base/test/simple_test_tick_clock.h" 17 #include "base/test/simple_test_tick_clock.h"
17 #include "components/client_update_protocol/ecdsa.h" 18 #include "components/client_update_protocol/ecdsa.h"
18 #include "components/network_time/network_time_pref_names.h" 19 #include "components/network_time/network_time_pref_names.h"
19 #include "components/network_time/network_time_test_utils.h" 20 #include "components/network_time/network_time_test_utils.h"
20 #include "components/prefs/testing_pref_service.h" 21 #include "components/prefs/testing_pref_service.h"
21 #include "net/http/http_response_headers.h" 22 #include "net/http/http_response_headers.h"
22 #include "net/test/embedded_test_server/embedded_test_server.h" 23 #include "net/test/embedded_test_server/embedded_test_server.h"
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 out_network_time); 476 out_network_time);
476 // Should see no backoff in the success case. 477 // Should see no backoff in the success case.
477 EXPECT_EQ(base::TimeDelta::FromMinutes(60), 478 EXPECT_EQ(base::TimeDelta::FromMinutes(60),
478 tracker_->GetTimerDelayForTesting()); 479 tracker_->GetTimerDelayForTesting());
479 480
480 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 481 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
481 histograms.ExpectTotalCount(kFetchValidHistogram, 1); 482 histograms.ExpectTotalCount(kFetchValidHistogram, 1);
482 histograms.ExpectBucketCount(kFetchValidHistogram, true, 1); 483 histograms.ExpectBucketCount(kFetchValidHistogram, true, 1);
483 } 484 }
484 485
486 TEST_F(NetworkTimeTrackerTest, StartTimeFetch) {
487 test_server_->RegisterRequestHandler(base::Bind(&GoodTimeResponseHandler));
488 EXPECT_TRUE(test_server_->Start());
489 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
490
491 base::Time out_network_time;
492 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC_ATTEMPT,
493 tracker_->GetNetworkTime(&out_network_time, nullptr));
494
495 base::RunLoop run_loop;
496 EXPECT_TRUE(tracker_->StartTimeFetch(run_loop.QuitClosure()));
497 tracker_->WaitForFetchForTesting(123123123);
498 run_loop.Run();
499
500 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
501 tracker_->GetNetworkTime(&out_network_time, nullptr));
502 EXPECT_EQ(base::Time::UnixEpoch() +
503 base::TimeDelta::FromMilliseconds(1461621971825),
504 out_network_time);
505 // Should see no backoff in the success case.
506 EXPECT_EQ(base::TimeDelta::FromMinutes(60),
507 tracker_->GetTimerDelayForTesting());
508 }
509
510 // Tests that when StartTimeFetch() is called with a query already in
511 // progress, it calls the callback when that query completes.
512 TEST_F(NetworkTimeTrackerTest, StartTimeFetchWithQueryInProgress) {
513 test_server_->RegisterRequestHandler(base::Bind(&GoodTimeResponseHandler));
514 EXPECT_TRUE(test_server_->Start());
515 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
516
517 base::Time out_network_time;
518 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC_ATTEMPT,
519 tracker_->GetNetworkTime(&out_network_time, nullptr));
520
521 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
522
523 base::RunLoop run_loop;
524 EXPECT_TRUE(tracker_->StartTimeFetch(run_loop.QuitClosure()));
525 tracker_->WaitForFetchForTesting(123123123);
526 run_loop.Run();
527
528 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
529 tracker_->GetNetworkTime(&out_network_time, nullptr));
530 EXPECT_EQ(base::Time::UnixEpoch() +
531 base::TimeDelta::FromMilliseconds(1461621971825),
532 out_network_time);
533 // Should see no backoff in the success case.
534 EXPECT_EQ(base::TimeDelta::FromMinutes(60),
535 tracker_->GetTimerDelayForTesting());
536 }
537
538 // Tests that StartTimeFetch() returns false if called while network
539 // time is available.
540 TEST_F(NetworkTimeTrackerTest, StartTimeFetchWhileSynced) {
541 test_server_->RegisterRequestHandler(base::Bind(&GoodTimeResponseHandler));
542 EXPECT_TRUE(test_server_->Start());
543 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
544
545 base::Time in_network_time = clock_->Now();
546 UpdateNetworkTime(in_network_time, resolution_, latency_,
547 tick_clock_->NowTicks());
548
549 // No query should be started so long as NetworkTimeTracker is synced.
550 base::RunLoop run_loop;
551 EXPECT_FALSE(tracker_->StartTimeFetch(run_loop.QuitClosure()));
552 }
553
554 // Tests that StartTimeFetch() returns false if the field trial
555 // is not configured to allow on-demand time fetches.
556 TEST_F(NetworkTimeTrackerTest, StartTimeFetchWithoutVariationsParam) {
557 field_trial_test_->SetNetworkQueriesWithVariationsService(
558 true, 0.0, FieldTrialTest::DISABLE_FETCHES_ON_DEMAND);
559 test_server_->RegisterRequestHandler(base::Bind(&GoodTimeResponseHandler));
560 EXPECT_TRUE(test_server_->Start());
561 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
562
563 base::Time out_network_time;
564 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC_ATTEMPT,
565 tracker_->GetNetworkTime(&out_network_time, nullptr));
566
567 base::RunLoop run_loop;
568 EXPECT_FALSE(tracker_->StartTimeFetch(run_loop.QuitClosure()));
569 }
570
485 TEST_F(NetworkTimeTrackerTest, NoNetworkQueryWhileSynced) { 571 TEST_F(NetworkTimeTrackerTest, NoNetworkQueryWhileSynced) {
486 test_server_->RegisterRequestHandler(base::Bind(&GoodTimeResponseHandler)); 572 test_server_->RegisterRequestHandler(base::Bind(&GoodTimeResponseHandler));
487 EXPECT_TRUE(test_server_->Start()); 573 EXPECT_TRUE(test_server_->Start());
488 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/")); 574 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
489 575
490 field_trial_test_->SetNetworkQueriesWithVariationsService( 576 field_trial_test_->SetNetworkQueriesWithVariationsService(
491 true, 0.0, FieldTrialTest::ENABLE_FETCHES_ON_DEMAND); 577 true, 0.0, FieldTrialTest::ENABLE_FETCHES_ON_DEMAND);
492 base::Time in_network_time = clock_->Now(); 578 base::Time in_network_time = clock_->Now();
493 UpdateNetworkTime(in_network_time, resolution_, latency_, 579 UpdateNetworkTime(in_network_time, resolution_, latency_,
494 tick_clock_->NowTicks()); 580 tick_clock_->NowTicks());
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SUBSEQUENT_SYNC_PENDING, 799 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SUBSEQUENT_SYNC_PENDING,
714 tracker_->GetNetworkTime(&out_network_time, nullptr)); 800 tracker_->GetNetworkTime(&out_network_time, nullptr));
715 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 801 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
716 histograms.ExpectTotalCount(kFetchValidHistogram, 1); 802 histograms.ExpectTotalCount(kFetchValidHistogram, 1);
717 histograms.ExpectBucketCount(kFetchValidHistogram, false, 1); 803 histograms.ExpectBucketCount(kFetchValidHistogram, false, 1);
718 804
719 tracker_->WaitForFetchForTesting(123123123); 805 tracker_->WaitForFetchForTesting(123123123);
720 } 806 }
721 807
722 } // namespace network_time 808 } // namespace network_time
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698