You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.9 KiB

  1. var BufferPool = require('../lib/BufferPool');
  2. require('should');
  3. describe('BufferPool', function() {
  4. describe('#ctor', function() {
  5. it('allocates pool', function() {
  6. var db = new BufferPool(1000);
  7. db.size.should.eql(1000);
  8. });
  9. });
  10. describe('#get', function() {
  11. it('grows the pool if necessary', function() {
  12. var db = new BufferPool(1000);
  13. var buf = db.get(2000);
  14. db.size.should.be.above(1000);
  15. db.used.should.eql(2000);
  16. buf.length.should.eql(2000);
  17. });
  18. it('grows the pool after the first call, if necessary', function() {
  19. var db = new BufferPool(1000);
  20. var buf = db.get(1000);
  21. db.used.should.eql(1000);
  22. db.size.should.eql(1000);
  23. buf.length.should.eql(1000);
  24. var buf2 = db.get(1000);
  25. db.used.should.eql(2000);
  26. db.size.should.be.above(1000);
  27. buf2.length.should.eql(1000);
  28. });
  29. it('grows the pool according to the growStrategy if necessary', function() {
  30. var db = new BufferPool(1000, function(db, length) {
  31. return db.size + 2345;
  32. });
  33. var buf = db.get(2000);
  34. db.size.should.eql(3345);
  35. buf.length.should.eql(2000);
  36. });
  37. it('doesnt grow the pool if theres enough room available', function() {
  38. var db = new BufferPool(1000);
  39. var buf = db.get(1000);
  40. db.size.should.eql(1000);
  41. buf.length.should.eql(1000);
  42. });
  43. });
  44. describe('#reset', function() {
  45. it('shinks the pool', function() {
  46. var db = new BufferPool(1000);
  47. var buf = db.get(2000);
  48. db.reset(true);
  49. db.size.should.eql(1000);
  50. });
  51. it('shrinks the pool according to the shrinkStrategy', function() {
  52. var db = new BufferPool(1000, function(db, length) {
  53. return db.used + length;
  54. }, function(db) {
  55. return 0;
  56. });
  57. var buf = db.get(2000);
  58. db.reset(true);
  59. db.size.should.eql(0);
  60. });
  61. });
  62. });