23
23
require ( '../common' ) ;
24
24
const assert = require ( 'assert' ) ;
25
25
const net = require ( 'net' ) ;
26
+ const debuglog = require ( 'util' ) . debuglog ( 'test' ) ;
26
27
27
- const N = 1024 * 1024 ;
28
- const part_N = N / 3 ;
29
28
let chars_recved = 0 ;
30
29
let npauses = 0 ;
31
-
32
- console . log ( 'build big string' ) ;
33
- const body = 'C' . repeat ( N ) ;
30
+ let totalLength = 0 ;
34
31
35
32
const server = net . createServer ( ( connection ) => {
36
- connection . write ( body . slice ( 0 , part_N ) ) ;
37
- connection . write ( body . slice ( part_N , 2 * part_N ) ) ;
38
- assert . strictEqual ( connection . write ( body . slice ( 2 * part_N , N ) ) , false ) ;
39
- console . log ( `bufferSize: ${ connection . bufferSize } ` , 'expecting' , N ) ;
40
- assert . ok ( connection . bufferSize >= 0 &&
41
- connection . writableLength <= N ) ;
33
+ const body = 'C' . repeat ( 1024 ) ;
34
+ let n = 1 ;
35
+ debuglog ( 'starting write loop' ) ;
36
+ while ( connection . write ( body ) ) {
37
+ n ++ ;
38
+ }
39
+ debuglog ( 'ended write loop' ) ;
40
+ // Now that we're throttled, do some more writes to make sure the data isn't
41
+ // lost.
42
+ connection . write ( body ) ;
43
+ connection . write ( body ) ;
44
+ n += 2 ;
45
+ totalLength = n * body . length ;
46
+ assert . ok ( connection . bufferSize >= 0 , `bufferSize: ${ connection . bufferSize } ` ) ;
47
+ assert . ok (
48
+ connection . writableLength <= totalLength ,
49
+ `writableLength: ${ connection . writableLength } , totalLength: ${ totalLength } `
50
+ ) ;
42
51
connection . end ( ) ;
43
52
} ) ;
44
53
45
54
server . listen ( 0 , ( ) => {
46
55
const port = server . address ( ) . port ;
47
- console . log ( `server started on port ${ port } ` ) ;
56
+ debuglog ( `server started on port ${ port } ` ) ;
48
57
let paused = false ;
49
58
const client = net . createConnection ( port ) ;
50
59
client . setEncoding ( 'ascii' ) ;
51
60
client . on ( 'data' , ( d ) => {
52
61
chars_recved += d . length ;
53
- console . log ( `got ${ chars_recved } ` ) ;
62
+ debuglog ( `got ${ chars_recved } ` ) ;
54
63
if ( ! paused ) {
55
64
client . pause ( ) ;
56
65
npauses += 1 ;
57
66
paused = true ;
58
- console . log ( 'pause' ) ;
67
+ debuglog ( 'pause' ) ;
59
68
const x = chars_recved ;
60
69
setTimeout ( ( ) => {
61
70
assert . strictEqual ( chars_recved , x ) ;
62
71
client . resume ( ) ;
63
- console . log ( 'resume' ) ;
72
+ debuglog ( 'resume' ) ;
64
73
paused = false ;
65
74
} , 100 ) ;
66
75
}
@@ -74,6 +83,6 @@ server.listen(0, () => {
74
83
75
84
76
85
process . on ( 'exit' , ( ) => {
77
- assert . strictEqual ( chars_recved , N ) ;
86
+ assert . strictEqual ( chars_recved , totalLength ) ;
78
87
assert . strictEqual ( npauses > 2 , true ) ;
79
88
} ) ;
0 commit comments