promises.tap.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. var tap = require('tap')
  3. , test = tap.test
  4. , createNamespace = require('../context.js').createNamespace
  5. ;
  6. test("continuation-local state with promises", function (t) {
  7. t.plan(4);
  8. var namespace = createNamespace('namespace');
  9. namespace.run(function () {
  10. namespace.set('test', 0xabad1dea);
  11. t.test("chained promises", function (t) {
  12. if (!global.Promise) return t.end();
  13. namespace.run(function () {
  14. namespace.set('test', 31337);
  15. t.equal(namespace.get('test'), 31337, "state has been mutated");
  16. Promise.resolve()
  17. .then(function () {
  18. t.equal(namespace.get('test'), 31337,
  19. "mutated state has persisted to first continuation");
  20. })
  21. .then(function () {
  22. t.equal(namespace.get('test'), 31337,
  23. "mutated state has persisted to second continuation");
  24. })
  25. .then(function () {
  26. t.equal(namespace.get('test'), 31337,
  27. "mutated state has persisted to third continuation");
  28. t.end();
  29. });
  30. });
  31. });
  32. t.test("chained unwrapped promises", function (t) {
  33. if (!global.Promise) return t.end();
  34. namespace.run(function () {
  35. namespace.set('test', 999);
  36. t.equal(namespace.get('test'), 999, "state has been mutated");
  37. Promise.resolve()
  38. .then(function () {
  39. t.equal(namespace.get('test'), 999,
  40. "mutated state has persisted to first continuation");
  41. return Promise.resolve();
  42. })
  43. .then(function () {
  44. t.equal(namespace.get('test'), 999,
  45. "mutated state has persisted to second continuation");
  46. return Promise.resolve();
  47. })
  48. .then(function () {
  49. t.equal(namespace.get('test'), 999,
  50. "mutated state has persisted to third continuation");
  51. t.end();
  52. });
  53. });
  54. });
  55. t.test("nested promises", function (t) {
  56. if (!global.Promise) return t.end();
  57. namespace.run(function () {
  58. namespace.set('test', 54321);
  59. t.equal(namespace.get('test'), 54321, "state has been mutated");
  60. Promise.resolve()
  61. .then(function () {
  62. t.equal(namespace.get('test'), 54321,
  63. "mutated state has persisted to first continuation");
  64. Promise.resolve()
  65. .then(function () {
  66. t.equal(namespace.get('test'), 54321,
  67. "mutated state has persisted to second continuation");
  68. Promise.resolve()
  69. .then(function () {
  70. t.equal(namespace.get('test'), 54321,
  71. "mutated state has persisted to third continuation");
  72. t.end();
  73. });
  74. });
  75. });
  76. });
  77. });
  78. t.test("forked continuations", function (t) {
  79. if (!global.Promise) return t.end();
  80. namespace.run(function () {
  81. namespace.set('test', 10101);
  82. t.equal(namespace.get('test'), 10101, "state has been mutated");
  83. var promise = Promise.resolve();
  84. promise
  85. .then(function () {
  86. t.equal(namespace.get('test'), 10101,
  87. "mutated state has persisted to first continuation");
  88. });
  89. promise
  90. .then(function () {
  91. t.equal(namespace.get('test'), 10101,
  92. "mutated state has persisted to second continuation");
  93. });
  94. promise
  95. .then(function () {
  96. t.equal(namespace.get('test'), 10101,
  97. "mutated state has persisted to third continuation");
  98. t.end();
  99. });
  100. });
  101. });
  102. });
  103. });