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 #include "net/socket/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/platform_thread.h" | 10 #include "base/platform_thread.h" |
11 #include "base/scoped_vector.h" | 11 #include "base/scoped_vector.h" |
| 12 #include "base/string_util.h" |
12 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
13 #include "net/base/net_log_unittest.h" | 14 #include "net/base/net_log_unittest.h" |
14 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
15 #include "net/base/request_priority.h" | 16 #include "net/base/request_priority.h" |
16 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
17 #include "net/socket/client_socket.h" | 18 #include "net/socket/client_socket.h" |
18 #include "net/socket/client_socket_factory.h" | 19 #include "net/socket/client_socket_factory.h" |
19 #include "net/socket/client_socket_handle.h" | 20 #include "net/socket/client_socket_handle.h" |
20 #include "net/socket/socket_test_util.h" | 21 #include "net/socket/socket_test_util.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 virtual const std::string& name() const { return base_.name(); } | 336 virtual const std::string& name() const { return base_.name(); } |
336 | 337 |
337 const TestClientSocketPoolBase* base() const { return &base_; } | 338 const TestClientSocketPoolBase* base() const { return &base_; } |
338 | 339 |
339 int NumConnectJobsInGroup(const std::string& group_name) const { | 340 int NumConnectJobsInGroup(const std::string& group_name) const { |
340 return base_.NumConnectJobsInGroup(group_name); | 341 return base_.NumConnectJobsInGroup(group_name); |
341 } | 342 } |
342 | 343 |
343 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } | 344 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } |
344 | 345 |
| 346 void EnableBackupJobs() { base_.EnableBackupJobs(); } |
| 347 |
345 private: | 348 private: |
346 ~TestClientSocketPool() {} | 349 ~TestClientSocketPool() {} |
347 | 350 |
348 TestClientSocketPoolBase base_; | 351 TestClientSocketPoolBase base_; |
349 | 352 |
350 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); | 353 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); |
351 }; | 354 }; |
352 | 355 |
353 } // namespace | 356 } // namespace |
354 | 357 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 EXPECT_EQ(OK, StartRequest("d", kDefaultPriority)); | 579 EXPECT_EQ(OK, StartRequest("d", kDefaultPriority)); |
577 | 580 |
578 EXPECT_EQ(static_cast<int>(requests_.size()), | 581 EXPECT_EQ(static_cast<int>(requests_.size()), |
579 client_socket_factory_.allocation_count()); | 582 client_socket_factory_.allocation_count()); |
580 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); | 583 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); |
581 | 584 |
582 EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", kDefaultPriority)); | 585 EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", kDefaultPriority)); |
583 EXPECT_EQ(ERR_IO_PENDING, StartRequest("f", kDefaultPriority)); | 586 EXPECT_EQ(ERR_IO_PENDING, StartRequest("f", kDefaultPriority)); |
584 EXPECT_EQ(ERR_IO_PENDING, StartRequest("g", kDefaultPriority)); | 587 EXPECT_EQ(ERR_IO_PENDING, StartRequest("g", kDefaultPriority)); |
585 | 588 |
586 ReleaseAllConnections(KEEP_ALIVE); | 589 ReleaseAllConnections(NO_KEEP_ALIVE); |
587 | 590 |
588 EXPECT_EQ(static_cast<int>(requests_.size()), | 591 EXPECT_EQ(static_cast<int>(requests_.size()), |
589 client_socket_factory_.allocation_count()); | 592 client_socket_factory_.allocation_count()); |
590 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); | 593 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); |
591 | 594 |
592 EXPECT_EQ(1, GetOrderOfRequest(1)); | 595 EXPECT_EQ(1, GetOrderOfRequest(1)); |
593 EXPECT_EQ(2, GetOrderOfRequest(2)); | 596 EXPECT_EQ(2, GetOrderOfRequest(2)); |
594 EXPECT_EQ(3, GetOrderOfRequest(3)); | 597 EXPECT_EQ(3, GetOrderOfRequest(3)); |
595 EXPECT_EQ(4, GetOrderOfRequest(4)); | 598 EXPECT_EQ(4, GetOrderOfRequest(4)); |
596 EXPECT_EQ(5, GetOrderOfRequest(5)); | 599 EXPECT_EQ(5, GetOrderOfRequest(5)); |
(...skipping 15 matching lines...) Expand all Loading... |
612 EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); | 615 EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); |
613 EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); | 616 EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); |
614 | 617 |
615 EXPECT_EQ(static_cast<int>(requests_.size()), | 618 EXPECT_EQ(static_cast<int>(requests_.size()), |
616 client_socket_factory_.allocation_count()); | 619 client_socket_factory_.allocation_count()); |
617 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); | 620 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); |
618 | 621 |
619 // Now create a new group and verify that we don't starve it. | 622 // Now create a new group and verify that we don't starve it. |
620 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kDefaultPriority)); | 623 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kDefaultPriority)); |
621 | 624 |
622 ReleaseAllConnections(KEEP_ALIVE); | 625 ReleaseAllConnections(NO_KEEP_ALIVE); |
623 | 626 |
624 EXPECT_EQ(static_cast<int>(requests_.size()), | 627 EXPECT_EQ(static_cast<int>(requests_.size()), |
625 client_socket_factory_.allocation_count()); | 628 client_socket_factory_.allocation_count()); |
626 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); | 629 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); |
627 | 630 |
628 EXPECT_EQ(1, GetOrderOfRequest(1)); | 631 EXPECT_EQ(1, GetOrderOfRequest(1)); |
629 EXPECT_EQ(2, GetOrderOfRequest(2)); | 632 EXPECT_EQ(2, GetOrderOfRequest(2)); |
630 EXPECT_EQ(3, GetOrderOfRequest(3)); | 633 EXPECT_EQ(3, GetOrderOfRequest(3)); |
631 EXPECT_EQ(4, GetOrderOfRequest(4)); | 634 EXPECT_EQ(4, GetOrderOfRequest(4)); |
632 EXPECT_EQ(5, GetOrderOfRequest(5)); | 635 EXPECT_EQ(5, GetOrderOfRequest(5)); |
(...skipping 10 matching lines...) Expand all Loading... |
643 EXPECT_EQ(OK, StartRequest("b", HIGHEST)); | 646 EXPECT_EQ(OK, StartRequest("b", HIGHEST)); |
644 EXPECT_EQ(OK, StartRequest("a", LOWEST)); | 647 EXPECT_EQ(OK, StartRequest("a", LOWEST)); |
645 | 648 |
646 EXPECT_EQ(static_cast<int>(requests_.size()), | 649 EXPECT_EQ(static_cast<int>(requests_.size()), |
647 client_socket_factory_.allocation_count()); | 650 client_socket_factory_.allocation_count()); |
648 | 651 |
649 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", LOWEST)); | 652 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", LOWEST)); |
650 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); | 653 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
651 EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", HIGHEST)); | 654 EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", HIGHEST)); |
652 | 655 |
653 ReleaseAllConnections(KEEP_ALIVE); | 656 ReleaseAllConnections(NO_KEEP_ALIVE); |
654 | 657 |
655 // We're re-using one socket for group "a", and one for "b". | |
656 EXPECT_EQ(static_cast<int>(requests_.size()) - 2, | |
657 client_socket_factory_.allocation_count()); | |
658 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); | 658 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); |
659 | 659 |
660 // First 4 requests don't have to wait, and finish in order. | 660 // First 4 requests don't have to wait, and finish in order. |
661 EXPECT_EQ(1, GetOrderOfRequest(1)); | 661 EXPECT_EQ(1, GetOrderOfRequest(1)); |
662 EXPECT_EQ(2, GetOrderOfRequest(2)); | 662 EXPECT_EQ(2, GetOrderOfRequest(2)); |
663 EXPECT_EQ(3, GetOrderOfRequest(3)); | 663 EXPECT_EQ(3, GetOrderOfRequest(3)); |
664 EXPECT_EQ(4, GetOrderOfRequest(4)); | 664 EXPECT_EQ(4, GetOrderOfRequest(4)); |
665 | 665 |
666 // Request ("b", HIGHEST) has the highest priority, then ("a", MEDIUM), | 666 // Request ("b", HIGHEST) has the highest priority, then ("a", MEDIUM), |
667 // and then ("c", LOWEST). | 667 // and then ("c", LOWEST). |
(...skipping 13 matching lines...) Expand all Loading... |
681 EXPECT_EQ(OK, StartRequest("b", HIGHEST)); | 681 EXPECT_EQ(OK, StartRequest("b", HIGHEST)); |
682 EXPECT_EQ(OK, StartRequest("b", MEDIUM)); | 682 EXPECT_EQ(OK, StartRequest("b", MEDIUM)); |
683 | 683 |
684 EXPECT_EQ(static_cast<int>(requests_.size()), | 684 EXPECT_EQ(static_cast<int>(requests_.size()), |
685 client_socket_factory_.allocation_count()); | 685 client_socket_factory_.allocation_count()); |
686 | 686 |
687 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", MEDIUM)); | 687 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", MEDIUM)); |
688 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); | 688 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
689 EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", HIGHEST)); | 689 EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", HIGHEST)); |
690 | 690 |
691 ReleaseAllConnections(KEEP_ALIVE); | 691 ReleaseAllConnections(NO_KEEP_ALIVE); |
692 | 692 |
693 // We're re-using one socket for group "a", and one for "b". | 693 EXPECT_EQ(static_cast<int>(requests_.size()), |
694 EXPECT_EQ(static_cast<int>(requests_.size()) - 2, | |
695 client_socket_factory_.allocation_count()); | 694 client_socket_factory_.allocation_count()); |
696 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); | 695 EXPECT_EQ(requests_.size() - kDefaultMaxSockets, completion_count_); |
697 | 696 |
698 // First 4 requests don't have to wait, and finish in order. | 697 // First 4 requests don't have to wait, and finish in order. |
699 EXPECT_EQ(1, GetOrderOfRequest(1)); | 698 EXPECT_EQ(1, GetOrderOfRequest(1)); |
700 EXPECT_EQ(2, GetOrderOfRequest(2)); | 699 EXPECT_EQ(2, GetOrderOfRequest(2)); |
701 EXPECT_EQ(3, GetOrderOfRequest(3)); | 700 EXPECT_EQ(3, GetOrderOfRequest(3)); |
702 EXPECT_EQ(4, GetOrderOfRequest(4)); | 701 EXPECT_EQ(4, GetOrderOfRequest(4)); |
703 | 702 |
704 // Request ("b", 7) has the highest priority, but we can't make new socket for | 703 // Request ("b", 7) has the highest priority, but we can't make new socket for |
(...skipping 24 matching lines...) Expand all Loading... |
729 // actually become pending until 2ms after they have been created. In order | 728 // actually become pending until 2ms after they have been created. In order |
730 // to flush all tasks, we need to wait so that we know there are no | 729 // to flush all tasks, we need to wait so that we know there are no |
731 // soon-to-be-pending tasks waiting. | 730 // soon-to-be-pending tasks waiting. |
732 PlatformThread::Sleep(10); | 731 PlatformThread::Sleep(10); |
733 MessageLoop::current()->RunAllPending(); | 732 MessageLoop::current()->RunAllPending(); |
734 | 733 |
735 // The next synchronous request should wait for its turn. | 734 // The next synchronous request should wait for its turn. |
736 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | 735 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
737 EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", kDefaultPriority)); | 736 EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", kDefaultPriority)); |
738 | 737 |
739 ReleaseAllConnections(KEEP_ALIVE); | 738 ReleaseAllConnections(NO_KEEP_ALIVE); |
740 | 739 |
741 EXPECT_EQ(static_cast<int>(requests_.size()), | 740 EXPECT_EQ(static_cast<int>(requests_.size()), |
742 client_socket_factory_.allocation_count()); | 741 client_socket_factory_.allocation_count()); |
743 | 742 |
744 EXPECT_EQ(1, GetOrderOfRequest(1)); | 743 EXPECT_EQ(1, GetOrderOfRequest(1)); |
745 EXPECT_EQ(2, GetOrderOfRequest(2)); | 744 EXPECT_EQ(2, GetOrderOfRequest(2)); |
746 EXPECT_EQ(3, GetOrderOfRequest(3)); | 745 EXPECT_EQ(3, GetOrderOfRequest(3)); |
747 EXPECT_EQ(4, GetOrderOfRequest(4)); | 746 EXPECT_EQ(4, GetOrderOfRequest(4)); |
748 EXPECT_EQ(5, GetOrderOfRequest(5)); | 747 EXPECT_EQ(5, GetOrderOfRequest(5)); |
749 | 748 |
(...skipping 20 matching lines...) Expand all Loading... |
770 EXPECT_FALSE(pool_->base()->may_have_stalled_group()); | 769 EXPECT_FALSE(pool_->base()->may_have_stalled_group()); |
771 | 770 |
772 // Request one more socket while we are at the maximum sockets limit. | 771 // Request one more socket while we are at the maximum sockets limit. |
773 // This should flip the may_have_stalled_group flag. | 772 // This should flip the may_have_stalled_group flag. |
774 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kDefaultPriority)); | 773 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kDefaultPriority)); |
775 EXPECT_TRUE(pool_->base()->may_have_stalled_group()); | 774 EXPECT_TRUE(pool_->base()->may_have_stalled_group()); |
776 | 775 |
777 // After releasing first connection for "a", we're still at the | 776 // After releasing first connection for "a", we're still at the |
778 // maximum sockets limit, but every group's pending queue is empty, | 777 // maximum sockets limit, but every group's pending queue is empty, |
779 // so we reset the flag. | 778 // so we reset the flag. |
780 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE)); | 779 EXPECT_TRUE(ReleaseOneConnection(NO_KEEP_ALIVE)); |
781 EXPECT_FALSE(pool_->base()->may_have_stalled_group()); | 780 EXPECT_FALSE(pool_->base()->may_have_stalled_group()); |
782 | 781 |
783 // Requesting additional socket while at the total limit should | 782 // Requesting additional socket while at the total limit should |
784 // flip the flag back to "on". | 783 // flip the flag back to "on". |
785 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kDefaultPriority)); | 784 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kDefaultPriority)); |
786 EXPECT_TRUE(pool_->base()->may_have_stalled_group()); | 785 EXPECT_TRUE(pool_->base()->may_have_stalled_group()); |
787 | 786 |
788 // We'll request one more socket to verify that we don't reset the flag | 787 // We'll request one more socket to verify that we don't reset the flag |
789 // too eagerly. | 788 // too eagerly. |
790 EXPECT_EQ(ERR_IO_PENDING, StartRequest("d", kDefaultPriority)); | 789 EXPECT_EQ(ERR_IO_PENDING, StartRequest("d", kDefaultPriority)); |
791 EXPECT_TRUE(pool_->base()->may_have_stalled_group()); | 790 EXPECT_TRUE(pool_->base()->may_have_stalled_group()); |
792 | 791 |
793 // We're at the maximum socket limit, and still have one request pending | 792 // We're at the maximum socket limit, and still have one request pending |
794 // for "d". Flag should be "on". | 793 // for "d". Flag should be "on". |
795 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE)); | 794 EXPECT_TRUE(ReleaseOneConnection(NO_KEEP_ALIVE)); |
796 EXPECT_TRUE(pool_->base()->may_have_stalled_group()); | 795 EXPECT_TRUE(pool_->base()->may_have_stalled_group()); |
797 | 796 |
798 // Now every group's pending queue should be empty again. | 797 // Now every group's pending queue should be empty again. |
799 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE)); | 798 EXPECT_TRUE(ReleaseOneConnection(NO_KEEP_ALIVE)); |
800 EXPECT_FALSE(pool_->base()->may_have_stalled_group()); | 799 EXPECT_FALSE(pool_->base()->may_have_stalled_group()); |
801 | 800 |
802 ReleaseAllConnections(KEEP_ALIVE); | 801 ReleaseAllConnections(NO_KEEP_ALIVE); |
803 EXPECT_FALSE(pool_->base()->may_have_stalled_group()); | 802 EXPECT_FALSE(pool_->base()->may_have_stalled_group()); |
804 } | 803 } |
805 | 804 |
806 TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { | 805 TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
807 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); | 806 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
808 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | 807 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
809 | 808 |
810 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 809 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
811 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 810 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
812 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 811 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
848 BoundNetLog())); | 847 BoundNetLog())); |
849 } | 848 } |
850 | 849 |
851 // One will be stalled, cancel all the handles now. | 850 // One will be stalled, cancel all the handles now. |
852 // This should hit the OnAvailableSocketSlot() code where we previously had | 851 // This should hit the OnAvailableSocketSlot() code where we previously had |
853 // stalled groups, but no longer have any. | 852 // stalled groups, but no longer have any. |
854 for (size_t i = 0; i < arraysize(handles); ++i) | 853 for (size_t i = 0; i < arraysize(handles); ++i) |
855 handles[i].Reset(); | 854 handles[i].Reset(); |
856 } | 855 } |
857 | 856 |
| 857 TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimit) { |
| 858 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 859 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 860 |
| 861 for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 862 ClientSocketHandle handle; |
| 863 TestCompletionCallback callback; |
| 864 EXPECT_EQ(OK, |
| 865 InitHandle(&handle, IntToString(i), kDefaultPriority, &callback, |
| 866 pool_, BoundNetLog())); |
| 867 } |
| 868 |
| 869 // Stall a group |
| 870 ClientSocketHandle handle; |
| 871 TestCompletionCallback callback; |
| 872 EXPECT_EQ(ERR_IO_PENDING, |
| 873 InitHandle(&handle, "foo", kDefaultPriority, &callback, pool_, |
| 874 BoundNetLog())); |
| 875 |
| 876 // Cancel the stalled request. |
| 877 handle.Reset(); |
| 878 |
| 879 // Flush all the DoReleaseSocket tasks. |
| 880 MessageLoop::current()->RunAllPending(); |
| 881 |
| 882 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 883 EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
| 884 |
| 885 for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 886 ClientSocketHandle handle; |
| 887 TestCompletionCallback callback; |
| 888 EXPECT_EQ(OK, |
| 889 InitHandle(&handle, StringPrintf("Take 2: %d", i), |
| 890 kDefaultPriority, &callback, pool_, BoundNetLog())); |
| 891 } |
| 892 |
| 893 EXPECT_EQ(2 * kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 894 EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 895 |
| 896 // Before the next round of DoReleaseSocket tasks run, we will hit the |
| 897 // socket limit. |
| 898 |
| 899 EXPECT_EQ(ERR_IO_PENDING, |
| 900 InitHandle(&handle, "foo", kDefaultPriority, &callback, pool_, |
| 901 BoundNetLog())); |
| 902 |
| 903 // But if we wait for it, the released idle sockets will be closed in |
| 904 // preference of the waiting request. |
| 905 |
| 906 EXPECT_EQ(OK, callback.WaitForResult()); |
| 907 } |
| 908 |
| 909 // Regression test for http://crbug.com/40952. |
| 910 TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
| 911 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 912 pool_->EnableBackupJobs(); |
| 913 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 914 |
| 915 for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 916 ClientSocketHandle handle; |
| 917 TestCompletionCallback callback; |
| 918 EXPECT_EQ(OK, |
| 919 InitHandle(&handle, IntToString(i), kDefaultPriority, &callback, |
| 920 pool_, BoundNetLog())); |
| 921 } |
| 922 |
| 923 // Flush all the DoReleaseSocket tasks. |
| 924 MessageLoop::current()->RunAllPending(); |
| 925 |
| 926 // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 927 // reuse a socket. |
| 928 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 929 ClientSocketHandle handle; |
| 930 TestCompletionCallback callback; |
| 931 |
| 932 // "0" is special here, since it should be the first entry in the sorted map, |
| 933 // which is the one which we would close an idle socket for. We shouldn't |
| 934 // close an idle socket though, since we should reuse the idle socket. |
| 935 EXPECT_EQ( |
| 936 OK, |
| 937 InitHandle( |
| 938 &handle, "0", kDefaultPriority, &callback, pool_, BoundNetLog())); |
| 939 |
| 940 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 941 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 942 } |
| 943 |
858 TEST_F(ClientSocketPoolBaseTest, PendingRequests) { | 944 TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
859 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | 945 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
860 | 946 |
861 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 947 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
862 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 948 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
863 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); | 949 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
864 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); | 950 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
865 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); | 951 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
866 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); | 952 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
867 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); | 953 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1174 EXPECT_TRUE(pool_->base()->may_have_stalled_group()); | 1260 EXPECT_TRUE(pool_->base()->may_have_stalled_group()); |
1175 | 1261 |
1176 // Release the first two sockets from "a", which will make room | 1262 // Release the first two sockets from "a", which will make room |
1177 // for requests from "c". After that "a" will have no active sockets | 1263 // for requests from "c". After that "a" will have no active sockets |
1178 // and one pending request. | 1264 // and one pending request. |
1179 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE)); | 1265 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE)); |
1180 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE)); | 1266 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE)); |
1181 | 1267 |
1182 // Closing idle sockets should not get us into trouble, but in the bug | 1268 // Closing idle sockets should not get us into trouble, but in the bug |
1183 // we were hitting a CHECK here. | 1269 // we were hitting a CHECK here. |
1184 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a")); | 1270 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
1185 pool_->CloseIdleSockets(); | 1271 pool_->CloseIdleSockets(); |
1186 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | |
1187 } | 1272 } |
1188 | 1273 |
1189 TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { | 1274 TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
1190 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | 1275 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
1191 | 1276 |
1192 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 1277 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
1193 TestSocketRequest req(&request_order_, &completion_count_); | 1278 TestSocketRequest req(&request_order_, &completion_count_); |
1194 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | 1279 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
1195 int rv = InitHandle(req.handle(), "a", LOWEST, &req, pool_, log.bound()); | 1280 int rv = InitHandle(req.handle(), "a", LOWEST, &req, pool_, log.bound()); |
1196 EXPECT_EQ(ERR_IO_PENDING, rv); | 1281 EXPECT_EQ(ERR_IO_PENDING, rv); |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1623 TestReleasingSocketRequest request(pool_.get()); | 1708 TestReleasingSocketRequest request(pool_.get()); |
1624 EXPECT_EQ(ERR_IO_PENDING, InitHandle(request.handle(), "a", kDefaultPriority, | 1709 EXPECT_EQ(ERR_IO_PENDING, InitHandle(request.handle(), "a", kDefaultPriority, |
1625 &request, pool_, BoundNetLog())); | 1710 &request, pool_, BoundNetLog())); |
1626 | 1711 |
1627 EXPECT_EQ(OK, request.WaitForResult()); | 1712 EXPECT_EQ(OK, request.WaitForResult()); |
1628 } | 1713 } |
1629 | 1714 |
1630 } // namespace | 1715 } // namespace |
1631 | 1716 |
1632 } // namespace net | 1717 } // namespace net |
OLD | NEW |