-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtestbufferstream.nim
244 lines (187 loc) · 6.63 KB
/
testbufferstream.nim
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
{.used.}
# Nim-Libp2p
# Copyright (c) 2023-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import chronos, stew/byteutils
import ../libp2p/stream/bufferstream, ../libp2p/stream/lpstream, ../libp2p/errors
import ./helpers
suite "BufferStream":
teardown:
checkTrackers()
asyncTest "push data to buffer":
let buff = BufferStream.new()
check buff.len == 0
var data = "12345"
await buff.pushData(data.toBytes())
check buff.len == 5
await buff.close()
asyncTest "push and wait":
let buff = BufferStream.new()
check buff.len == 0
let fut0 = buff.pushData("1234".toBytes())
let fut1 = buff.pushData("5".toBytes())
check buff.len == 4 # the second write should not be visible yet
var data: array[1, byte]
check:
1 == await buff.readOnce(addr data[0], data.len)
check ['1'] == string.fromBytes(data)
await fut0
await fut1
check buff.len == 4
await buff.close()
asyncTest "read with size":
let buff = BufferStream.new()
check buff.len == 0
await buff.pushData("12345".toBytes())
var data: array[3, byte]
await buff.readExactly(addr data[0], data.len)
check ['1', '2', '3'] == string.fromBytes(data)
await buff.close()
asyncTest "readExactly":
let buff = BufferStream.new()
check buff.len == 0
await buff.pushData("12345".toBytes())
check buff.len == 5
var data: array[2, byte]
await buff.readExactly(addr data[0], data.len)
check string.fromBytes(data) == ['1', '2']
await buff.close()
asyncTest "readExactly raises":
let buff = BufferStream.new()
check buff.len == 0
await buff.pushData("123".toBytes())
var data: array[5, byte]
var readFut = buff.readExactly(addr data[0], data.len)
await buff.close()
expect LPStreamIncompleteError:
await readFut
asyncTest "readOnce":
let buff = BufferStream.new()
check buff.len == 0
var data: array[3, byte]
let readFut = buff.readOnce(addr data[0], data.len)
await buff.pushData("123".toBytes())
check buff.len == 3
check (await readFut) == 3
check string.fromBytes(data) == ['1', '2', '3']
await buff.close()
asyncTest "reads should happen in order":
let buff = BufferStream.new()
check buff.len == 0
proc writer1() {.async.} =
await buff.pushData("Msg 1".toBytes())
await buff.pushData("Msg 2".toBytes())
await buff.pushData("Msg 3".toBytes())
let writerFut1 = writer1()
var data: array[5, byte]
await buff.readExactly(addr data[0], data.len)
check string.fromBytes(data) == "Msg 1"
await buff.readExactly(addr data[0], data.len)
check string.fromBytes(data) == "Msg 2"
await buff.readExactly(addr data[0], data.len)
check string.fromBytes(data) == "Msg 3"
await writerFut1
proc writer2() {.async.} =
await buff.pushData("Msg 4".toBytes())
await buff.pushData("Msg 5".toBytes())
await buff.pushData("Msg 6".toBytes())
let writerFut2 = writer2()
await buff.readExactly(addr data[0], data.len)
check string.fromBytes(data) == "Msg 4"
await buff.readExactly(addr data[0], data.len)
check string.fromBytes(data) == "Msg 5"
await buff.readExactly(addr data[0], data.len)
check string.fromBytes(data) == "Msg 6"
await buff.close()
await writerFut2
asyncTest "small reads":
let buff = BufferStream.new()
check buff.len == 0
var str: string
proc writer() {.async.} =
for i in 0 ..< 10:
await buff.pushData("123".toBytes())
str &= "123"
await buff.close() # all data should still be read after close
var str2: string
proc reader() {.async.} =
var data: array[2, byte]
expect LPStreamEOFError:
while true:
let x = await buff.readOnce(addr data[0], data.len)
str2 &= string.fromBytes(data[0 ..< x])
await allFuturesThrowing(allFinished(reader(), writer()))
check str == str2
await buff.close()
asyncTest "read all data after eof":
let buff = BufferStream.new()
check buff.len == 0
await buff.pushData("12345".toBytes())
var data: array[2, byte]
check:
(await buff.readOnce(addr data[0], data.len)) == 2
await buff.pushEof()
check:
not buff.atEof()
(await buff.readOnce(addr data[0], data.len)) == 2
not buff.atEof()
(await buff.readOnce(addr data[0], data.len)) == 1
buff.atEof()
# exactly one 0-byte read
(await buff.readOnce(addr data[0], data.len)) == 0
expect LPStreamEOFError:
discard (await buff.readOnce(addr data[0], data.len))
await buff.close() # all data should still be read after close
asyncTest "read more data after eof":
let buff = BufferStream.new()
check buff.len == 0
await buff.pushData("12345".toBytes())
var data: array[5, byte]
check:
(await buff.readOnce(addr data[0], 1)) == 1
# 4 bytes in readBuf
await buff.pushEof()
check:
not buff.atEof()
(await buff.readOnce(addr data[0], 1)) == 1
# 3 bytes in readBuf, eof marker processed
not buff.atEof()
(await buff.readOnce(addr data[0], data.len)) == 3 # 0 bytes in readBuf
buff.atEof()
# exactly one 0-byte read
(await buff.readOnce(addr data[0], data.len)) == 0
expect LPStreamEOFError:
discard (await buff.readOnce(addr data[0], data.len))
await buff.close() # all data should still be read after close
asyncTest "shouldn't get stuck on close":
var stream = BufferStream.new()
var
fut = stream.pushData(toBytes("hello"))
fut2 = stream.pushData(toBytes("again"))
await stream.close()
# Both writes should be completed on close (technically, the should maybe
# be cancelled, at least the second one...
check await fut.withTimeout(100.milliseconds)
check await fut2.withTimeout(100.milliseconds)
await stream.close()
asyncTest "no push after close":
var stream = BufferStream.new()
await stream.pushData("123".toBytes())
var data: array[3, byte]
await stream.readExactly(addr data[0], data.len)
await stream.close()
expect LPStreamEOFError:
await stream.pushData("123".toBytes())
asyncTest "no concurrent pushes":
var stream = BufferStream.new()
await stream.pushData("123".toBytes())
let push = stream.pushData("123".toBytes())
expect AssertionDefect:
await stream.pushData("123".toBytes())
await stream.closeWithEOF()
await push