blob: 37693b9acc0fc9297ca7b97e5c99dc3f79fd0674 [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/run_loop.h"
#include "net/test/embedded_test_server/embedded_test_server_connection_listener.h"
namespace net {
class StreamSocket;
namespace test_server {
// Waits for a specified number of connection attempts to be seen.
class SimpleConnectionListener : public EmbeddedTestServerConnectionListener {
enum AllowAdditionalConnections {
// Add an expect failure if more than the specified number of connections
// are seen.
// Silently ignores extra connection attempts.
// A connection listener that waits for the specified number of total
// connections when WaitForConnections() is called. Must be created on a
// thread with a SingleThreadedTaskRunner.
int expected_connections,
AllowAdditionalConnections allow_additional_connections);
SimpleConnectionListener(const SimpleConnectionListener&) = delete;
SimpleConnectionListener& operator=(const SimpleConnectionListener&) = delete;
// Must be torn down only after the EmbeddedTestServer it's attached to is
// shut down.
~SimpleConnectionListener() override;
std::unique_ptr<StreamSocket> AcceptedSocket(
std::unique_ptr<StreamSocket> socket) override;
void ReadFromSocket(const StreamSocket& socket, int rv) override;
void OnResponseCompletedSuccessfully(
std::unique_ptr<StreamSocket> socket) override;
// Wait until the expected number of connections have been seen.
void WaitForConnections();
int seen_connections_ = 0;
const int expected_connections_;
const AllowAdditionalConnections allow_additional_connections_;
base::RunLoop run_loop_;
} // namespace test_server
} // namespace net