Skip to content

Commit f364694

Browse files
committed
some improvements to check args and some call test update
1 parent 4eacd99 commit f364694

File tree

4 files changed

+58
-24
lines changed

4 files changed

+58
-24
lines changed

lib/connection.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ TarantoolConnection.prototype._processResponse = function(buffer){
175175
if (success)
176176
dfd.resolve(this._processResponseBody(task[0], obj[1][tarantoolConstants.KeysCode.data]));
177177
else
178-
dfd.reject(obj[1][tarantoolConstants.KeysCode.error]);
178+
dfd.reject(new Error(obj[1][tarantoolConstants.KeysCode.error]));
179179
if (this.awaitingDestroy && this.commandsQueue.length == 1)
180180
{
181181
this.commandsQueue[0][2].resolve(true);
@@ -192,7 +192,7 @@ TarantoolConnection.prototype.onConnect = function(){
192192
};
193193

194194
TarantoolConnection.prototype.onError = function(error){
195-
this._interupt();
195+
this._interupt(error);
196196
this._stubMethods();
197197
this.socket.destroy();
198198
this.commandsQueue = [];
@@ -222,7 +222,7 @@ TarantoolConnection.prototype.connect = function(){
222222
}
223223
else
224224
{
225-
reject(this.awaitingDestroy ? 'already destroyed' : 'already connected');
225+
reject(new Error(this.awaitingDestroy ? 'already destroyed' : 'already connected'));
226226
if (this.options.log)
227227
{
228228
console.log('connection already destroyed');
@@ -270,24 +270,29 @@ TarantoolConnection.prototype.select = function(spaceId, indexId, limit, offset,
270270

271271
TarantoolConnection.prototype.delete = function(spaceId, indexId, key){
272272
return new Promise(function (resolve, reject) {
273-
var reqId = requestId.getId();
274-
var header = this._header(tarantoolConstants.RequestCode.rqDelete, reqId);
275-
var buffered = {
276-
spaceId: msgpack.encode(spaceId),
277-
indexId: msgpack.encode(indexId),
278-
key: msgpack.encode(key)
279-
};
280-
var body = Buffer.concat([new Buffer([0x86,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
281-
new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
282-
new Buffer([tarantoolConstants.KeysCode.key]), buffered.key]);
283-
this._request(header, body);
284-
this.commandsQueue.push([tarantoolConstants.RequestCode.rqSelect, reqId, {resolve: resolve, reject: reject}]);
273+
if (Array.isArray(key))
274+
{
275+
var reqId = requestId.getId();
276+
var header = this._header(tarantoolConstants.RequestCode.rqDelete, reqId);
277+
var buffered = {
278+
spaceId: msgpack.encode(spaceId),
279+
indexId: msgpack.encode(indexId),
280+
key: msgpack.encode(key)
281+
};
282+
var body = Buffer.concat([new Buffer([0x86,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
283+
new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
284+
new Buffer([tarantoolConstants.KeysCode.key]), buffered.key]);
285+
this._request(header, body);
286+
this.commandsQueue.push([tarantoolConstants.RequestCode.rqSelect, reqId, {resolve: resolve, reject: reject}]);
287+
}
288+
else
289+
reject(new Error('need array'));
285290
}.bind(this));
286291
};
287292

288293
TarantoolConnection.prototype.update = function(spaceId, indexId, key, ops){
289294
return new Promise(function (resolve, reject) {
290-
if (Array.isArray(ops)){
295+
if (Array.isArray(ops) && Array.isArray(key)){
291296
var reqId = requestId.getId();
292297
var header = this._header(tarantoolConstants.RequestCode.rqUpdate, reqId);
293298
var buffered = {
@@ -325,7 +330,7 @@ TarantoolConnection.prototype.eval = function(expression){
325330
};
326331

327332
TarantoolConnection.prototype.call = function(functionName){
328-
var tuple = Array.prototype.slice.call(arguments, 1);
333+
var tuple = arguments.length > 1 ? Array.prototype.slice.call(arguments, 1): [];
329334
return new Promise(function (resolve, reject) {
330335
var reqId = requestId.getId();
331336
var header = this._header(tarantoolConstants.RequestCode.rqCall, reqId);
@@ -412,7 +417,7 @@ TarantoolConnection.prototype.destroy = function(interupt){
412417
return new Promise(function (resolve, reject) {
413418
if (interupt)
414419
{
415-
this._interupt('force destroy socket');
420+
this._interupt(new Error('force destroy socket'));
416421
this.socket.destroy();
417422
resolve(true);
418423
}
@@ -437,7 +442,7 @@ TarantoolConnection.prototype.destroy = function(interupt){
437442

438443
TarantoolConnection.prototype._notAvailableMethod = function(){
439444
return new Promise(function (resolve, reject) {
440-
reject('connection will be destroyed or already destroyed, create another one');
445+
reject(new Error('connection will be destroyed or already destroyed, create another one'));
441446
});
442447
};
443448

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tarantool-driver",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Tarantool driver for 1.6",
55
"main": "index.js",
66
"scripts": {

test/app.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ describe('Tarantool Connection tests', function(){
3636
console.log('before call');
3737
try{
3838
Promise.all([conn.delete(514, 0, [1]),conn.delete(514, 0, [2]),conn.delete(514, 0, [3]),conn.delete(514, 0, [4] )])
39+
.then(function(){
40+
return conn.call('clearaddmore');
41+
})
3942
.then(function(){
4043
done();
4144
})
@@ -100,6 +103,15 @@ describe('Tarantool Connection tests', function(){
100103
done();
101104
}, function(e){done(e);});
102105
});
106+
it('dup error', function(done){
107+
conn.insert(512, insertTuple)
108+
.then(function(a){
109+
done(new Error('can insert'));
110+
}, function(e){
111+
assert(e instanceof Error);
112+
done();
113+
});
114+
});
103115
it('update', function(done){
104116
conn.update(512, 0, [50], [['+',3,10]])
105117
.then(function(a){
@@ -110,8 +122,8 @@ describe('Tarantool Connection tests', function(){
110122
});
111123
it('a lot of insert', function(done){
112124
var promises = [];
113-
for (var i = 0; i <= 1000; i++) {
114-
conn.insert(512, ['key' + i, i]);
125+
for (var i = 0; i <= 5000; i++) {
126+
conn.insert(515, ['key' + i, i]);
115127
}
116128
Promise.all(promises)
117129
.then(function(pr){

test/box.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,29 @@ if not box.space.batched then
5151
end
5252

5353
function batch (data)
54-
for index, value in pairs(data) do
54+
print(data)
55+
for index,value in pairs(data) do
5556
box.space.batched:insert(value)
5657
end
5758
end
5859

5960
function myget(id)
6061
val = box.space.batched:select{id}
6162
return val[1]
62-
end
63+
end
64+
65+
if not box.space.toaddmore then
66+
local toaddmore = box.schema.space.create('toaddmore')
67+
toaddmore:create_index('primary', {type = 'TREE', unique = true, parts = {1, 'STR'}})
68+
box.schema.user.grant('test', 'read,write,execute', 'space', 'toaddmore')
69+
box.schema.user.grant('test', 'read,write,execute', 'space', '_index')
70+
end
71+
72+
function clearaddmore()
73+
local values = box.space.toaddmore:select{}
74+
for k,v in pairs(values) do
75+
if v[1] then
76+
box.space.toaddmore:delete{v[1]}
77+
end
78+
end
79+
end

0 commit comments

Comments
 (0)