|
3 | 3 | #include "commands.hpp" |
4 | 4 | #include <cstdio> |
5 | 5 | #include <numeric> |
| 6 | +#include <random> |
6 | 7 |
|
7 | 8 | TEST(Send_Tests, Send_ValidValues) { |
8 | 9 | using namespace imkcpp; |
@@ -132,6 +133,87 @@ TEST(Send_Tests, Send_ValidValues) { |
132 | 133 | std::cout << "Average result: " << average_duration.count() << " microseconds" << std::endl; |
133 | 134 | } |
134 | 135 |
|
| 136 | +TEST(Send_Tests, Send_LossyScenario) { |
| 137 | + using namespace imkcpp; |
| 138 | + |
| 139 | + constexpr float loss_ratio = 0.5f; |
| 140 | + |
| 141 | + constexpr size_t max_segment_size = MTU_TO_MSS<constants::IKCP_MTU_DEF>(); |
| 142 | + constexpr size_t size = max_segment_size * 120; |
| 143 | + |
| 144 | + ImKcpp<constants::IKCP_MTU_DEF> kcp_output(0); |
| 145 | + kcp_output.set_send_window(2048); |
| 146 | + kcp_output.set_receive_window(2048); |
| 147 | + kcp_output.set_interval(10); |
| 148 | + kcp_output.set_congestion_window_enabled(false); |
| 149 | + kcp_output.update(0, [](std::span<const std::byte>) { }); |
| 150 | + |
| 151 | + ImKcpp<constants::IKCP_MTU_DEF> kcp_input(0); |
| 152 | + kcp_input.set_send_window(2048); |
| 153 | + kcp_input.set_receive_window(2048); |
| 154 | + kcp_input.set_interval(10); |
| 155 | + kcp_input.set_congestion_window_enabled(false); |
| 156 | + kcp_input.update(0, [](std::span<const std::byte>) { }); |
| 157 | + |
| 158 | + std::vector<std::byte> send_buffer(size); |
| 159 | + for (u32 j = 0; j < size; ++j) { |
| 160 | + send_buffer[j] = static_cast<std::byte>(j); |
| 161 | + } |
| 162 | + |
| 163 | + std::vector<std::byte> recv_buffer(size); |
| 164 | + |
| 165 | + auto send_result = kcp_output.send(send_buffer); |
| 166 | + ASSERT_TRUE(send_result.has_value()) << err_to_str(send_result.error()); |
| 167 | + ASSERT_EQ(send_result.value(), size); |
| 168 | + |
| 169 | + std::random_device rd; |
| 170 | + std::mt19937 gen(rd()); |
| 171 | + std::uniform_real_distribution dis(0.0f, 1.0f); |
| 172 | + |
| 173 | + auto should_drop = [&]() -> bool { |
| 174 | + const auto random = dis(gen); |
| 175 | + return random < loss_ratio; |
| 176 | + }; |
| 177 | + |
| 178 | + auto output_to_input = [&](const std::span<const std::byte> data) { |
| 179 | + if (should_drop()) { |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + kcp_input.input(data); |
| 184 | + }; |
| 185 | + |
| 186 | + auto input_to_output = [&](const std::span<const std::byte> data) { |
| 187 | + if (should_drop()) { |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + kcp_output.input(data); |
| 192 | + }; |
| 193 | + |
| 194 | + size_t update_idx = 0; |
| 195 | + |
| 196 | + while (kcp_output.get_state() == State::Alive && kcp_input.peek_size() != size) { |
| 197 | + const auto now = static_cast<u32>(update_idx * 10); |
| 198 | + |
| 199 | + kcp_output.update(now, output_to_input); |
| 200 | + kcp_input.update(now, input_to_output); |
| 201 | + |
| 202 | + ++update_idx; |
| 203 | + |
| 204 | + ASSERT_LT(update_idx, 10000); |
| 205 | + } |
| 206 | + |
| 207 | + ASSERT_EQ(kcp_output.get_state(), State::Alive); |
| 208 | + |
| 209 | + auto recv_result = kcp_input.recv(recv_buffer); |
| 210 | + ASSERT_TRUE(recv_result.has_value()) << err_to_str(recv_result.error()); |
| 211 | + |
| 212 | + for (size_t j = 0; j < size; ++j) { |
| 213 | + EXPECT_EQ(send_buffer.at(j), recv_buffer.at(j)); |
| 214 | + } |
| 215 | +} |
| 216 | + |
135 | 217 | TEST(Send_Tests, Send_FragmentedValidValues) { |
136 | 218 | using namespace imkcpp; |
137 | 219 |
|
|
0 commit comments