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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | 1 1 1 1 1 1 1 16 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /* jshint jasmine: true */ 'use strict'; var StreamingCache = require('../index'); var Duplex = require('stream').Duplex; var Writable = require('stream').Writable; var cache = new StreamingCache(); describe('streaming cache', function () { var s; beforeEach(function () { cache = new StreamingCache() s = cache.set('a'); }) it('cache.set needs to be called with a key', function () { expect(function () {cache.set();}).toThrow('Key expected'); }); it('cache.set should return a stream', function () { expect(s).toEqual(jasmine.any(Duplex)); }); it('Writing to stream should set data', function (done) { s.write('a'); s.write('b'); cache.getData('a', function (err, data) { expect(data.toString()).toEqual('ab') done(); }); s.end(null); }); it('reading from a duplex stream should return all data', function(done){ var res = ''; var stream = new Writable({ write(chunk, encoding, callback) { res += chunk; callback(); } }); stream.on('finish', function(){ expect(res).toEqual('abc'); done(); }) var cacheStream = s; cacheStream.pipe(stream); cacheStream._read(); cacheStream._read(); cacheStream.write('a'); cacheStream.write('b'); cacheStream.write('c'); cacheStream.end(); }); it('getting stream should return readstream', function () { var r = cache.get('a'); expect(r).toEqual(jasmine.any(require('../lib/readStream'))); }); it('should be written to and readable when set has ended', function (done) { s.write('ggg'); var r = cache.get('a'); r.on('data', function (chunk) { expect(chunk.toString()).toEqual('ggg') done() }) s.end(); expect(r).toEqual(jasmine.any(require('../lib/readStream'))); }); it('should be written to and readable when set is pending', function (done) { var dataSpy = jasmine.createSpy(); var endSpy = jasmine.createSpy(); s.write('hhh'); var r = cache.get('a'); r.on('data', dataSpy); r.on('end', endSpy); s.write('ggg'); s.end('iii'); setTimeout(function () { expect(dataSpy).toHaveBeenCalled(); expect(dataSpy.calls.length).toEqual(1); expect(dataSpy.calls[0].args.toString()).toEqual('hhhgggiii'); expect(endSpy.calls.length).toEqual(1); done(); }, 200) }); it('should handle sync setData', function () { expect(cache.setData).toThrow(); cache.setData('b', new Buffer(100)); expect(cache.cache.get('b').status).toEqual(2); expect(cache.cache.get('b').data.length).toEqual(100); }); it('should handle getData when data is missing', function (done) { expect(cache.getData).toThrow(); expect(function () { cache.getData('c') }).toThrow(); expect(cache.getData('c', function (err, data) { expect(data).toEqual(undefined); expect(err).toEqual('Cache miss'); done(); })); }); it('should handle setmetadata', function () { cache.setMetadata('abc', {a: 'c'}); cache.setData('abc', 'test'); expect(cache.setMetadata).toThrow(); expect(cache.cache.get('abc').data).toEqual('test'); expect(cache.getMetadata('abc').a).toEqual('c'); }); it('should save the length to metadata', function (done) { cache.setMetadata('abc', {a: 'b'}); var s = cache.set('abc'); s.write('a'); s.write('b'); s.end(''); setTimeout(function () { expect(cache.cache.get('abc').data.toString()).toEqual('ab'); expect(cache.getMetadata('abc').a).toEqual('b'); expect(cache.getMetadata('abc').length).toEqual(2); done(); }, 10) }); it('should handle getmetadata', function () { cache.cache.set('abc', {data: 1, metaData: 'bbb'}); expect(cache.getMetadata).toThrow(); cache.getMetadata('abc', 'bbb'); }); it('should initially set metadata once key is set', function () { cache.set('abc', {data: 'test'}); expect(cache.getMetadata('abc')).toEqual({}); }); it('should reset the cache when called', function () { cache.setData('aaa', 'value'); cache.reset(); expect(cache.exists('aaa')).toEqual(false); }); it('should handle getData', function (done) { cache.setData('b', new Buffer(100)); cache.getData('b', function (err, data) { expect(data.length).toEqual(100); done(); }); }); it('should handle key exists', function () { expect(cache.exists).toThrow(); expect(cache.exists('aaa')).toEqual(false); cache.setData('aaa', 'value'); expect(cache.exists('aaa')).toEqual(true); }); }); describe('streaming cache short timeout', function () { var s; beforeEach(function () { cache = new StreamingCache({maxAge: 100}) s = cache.set('b'); }); it('Writing to stream should set data', function (done) { s.write('a'); s.write('b'); s.end(null); setTimeout(function () { expect(s.read().toString() + s.read().toString()).toEqual('ab') cache.getData('b', function (err, data) { expect(data).toEqual(null) done(); }); }, 130); }); }); |