deferred_async_test.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2013 The Closure Library Authors. All Rights Reserved.
  5. -->
  6. <head>
  7. <title>Closure Unit Tests - goog.async.Deferred</title>
  8. <script src="../../../../../closure/goog/base.js"></script>
  9. <script>
  10. goog.require('goog.async.Deferred');
  11. goog.require('goog.testing.jsunit');
  12. </script>
  13. </head>
  14. <body>
  15. <script>
  16. function shouldRunTests() {
  17. return !!Error.captureStackTrace;
  18. }
  19. function setUpPage() {
  20. goog.async.Deferred.LONG_STACK_TRACES = true;
  21. }
  22. function testErrorStack() {
  23. var d;
  24. // Get the deferred from somewhere deep in the callstack.
  25. (function immediate() {
  26. (function immediate2() {
  27. d = new goog.async.Deferred();
  28. d.addCallback(function actuallyThrows() { throw new Error('Foo'); });
  29. })();
  30. })();
  31. window.setTimeout(function willThrow() {
  32. (function callbackCaller() { d.callback(); })();
  33. }, 0);
  34. return d.then(fail, function(error) {
  35. assertContains('Foo', error.stack);
  36. assertContains('testErrorStack', error.stack);
  37. assertContains('callbackCaller', error.stack);
  38. assertContains('willThrow', error.stack);
  39. assertContains('actuallyThrows', error.stack);
  40. assertContains('DEFERRED OPERATION', error.stack);
  41. assertContains('immediate', error.stack);
  42. assertContains('immediate2', error.stack);
  43. });
  44. }
  45. function testErrorStack_forErrback() {
  46. var d = new goog.async.Deferred();
  47. window.setTimeout(function willThrow() { d.errback(new Error('Foo')); }, 0);
  48. return d.then(fail, function(error) {
  49. assertContains('Foo', error.stack);
  50. assertContains('testErrorStack_forErrback', error.stack);
  51. assertContains('willThrow', error.stack);
  52. assertContains('DEFERRED OPERATION', error.stack);
  53. });
  54. }
  55. function testErrorStack_nested() {
  56. var d = new goog.async.Deferred();
  57. window.setTimeout(function async1() {
  58. var nested = new goog.async.Deferred();
  59. nested.addErrback(function nestedErrback(error) { d.errback(error); });
  60. window.setTimeout(function async2() {
  61. (function immediate() { nested.errback(new Error('Foo')); })();
  62. });
  63. }, 0);
  64. return d.then(fail, function(error) {
  65. assertContains('Foo', error.stack);
  66. assertContains('testErrorStack_nested', error.stack);
  67. assertContains('async1', error.stack);
  68. assertContains('async2', error.stack);
  69. assertContains('immediate', error.stack);
  70. assertContains('DEFERRED OPERATION', error.stack);
  71. });
  72. }
  73. function testErrorStack_doesNotTouchCustomStack() {
  74. var d = new goog.async.Deferred();
  75. d.addCallback(function actuallyThrows() {
  76. var e = new Error('Foo');
  77. e.stack = 'STACK';
  78. throw e;
  79. });
  80. window.setTimeout(function willThrow() {
  81. (function callbackCaller() { d.callback(); })();
  82. }, 0);
  83. return d.then(fail, function(error) { assertEquals('STACK', error.stack); });
  84. }
  85. </script>
  86. </body>
  87. </html>