flow-control.test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Module dependencies
  3. */
  4. var util = require('util');
  5. var assert = require('assert');
  6. var Machine = require('../');
  7. describe('flow control & artificial delay', function (){
  8. describe('given a machine with an asynchronous implementation', function () {
  9. describe('that declares itself synchronous', function () {
  10. var NM_DEF_FIXTURE = {
  11. sync: true,
  12. inputs: {},
  13. fn: function (inputs, exits){
  14. var sum = 1;
  15. setTimeout(function (){
  16. sum++;
  17. return exits.success(sum);
  18. }, 50);
  19. }
  20. };
  21. describe('calling .execSync()', function () {
  22. it('should throw a predictable error', function (){
  23. try {
  24. Machine.build(NM_DEF_FIXTURE).execSync();
  25. } catch (e) {
  26. // console.log('->',e);
  27. assert.equal(e.code,'E_MACHINE_INCONSISTENT');
  28. return;
  29. }//-•
  30. throw new Error('Expected an error, but instead it was successful.');
  31. });//</it>
  32. });//</describe :: calling .execSync()>
  33. describe('calling .exec()', function () {
  34. // FUTURE: make this cause an error instead of working-- eg. make this test pass:
  35. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  36. it.skip('should throw a predictable error', function (done){
  37. Machine.build(NM_DEF_FIXTURE).exec(function (err){
  38. if (err) {
  39. // console.log('->',err);
  40. try {
  41. assert.equal(err.code,'E_MACHINE_INCONSISTENT');
  42. } catch (e) { return done(e); }
  43. return done();
  44. }
  45. return done(new Error('Expected an error, but instead it was successful.'));
  46. });//<.exec()>
  47. });//</it>
  48. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  49. });//</describe :: calling .exec()>
  50. });//</describe :: that declares itself synchronous>
  51. describe('that DOES NOT declare itself synchronous', function () {
  52. var NM_DEF_FIXTURE = {
  53. inputs: {},
  54. fn: function (inputs, exits){
  55. var sum = 1;
  56. setTimeout(function (){
  57. sum++;
  58. return exits.success(sum);
  59. }, 50);
  60. }
  61. };
  62. describe('calling .execSync()', function () {
  63. it('should throw a predictable error', function (){
  64. try {
  65. Machine.build(NM_DEF_FIXTURE).execSync();
  66. } catch (e) {
  67. // console.log('->',e);
  68. assert.equal(e.code,'E_USAGE');
  69. return;
  70. }//-•
  71. throw new Error('Expected an error, but instead it was successful.');
  72. });//</it>
  73. });//</describe :: calling .execSync()>
  74. describe('calling .exec()', function () {
  75. it('should succeed, and should yield before triggering callback', function (done){
  76. var didYield;
  77. Machine.build(NM_DEF_FIXTURE).exec(function (err){
  78. if (err) { return done(err); }
  79. try {
  80. assert(didYield, new Error('Should have "yielded"!'));
  81. } catch (e) { return done(e); }
  82. return done();
  83. });//<.exec()>
  84. didYield = true;
  85. });//</it>
  86. });//</describe :: calling .exec()>
  87. });//</describe :: that DOES NOT declare itself synchronous>
  88. });//</describe :: given a machine with an asynchronous implementation>
  89. describe('given a machine with a synchronous implementation', function () {
  90. describe('that declares itself synchronous', function () {
  91. var NM_DEF_FIXTURE = {
  92. sync: true,
  93. inputs: {},
  94. fn: function (inputs, exits){
  95. var sum = 1+1;
  96. return exits.success(sum);
  97. }
  98. };
  99. describe('calling .execSync()', function () {
  100. it('should succeed', function (){
  101. Machine.build(NM_DEF_FIXTURE).execSync();
  102. });//</it>
  103. });//</describe :: calling .execSync()>
  104. describe('calling .exec()', function () {
  105. it('should succeed, and should yield before triggering callback', function (done){
  106. var didYield;
  107. Machine.build(NM_DEF_FIXTURE).exec(function (err){
  108. if (err) { return done(err); }
  109. try {
  110. assert(didYield, new Error('Should have "yielded"!'));
  111. } catch (e) { return done(e); }
  112. return done();
  113. });//<.exec()>
  114. didYield = true;
  115. });//</it>
  116. });//</describe :: calling .exec()>
  117. });//</describe :: that declares itself synchronous>
  118. describe('that DOES NOT declare itself synchronous', function () {
  119. var NM_DEF_FIXTURE = {
  120. inputs: {},
  121. fn: function (inputs, exits){
  122. var sum = 1+1;
  123. return exits.success(sum);
  124. }
  125. };
  126. describe('calling .execSync()', function () {
  127. it('should throw a predictable error', function (){
  128. try {
  129. Machine.build(NM_DEF_FIXTURE).execSync();
  130. } catch (e) {
  131. // console.log('->',e);
  132. assert.equal(e.code,'E_USAGE');
  133. return;
  134. }//-•
  135. throw new Error('Expected an error, but instead it was successful.');
  136. });//</it>
  137. });//</describe :: calling .execSync()>
  138. describe('calling .exec()', function () {
  139. it('should succeed, and should yield before triggering callback', function (done){
  140. var didYield;
  141. Machine.build(NM_DEF_FIXTURE).exec(function (err){
  142. if (err) { return done(err); }
  143. try {
  144. assert(didYield, new Error('Should have "yielded"!'));
  145. } catch (e) { return done(e); }
  146. return done();
  147. });//<.exec()>
  148. didYield = true;
  149. });//</it>
  150. });//</describe :: calling .exec()>
  151. });//</describe :: that DOES NOT declare itself synchronous>
  152. });//</describe :: given a machine with a synchronous implementation>
  153. });//</describe :: flow control & artificial delay>