backendlocker.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var P = require('bluebird');
  17. var should = require('should');
  18. var logger = require('memdb-logger').getLogger('test', __filename);
  19. var BackendLocker = require('../../app/backendlocker');
  20. var env = require('../env');
  21. describe('backendlocker test', function(){
  22. it('locker / heartbeat', function(cb){
  23. var docKey = 'player$p1', shardId = 's1';
  24. var locker = new BackendLocker({
  25. host : env.config.locking.host,
  26. port : env.config.locking.port,
  27. db : env.config.locking.db,
  28. shardId : shardId,
  29. heartbeatTimeout : 2000,
  30. heartbeatInterval : 1000,
  31. });
  32. return P.try(function(){
  33. return locker.start();
  34. })
  35. .then(function(){
  36. return locker.tryLock(docKey);
  37. })
  38. .then(function(){
  39. return locker.getHolderId(docKey)
  40. .then(function(ret){
  41. ret.should.eql(shardId);
  42. });
  43. })
  44. .then(function(ret){
  45. return locker.isHeld(docKey)
  46. .then(function(ret){
  47. ret.should.be.true; //jshint ignore:line
  48. });
  49. })
  50. .then(function(){
  51. // Can't lock again
  52. return locker.tryLock(docKey)
  53. .then(function(ret){
  54. ret.should.be.false; //jshint ignore:line
  55. });
  56. })
  57. .then(function(){
  58. return locker.unlock(docKey);
  59. })
  60. .finally(function(){
  61. return locker.stop();
  62. })
  63. .nodeify(cb);
  64. });
  65. });