|
| 1 | +// What a canary record keeps between canaries: the names its state has, which decide |
| 2 | +// whether a write must look the state up, and its class's rebuild data, which the |
| 3 | +// records of one class share. A rebuilt canary shows both. |
| 4 | +describe("Wrapper lifetime record sharing", function () { |
| 5 | + var runtime = require("ns:runtime"); |
| 6 | + var canary = runtime.getConfig("wrapperLifetimeMode") === "canary"; |
| 7 | + var Holder = com.tns.tests.WrapperHolder; |
| 8 | + var hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty); |
| 9 | + var Dummy = com.tns.tests.DummyClass.extend({}); |
| 10 | + |
| 11 | + function rebuilds() { |
| 12 | + return runtime.getWrapperLifetimeStats().canaryRebuilds; |
| 13 | + } |
| 14 | + |
| 15 | + function collect() { |
| 16 | + for (var i = 0; i < 2; i++) { |
| 17 | + gc(); |
| 18 | + java.lang.System.nanoTime(); |
| 19 | + java.lang.Runtime.getRuntime().gc(); |
| 20 | + java.lang.System.runFinalization(); |
| 21 | + gc(); |
| 22 | + java.lang.System.nanoTime(); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + // plant(object) runs on a new Dummy, then the Dummy is rebuilt from its state. |
| 27 | + function rebuilt(plant) { |
| 28 | + var before = rebuilds(); |
| 29 | + (function () { |
| 30 | + var object = new Dummy(); |
| 31 | + plant(object); |
| 32 | + Holder.held = object; |
| 33 | + })(); |
| 34 | + collect(); |
| 35 | + var object = Holder.held; |
| 36 | + if (canary) { |
| 37 | + expect(rebuilds()).toBeGreaterThan(before); |
| 38 | + } |
| 39 | + return object; |
| 40 | + } |
| 41 | + |
| 42 | + // Rebuilds every object a Java list holds; make(i) runs in a function of its own. |
| 43 | + function rebuiltAll(count, make) { |
| 44 | + var list = new java.util.ArrayList(); |
| 45 | + (function () { |
| 46 | + for (var i = 0; i < count; i++) { |
| 47 | + list.add(make(i)); |
| 48 | + } |
| 49 | + })(); |
| 50 | + var before = rebuilds(); |
| 51 | + collect(); |
| 52 | + var objects = []; |
| 53 | + for (var i = 0; i < count; i++) { |
| 54 | + objects.push(list.get(i)); |
| 55 | + } |
| 56 | + if (canary) { |
| 57 | + expect(rebuilds()).toBeGreaterThan(before); |
| 58 | + } |
| 59 | + return objects; |
| 60 | + } |
| 61 | + |
| 62 | + afterEach(function () { |
| 63 | + Holder.held = null; |
| 64 | + }); |
| 65 | + |
| 66 | + it("keeps rewrites of names written before and after more names than a record tags", function () { |
| 67 | + var object = rebuilt(function (o) { |
| 68 | + for (var i = 0; i < 10; i++) { |
| 69 | + o["n" + i] = i; |
| 70 | + } |
| 71 | + o.n0 = "first"; |
| 72 | + o.n9 = "last"; |
| 73 | + o.n5 = "middle"; |
| 74 | + }); |
| 75 | + expect(Object.keys(object)).toEqual(["n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"]); |
| 76 | + expect([object.n0, object.n5, object.n9, object.n1]).toEqual(["first", "middle", "last", 1]); |
| 77 | + }); |
| 78 | + |
| 79 | + it("keeps a rewrite of a name added by a define", function () { |
| 80 | + var object = rebuilt(function (o) { |
| 81 | + Object.defineProperty(o, "plain", { value: 1, writable: true, enumerable: true, configurable: true }); |
| 82 | + o.plain = 2; |
| 83 | + Object.defineProperty(o, "hidden", { value: 1, writable: true, enumerable: false, configurable: true }); |
| 84 | + o.hidden = 2; |
| 85 | + o.after = 1; |
| 86 | + o.after = 2; |
| 87 | + }); |
| 88 | + expect(Object.getOwnPropertyDescriptor(object, "plain")).toEqual({ value: 2, writable: true, enumerable: true, configurable: true }); |
| 89 | + expect(Object.getOwnPropertyDescriptor(object, "hidden")).toEqual({ value: 2, writable: true, enumerable: false, configurable: true }); |
| 90 | + expect(object.after).toBe(2); |
| 91 | + }); |
| 92 | + |
| 93 | + it("keeps a rewrite of a name made at run time, across collections", function () { |
| 94 | + var object = rebuilt(function (o) { |
| 95 | + var name = ["late", Date.now() % 7, "name"].join("_"); |
| 96 | + o[name] = 1; |
| 97 | + o.marker = name; |
| 98 | + gc(); |
| 99 | + gc(); |
| 100 | + o[name] = 2; |
| 101 | + o[String(name)] = 3; |
| 102 | + }); |
| 103 | + expect(object[object.marker]).toBe(3); |
| 104 | + expect(Object.keys(object)).toEqual([object.marker, "marker"]); |
| 105 | + }); |
| 106 | + |
| 107 | + it("keeps a name deleted and written again, then rewritten, on a later canary", function () { |
| 108 | + var object = rebuilt(function (o) { |
| 109 | + o.a = 1; |
| 110 | + o.b = 1; |
| 111 | + delete o.a; |
| 112 | + o.a = 2; |
| 113 | + }); |
| 114 | + (function () { |
| 115 | + var o = Holder.held; |
| 116 | + o.a = 3; |
| 117 | + o.b = 2; |
| 118 | + o.c = 1; |
| 119 | + o.c = 2; |
| 120 | + })(); |
| 121 | + object = null; |
| 122 | + collect(); |
| 123 | + object = Holder.held; |
| 124 | + expect(Object.keys(object)).toEqual(["b", "a", "c"]); |
| 125 | + expect([object.a, object.b, object.c]).toEqual([3, 2, 2]); |
| 126 | + }); |
| 127 | + |
| 128 | + it("rebuilds instances of one class with the class's prototype and methods", function () { |
| 129 | + var Cls = com.tns.tests.DummyClass.extend({ |
| 130 | + tag: function () { |
| 131 | + return "cls"; |
| 132 | + } |
| 133 | + }); |
| 134 | + var objects = rebuiltAll(4, function (i) { |
| 135 | + var o = new Cls(); |
| 136 | + o.index = i; |
| 137 | + return o; |
| 138 | + }); |
| 139 | + objects.forEach(function (o, i) { |
| 140 | + expect(Object.getPrototypeOf(o)).toBe(Cls.prototype); |
| 141 | + expect(o instanceof Cls).toBe(true); |
| 142 | + expect(o.tag()).toBe("cls"); |
| 143 | + expect(o.index).toBe(i); |
| 144 | + expect(o.getName()).toBe("dummy"); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it("rebuilds interface implementations with their own implementation object", function () { |
| 149 | + var objects = rebuiltAll(3, function (i) { |
| 150 | + return new java.lang.Runnable({ |
| 151 | + run: function () { |
| 152 | + this.ran = i; |
| 153 | + }, |
| 154 | + which: i |
| 155 | + }); |
| 156 | + }); |
| 157 | + objects.forEach(function (o, i) { |
| 158 | + expect(o.which).toBe(i); |
| 159 | + o.run(); |
| 160 | + expect(o.ran).toBe(i); |
| 161 | + expect(hasOwn(o, "which")).toBe(false); |
| 162 | + }); |
| 163 | + expect(Object.getPrototypeOf(objects[0])).not.toBe(Object.getPrototypeOf(objects[1])); |
| 164 | + }); |
| 165 | + |
| 166 | + it("rebuilds instances of more classes than a record can share data with at once", function () { |
| 167 | + var D = com.tns.tests.DummyClass; |
| 168 | + function k(n) { |
| 169 | + return function () { |
| 170 | + return n; |
| 171 | + }; |
| 172 | + } |
| 173 | + // One per line: extend() names a class by where it is called. |
| 174 | + var classes = [ |
| 175 | + D.extend({ cls: k(0) }), |
| 176 | + D.extend({ cls: k(1) }), |
| 177 | + D.extend({ cls: k(2) }), |
| 178 | + D.extend({ cls: k(3) }), |
| 179 | + D.extend({ cls: k(4) }), |
| 180 | + D.extend({ cls: k(5) }), |
| 181 | + D.extend({ cls: k(6) }), |
| 182 | + D.extend({ cls: k(7) }), |
| 183 | + D.extend({ cls: k(8) }), |
| 184 | + D.extend({ cls: k(9) }), |
| 185 | + D.extend({ cls: k(10) }), |
| 186 | + D.extend({ cls: k(11) }), |
| 187 | + D.extend({ cls: k(12) }), |
| 188 | + D.extend({ cls: k(13) }), |
| 189 | + D.extend({ cls: k(14) }), |
| 190 | + D.extend({ cls: k(15) }), |
| 191 | + D.extend({ cls: k(16) }), |
| 192 | + D.extend({ cls: k(17) }), |
| 193 | + D.extend({ cls: k(18) }), |
| 194 | + D.extend({ cls: k(19) }), |
| 195 | + ]; |
| 196 | + var count = classes.length; |
| 197 | + // Classes interleaved, so instances of one class register between other classes'. |
| 198 | + var objects = rebuiltAll(3 * count, function (i) { |
| 199 | + var o = new classes[i % count](); |
| 200 | + o.index = i; |
| 201 | + return o; |
| 202 | + }); |
| 203 | + objects.forEach(function (o, i) { |
| 204 | + expect(Object.getPrototypeOf(o)).toBe(classes[i % count].prototype); |
| 205 | + expect(o.cls()).toBe(i % count); |
| 206 | + expect(o.index).toBe(i); |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
| 210 | + it("rebuilds an instance of a class whose earlier instances' records were all freed", function () { |
| 211 | + var Cls = com.tns.tests.DummyClass.extend({ |
| 212 | + tag: function () { |
| 213 | + return "again"; |
| 214 | + } |
| 215 | + }); |
| 216 | + (function () { |
| 217 | + for (var i = 0; i < 50; i++) { |
| 218 | + new Cls().index = i; |
| 219 | + } |
| 220 | + })(); |
| 221 | + collect(); |
| 222 | + collect(); |
| 223 | + var objects = rebuiltAll(2, function (i) { |
| 224 | + var o = new Cls(); |
| 225 | + o.index = i; |
| 226 | + return o; |
| 227 | + }); |
| 228 | + expect(objects[1].tag()).toBe("again"); |
| 229 | + expect(Object.getPrototypeOf(objects[1])).toBe(Cls.prototype); |
| 230 | + expect(objects[1].index).toBe(1); |
| 231 | + }); |
| 232 | +}); |
0 commit comments