/proc/self/cwd/test/integration/integration_tcp_client.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <chrono> |
4 | | #include <cstddef> |
5 | | #include <cstdint> |
6 | | #include <memory> |
7 | | #include <string> |
8 | | |
9 | | #include "envoy/event/dispatcher.h" |
10 | | #include "envoy/network/address.h" |
11 | | #include "envoy/network/connection.h" |
12 | | #include "envoy/network/listen_socket.h" |
13 | | #include "envoy/network/socket.h" |
14 | | |
15 | | #include "test/integration/utility.h" |
16 | | #include "test/mocks/buffer/mocks.h" |
17 | | #include "test/test_common/utility.h" |
18 | | |
19 | | #include "absl/types/optional.h" |
20 | | #include "gtest/gtest.h" |
21 | | #include "gtest/gtest_pred_impl.h" |
22 | | |
23 | | namespace Envoy { |
24 | | /** |
25 | | * TCP client used during integration testing. |
26 | | */ |
27 | | class IntegrationTcpClient { |
28 | | public: |
29 | | IntegrationTcpClient(Event::Dispatcher& dispatcher, MockBufferFactory& factory, uint32_t port, |
30 | | Network::Address::IpVersion version, bool enable_half_close, |
31 | | const Network::ConnectionSocket::OptionsSharedPtr& options, |
32 | | Network::Address::InstanceConstSharedPtr source_address = nullptr, |
33 | | absl::string_view destination_address = ""); |
34 | | |
35 | | void close(); |
36 | | void close(Network::ConnectionCloseType close_type); |
37 | | void waitForData(const std::string& data, bool exact_match = true); |
38 | | // wait for at least `length` bytes to be received |
39 | | ABSL_MUST_USE_RESULT AssertionResult |
40 | | waitForData(size_t length, std::chrono::milliseconds timeout = TestUtility::DefaultTimeout); |
41 | | void waitForDisconnect(bool ignore_spurious_events = false); |
42 | | void waitForHalfClose(bool ignore_spurious_events = false); |
43 | | void waitForHalfClose(std::chrono::milliseconds timeout, bool ignore_spurious_events = false); |
44 | | void readDisable(bool disabled); |
45 | | ABSL_MUST_USE_RESULT AssertionResult |
46 | | write(const std::string& data, bool end_stream = false, bool verify = true, |
47 | | std::chrono::milliseconds timeout = TestUtility::DefaultTimeout); |
48 | 0 | const std::string& data() { return payload_reader_->data(); } |
49 | 21.7k | bool connected() const { return !disconnected_; } |
50 | | // clear up to the `count` number of bytes of received data |
51 | 0 | void clearData(size_t count = std::string::npos) { payload_reader_->clearData(count); } |
52 | 0 | Network::Connection* connection() const { return connection_.get(); } |
53 | | |
54 | | private: |
55 | | struct ConnectionCallbacks : public Network::ConnectionCallbacks { |
56 | 2.11k | ConnectionCallbacks(IntegrationTcpClient& parent) : parent_(parent) {} |
57 | | |
58 | | // Network::ConnectionCallbacks |
59 | | void onEvent(Network::ConnectionEvent event) override; |
60 | 0 | void onAboveWriteBufferHighWatermark() override {} |
61 | 0 | void onBelowWriteBufferLowWatermark() override {} |
62 | | |
63 | | IntegrationTcpClient& parent_; |
64 | | }; |
65 | | |
66 | | std::shared_ptr<WaitForPayloadReader> payload_reader_; |
67 | | std::shared_ptr<ConnectionCallbacks> callbacks_; |
68 | | Network::ClientConnectionPtr connection_; |
69 | | bool disconnected_{}; |
70 | | MockWatermarkBuffer* client_write_buffer_; |
71 | | }; |
72 | | |
73 | | using IntegrationTcpClientPtr = std::unique_ptr<IntegrationTcpClient>; |
74 | | |
75 | | } // namespace Envoy |