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