push.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2015 rain1017.
  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 env = require('../env');
  17. var quick = require('../../lib');
  18. var P = quick.Promise;
  19. var logger = quick.logger.getLogger('test', __filename);
  20. describe('push test', function(){
  21. beforeEach(env.initMemdbSync);
  22. afterEach(env.closeMemdbSync);
  23. it('push test', function(cb){
  24. var app = quick.mocks.app({serverId : 'area1', serverType : 'area'});
  25. app.set('memdbConfig', env.memdbConfig);
  26. app.set('controllersConfig', {basePath : 'lib/controllers'});
  27. app.set('pushConfig', {maxMsgCount : 2});
  28. app.load(quick.components.memdb);
  29. app.load(quick.components.controllers);
  30. return P.try(function(){
  31. return P.promisify(app.start, app)();
  32. })
  33. .then(function(){
  34. var autoconn = app.memdb.goose.autoconn;
  35. var push = app.controllers.push;
  36. return autoconn.transaction(function(){
  37. return P.try(function(){
  38. //p1 join c1 (connector s1)
  39. return push.joinAsync('c1', 'p1', 's1');
  40. })
  41. .then(function(){
  42. //send persistent msg to p1
  43. return push.pushAsync('c1', null, 'chat', 'hello1', true);
  44. })
  45. .then(function(){
  46. //p2 (offline) join c1
  47. return push.joinAsync('c1', 'p2', '');
  48. })
  49. .then(function(){
  50. //send persistent msg to p1 & p2 (p2 will not receive notify but can read msg history)
  51. return push.pushAsync('c1', null, 'chat', 'hello2', true);
  52. })
  53. .then(function(){
  54. //send msg to p1 (p2 will not see this message)
  55. return push.pushAsync('c1', null, 'notify', 'context', false);
  56. })
  57. .then(function(){
  58. //p2 is online (connector s2)
  59. return push.connectAsync('p2', 's2');
  60. })
  61. .then(function(){
  62. //send msg to p1 & p2 (both will receive notify)
  63. return push.pushAsync('c1', null, 'notify', 'context', false);
  64. })
  65. .then(function(){
  66. //send msg to p2 only
  67. return push.pushAsync('c1', ['p2'], 'notify', 'content', false);
  68. })
  69. .then(function(){
  70. //get message history
  71. return push.getMsgsAsync('c1', 0)
  72. .then(function(ret){
  73. ret.length.should.eql(2);
  74. ret[0].should.eql({
  75. route : 'chat',
  76. msg : 'hello1',
  77. seq : 0,
  78. });
  79. ret[1].should.eql({
  80. route : 'chat',
  81. msg : 'hello2',
  82. seq : 1,
  83. });
  84. });
  85. })
  86. .then(function(){
  87. //send 3rd persistent message, will exceed history limit, history will be cut down
  88. return push.pushAsync('c1', null, 'chat', 'hello3', true);
  89. })
  90. .then(function(){
  91. //get history
  92. return push.getMsgsAsync('c1', 0, 3)
  93. .then(function(ret){
  94. //can get only latest 2 messages
  95. ret.length.should.eql(2);
  96. ret[0].should.eql({
  97. route : 'chat',
  98. msg : 'hello2',
  99. seq : 1,
  100. });
  101. ret[1].should.eql({
  102. route : 'chat',
  103. msg : 'hello3',
  104. seq : 2,
  105. });
  106. });
  107. })
  108. .then(function(){
  109. return push.disconnectAsync('p1');
  110. })
  111. .then(function(){
  112. return push.quitAsync('c1', 'p1');
  113. })
  114. .then(function(){
  115. return push.quitAsync('c1', 'p2');
  116. });
  117. });
  118. })
  119. .then(function(){
  120. app.event.emit('transactionSuccess');
  121. }, function(err){
  122. app.event.emit('transactionFail');
  123. throw err;
  124. })
  125. .then(function(){
  126. return P.promisify(app.stop, app)();
  127. })
  128. .nodeify(cb);
  129. });
  130. });