timeout.test.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Module dependencies
  3. */
  4. var Machine = require('../');
  5. describe('timeout', function (){
  6. describe('machine that never triggers its exits', function (){
  7. describe('with timeout configured for 0.25 seconds', function (){
  8. var machineInstance;
  9. before(function (){
  10. machineInstance = Machine.build({
  11. timeout: 250,
  12. fn: function machineThatNeverTriggersItsExits(inputs, exits, env){
  13. // beep
  14. }
  15. });
  16. });
  17. it('should call its error exit with a timeout error', function (done){
  18. machineInstance().exec({
  19. error: function (err) {
  20. if (err.code === 'E_MACHINE_TIMEOUT') {
  21. return done();
  22. }
  23. return done(new Error('Expecting Error instance w/ `code` === "E_MACHINE_TIMEOUT", but instead got this error (code:'+err.code+'): '+err.stack));
  24. },
  25. success: function (){
  26. return done(new Error('wtf'));
  27. }
  28. });
  29. });
  30. });
  31. });
  32. describe('machine that triggers its exits after 0.5 seconds', function (){
  33. describe('with timeout configured for 0.25 seconds', function (){
  34. var machineInstance;
  35. before(function (){
  36. machineInstance = Machine.build({
  37. timeout: 250,
  38. fn: function machineThatNeverTriggersItsExits(inputs, exits, env){
  39. setTimeout(function (){
  40. return exits.success();
  41. }, 500);
  42. }
  43. });
  44. });
  45. it('should call its error exit with a timeout error', function (done){
  46. machineInstance().exec({
  47. error: function (err) {
  48. if (err.code === 'E_MACHINE_TIMEOUT') {
  49. return done();
  50. }
  51. return done(new Error('Expecting Error instance w/ `code` === "E_MACHINE_TIMEOUT", but instead got this error (code:'+err.code+'): '+err.stack));
  52. },
  53. success: function (){
  54. return done(new Error('Should not have called the success exit (should have been prevented by timeout alarm)'));
  55. }
  56. });
  57. });
  58. });
  59. });
  60. });