summaryrefslogtreecommitdiff
path: root/chromium/net/test/embedded_test_server/simple_connection_listener.cc
blob: cb36d760e732e147091bf212b83ae660795a370f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// 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 "net/test/embedded_test_server/simple_connection_listener.h"

#include "base/location.h"
#include "base/sequenced_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {
namespace test_server {

SimpleConnectionListener::SimpleConnectionListener(
    int expected_connections,
    AllowAdditionalConnections allow_additional_connections)
    : expected_connections_(expected_connections),
      allow_additional_connections_(allow_additional_connections),
      run_loop_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}

SimpleConnectionListener::~SimpleConnectionListener() = default;

void SimpleConnectionListener::AcceptedSocket(const StreamSocket& socket) {
  ++seen_connections_;
  if (allow_additional_connections_ != ALLOW_ADDITIONAL_CONNECTIONS)
    EXPECT_LE(seen_connections_, expected_connections_);
  if (seen_connections_ == expected_connections_)
    run_loop_task_runner_->PostTask(FROM_HERE, run_loop_.QuitClosure());
}

void SimpleConnectionListener::ReadFromSocket(const StreamSocket& socket,
                                              int rv) {}

void SimpleConnectionListener::WaitForConnections() {
  EXPECT_TRUE(run_loop_task_runner_->RunsTasksInCurrentSequence());
  run_loop_.Run();
}

}  // namespace test_server
}  // namespace net