/
read.js
233 lines (190 loc) · 7.51 KB
/
read.js
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
225
226
227
228
229
230
231
232
var common = require('./common');
var server = common.server;
var resource = common.resource;
var util = require('./util');
var chai = require('chai');
var chaiHttp = require('chai-http');
var should = chai.should();
var expect = chai.expect;
chai.use(chaiHttp);
var Promise = require('bluebird');
var info = util.httpSync('/info');
var bounds = {
xmin: info.bounds[0], ymin: info.bounds[1], zmin: info.bounds[2],
xmax: info.bounds[3], ymax: info.bounds[4], zmax: info.bounds[5]
};
describe('read', () => {
it('404s nonexistent resources', (done) => {
chai.request(server).get('/resource/i-do-not-exist/read')
.end((err, res) => {
res.should.have.status(404);
done();
});
});
it('reads all existing data', (done) => {
var schema = util.xyz;
util.read({ schema: schema })
.then((res) => {
res.should.have.status(200);
var numPoints = util.numPointsFrom(res.body, schema);
var pointSize = util.pointSizeFrom(schema);
var numBytes = numPoints * pointSize;
expect(numPoints).to.equal(info.numPoints);
var view = new DataView(res.body);
var x, y, z;
for (var offset = 0; offset < numBytes; offset += pointSize)
{
x = view.getFloat32(offset, true);
y = view.getFloat32(offset + 4, true);
z = view.getFloat32(offset + 8, true);
expect(x).to.be.within(bounds.xmin, bounds.xmax);
expect(y).to.be.within(bounds.ymin, bounds.ymax);
expect(z).to.be.within(bounds.zmin, bounds.zmax);
}
done();
});
});
it('works with depth ranges', (done) => {
var schema = util.xyz;
var depths = [];
var depthEnd = 16;
for (var i = 0; i < depthEnd; ++i) depths.push(i);
var reads = depths.reduce((p, c, i, a) => {
return p.concat(util.read({
schema: schema,
depthBegin: a[i],
depthEnd: i == depthEnd - 1 ? 128 : a[i] + 1
}));
}, []);
// TODO This depth is kind of magical at the moment, should probably
// expose it publicly through `info`.
var startDepth = 6;
var hier = util.httpSync('/hierarchy' +
'?depthBegin=' + startDepth +
'&depthEnd=' + depthEnd + '&vertical=true');
Promise.all(reads).then((results) => {
var numPoints = results.reduce((p, c) => {
return p + util.numPointsFrom(c.body, schema);
}, 0);
for (var i = startDepth; i < results.length; ++i) {
if (i - startDepth < hier.length) {
expect(hier[i - startDepth]).to.equal(
util.numPointsFrom(results[i].body, schema));
}
}
expect(numPoints).to.equal(info.numPoints);
done();
});
});
var climb = (randomize) => {
var concurrent = 12;
var schema = util.xyz;
var pointSize = util.pointSizeFrom(schema);
var bounds = info.bounds;
var baseDepth = 7;
// If we get back a small response, don't subdivide.
var stopSplitNumPoints = 2048;
var numPoints = 0;
var outstanding = 0;
var traverse = (bounds, depth) => {
var p = new Promise((resolve) => {
if (outstanding < concurrent) {
++outstanding;
var hier;
if (!randomize) {
hier = util.httpSync(
'/hierarchy?depth=' + depth +
'&bounds=' + JSON.stringify(bounds));
}
util.read({ schema: schema, bounds: bounds, depth: depth })
.then((result) => {
--outstanding;
var n = util.numPointsFrom(result.body, schema);
numPoints += n;
var next = [];
if (!randomize) expect(n).to.equal(hier ? hier.n : 0);
if (n) {
if (n < stopSplitNumPoints) {
next.push(traverse(bounds, depth + 1));
}
else util.split(bounds, randomize).forEach((b) => {
next.push(traverse(b, depth + 1));
});
return Promise.all(next);
}
else return null;
})
.then(resolve);
}
else setTimeout(() => {
traverse(bounds, depth).then(resolve);
}, 200);
});
return p;
};
return util.read({ schema: schema, depthEnd: baseDepth })
.then((result) => numPoints += util.numPointsFrom(result.body, schema))
.then(() => traverse(bounds, baseDepth))
.then(() => numPoints);
};
it('works with traversal-split bounds', (done) => {
climb()
.then((numPoints) => expect(numPoints).to.equal(info.numPoints))
.then(() => done());
});
it('works with arbitrarily split bounds', (done) => {
climb(true)
.then((numPoints) => expect(numPoints).to.equal(info.numPoints))
.then(() => done());
});
it('errors gracefully for out-of-range values', (done) => {
// This dataset has intensity values of 255, which won't fit into a
// signed byte.
var schema = [
{ name: 'X', type: 'floating', size: 4 },
{ name: 'Y', type: 'floating', size: 4 },
{ name: 'Z', type: 'floating', size: 4 },
{ name: 'Intensity', type: 'signed', size: 1 }
];
util.read({ depth: 4, schema: schema })
.then((res) => {
res.should.have.status(400);
done();
});
});
it('works when no spatial dimensions are specified', (done) => {
var schema = [{ name: 'Intensity', type: 'unsigned', size: 1 }];
util.read({ depth: 4, schema: schema })
.then((res) => {
res.should.have.status(200);
var numPoints = util.numPointsFrom(res.body, schema);
var pointSize = util.pointSizeFrom(schema);
var numBytes = numPoints * pointSize;
expect(numPoints).to.equal(numBytes);
done();
});
});
it('zeroes out unrecognized dimensions', (done) => {
var schema = [
{ name: 'X', type: 'floating', size: 4 },
{ name: 'Y', type: 'floating', size: 4 },
{ name: 'Z', type: 'floating', size: 4 },
{ name: 'DoesNotExist', type: 'unsigned', size: 1 }
];
util.read({ depth: 4, schema: schema })
.then((res) => {
res.should.have.status(200);
var numPoints = util.numPointsFrom(res.body, schema);
var pointSize = util.pointSizeFrom(schema);
var numBytes = numPoints * pointSize;
expect(numPoints).to.be.above(0);
var view = new DataView(res.body);
var dimOffset = util.getOffset('DoesNotExist', schema);
for (var offset = 0; offset < numBytes; offset += pointSize)
{
expect(view.getUint8(offset + dimOffset, true)).to.equal(0);
}
done();
});
});
});