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