bulkloader_test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.net.BulkLoaderTest');
  15. goog.setTestOnly('goog.net.BulkLoaderTest');
  16. goog.require('goog.events.Event');
  17. goog.require('goog.events.EventHandler');
  18. goog.require('goog.net.BulkLoader');
  19. goog.require('goog.net.EventType');
  20. goog.require('goog.testing.MockClock');
  21. goog.require('goog.testing.jsunit');
  22. /**
  23. * Test interval between sending uri requests to the server.
  24. */
  25. var DELAY_INTERVAL_BETWEEN_URI_REQUESTS = 5;
  26. /**
  27. * Test interval before a response is received for a URI request.
  28. */
  29. var DELAY_INTERVAL_FOR_URI_LOAD = 15;
  30. var clock;
  31. var loadSuccess, loadError;
  32. var successResponseTexts;
  33. function setUpPage() {
  34. clock = new goog.testing.MockClock(true);
  35. }
  36. function tearDownPage() {
  37. clock.dispose();
  38. }
  39. function setUp() {
  40. loadSuccess = false;
  41. loadError = false;
  42. successResponseTexts = [];
  43. }
  44. /**
  45. * Gets the successful bulkloader for the specified uris with some
  46. * modifications for testability.
  47. * <ul>
  48. * <li> Added onSuccess methods to simulate success while loading uris.
  49. * <li> The send function of the XhrManager used by the bulkloader
  50. * calls the onSuccess function after a specified time interval.
  51. * </ul>
  52. * @param {Array<string>} uris The URIs.
  53. */
  54. function getSuccessfulBulkLoader(uris) {
  55. var bulkLoader = new goog.net.BulkLoader(uris);
  56. bulkLoader.load = function() {
  57. var uris = this.helper_.getUris();
  58. for (var i = 0; i < uris.length; i++) {
  59. // This clock tick simulates a delay for processing every URI.
  60. clock.tick(DELAY_INTERVAL_BETWEEN_URI_REQUESTS);
  61. // This timeout determines how many ticks after the send request
  62. // all the URIs will complete loading. This delays the load of
  63. // the first uri and every subsequent uri by 15 ticks.
  64. setTimeout(
  65. goog.bind(this.onSuccess, this, i, uris[i]),
  66. DELAY_INTERVAL_FOR_URI_LOAD);
  67. }
  68. };
  69. bulkLoader.onSuccess = function(id, uri) {
  70. var xhrIo = {
  71. getResponseText: function() { return uri; },
  72. isSuccess: function() { return true; },
  73. dispose: function() {}
  74. };
  75. this.handleEvent_(
  76. id, new goog.events.Event(goog.net.EventType.COMPLETE, xhrIo));
  77. };
  78. var eventHandler = new goog.events.EventHandler();
  79. eventHandler.listen(bulkLoader, goog.net.EventType.SUCCESS, handleSuccess);
  80. eventHandler.listen(bulkLoader, goog.net.EventType.ERROR, handleError);
  81. return bulkLoader;
  82. }
  83. /**
  84. * Gets the non-successful bulkloader for the specified uris with some
  85. * modifications for testability.
  86. * <ul>
  87. * <li> Added onSuccess and onError methods to simulate success and error
  88. * while loading uris.
  89. * <li> The send function of the XhrManager used by the bulkloader
  90. * calls the onSuccess or onError function after a specified time
  91. * interval.
  92. * </ul>
  93. * @param {Array<string>} uris The URIs.
  94. */
  95. function getNonSuccessfulBulkLoader(uris) {
  96. var bulkLoader = new goog.net.BulkLoader(uris);
  97. bulkLoader.load = function() {
  98. var uris = this.helper_.getUris();
  99. for (var i = 0; i < uris.length; i++) {
  100. // This clock tick simulates a delay for processing every URI.
  101. clock.tick(DELAY_INTERVAL_BETWEEN_URI_REQUESTS);
  102. // This timeout determines how many ticks after the send request
  103. // all the URIs will complete loading in error. This delays the load
  104. // of the first uri and every subsequent uri by 15 ticks. The URI
  105. // with id == 2 is in error.
  106. if (i != 2) {
  107. setTimeout(
  108. goog.bind(this.onSuccess, this, i, uris[i]),
  109. DELAY_INTERVAL_FOR_URI_LOAD);
  110. } else {
  111. setTimeout(
  112. goog.bind(this.onError, this, i, uris[i]),
  113. DELAY_INTERVAL_FOR_URI_LOAD);
  114. }
  115. }
  116. };
  117. bulkLoader.onSuccess = function(id, uri) {
  118. var xhrIo = {
  119. getResponseText: function() { return uri; },
  120. isSuccess: function() { return true; },
  121. dispose: function() {}
  122. };
  123. this.handleEvent_(
  124. id, new goog.events.Event(goog.net.EventType.COMPLETE, xhrIo));
  125. };
  126. bulkLoader.onError = function(id) {
  127. var xhrIo = {
  128. getResponseText: function() { return null; },
  129. isSuccess: function() { return false; },
  130. dispose: function() {}
  131. };
  132. this.handleEvent_(
  133. id, new goog.events.Event(goog.net.EventType.ERROR, xhrIo));
  134. };
  135. var eventHandler = new goog.events.EventHandler();
  136. eventHandler.listen(bulkLoader, goog.net.EventType.SUCCESS, handleSuccess);
  137. eventHandler.listen(bulkLoader, goog.net.EventType.ERROR, handleError);
  138. return bulkLoader;
  139. }
  140. function handleSuccess(e) {
  141. loadSuccess = true;
  142. successResponseTexts = e.target.getResponseTexts();
  143. }
  144. function handleError(e) {
  145. loadError = true;
  146. }
  147. /**
  148. * Test successful loading of URIs using the bulkloader.
  149. */
  150. function testBulkLoaderLoadSuccess() {
  151. var uris = ['a', 'b', 'c'];
  152. var bulkLoader = getSuccessfulBulkLoader(uris);
  153. assertArrayEquals(uris, bulkLoader.getRequestUris());
  154. bulkLoader.load();
  155. clock.tick(2);
  156. assertFalse('The bulk loader is not yet loaded (after 2 ticks)', loadSuccess);
  157. clock.tick(3);
  158. assertFalse('The bulk loader is not yet loaded (after 5 ticks)', loadSuccess);
  159. clock.tick(5);
  160. assertFalse(
  161. 'The bulk loader is not yet loaded (after 10 ticks)', loadSuccess);
  162. clock.tick(5);
  163. assertTrue('The bulk loader is loaded (after 15 ticks)', loadSuccess);
  164. assertArrayEquals(
  165. 'Ensure that the response texts are present', successResponseTexts, uris);
  166. }
  167. /**
  168. * Test error loading URIs using the bulkloader.
  169. */
  170. function testBulkLoaderLoadError() {
  171. var uris = ['a', 'b', 'c'];
  172. var bulkLoader = getNonSuccessfulBulkLoader(uris);
  173. bulkLoader.load();
  174. clock.tick(2);
  175. assertFalse('The bulk loader is not yet loaded (after 2 ticks)', loadError);
  176. clock.tick(3);
  177. assertFalse('The bulk loader is not yet loaded (after 5 ticks)', loadError);
  178. clock.tick(5);
  179. assertFalse('The bulk loader is not yet loaded (after 10 ticks)', loadError);
  180. clock.tick(5);
  181. assertFalse(
  182. 'The bulk loader is not loaded successfully (after 15 ticks)',
  183. loadSuccess);
  184. assertTrue('The bulk loader is loaded in error (after 15 ticks)', loadError);
  185. }