timers.tap.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 timers", function (t) {
  7. t.plan(4);
  8. var namespace = createNamespace('namespace');
  9. namespace.run(function () {
  10. namespace.set('test', 0xabad1dea);
  11. t.test("process.nextTick", function (t) {
  12. namespace.run(function () {
  13. namespace.set('test', 31337);
  14. t.equal(namespace.get('test'), 31337, "state has been mutated");
  15. process.nextTick(function () {
  16. t.equal(namespace.get('test'), 31337,
  17. "mutated state has persisted to process.nextTick's callback");
  18. t.end();
  19. });
  20. });
  21. });
  22. t.test("setImmediate", function (t) {
  23. // setImmediate only in Node > 0.9.x
  24. if (!global.setImmediate) return t.end();
  25. namespace.run(function () {
  26. namespace.set('test', 999);
  27. t.equal(namespace.get('test'), 999, "state has been mutated");
  28. setImmediate(function () {
  29. t.equal(namespace.get('test'), 999,
  30. "mutated state has persisted to setImmediate's callback");
  31. t.end();
  32. });
  33. });
  34. });
  35. t.test("setTimeout", function (t) {
  36. namespace.run(function () {
  37. namespace.set('test', 54321);
  38. t.equal(namespace.get('test'), 54321, "state has been mutated");
  39. setTimeout(function () {
  40. t.equal(namespace.get('test'), 54321,
  41. "mutated state has persisted to setTimeout's callback");
  42. t.end();
  43. });
  44. });
  45. });
  46. t.test("setInterval", function (t) {
  47. namespace.run(function () {
  48. namespace.set('test', 10101);
  49. t.equal(namespace.get('test'), 10101,
  50. "continuation-local state has been mutated");
  51. var ref = setInterval(function () {
  52. t.equal(namespace.get('test'), 10101,
  53. "mutated state has persisted to setInterval's callback");
  54. clearInterval(ref);
  55. t.end();
  56. }, 20);
  57. });
  58. });
  59. });
  60. });