test-sigusr2.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2012, Ben Noordhuis <info@bnoordhuis.nl>
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. var path = require('path');
  15. var http = require('http');
  16. var shelljs = require('shelljs');
  17. var test = require('tap').test;
  18. var heapdump = require('../');
  19. process.chdir(__dirname);
  20. function testSigUsr2(test){
  21. var server = http.createServer(function(req, res) {
  22. res.writeHeader(200, {'Content-Type':'text/plain;charset=utf-8',
  23. 'Content-Length':'2'});
  24. res.end('OK');
  25. });
  26. server.on('listening', function(){
  27. console.log('Listening on http://127.0.0.1:8000/');
  28. console.log('now sending SIGUSR2 to %d', process.pid);
  29. var heapSnapshotFile = 'heapdump-*.heapsnapshot';
  30. shelljs.rm('-f', heapSnapshotFile);
  31. var killCmd = shelljs.which('kill');
  32. var cmd = [killCmd, '-usr2', process.pid].join(' ');
  33. shelljs.exec(cmd);
  34. function waitForHeapdump(){
  35. var files = shelljs.ls(heapSnapshotFile);
  36. test.equals(files.length, 1, 'Heap file should be present');
  37. server.close();
  38. test.end();
  39. }
  40. setTimeout(waitForHeapdump, 500);
  41. });
  42. server.listen(0);
  43. }
  44. test('test heapdump on SIGUSR2', testSigUsr2);