| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/guid.h" | 8 #include "base/guid.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 base::Thread thread_; | 135 base::Thread thread_; |
| 136 }; | 136 }; |
| 137 | 137 |
| 138 TEST_F(PortServerTest, Reserve) { | 138 TEST_F(PortServerTest, Reserve) { |
| 139 std::string path = GenerateRandomPath(); | 139 std::string path = GenerateRandomPath(); |
| 140 PortServer server(path); | 140 PortServer server(path); |
| 141 | 141 |
| 142 std::string request; | 142 std::string request; |
| 143 RunServer(path, "12345\n", &request); | 143 RunServer(path, "12345\n", &request); |
| 144 | 144 |
| 145 int port = 0; | 145 uint16 port = 0; |
| 146 scoped_ptr<PortReservation> reservation; | 146 scoped_ptr<PortReservation> reservation; |
| 147 Status status = server.ReservePort(&port, &reservation); | 147 Status status = server.ReservePort(&port, &reservation); |
| 148 ASSERT_EQ(kOk, status.code()) << status.message(); | 148 ASSERT_EQ(kOk, status.code()) << status.message(); |
| 149 ASSERT_EQ(port, 12345); | 149 ASSERT_EQ(12345u, port); |
| 150 } | 150 } |
| 151 | 151 |
| 152 TEST_F(PortServerTest, ReserveResetReserve) { | 152 TEST_F(PortServerTest, ReserveResetReserve) { |
| 153 std::string path = GenerateRandomPath(); | 153 std::string path = GenerateRandomPath(); |
| 154 PortServer server(path); | 154 PortServer server(path); |
| 155 | 155 |
| 156 std::string request; | 156 std::string request; |
| 157 RunServer(path, "12345\n", &request); | 157 RunServer(path, "12345\n", &request); |
| 158 | 158 |
| 159 int port = 0; | 159 uint16 port = 0; |
| 160 scoped_ptr<PortReservation> reservation; | 160 scoped_ptr<PortReservation> reservation; |
| 161 Status status = server.ReservePort(&port, &reservation); | 161 Status status = server.ReservePort(&port, &reservation); |
| 162 ASSERT_EQ(kOk, status.code()) << status.message(); | 162 ASSERT_EQ(kOk, status.code()) << status.message(); |
| 163 ASSERT_EQ(port, 12345); | 163 ASSERT_EQ(12345u, port); |
| 164 | 164 |
| 165 reservation.reset(); | 165 reservation.reset(); |
| 166 status = server.ReservePort(&port, &reservation); | 166 status = server.ReservePort(&port, &reservation); |
| 167 ASSERT_EQ(kOk, status.code()) << status.message(); | 167 ASSERT_EQ(kOk, status.code()) << status.message(); |
| 168 ASSERT_EQ(port, 12345); | 168 ASSERT_EQ(12345u, port); |
| 169 } | 169 } |
| 170 | 170 |
| 171 TEST_F(PortServerTest, ReserveReserve) { | 171 TEST_F(PortServerTest, ReserveReserve) { |
| 172 std::string path = GenerateRandomPath(); | 172 std::string path = GenerateRandomPath(); |
| 173 PortServer server(path); | 173 PortServer server(path); |
| 174 | 174 |
| 175 std::string request; | 175 std::string request; |
| 176 RunServer(path, "12345\n", &request); | 176 RunServer(path, "12345\n", &request); |
| 177 | 177 |
| 178 int port = 0; | 178 uint16 port = 0; |
| 179 scoped_ptr<PortReservation> reservation; | 179 scoped_ptr<PortReservation> reservation; |
| 180 Status status = server.ReservePort(&port, &reservation); | 180 Status status = server.ReservePort(&port, &reservation); |
| 181 ASSERT_EQ(kOk, status.code()) << status.message(); | 181 ASSERT_EQ(kOk, status.code()) << status.message(); |
| 182 ASSERT_EQ(port, 12345); | 182 ASSERT_EQ(12345u, port); |
| 183 | 183 |
| 184 RunServer(path, "12346\n", &request); | 184 RunServer(path, "12346\n", &request); |
| 185 status = server.ReservePort(&port, &reservation); | 185 status = server.ReservePort(&port, &reservation); |
| 186 ASSERT_EQ(kOk, status.code()) << status.message(); | 186 ASSERT_EQ(kOk, status.code()) << status.message(); |
| 187 ASSERT_EQ(port, 12346); | 187 ASSERT_EQ(12345u, port); |
| 188 } | 188 } |
| 189 #endif | 189 #endif |
| 190 | 190 |
| 191 TEST(PortManagerTest, ReservePort) { | 191 TEST(PortManagerTest, ReservePort) { |
| 192 PortManager mgr(15000, 16000); | 192 PortManager mgr(15000, 16000); |
| 193 int port = 0; | 193 uint16 port = 0; |
| 194 scoped_ptr<PortReservation> reservation; | 194 scoped_ptr<PortReservation> reservation; |
| 195 Status status = mgr.ReservePort(&port, &reservation); | 195 Status status = mgr.ReservePort(&port, &reservation); |
| 196 ASSERT_EQ(kOk, status.code()) << status.message(); | 196 ASSERT_EQ(kOk, status.code()) << status.message(); |
| 197 | 197 |
| 198 ASSERT_GE(port, 15000); | 198 ASSERT_GE(port, 15000); |
| 199 ASSERT_LE(port, 16000); | 199 ASSERT_LE(port, 16000); |
| 200 ASSERT_TRUE(reservation); | 200 ASSERT_TRUE(reservation); |
| 201 } | 201 } |
| 202 | 202 |
| 203 TEST(PortManagerTest, ReservePortFromPool) { | 203 TEST(PortManagerTest, ReservePortFromPool) { |
| 204 PortManager mgr(15000, 16000); | 204 PortManager mgr(15000, 16000); |
| 205 int first_port = 0, port = 1; | 205 uint16 first_port = 0, port = 1; |
| 206 for (int i = 0; i < 10; i++) { | 206 for (int i = 0; i < 10; i++) { |
| 207 scoped_ptr<PortReservation> reservation; | 207 scoped_ptr<PortReservation> reservation; |
| 208 Status status = mgr.ReservePortFromPool(&port, &reservation); | 208 Status status = mgr.ReservePortFromPool(&port, &reservation); |
| 209 ASSERT_EQ(kOk, status.code()) << status.message(); | 209 ASSERT_EQ(kOk, status.code()) << status.message(); |
| 210 ASSERT_TRUE(reservation); | 210 ASSERT_TRUE(reservation); |
| 211 ASSERT_GE(port, 15000); | 211 ASSERT_GE(port, 15000); |
| 212 ASSERT_LE(port, 16000); | 212 ASSERT_LE(port, 16000); |
| 213 if (i == 0) | 213 if (i == 0) |
| 214 first_port = port; | 214 first_port = port; |
| 215 ASSERT_EQ(port, first_port); | 215 ASSERT_EQ(port, first_port); |
| 216 } | 216 } |
| 217 } | 217 } |
| OLD | NEW |