summaryrefslogtreecommitdiff
path: root/node_modules/ws/test/BufferPool.test.js
blob: 1ee7ff0fe32b13a3287f0179660e2405f52c564b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var BufferPool = require('../lib/BufferPool');
require('should');

describe('BufferPool', function() {  
  describe('#ctor', function() {
    it('allocates pool', function() {
      var db = new BufferPool(1000);
      db.size.should.eql(1000);
    });
  });
  describe('#get', function() {
    it('grows the pool if necessary', function() {
      var db = new BufferPool(1000);
      var buf = db.get(2000);
      db.size.should.be.above(1000);
      db.used.should.eql(2000);
      buf.length.should.eql(2000);
    });
    it('grows the pool after the first call, if necessary', function() {
      var db = new BufferPool(1000);
      var buf = db.get(1000);
      db.used.should.eql(1000);
      db.size.should.eql(1000);
      buf.length.should.eql(1000);
      var buf2 = db.get(1000);
      db.used.should.eql(2000);
      db.size.should.be.above(1000);
      buf2.length.should.eql(1000);
    });
    it('grows the pool according to the growStrategy if necessary', function() {
      var db = new BufferPool(1000, function(db, length) {
        return db.size + 2345;
      });
      var buf = db.get(2000);
      db.size.should.eql(3345);
      buf.length.should.eql(2000);
    });
    it('doesnt grow the pool if theres enough room available', function() {
      var db = new BufferPool(1000);
      var buf = db.get(1000);
      db.size.should.eql(1000);
      buf.length.should.eql(1000);      
    });
  });
  describe('#reset', function() {
    it('shinks the pool', function() {
      var db = new BufferPool(1000);
      var buf = db.get(2000);
      db.reset(true);
      db.size.should.eql(1000);
    });
    it('shrinks the pool according to the shrinkStrategy', function() {
      var db = new BufferPool(1000, function(db, length) {
        return db.used + length;
      }, function(db) {
        return 0;
      });
      var buf = db.get(2000);
      db.reset(true);
      db.size.should.eql(0);
    });
  });
});