|
| 1 | +const test = require("mocha-sinon-chai"); |
| 2 | + |
| 3 | +const { Origin, sources } = require("../src/Origin"); |
| 4 | + |
| 5 | +test.describe("Origin stats", () => { |
| 6 | + let sandbox; |
| 7 | + let TestOrigin; |
| 8 | + let testOrigin; |
| 9 | + let queriedOrigin; |
| 10 | + let hasToReject; |
| 11 | + |
| 12 | + test.beforeEach(() => { |
| 13 | + hasToReject = false; |
| 14 | + sandbox = test.sinon.createSandbox(); |
| 15 | + TestOrigin = class extends Origin { |
| 16 | + _read(query) { |
| 17 | + const cached = this._cache.get(query); |
| 18 | + if (cached) { |
| 19 | + return cached; |
| 20 | + } |
| 21 | + const resultPromise = new Promise((resolve, reject) => { |
| 22 | + setTimeout(() => { |
| 23 | + hasToReject ? reject(new Error()) : resolve(); |
| 24 | + }, 50); |
| 25 | + }); |
| 26 | + this._cache.set(query, resultPromise); |
| 27 | + return resultPromise; |
| 28 | + } |
| 29 | + }; |
| 30 | + testOrigin = new TestOrigin("foo-id"); |
| 31 | + queriedOrigin = testOrigin.query("foo"); |
| 32 | + }); |
| 33 | + |
| 34 | + test.afterEach(() => { |
| 35 | + sandbox.restore(); |
| 36 | + sources.clear(); |
| 37 | + }); |
| 38 | + |
| 39 | + test.describe("without query", () => { |
| 40 | + test.describe("dispatch property", () => { |
| 41 | + test.it("should return 0 until read is dispatched", () => { |
| 42 | + test.expect(testOrigin.read.stats.dispatch).to.equal(0); |
| 43 | + }); |
| 44 | + |
| 45 | + test.it("should return 1 when read is dispatched", () => { |
| 46 | + testOrigin.read(); |
| 47 | + test.expect(testOrigin.read.stats.dispatch).to.equal(1); |
| 48 | + }); |
| 49 | + |
| 50 | + test.it( |
| 51 | + "should return 1 when read is dispatched many times and cache is not cleaned", |
| 52 | + () => { |
| 53 | + testOrigin.read(); |
| 54 | + testOrigin.read(); |
| 55 | + testOrigin.read(); |
| 56 | + testOrigin.read(); |
| 57 | + test.expect(testOrigin.read.stats.dispatch).to.equal(1); |
| 58 | + } |
| 59 | + ); |
| 60 | + |
| 61 | + test.it( |
| 62 | + "should return 2 when read is dispatched many times and cache is cleaned once", |
| 63 | + () => { |
| 64 | + testOrigin.read(); |
| 65 | + testOrigin.clean(); |
| 66 | + testOrigin.read(); |
| 67 | + testOrigin.read(); |
| 68 | + testOrigin.read(); |
| 69 | + test.expect(testOrigin.read.stats.dispatch).to.equal(2); |
| 70 | + } |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + test.describe("success property", () => { |
| 75 | + test.it("should return 0 until read method has finished", () => { |
| 76 | + test.expect(testOrigin.read.stats.success).to.equal(0); |
| 77 | + }); |
| 78 | + |
| 79 | + test.it("should return 1 when read has finished", () => { |
| 80 | + return testOrigin.read().then(() => { |
| 81 | + return test.expect(testOrigin.read.stats.success).to.equal(1); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + test.it("should return 4 when read has finished and it has been called 4 times", () => { |
| 86 | + return Promise.all([ |
| 87 | + testOrigin.read(), |
| 88 | + testOrigin.read(), |
| 89 | + testOrigin.read(), |
| 90 | + testOrigin.read() |
| 91 | + ]).then(() => { |
| 92 | + return test.expect(testOrigin.read.stats.success).to.equal(4); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + test.describe("error property", () => { |
| 98 | + test.it("should return 0 until read method has finished", () => { |
| 99 | + test.expect(testOrigin.read.stats.error).to.equal(0); |
| 100 | + }); |
| 101 | + |
| 102 | + test.it("should return 1 when read has finished with an error", () => { |
| 103 | + hasToReject = true; |
| 104 | + return testOrigin.read().then( |
| 105 | + () => { |
| 106 | + return test.assert.fail(); |
| 107 | + }, |
| 108 | + () => { |
| 109 | + return test.expect(testOrigin.read.stats.error).to.equal(1); |
| 110 | + } |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + test.it( |
| 115 | + "should return 4 when read has finished with error and it has been called 4 times", |
| 116 | + () => { |
| 117 | + hasToReject = true; |
| 118 | + return Promise.all([ |
| 119 | + testOrigin.read(), |
| 120 | + testOrigin.read(), |
| 121 | + testOrigin.read(), |
| 122 | + testOrigin.read() |
| 123 | + ]).then( |
| 124 | + () => { |
| 125 | + return test.assert.fail(); |
| 126 | + }, |
| 127 | + () => { |
| 128 | + return test.expect(testOrigin.read.stats.error).to.equal(4); |
| 129 | + } |
| 130 | + ); |
| 131 | + } |
| 132 | + ); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + test.describe("with query", () => { |
| 137 | + test.describe("dispatch property", () => { |
| 138 | + test.it("should return 0 until read is dispatched", () => { |
| 139 | + test.expect(queriedOrigin.read.stats.dispatch).to.equal(0); |
| 140 | + }); |
| 141 | + |
| 142 | + test.it("should return 1 when read is dispatched", () => { |
| 143 | + queriedOrigin.read(); |
| 144 | + test.expect(queriedOrigin.read.stats.dispatch).to.equal(1); |
| 145 | + }); |
| 146 | + |
| 147 | + test.it( |
| 148 | + "should return 1 when read is dispatched many times and cache is not cleaned", |
| 149 | + () => { |
| 150 | + queriedOrigin.read(); |
| 151 | + queriedOrigin.read(); |
| 152 | + queriedOrigin.read(); |
| 153 | + queriedOrigin.read(); |
| 154 | + test.expect(queriedOrigin.read.stats.dispatch).to.equal(1); |
| 155 | + } |
| 156 | + ); |
| 157 | + |
| 158 | + test.it( |
| 159 | + "should return 2 when read is dispatched many times and cache is cleaned once", |
| 160 | + () => { |
| 161 | + queriedOrigin.read(); |
| 162 | + queriedOrigin.clean(); |
| 163 | + queriedOrigin.read(); |
| 164 | + queriedOrigin.read(); |
| 165 | + queriedOrigin.read(); |
| 166 | + test.expect(queriedOrigin.read.stats.dispatch).to.equal(2); |
| 167 | + } |
| 168 | + ); |
| 169 | + }); |
| 170 | + |
| 171 | + test.describe("success property", () => { |
| 172 | + test.it("should return 0 until read method has finished", () => { |
| 173 | + test.expect(queriedOrigin.read.stats.success).to.equal(0); |
| 174 | + }); |
| 175 | + |
| 176 | + test.it("should return 1 when read has finished", () => { |
| 177 | + return queriedOrigin.read().then(() => { |
| 178 | + return test.expect(queriedOrigin.read.stats.success).to.equal(1); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + test.it("should return 4 when read has finished and it has been called 4 times", () => { |
| 183 | + return Promise.all([ |
| 184 | + queriedOrigin.read(), |
| 185 | + queriedOrigin.read(), |
| 186 | + queriedOrigin.read(), |
| 187 | + queriedOrigin.read() |
| 188 | + ]).then(() => { |
| 189 | + return test.expect(queriedOrigin.read.stats.success).to.equal(4); |
| 190 | + }); |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + test.describe("error property", () => { |
| 195 | + test.it("should return 0 until read method has finished", () => { |
| 196 | + test.expect(queriedOrigin.read.stats.error).to.equal(0); |
| 197 | + }); |
| 198 | + |
| 199 | + test.it("should return 1 when read has finished with an error", () => { |
| 200 | + hasToReject = true; |
| 201 | + return queriedOrigin.read().then( |
| 202 | + () => { |
| 203 | + return test.assert.fail(); |
| 204 | + }, |
| 205 | + () => { |
| 206 | + return test.expect(queriedOrigin.read.stats.error).to.equal(1); |
| 207 | + } |
| 208 | + ); |
| 209 | + }); |
| 210 | + |
| 211 | + test.it( |
| 212 | + "should return 4 when read has finished with error and it has been called 4 times", |
| 213 | + () => { |
| 214 | + hasToReject = true; |
| 215 | + return Promise.all([ |
| 216 | + queriedOrigin.read(), |
| 217 | + queriedOrigin.read(), |
| 218 | + queriedOrigin.read(), |
| 219 | + queriedOrigin.read() |
| 220 | + ]).then( |
| 221 | + () => { |
| 222 | + return test.assert.fail(); |
| 223 | + }, |
| 224 | + () => { |
| 225 | + return test.expect(queriedOrigin.read.stats.error).to.equal(4); |
| 226 | + } |
| 227 | + ); |
| 228 | + } |
| 229 | + ); |
| 230 | + }); |
| 231 | + }); |
| 232 | +}); |
0 commit comments