@@ -31,32 +31,37 @@ impl PayloadQueuer for OutboundQueue {
31
31
}
32
32
33
33
impl SocketDescriptorFlusher for OutboundQueue {
34
- fn try_flush_one ( & mut self , descriptor : & mut impl SocketDescriptor ) -> bool {
34
+ fn try_flush ( & mut self , descriptor : & mut impl SocketDescriptor ) -> bool {
35
35
// Exit early if a previous full write failed and haven't heard that there may be more
36
36
// room available
37
37
if self . blocked {
38
38
return false ;
39
39
}
40
40
41
- let full_write_succeeded = match self . buffer . front ( ) {
42
- None => true ,
43
- Some ( next_buff) => {
44
- let should_be_reading = self . buffer . len ( ) < self . soft_limit ;
45
- let pending = & next_buff[ self . buffer_first_msg_offset ..] ;
46
- let data_sent = descriptor. send_data ( pending, should_be_reading) ;
47
- self . buffer_first_msg_offset += data_sent;
48
- self . buffer_first_msg_offset == next_buff. len ( )
41
+ loop {
42
+ if self . buffer . is_empty ( ) {
43
+ return true ;
49
44
}
50
- } ;
51
45
52
- if full_write_succeeded {
53
- self . buffer_first_msg_offset = 0 ;
54
- self . buffer . pop_front ( ) ;
55
- } else {
56
- self . blocked = true ;
46
+ let full_write_succeeded = match self . buffer . front ( ) {
47
+ None => true ,
48
+ Some ( next_buff) => {
49
+ let should_be_reading = self . buffer . len ( ) < self . soft_limit ;
50
+ let pending = & next_buff[ self . buffer_first_msg_offset ..] ;
51
+ let data_sent = descriptor. send_data ( pending, should_be_reading) ;
52
+ self . buffer_first_msg_offset += data_sent;
53
+ self . buffer_first_msg_offset == next_buff. len ( )
54
+ }
55
+ } ;
56
+
57
+ if full_write_succeeded {
58
+ self . buffer_first_msg_offset = 0 ;
59
+ self . buffer . pop_front ( ) ;
60
+ } else {
61
+ self . blocked = true ;
62
+ return false ;
63
+ }
57
64
}
58
-
59
- full_write_succeeded
60
65
}
61
66
62
67
fn unblock ( & mut self ) {
@@ -87,40 +92,40 @@ mod tests {
87
92
use super :: * ;
88
93
use ln:: peers:: test_util:: * ;
89
94
90
- // Test that a try_flush_one () call with no queued data doesn't write anything
95
+ // Test that a try_flush () call with no queued data doesn't write anything
91
96
#[ test]
92
97
fn empty_does_not_write ( ) {
93
98
let mut descriptor = SocketDescriptorMock :: new ( ) ;
94
99
let mut empty = OutboundQueue :: new ( 10 ) ;
95
100
96
- assert ! ( empty. try_flush_one ( & mut descriptor) ) ;
101
+ assert ! ( empty. try_flush ( & mut descriptor) ) ;
97
102
descriptor. assert_called_with ( vec ! [ ] ) ;
98
103
99
104
}
100
105
101
- // Test that try_flush_one () sends the push_back
106
+ // Test that try_flush () sends the push_back
102
107
#[ test]
103
108
fn push_back_drain ( ) {
104
109
let mut descriptor = SocketDescriptorMock :: new ( ) ;
105
110
let mut queue = OutboundQueue :: new ( 10 ) ;
106
111
107
112
queue. push_back ( vec ! [ 1 ] ) ;
108
- assert ! ( queue. try_flush_one ( & mut descriptor) ) ;
113
+ assert ! ( queue. try_flush ( & mut descriptor) ) ;
109
114
110
115
descriptor. assert_called_with ( vec ! [ ( vec![ 1 ] , true ) ] ) ;
111
116
}
112
117
113
- // Test that try_flush_one () sends just first push_back
118
+ // Test that try_flush () sends all
114
119
#[ test]
115
120
fn push_back_push_back_drain_drain ( ) {
116
121
let mut descriptor = SocketDescriptorMock :: new ( ) ;
117
122
let mut queue = OutboundQueue :: new ( 10 ) ;
118
123
119
124
queue. push_back ( vec ! [ 1 ] ) ;
120
125
queue. push_back ( vec ! [ 2 ] ) ;
121
- assert ! ( queue. try_flush_one ( & mut descriptor) ) ;
126
+ assert ! ( queue. try_flush ( & mut descriptor) ) ;
122
127
123
- descriptor. assert_called_with ( vec ! [ ( vec![ 1 ] , true ) ] ) ;
128
+ descriptor. assert_called_with ( vec ! [ ( vec![ 1 ] , true ) , ( vec! [ 2 ] , true ) ] ) ;
124
129
}
125
130
126
131
// Test that descriptor that can't write all bytes returns valid response
@@ -130,27 +135,40 @@ mod tests {
130
135
let mut queue = OutboundQueue :: new ( 10 ) ;
131
136
132
137
queue. push_back ( vec ! [ 1 , 2 , 3 ] ) ;
133
- assert ! ( !queue. try_flush_one ( & mut descriptor) ) ;
138
+ assert ! ( !queue. try_flush ( & mut descriptor) ) ;
134
139
135
140
descriptor. assert_called_with ( vec ! [ ( vec![ 1 , 2 , 3 ] , true ) ] ) ;
136
141
}
137
142
143
+ // Test that descriptor that can't write all bytes (in second pushed item) returns valid response
144
+ #[ test]
145
+ fn push_back_drain_partial_multiple_push ( ) {
146
+ let mut descriptor = SocketDescriptorMock :: with_fixed_size ( 2 ) ;
147
+ let mut queue = OutboundQueue :: new ( 10 ) ;
148
+
149
+ queue. push_back ( vec ! [ 1 ] ) ;
150
+ queue. push_back ( vec ! [ 2 , 3 ] ) ;
151
+ assert ! ( !queue. try_flush( & mut descriptor) ) ;
152
+
153
+ descriptor. assert_called_with ( vec ! [ ( vec![ 1 ] , true ) , ( vec![ 2 , 3 ] , true ) ] ) ;
154
+ }
155
+
138
156
// Test the bookkeeping for multiple partial writes
139
157
#[ test]
140
- fn push_back_drain_partial_drain_partial_try_flush_one ( ) {
158
+ fn push_back_drain_partial_drain_partial_try_flush ( ) {
141
159
let mut descriptor = SocketDescriptorMock :: with_fixed_size ( 1 ) ;
142
160
let mut queue = OutboundQueue :: new ( 10 ) ;
143
161
144
162
queue. push_back ( vec ! [ 1 , 2 , 3 ] ) ;
145
- assert ! ( !queue. try_flush_one ( & mut descriptor) ) ;
163
+ assert ! ( !queue. try_flush ( & mut descriptor) ) ;
146
164
147
165
descriptor. make_room ( 1 ) ;
148
166
queue. unblock ( ) ;
149
- assert ! ( !queue. try_flush_one ( & mut descriptor) ) ;
167
+ assert ! ( !queue. try_flush ( & mut descriptor) ) ;
150
168
151
169
descriptor. make_room ( 1 ) ;
152
170
queue. unblock ( ) ;
153
- assert ! ( queue. try_flush_one ( & mut descriptor) ) ;
171
+ assert ! ( queue. try_flush ( & mut descriptor) ) ;
154
172
155
173
descriptor. assert_called_with ( vec ! [ ( vec![ 1 , 2 , 3 ] , true ) , ( vec![ 2 , 3 ] , true ) , ( vec![ 3 ] , true ) ] ) ;
156
174
}
@@ -162,27 +180,27 @@ mod tests {
162
180
163
181
// Fail write and move to blocked state
164
182
queue. push_back ( vec ! [ 1 , 2 ] ) ;
165
- assert ! ( !queue. try_flush_one ( & mut descriptor) ) ;
183
+ assert ! ( !queue. try_flush ( & mut descriptor) ) ;
166
184
descriptor. assert_called_with ( vec ! [ ( vec![ 1 , 2 ] , true ) ] ) ;
167
185
168
186
// Make room but don't signal
169
187
descriptor. make_room ( 1 ) ;
170
- assert ! ( !queue. try_flush_one ( & mut descriptor) ) ;
188
+ assert ! ( !queue. try_flush ( & mut descriptor) ) ;
171
189
assert ! ( queue. is_blocked( ) ) ;
172
190
descriptor. assert_called_with ( vec ! [ ( vec![ 1 , 2 ] , true ) ] ) ;
173
191
174
192
// Unblock and try again
175
193
queue. unblock ( ) ;
176
194
177
195
// Partial write will succeed, but still move to blocked
178
- assert ! ( !queue. try_flush_one ( & mut descriptor) ) ;
196
+ assert ! ( !queue. try_flush ( & mut descriptor) ) ;
179
197
assert ! ( queue. is_blocked( ) ) ;
180
198
descriptor. assert_called_with ( vec ! [ ( vec![ 1 , 2 ] , true ) , ( vec![ 1 , 2 ] , true ) ] ) ;
181
199
182
200
// Make room and signal which will succeed in writing the final piece
183
201
descriptor. make_room ( 1 ) ;
184
202
queue. unblock ( ) ;
185
- assert ! ( queue. try_flush_one ( & mut descriptor) ) ;
203
+ assert ! ( queue. try_flush ( & mut descriptor) ) ;
186
204
assert ! ( !queue. is_blocked( ) ) ;
187
205
descriptor. assert_called_with ( vec ! [ ( vec![ 1 , 2 ] , true ) , ( vec![ 1 , 2 ] , true ) , ( vec![ 2 ] , true ) ] ) ;
188
206
}
@@ -194,7 +212,7 @@ mod tests {
194
212
let mut queue = OutboundQueue :: new ( 1 ) ;
195
213
196
214
queue. push_back ( vec ! [ 1 ] ) ;
197
- assert ! ( queue. try_flush_one ( & mut descriptor) ) ;
215
+ assert ! ( queue. try_flush ( & mut descriptor) ) ;
198
216
descriptor. assert_called_with ( vec ! [ ( vec![ 1 ] , false ) ] ) ;
199
217
}
200
218
@@ -207,9 +225,7 @@ mod tests {
207
225
queue. push_back ( vec ! [ 1 ] ) ;
208
226
queue. push_back ( vec ! [ 2 ] ) ;
209
227
queue. push_back ( vec ! [ 3 ] ) ;
210
- assert ! ( queue. try_flush_one( & mut descriptor) ) ;
211
- assert ! ( queue. try_flush_one( & mut descriptor) ) ;
212
- assert ! ( queue. try_flush_one( & mut descriptor) ) ;
228
+ assert ! ( queue. try_flush( & mut descriptor) ) ;
213
229
descriptor. assert_called_with ( vec ! [ ( vec![ 1 ] , false ) , ( vec![ 2 ] , false ) , ( vec![ 3 ] , true ) ] ) ;
214
230
}
215
231
@@ -223,7 +239,7 @@ mod tests {
223
239
queue. push_back ( vec ! [ 1 ] ) ;
224
240
assert ! ( !queue. is_empty( ) ) ;
225
241
226
- assert ! ( queue. try_flush_one ( & mut descriptor) ) ;
242
+ assert ! ( queue. try_flush ( & mut descriptor) ) ;
227
243
assert ! ( queue. is_empty( ) ) ;
228
244
}
229
245
@@ -244,12 +260,8 @@ mod tests {
244
260
queue. push_back ( vec ! [ 2 ] ) ;
245
261
assert_eq ! ( queue. queue_space( ) , 0 ) ;
246
262
247
- // at soft limit
248
- assert ! ( queue. try_flush_one( & mut descriptor) ) ;
249
- assert_eq ! ( queue. queue_space( ) , 0 ) ;
250
-
251
263
// below soft limt
252
- assert ! ( queue. try_flush_one ( & mut descriptor) ) ;
264
+ assert ! ( queue. try_flush ( & mut descriptor) ) ;
253
265
assert_eq ! ( queue. queue_space( ) , 1 ) ;
254
266
}
255
267
}
0 commit comments