connectionService.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var should = require('should');
  2. var ConnectionService = require('../../lib/common/service/connectionService');
  3. var mockApp = {
  4. settings: {
  5. serverId: 'connector-server-1'
  6. },
  7. get: function(key) {
  8. return this.settings[key];
  9. },
  10. getServerId: function() {
  11. return this.get('serverId');
  12. }
  13. };
  14. describe('connection service test', function() {
  15. describe('#addLoginedUser', function() {
  16. it('should add logined userstate and could fetch it later', function() {
  17. var service = new ConnectionService(mockApp);
  18. should.exist(service);
  19. service.loginedCount.should.equal(0);
  20. var uid = 'uid1';
  21. var info = {msg: 'some other message'};
  22. service.addLoginedUser(uid, info);
  23. service.loginedCount.should.equal(1);
  24. var record = service.logined[uid];
  25. should.exist(record);
  26. record.should.eql(info);
  27. });
  28. });
  29. describe('#increaseConnectionCount', function() {
  30. it('should increate connection count and could fetch it later', function() {
  31. var service = new ConnectionService(mockApp);
  32. should.exist(service);
  33. service.connCount.should.equal(0);
  34. service.increaseConnectionCount();
  35. service.connCount.should.equal(1);
  36. });
  37. });
  38. describe('#removeLoginedUser', function() {
  39. it('should remove logined userstate info with the uid', function() {
  40. var service = new ConnectionService(mockApp);
  41. should.exist(service);
  42. service.loginedCount.should.equal(0);
  43. var uid = 'uid1';
  44. var info = {msg: 'some other message'};
  45. service.addLoginedUser(uid, info);
  46. service.loginedCount.should.equal(1);
  47. var record = service.logined[uid];
  48. should.exist(record);
  49. var uid2 = 'uid2';
  50. service.removeLoginedUser(uid2);
  51. service.loginedCount.should.equal(1);
  52. record = service.logined[uid];
  53. should.exist(record);
  54. service.removeLoginedUser(uid);
  55. service.loginedCount.should.equal(0);
  56. record = service.logined[uid];
  57. should.not.exist(record);
  58. });
  59. });
  60. describe('#decreaseConnectionCount', function() {
  61. it('should decrease connection count only if uid is empty', function() {
  62. var service = new ConnectionService(mockApp);
  63. should.exist(service);
  64. service.increaseConnectionCount();
  65. service.connCount.should.equal(1);
  66. service.decreaseConnectionCount();
  67. service.connCount.should.equal(0);
  68. });
  69. it('should keep zero if connection count become zero', function() {
  70. var service = new ConnectionService(mockApp);
  71. should.exist(service);
  72. service.connCount.should.equal(0);
  73. service.decreaseConnectionCount();
  74. service.connCount.should.equal(0);
  75. });
  76. it('should remove the logined info if uid is specified', function() {
  77. var service = new ConnectionService(mockApp);
  78. should.exist(service);
  79. service.increaseConnectionCount();
  80. var uid = 'uid1';
  81. var info = {msg: 'some other message'};
  82. service.addLoginedUser(uid, info);
  83. service.connCount.should.equal(1);
  84. service.logined[uid].should.eql(info);
  85. service.decreaseConnectionCount(uid);
  86. service.connCount.should.equal(0);
  87. should.not.exist(service.logined[uid]);
  88. });
  89. });
  90. it('should getStatisticsInfo', function(done){
  91. var service = new ConnectionService(mockApp);
  92. var uid1 = 'uid1', uid2 = 'uid2';
  93. var info1 = 'msg1', info2 = 'msg2';
  94. service.increaseConnectionCount();
  95. service.increaseConnectionCount();
  96. service.increaseConnectionCount();
  97. service.addLoginedUser(uid1, info1);
  98. service.addLoginedUser(uid2, info2);
  99. var sinfo = service.getStatisticsInfo();
  100. sinfo.should.have.property('serverId', 'connector-server-1');
  101. sinfo.should.have.property('totalConnCount', 3);
  102. sinfo.should.have.property('loginedCount', 2);
  103. var infos = sinfo.loginedList;
  104. should.exist(infos);
  105. infos.length.should.equal(2);
  106. infos.should.include(info1);
  107. infos.should.include(info2);
  108. done();
  109. });
  110. });