Repository: basho/riak-nodejs-client
Pull request #55 (2015-05-11 22:14:57)

READY: CLIENTS-402
Add the timeout field to RpbYokozunaIndexPutReq


Patch:
diff --git a/lib/commands/kv/deletevalue.js b/lib/commands/kv/deletevalue.js
index 42a9054..c7e1b76 100644
--- a/lib/commands/kv/deletevalue.js
+++ b/lib/commands/kv/deletevalue.js
@@ -273,7 +273,8 @@ Builder.prototype = {
     withVClock : function(vclock) {
         this.vclock = vclock;
         return this;
-    },/**
+    },
+    /**
     * Set a timeout for this operation.
     * @method withTimeout
     * @param {Number} timeout a timeout in milliseconds.
@@ -283,7 +284,6 @@ Builder.prototype = {
         this.timeout = timeout;
         return this;
     },
-
     /**
      * Set the callback to be executed when the operation completes.
      * @method withCallback
diff --git a/lib/commands/yokozuna/storeindex.js b/lib/commands/yokozuna/storeindex.js
index 9bbc20b..19b56d1 100644
--- a/lib/commands/yokozuna/storeindex.js
+++ b/lib/commands/yokozuna/storeindex.js
@@ -43,27 +43,23 @@ var RpbYokozunaIndex = require('../../protobuf/riakprotobuf').getProtoFor('RpbYo
  * @param {Object} options The options for this command
  * @param {String} options.indexName The name of the index.
  * @param {String} [options.schemaName=_yz_default] The name of the schema for this index.
+ * @param {Number} [options.timeout] Set a timeout for this operation.
  * @param {Function} callback The callback to be executed when the operation completes.
  * @param {String} callback.err An error message. Will be null if no error.
  * @param {Boolean} callback.response The operation either succeeds or errors. This will be true.
  * @extends CommandBase
  */
 function StoreIndex(options, callback) {
-
     CommandBase.call(this, 'RpbYokozunaIndexPutReq' , 'RpbPutResp', callback);
     var self = this;
     Joi.validate(options, schema, function(err, options) {
-
         if (err) {
             throw err;
         }
-
         self.options = options;
     });
 
     this.remainingTries = 1;
-
-
 }
 
 inherits(StoreIndex, CommandBase);
@@ -73,27 +69,33 @@ StoreIndex.prototype.constructPbRequest = function() {
     var protobuf = this.getPbReqBuilder();
 
     var pbIndex = new RpbYokozunaIndex();
+
     pbIndex.setName(new Buffer(this.options.indexName));
+
     if (this.options.schemaName) {
         pbIndex.setSchema(new Buffer(this.options.schemaName));
     }
+
     protobuf.index = pbIndex;
+
+    if (this.options.timeout) {
+        protobuf.setTimeout(this.options.timeout);
+    }
+
     return protobuf;
 
 };
 
 StoreIndex.prototype.onSuccess = function(RpbPutResp) {
-
     // RpbPutResp is simply null (no body)
-
     this._callback(null, true);
     return true;
 };
 
-
 var schema = Joi.object().keys({
     indexName: Joi.string().required(),
-    schemaName: Joi.string().default(null).optional()
+    schemaName: Joi.string().default(null).optional(),
+    timeout: Joi.number().default(null).optional()
 });
 
 /**
@@ -136,6 +138,16 @@ Builder.prototype = {
         this.schemaName = schemaName;
         return this;
     },
+    /**
+    * Set a timeout for this operation.
+    * @method withTimeout
+    * @param {Number} timeout a timeout in milliseconds.
+    * @chainable
+    */
+    withTimeout : function(timeout) {
+        this.timeout = timeout;
+        return this;
+    },
     /**
      * Set the callback to be executed when the operation completes.
      * @method withCallback


Test patch:
diff --git a/test/unit/yokozuna/storeindex.js b/test/unit/yokozuna/storeindex.js
index 4572de6..ce05a4f 100644
--- a/test/unit/yokozuna/storeindex.js
+++ b/test/unit/yokozuna/storeindex.js
@@ -20,7 +20,6 @@ var RpbErrorResp = require('../../../lib/protobuf/riakprotobuf').getProtoFor('Rp
 
 var assert = require('assert');
 
-
 describe('StoreIndex', function() {
     describe('Build', function() {
         it('should build a RpbYokozunaIndexPutReq correctly', function(done) {
@@ -28,55 +27,83 @@ describe('StoreIndex', function() {
             var store = new StoreIndex.Builder()
                     .withIndexName('indexName')
                     .withSchemaName('schemaName')
+                    .withTimeout(60000)
                     .withCallback(function(){})
                     .build();
             
             var protobuf = store.constructPbRequest();
+            assert.equal(protobuf.getTimeout(), 60000);
+
             var index = protobuf.index;
             assert.equal(index.getName().toString('utf8'), 'indexName');
             assert.equal(index.getSchema().toString('utf8'), 'schemaName');
             done();
         });
+
+        it('should only require name and callback', function(done) {
+
+            assert.throws(function () {
+                new StoreIndex.Builder().build();
+            });
+
+            assert.throws(function () {
+                new StoreIndex.Builder()
+                    .withIndexName('indexName')
+                    .build();
+            });
+
+            assert.throws(function () {
+                new StoreIndex.Builder()
+                    .withCallback(function () {})
+                    build();
+            });
+
+            assert.doesNotThrow(function () {
+                new StoreIndex.Builder()
+                    .withIndexName('indexName')
+                    .withCallback(function () {})
+                    .build();
+            });
+
+            done();
+        });
     });
     
     it('should take a RpbPutResp and call the users callback with the response', function(done) {
-           
             
-            var callback = function(err, response) {
-              
-                assert.equal(response, true);
+        var callback = function(err, response) {
+            assert.equal(response, true);
+            done();
+        };
+        
+        var store = new StoreIndex.Builder()
+                .withIndexName('indexName')
+                .withCallback(callback)
+                .build();
+        
+        // the RpbPutResp is a null message (no body) from riak
+        store.onSuccess(null);
+        
+    });
+        
+    it ('should take a RpbErrorResp and call the users callback with the error message', function(done) {
+        var rpbErrorResp = new RpbErrorResp();
+        rpbErrorResp.setErrmsg(new Buffer('this is an error'));
+        
+        var callback = function(err, response) {
+            if (err) {
+                assert.equal(err,'this is an error');
                 done();
-            };
-            
-            var store = new StoreIndex.Builder()
-                    .withIndexName('indexName')
-                    .withCallback(callback)
-                    .build();
-            
-            // the RpbPutResp is a null message (no body) from riak
-            store.onSuccess(null);
-            
-        });
+            }
+        };
         
-        it ('should take a RpbErrorResp and call the users callback with the error message', function(done) {
-           var rpbErrorResp = new RpbErrorResp();
-           rpbErrorResp.setErrmsg(new Buffer('this is an error'));
-           
-           var callback = function(err, response) {
-               if (err) {
-                   assert.equal(err,'this is an error');
-                   done();
-               }
-           };
-           
-           var store = new StoreIndex.Builder()
-                    .withIndexName('indexName')
-                    .withCallback(callback)
-                    .build();
-            
-            store.onRiakError(rpbErrorResp);
-           
-       });
-
+        var store = new StoreIndex.Builder()
+                .withIndexName('indexName')
+                .withCallback(callback)
+                .build();
+        
+        store.onRiakError(rpbErrorResp);
+        
+    });
     
 });