Skip to content

Commit

Permalink
SPRXCLT-12: factor common code in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Monjalet committed Aug 2, 2023
1 parent 221c70d commit 7d93f84
Showing 1 changed file with 20 additions and 42 deletions.
62 changes: 20 additions & 42 deletions tests/unit/sproxyd.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ function _batchDelKeys(n) {
return list;
}

function checkKeyContent(client, key, body, reqUid, callback) {
client.get(key, undefined, reqUid, (err, stream) => {
let ret = Buffer.alloc(0);
if (err) {
done(err);
} else {
stream.on('data', val => {
ret = Buffer.concat([ret, val]);
});
stream.on('end', () => {
assert.deepStrictEqual(ret, body);
callback();
});
}
});
}

function makeResponse(res, code, message, data, md) {
/* eslint-disable no-param-reassign */
res.statusCode = code;
Expand Down Expand Up @@ -230,20 +247,7 @@ const clientImmutableWithFailover = new Sproxy({
});

it('should get some data via sproxyd', done => {
client.get(savedKey, undefined, reqUid, (err, stream) => {
let ret = Buffer.alloc(0);
if (err) {
done(err);
} else {
stream.on('data', val => {
ret = Buffer.concat([ret, val]);
});
stream.on('end', () => {
assert.deepStrictEqual(ret, upload);
done();
});
}
});
checkKeyContent(client, savedKey, upload, reqUid, done);
});

it('should delete some data via sproxyd', done => {
Expand Down Expand Up @@ -344,35 +348,9 @@ const clientImmutableWithFailover = new Sproxy({
next();
}),
// Get data for the first key and assert that it matches the original upload
next => client.get(savedKey1, undefined, reqUid, (err, stream) => {
if (err) {
done(err);
return;
}
let ret = Buffer.alloc(0);
stream.on('data', val => {
ret = Buffer.concat([ret, val]);
});
stream.on('end', () => {
assert.deepStrictEqual(ret, upload);
next();
});
}),
next => checkKeyContent(client, savedKey1, upload, reqUid, next),
// Get data for the second key and assert that it matches the original upload
_ => client.get(savedKey2, undefined, reqUid, (err, stream) => {
if (err) {
done(err);
return;
}
ret = Buffer.alloc(0);
stream.on('data', val => {
ret = Buffer.concat([ret, val]);
});
stream.on('end', () => {
assert.deepStrictEqual(ret, upload);
done();
});
}),
_ => checkKeyContent(client, savedKey2, upload, reqUid, done),
]);
});
});
Expand Down

0 comments on commit 7d93f84

Please sign in to comment.