File: bulkloader_test.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (224 lines) | stat: -rw-r--r-- 6,771 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

goog.provide('goog.net.BulkLoaderTest');
goog.setTestOnly('goog.net.BulkLoaderTest');

goog.require('goog.events.Event');
goog.require('goog.events.EventHandler');
goog.require('goog.net.BulkLoader');
goog.require('goog.net.EventType');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.jsunit');


/**
 * Test interval between sending uri requests to the server.
 */
var DELAY_INTERVAL_BETWEEN_URI_REQUESTS = 5;


/**
 * Test interval before a response is received for a URI request.
 */
var DELAY_INTERVAL_FOR_URI_LOAD = 15;

var clock;
var loadSuccess, loadError;
var successResponseTexts;

function setUpPage() {
  clock = new goog.testing.MockClock(true);
}

function tearDownPage() {
  clock.dispose();
}

function setUp() {
  loadSuccess = false;
  loadError = false;
  successResponseTexts = [];
}


/**
 * Gets the successful bulkloader for the specified uris with some
 * modifications for testability.
 * <ul>
 *   <li> Added onSuccess methods to simulate success while loading uris.
 *   <li> The send function of the XhrManager used by the bulkloader
 *        calls the onSuccess function after a specified time interval.
 * </ul>
 * @param {Array<string>} uris The URIs.
 */
function getSuccessfulBulkLoader(uris) {
  var bulkLoader = new goog.net.BulkLoader(uris);
  bulkLoader.load = function() {
    var uris = this.helper_.getUris();
    for (var i = 0; i < uris.length; i++) {
      // This clock tick simulates a delay for processing every URI.
      clock.tick(DELAY_INTERVAL_BETWEEN_URI_REQUESTS);
      // This timeout determines how many ticks after the send request
      // all the URIs will complete loading. This delays the load of
      // the first uri and every subsequent uri by 15 ticks.
      setTimeout(
          goog.bind(this.onSuccess, this, i, uris[i]),
          DELAY_INTERVAL_FOR_URI_LOAD);
    }
  };

  bulkLoader.onSuccess = function(id, uri) {
    var xhrIo = {
      getResponseText: function() { return uri; },
      isSuccess: function() { return true; },
      dispose: function() {}
    };
    this.handleEvent_(
        id, new goog.events.Event(goog.net.EventType.COMPLETE, xhrIo));
  };

  var eventHandler = new goog.events.EventHandler();
  eventHandler.listen(bulkLoader, goog.net.EventType.SUCCESS, handleSuccess);
  eventHandler.listen(bulkLoader, goog.net.EventType.ERROR, handleError);

  return bulkLoader;
}


/**
 * Gets the non-successful bulkloader for the specified uris with some
 * modifications for testability.
 * <ul>
 *   <li> Added onSuccess and onError methods to simulate success and error
 *        while loading uris.
 *   <li> The send function of the XhrManager used by the bulkloader
 *        calls the onSuccess or onError function after a specified time
 *        interval.
 * </ul>
 * @param {Array<string>} uris The URIs.
 */
function getNonSuccessfulBulkLoader(uris) {
  var bulkLoader = new goog.net.BulkLoader(uris);
  bulkLoader.load = function() {
    var uris = this.helper_.getUris();
    for (var i = 0; i < uris.length; i++) {
      // This clock tick simulates a delay for processing every URI.
      clock.tick(DELAY_INTERVAL_BETWEEN_URI_REQUESTS);

      // This timeout determines how many ticks after the send request
      // all the URIs will complete loading in error. This delays the load
      // of the first uri and every subsequent uri by 15 ticks. The URI
      // with id == 2 is in error.
      if (i != 2) {
        setTimeout(
            goog.bind(this.onSuccess, this, i, uris[i]),
            DELAY_INTERVAL_FOR_URI_LOAD);
      } else {
        setTimeout(
            goog.bind(this.onError, this, i, uris[i]),
            DELAY_INTERVAL_FOR_URI_LOAD);
      }
    }
  };

  bulkLoader.onSuccess = function(id, uri) {
    var xhrIo = {
      getResponseText: function() { return uri; },
      isSuccess: function() { return true; },
      dispose: function() {}
    };
    this.handleEvent_(
        id, new goog.events.Event(goog.net.EventType.COMPLETE, xhrIo));
  };

  bulkLoader.onError = function(id) {
    var xhrIo = {
      getResponseText: function() { return null; },
      isSuccess: function() { return false; },
      dispose: function() {}
    };
    this.handleEvent_(
        id, new goog.events.Event(goog.net.EventType.ERROR, xhrIo));
  };

  var eventHandler = new goog.events.EventHandler();
  eventHandler.listen(bulkLoader, goog.net.EventType.SUCCESS, handleSuccess);
  eventHandler.listen(bulkLoader, goog.net.EventType.ERROR, handleError);

  return bulkLoader;
}

function handleSuccess(e) {
  loadSuccess = true;
  successResponseTexts = e.target.getResponseTexts();
}

function handleError(e) {
  loadError = true;
}


/**
 * Test successful loading of URIs using the bulkloader.
 */
function testBulkLoaderLoadSuccess() {
  var uris = ['a', 'b', 'c'];
  var bulkLoader = getSuccessfulBulkLoader(uris);
  assertArrayEquals(uris, bulkLoader.getRequestUris());

  bulkLoader.load();

  clock.tick(2);
  assertFalse('The bulk loader is not yet loaded (after 2 ticks)', loadSuccess);

  clock.tick(3);
  assertFalse('The bulk loader is not yet loaded (after 5 ticks)', loadSuccess);

  clock.tick(5);
  assertFalse(
      'The bulk loader is not yet loaded (after 10 ticks)', loadSuccess);

  clock.tick(5);
  assertTrue('The bulk loader is loaded (after 15 ticks)', loadSuccess);

  assertArrayEquals(
      'Ensure that the response texts are present', successResponseTexts, uris);
}


/**
 * Test error loading URIs using the bulkloader.
 */
function testBulkLoaderLoadError() {
  var uris = ['a', 'b', 'c'];
  var bulkLoader = getNonSuccessfulBulkLoader(uris);

  bulkLoader.load();

  clock.tick(2);
  assertFalse('The bulk loader is not yet loaded (after 2 ticks)', loadError);

  clock.tick(3);
  assertFalse('The bulk loader is not yet loaded (after 5 ticks)', loadError);

  clock.tick(5);
  assertFalse('The bulk loader is not yet loaded (after 10 ticks)', loadError);

  clock.tick(5);
  assertFalse(
      'The bulk loader is not loaded successfully (after 15 ticks)',
      loadSuccess);
  assertTrue('The bulk loader is loaded in error (after 15 ticks)', loadError);
}