File: test_receive_buffer.cpp

package info (click to toggle)
libtorrent-rasterbar 2.0.11-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 18,304 kB
  • sloc: cpp: 190,670; python: 7,142; makefile: 1,374; ansic: 574; sh: 317; xml: 104
file content (258 lines) | stat: -rw-r--r-- 6,229 bytes parent folder | download | duplicates (3)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*

Copyright (c) 2016, Alden Torres
Copyright (c) 2016-2019, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/config.hpp"
#include "test.hpp"
#include "libtorrent/aux_/receive_buffer.hpp"

using namespace lt;
using lt::aux::receive_buffer;

TORRENT_TEST(recv_buffer_init)
{
	receive_buffer b;

	b.cut(0, 10);

	TEST_EQUAL(b.packet_size(), 10);
	TEST_EQUAL(b.packet_bytes_remaining(), 10);
	TEST_EQUAL(b.packet_finished(), false);
	TEST_EQUAL(b.pos(), 0);
	TEST_EQUAL(b.capacity(), 0);
}

TORRENT_TEST(recv_buffer_pos_at_end_false)
{
	receive_buffer b;

	b.cut(0, 1000);
	// allocate some space to receive into
	b.reserve(1000);

	b.received(1000);
	b.advance_pos(999);

	TEST_EQUAL(b.pos_at_end(), false);
}

TORRENT_TEST(recv_buffer_pos_at_end_true)
{
	receive_buffer b;
	b.cut(0, 1000);
	b.reserve(1000);
	b.reserve(1000);
	b.received(1000);
	b.advance_pos(1000);
	TEST_EQUAL(b.pos_at_end(), true);
}

TORRENT_TEST(recv_buffer_packet_finished)
{
	receive_buffer b;
	// packet_size = 10
	b.cut(0, 10);
	b.reserve(1000);
	b.reserve(1000);
	b.received(1000);

	for (int i = 0; i < 10; ++i)
	{
		TEST_EQUAL(b.packet_finished(), false);
		b.advance_pos(1);
	}
	TEST_EQUAL(b.packet_finished(), true);
}

TORRENT_TEST(recv_buffer_grow_floor)
{
	receive_buffer b;
	b.reset(1337);
	b.grow(100000);

	// the exact size depends on the OS allocator. Technically there's no upper
	// bound, but it's likely withint some reasonable size
	TEST_CHECK(b.capacity() >= 1337);
	TEST_CHECK(b.capacity() < 1337 + 1000);
}

TORRENT_TEST(recv_buffer_grow)
{
	receive_buffer b;
	b.reserve(200);
	b.grow(100000);
	// grow by 50%
	TEST_CHECK(b.capacity() >= 300);
	TEST_CHECK(b.capacity() < 300 + 500);
}

TORRENT_TEST(recv_buffer_grow_limit)
{
	receive_buffer b;
	b.reserve(2000);
	b.grow(2100);
	// grow by 50%, but capped by 2100 bytes
	TEST_CHECK(b.capacity() >= 2100);
	TEST_CHECK(b.capacity() < 2100 + 500);
	printf("capacity: %d\n", b.capacity());
}

TORRENT_TEST(recv_buffer_reserve_minimum_grow)
{
	receive_buffer b;
	b.reset(1337);
	b.reserve(20);

	// we only asked for 20 more bytes, but since the message size was set to
	// 1337, that's the minimum size to grow to
	TEST_CHECK(b.capacity() >= 1337);
	TEST_CHECK(b.capacity() < 1337 + 1000);
}

TORRENT_TEST(recv_buffer_reserve_grow)
{
	receive_buffer b;
	b.reserve(20);

	TEST_CHECK(b.capacity() >= 20);
	TEST_CHECK(b.capacity() < 20 + 500);
}

TORRENT_TEST(recv_buffer_reserve)
{
	receive_buffer b;
	auto range1 = b.reserve(100);

	int const capacity = b.capacity();

	b.reset(20);
	b.received(20);

	TEST_EQUAL(b.capacity(), capacity);

	auto range2 = b.reserve(50);

	TEST_EQUAL(b.capacity(), capacity);
	TEST_EQUAL(range1.begin() + 20, range2.begin());
	TEST_CHECK(range1.size() >= 20);
	TEST_CHECK(range2.size() >= 50);
}

TORRENT_TEST(receive_buffer_normalize)
{
	receive_buffer b;
	b.reset(16000);

	// receive one large packet, to allocate a large receive buffer
	for (int i = 0; i < 16; ++i)
	{
		b.reserve(1000);
		b.received(1000);
		b.normalize();
	}

	TEST_CHECK(b.capacity() >= 16000);
	int const start_capacity = b.capacity();

	// then receive lots of small packets. We should eventually re-allocate down
	// to a smaller buffer
	for (int i = 0; i < 15; ++i)
	{
		b.reset(160);
		b.reserve(160);
		b.received(160);
		b.normalize();
		printf("capacity: %d watermark: %d\n", b.capacity(), b.watermark());
	}

	TEST_CHECK(b.capacity() <= start_capacity / 2);
	printf("capacity: %d\n", b.capacity());
}

TORRENT_TEST(receive_buffer_max_receive)
{
	receive_buffer b;
	b.reset(2000);
	b.reserve(2000);
	b.received(2000);
	b.normalize();

	b.reset(20);
	int const max_receive = b.max_receive();
	TEST_CHECK(max_receive >= 2000);
	b.received(20);
	TEST_EQUAL(b.max_receive(), max_receive - 20);
}

TORRENT_TEST(receive_buffer_watermark)
{
	receive_buffer b;
	b.reset(0x4000);
	b.reserve(33500000);
	b.received(33500000);
	b.normalize();

	TEST_EQUAL(b.watermark(), 33500000);
}

#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)

TORRENT_TEST(recv_buffer_mutable_buffers)
{
	receive_buffer b;
	b.reserve(1100);
	b.cut(0, 100); // packet size = 100
	b.received(1100);
	int packet_transferred = b.advance_pos(1100);
	// this is just the first packet
	TEST_EQUAL(packet_transferred, 100);
	// the next packet is 1000, and we're done with the first 100 bytes now
	b.cut(100, 1000); // packet size = 1000
	packet_transferred = b.advance_pos(999);
	TEST_EQUAL(packet_transferred, 999);
	span<char> vec = b.mutable_buffer(999);

	// previous packet
	//   |
	//   v   buffer
	// - - - -----------------------
	//       ^
	//       |
	// m_recv_start

	//       |----------------------| 1000 packet size
	//       |---------------------|  999 buffer

	TEST_EQUAL(vec.size(), 999);
}

#endif