Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. "use strict";
  2. const common = require("../common");
  3. const assert = require("assert");
  4. const { AsyncContext } = require("async_hooks");
  5.  
  6. const asyncContext = new AsyncContext();
  7.  
  8. const tasks = [];
  9.  
  10. asyncContext.enter(store => {
  11. store.set("hello", "world");
  12. tasks.push(() => {
  13. setTimeout(() => {
  14. assert.strictEqual(asyncContext.getStore().get("hello"), "world");
  15. }, 200);
  16. });
  17. });
  18.  
  19. asyncContext.enter(store => {
  20. store.set("hello", "earth");
  21. tasks.push(() => {
  22. setTimeout(() => {
  23. assert.strictEqual(asyncContext.getStore().get("hello"), "earth");
  24. }, 200);
  25. });
  26. });
  27.  
  28. process.once('uncaughtException', () => {
  29. assert.strictEqual(asyncContext.getStore().get("hello"), "uncaughtException");
  30. });
  31.  
  32. asyncContext.enter(store => {
  33. store.set("hello", "uncaughtException");
  34. tasks.push(() => {
  35. setTimeout(() => {
  36. try {
  37. throw new Error('test');
  38. } catch (err) {
  39. process.nextTick(() => {
  40. throw err;
  41. });
  42. }
  43. }, 200);
  44. });
  45. });
  46.  
  47. setTimeout(() => {
  48. asyncContext.enter(store => {
  49. store.set("hello", "moon");
  50. setTimeout(() => {
  51. assert.strictEqual(asyncContext.getStore().get("hello"), "moon");
  52. setTimeout(() => {
  53. tasks.forEach(task => {
  54. task();
  55. });
  56. }, 100);
  57. }, 200);
  58. });
  59. }, 100);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement