| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
import QUnit from "qunit-tests"; |
| 4 |
|
| 5 |
QUnit.test("public api", function (assert) { |
| 6 |
assert.equal(typeof cockpit.cache, "function", "cockpit.cache is a function"); |
| 7 |
}); |
| 8 |
|
| 9 |
QUnit.test("single cache", function (assert) { |
| 10 |
const done = assert.async(); |
| 11 |
assert.expect(6); |
| 12 |
|
| 13 |
let closed = false; |
| 14 |
|
| 15 |
function provider(result, key) { |
| 16 |
assert.equal(key, "test-key-1", "provider got right key"); |
| 17 |
assert.equal(typeof result, "function", "provider got result function"); |
| 18 |
|
| 19 |
const timer = window.setTimeout(function() { |
| 20 |
result({ myobject: "value" }); |
| 21 |
}, 200); |
| 22 |
|
| 23 |
return { |
| 24 |
close: function() { |
| 25 |
window.clearTimeout(timer); |
| 26 |
closed = true; |
| 27 |
} |
| 28 |
}; |
| 29 |
} |
| 30 |
|
| 31 |
function consumer(value, key) { |
| 32 |
assert.equal(key, "test-key-1", "consumer got right key"); |
| 33 |
assert.deepEqual(value, { myobject: "value" }); |
| 34 |
|
| 35 |
assert.equal(closed, false, "cache is not closed"); |
| 36 |
cache.close(); |
| 37 |
assert.equal(closed, true, "cache is closed"); |
| 38 |
|
| 39 |
done(); |
| 40 |
} |
| 41 |
|
| 42 |
const cache = cockpit.cache("test-key-1", provider, consumer); |
| 43 |
}); |
| 44 |
|
| 45 |
QUnit.test("multi cache", function (assert) { |
| 46 |
const done = assert.async(); |
| 47 |
assert.expect(12); |
| 48 |
|
| 49 |
let closed1 = false; |
| 50 |
|
| 51 |
function provider1(result, key) { |
| 52 |
assert.equal(key, "test-key-b", "provider1 got right key"); |
| 53 |
assert.equal(typeof result, "function", "provider1 got result function"); |
| 54 |
|
| 55 |
result({ myobject: "value1" }); |
| 56 |
|
| 57 |
return { |
| 58 |
close: function() { |
| 59 |
closed1 = true; |
| 60 |
} |
| 61 |
}; |
| 62 |
} |
| 63 |
|
| 64 |
function provider2(result, key) { |
| 65 |
assert.equal(key, "test-key-b", "provider2 got right key"); |
| 66 |
assert.equal(typeof result, "function", "provider2 got result function"); |
| 67 |
|
| 68 |
const timer = window.setTimeout(function() { |
| 69 |
result({ myobject: "value2" }); |
| 70 |
}, 200); |
| 71 |
|
| 72 |
return { |
| 73 |
close: function() { |
| 74 |
window.clearTimeout(timer); |
| 75 |
} |
| 76 |
}; |
| 77 |
} |
| 78 |
|
| 79 |
function consumer1(value, key) { |
| 80 |
assert.equal(key, "test-key-b", "consumer1 got right key"); |
| 81 |
assert.deepEqual(value, { myobject: "value1" }, "consumer1 got right value"); |
| 82 |
} |
| 83 |
|
| 84 |
let count = 0; |
| 85 |
|
| 86 |
function consumer2(value, key) { |
| 87 |
assert.equal(key, "test-key-b", "consumer2 got right key"); |
| 88 |
count++; |
| 89 |
if (count === 1) { |
| 90 |
assert.deepEqual(value, { myobject: "value1" }, "consumer2 got value from producer1"); |
| 91 |
assert.equal(closed1, false, "cache1 is not closed"); |
| 92 |
cache1.close(); |
| 93 |
assert.equal(closed1, true, "cache1 is closed"); |
| 94 |
} else if (count === 2) { |
| 95 |
assert.deepEqual(value, { myobject: "value2" }, "cache2 provided another value"); |
| 96 |
done(); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
const cache1 = cockpit.cache("test-key-b", provider1, consumer1); |
| 101 |
cockpit.cache("test-key-b", provider2, consumer2); |
| 102 |
}); |
| 103 |
|
| 104 |
QUnit.start(); |