@@ -30,36 +30,36 @@ const assert = require('assert');
30
30
const tls = require ( 'tls' ) ;
31
31
const fixtures = require ( '../common/fixtures' ) ;
32
32
33
- assert . strictEqual (
34
- typeof global . gc ,
35
- 'function' ,
36
- `Type of global.gc is not a function. Type: ${ typeof global . gc } .` +
37
- ' Run this test with --expose-gc'
38
- ) ;
33
+ // Test that the implicit listener for an 'connect' event on tls.Sockets is
34
+ // added using `once()`, i.e. can be gc'ed once that event has occurred.
39
35
40
- tls . createServer ( {
36
+ const server = tls . createServer ( {
41
37
cert : fixtures . readSync ( 'test_cert.pem' ) ,
42
38
key : fixtures . readSync ( 'test_key.pem' )
43
- } ) . listen ( common . PORT ) ;
39
+ } ) . listen ( 0 ) ;
40
+
41
+ let collected = false ;
42
+ const gcListener = { ongc ( ) { collected = true ; } } ;
44
43
45
44
{
46
- // 2**26 == 64M entries
47
- const junk = new Array ( 2 ** 26 ) . fill ( 0 ) ;
45
+ const gcObject = { } ;
46
+ common . onGC ( gcObject , gcListener ) ;
48
47
49
- const options = { rejectUnauthorized : false } ;
50
- tls . connect ( common . PORT , '127.0.0.1' , options , function ( ) {
51
- assert . notStrictEqual ( junk . length , 0 ) ; // keep reference alive
52
- setTimeout ( done , 10 ) ;
53
- global . gc ( ) ;
54
- } ) ;
48
+ const sock = tls . connect (
49
+ server . address ( ) . port ,
50
+ { rejectUnauthorized : false } ,
51
+ common . mustCall ( ( ) => {
52
+ assert . strictEqual ( gcObject , gcObject ) ; // keep reference alive
53
+ assert . strictEqual ( collected , false ) ;
54
+ setImmediate ( done , sock ) ;
55
+ } ) ) ;
55
56
}
56
57
57
- function done ( ) {
58
- const before = process . memoryUsage ( ) . rss ;
58
+ function done ( sock ) {
59
59
global . gc ( ) ;
60
- const after = process . memoryUsage ( ) . rss ;
61
- const reclaimed = ( before - after ) / 1024 ;
62
- console . log ( '%d kB reclaimed' , reclaimed ) ;
63
- assert ( reclaimed > 256 * 1024 ) ; // it's more like 512M on x64
64
- process . exit ( ) ;
60
+ setImmediate ( ( ) => {
61
+ assert . strictEqual ( collected , true ) ;
62
+ sock . end ( ) ;
63
+ server . close ( ) ;
64
+ } ) ;
65
65
}
0 commit comments