Skip to content
Browse files

Added binary-safety test which SETs/GETs a PNG image; works fine, no …

…new release needed.
  • Loading branch information...
1 parent 4553931 commit 62e62a1d7777195cf4a386cb6302736f6ff580c4 @fictorial fictorial committed Apr 19, 2010
Showing with 55 additions and 23 deletions.
  1. +0 −6 TODO.md
  2. BIN test/sample.png
  3. +55 −17 test/test.js
View
6 TODO.md
@@ -8,12 +8,6 @@
reply, does not handle multi-bulk replies inside multi-bulk replies.
This is required for MULTI/EXEC.
-- Write an additional test of binary safety by reading a PNG from
- disk, writing it to redis, and reading it back, then comparing
- byte for byte that what we put in is what we got back. This sounds
- wonky but it might be useful for sending "binary" data (images,
- sounds, whatever) via PUBSUB (see NodeRed).
-
## Later
- Provide wrapper around the pretty-raw sort method?
View
BIN test/sample.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
72 test/test.js
@@ -1687,27 +1687,62 @@ var allTestFunctions = [
// after each test and we need 2 steps. If it was run with the other tests the
// first step's side effects would be flushed when the second step runs.
-function testLargeGetSet(callback) {
+function testLargeGetSet() {
+ showTestBanner("testLargeGetSet");
+
var fileContents = fs.readFileSync(__filename);
if (process._byteLength(fileContents) < client.requestBuffer.length)
assert.fail("the request buffer will not be forced to grow", "testGET (large; 0)");
client.set('largetestfile', fileContents, function (err, value) {
- if (err) assert.fail(err, "testGET (large; 1)");
- assert.equal(value, true, "testGET (large; 2)");
+ if (err) assert.fail(err, "testGET (large; 1)");
+ assert.equal(value, true, "testGET (large; 2)");
+
+ client.get('largetestfile', function (err, value) {
+ if (err) assert.fail(err, "testGET (large; 3)");
+ checkEqual(value.utf8Slice(0, value.length), fileContents, "testGET (large; 4)");
+ testStoreAnImage();
+ });
+ });
+}
+
+// Test binary-safety of values by storing an image (a PNG file).
+// This might be used in a PUBSUB-based app or some-such (hand-waving).
+
+function testStoreAnImage(callback) {
+ showTestBanner("testStoreAnImage");
+
+ var fileContents = fs.readFileSync('sample.png', 'binary');
+
+ if (!fileContents || fileContents.length == 0) {
+ sys.error("\nFailed to load sample.png -- skipping binary-safety test.\n");
+ testSUBSCRIBEandPUBLISH();
+ return;
+ }
+
+ // You can pass Buffer objects to the client's methods.
+
+ var imageBuffer = new Buffer(process._byteLength(fileContents));
+ imageBuffer.binaryWrite(fileContents, 0);
- client.get('largetestfile', function (err, value) {
- if (err) assert.fail(err, "testGET (large; 3)");
- checkEqual(value.utf8Slice(0, value.length), fileContents, "testGET (large; 4)");
- callback();
- });
+ client.set('png_image', imageBuffer, function (err, value) {
+ if (err) assert.fail(err, "testStoreAnImage (large; 1)");
+ assert.equal(value, true, "testStoreAnImage (large; 2)");
+
+ client.get('png_image', function (err, value) {
+ if (err) assert.fail(err, "testStoreAnImage (large; 3)");
+ checkEqual(value.binarySlice(0, value.length),
+ imageBuffer.binarySlice(0, imageBuffer.length),
+ "testStoreAnImage (large; 4)");
+ testSUBSCRIBEandPUBLISH();
+ });
});
}
function checkIfDone() {
if (client.originalCommands.length == 0) {
- testLargeGetSet(testSUBSCRIBEandPUBLISH);
+ testLargeGetSet();
var checks = 0;
setInterval(function () {
@@ -1727,16 +1762,19 @@ function checkIfDone() {
}
}
+function showTestBanner(name) {
+ if (verbose) {
+ sys.error("");
+ log("info", "Testing " + name);
+ sys.error("=========================================");
+ } else if (!quiet) {
+ sys.print(".");
+ }
+}
+
function runAllTests() {
allTestFunctions.forEach(function (testFunction) {
- if (verbose) {
- sys.error("");
- log("info", "Testing " + testFunction.name.replace(/^test/, ''));
- sys.error("=========================================");
- } else if (!quiet) {
- sys.print(".");
- }
-
+ showTestBanner(testFunction.name.replace(/^test/, ''));
clearTestDatabasesBeforeEachTest();
testFunction();
});

0 comments on commit 62e62a1

Please sign in to comment.
Something went wrong with that request. Please try again.