database.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 _ = require('lodash');
  18. var should = require('should');
  19. var env = require('../env');
  20. var Database = require('../../app/database');
  21. var logger = require('memdb-logger').getLogger('test', __filename);
  22. describe('database test', function(){
  23. beforeEach(env.flushdb);
  24. // it('find/update/insert/remove/commit/rollback', function(cb){
  25. // //tested in ../lib/connection
  26. // });
  27. // it('index test', function(cb){
  28. // //tested in ../lib/connection
  29. // });
  30. it('persistent / idle timeout', function(cb){
  31. var config = env.shardConfig('s1');
  32. config.persistentDelay = 50;
  33. config.idleTimeout = 500;
  34. var db = new Database(config);
  35. var conn = null;
  36. var collName = 'player', doc = {_id : '1', name : 'rain'};
  37. return P.try(function(){
  38. return db.start();
  39. })
  40. .then(function(){
  41. conn = db.getConnection(db.connect().connId);
  42. return conn.insert(collName, doc);
  43. })
  44. .then(function(){
  45. return conn.commit();
  46. })
  47. .delay(300) // doc persistented
  48. .then(function(){
  49. // read from backend
  50. return db.shard.backend.get(collName, doc._id)
  51. .then(function(ret){
  52. ret.should.eql(doc);
  53. });
  54. })
  55. .delay(500) // doc idle timed out
  56. .then(function(){
  57. db.shard._isLoaded(collName + '$' + doc._id).should.eql(false);
  58. })
  59. .then(function(){
  60. return conn.remove(collName, doc._id);
  61. })
  62. .then(function(){
  63. return conn.commit();
  64. })
  65. .then(function(){
  66. return db.stop();
  67. })
  68. .nodeify(cb);
  69. });
  70. it('restore from slave', function(cb){
  71. var db1 = null, db2 = null;
  72. var conn = null;
  73. var player1 = {_id : 'p1', name : 'rain', age: 30};
  74. var player2 = {_id : 'p2', name : 'snow', age: 25};
  75. return P.try(function(){
  76. var config = env.shardConfig('s1');
  77. config.heartbeatInterval = -1; // disable heartbeat
  78. config.gcInterval = 3600 * 1000; // disable gc
  79. db1 = new Database(config);
  80. return db1.start();
  81. })
  82. .then(function(){
  83. conn = db1.getConnection(db1.connect().connId);
  84. })
  85. .then(function(){
  86. return conn.insert('player', player1);
  87. })
  88. .then(function(){
  89. return conn.insert('player', player2);
  90. })
  91. .then(function(){
  92. return conn.commit();
  93. })
  94. .then(function(){
  95. db1.shard.state = 4; // Db is suddenly stopped
  96. })
  97. .then(function(){
  98. //restart db
  99. db2 = new Database(env.shardConfig('s1'));
  100. return db2.start();
  101. })
  102. .then(function(){
  103. conn = db2.getConnection(db2.connect().connId);
  104. })
  105. .then(function(){
  106. return P.try(function(){
  107. return conn.find('player', player1._id);
  108. })
  109. .then(function(ret){
  110. ret.should.eql(player1);
  111. });
  112. })
  113. .then(function(){
  114. return P.try(function(){
  115. return conn.find('player', player2._id);
  116. })
  117. .then(function(ret){
  118. ret.should.eql(player2);
  119. });
  120. })
  121. .then(function(){
  122. conn.close();
  123. return db2.stop();
  124. })
  125. .finally(function(){
  126. // clean up
  127. db1.shard.state = 2;
  128. return db1.stop(true);
  129. })
  130. .nodeify(cb);
  131. });
  132. });