test.worker-quit-keep-alive.js 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * Module dependencies.
  3. */
  4. var cluster = require('../')
  5. , http = require('http');
  6. require('./common');
  7. var server = http.createServer(function(req, res){
  8. res.writeHead(200);
  9. res.end('Hello World');
  10. });
  11. // request options
  12. var options = {
  13. host: 'localhost'
  14. , port: 3000
  15. , headers: { Connection: 'keep-alive' }
  16. };
  17. // cluster
  18. cluster = cluster(server)
  19. .set('workers', 1)
  20. .set('timeout', 1000)
  21. .listen(3000);
  22. cluster.on('listening', function(){
  23. http.get(options, function(res){
  24. res.statusCode.should.equal(200);
  25. // kill the worker
  26. var pid = cluster.children[0].proc.pid;
  27. process.kill(pid, 'SIGQUIT');
  28. });
  29. });
  30. var timeout;
  31. cluster.on('worker timeout', function(worker){
  32. worker.id.should.equal(0);
  33. timeout = true;
  34. });
  35. cluster.on('worker connected', function(worker){
  36. if (timeout) {
  37. worker.id.should.equal(0);
  38. http.get(options, function(res){
  39. res.statusCode.should.equal(200);
  40. cluster.close();
  41. });
  42. }
  43. });