|
| 1 | +describe("Wrapper lifetime leak probe", function () { |
| 2 | + var runtime = require("ns:runtime"); |
| 3 | + var mode = runtime.getConfig("wrapperLifetimeMode"); |
| 4 | + var canary = mode === "canary"; |
| 5 | + var Holder = com.tns.tests.WrapperHolder; |
| 6 | + // WeakRefs, so the spec itself keeps nothing it planted alive. |
| 7 | + var planted = []; |
| 8 | + // Alive for the whole suite: what leaks is the entry's value. |
| 9 | + var probeMap = new WeakMap(); |
| 10 | + var report; |
| 11 | + var canaryRebuilds; |
| 12 | + |
| 13 | + function cyclesThrough(edge) { |
| 14 | + return report.cycles.filter(function (cycle) { |
| 15 | + return cycle.path.indexOf("--" + edge + "-->") !== -1; |
| 16 | + }); |
| 17 | + } |
| 18 | + |
| 19 | + // Each probe dumps the Java heap, so every shape is planted once and probed once, on a |
| 20 | + // later task: a WeakRef keeps its target alive until the job that made it ends. |
| 21 | + beforeAll(function (done) { |
| 22 | + // A cycle through Java whose object's canary was rebuilt since: the record also |
| 23 | + // keeps a copy of the rebuilt canary, which must not read as a root. |
| 24 | + (function () { |
| 25 | + var rebuilt = new java.lang.Runnable({ run: function () {} }); |
| 26 | + var list = new java.util.ArrayList(); |
| 27 | + list.add(rebuilt); |
| 28 | + rebuilt.probeRebuiltList = list; |
| 29 | + planted.push(new WeakRef(rebuilt)); |
| 30 | + Holder.held = list; |
| 31 | + })(); |
| 32 | + // The canary dies on a later job: a WeakRef keeps its target until the job ends. |
| 33 | + setTimeout(function () { |
| 34 | + gc(); |
| 35 | + java.lang.System.nanoTime(); |
| 36 | + java.lang.Runtime.getRuntime().gc(); |
| 37 | + gc(); |
| 38 | + java.lang.System.nanoTime(); |
| 39 | + var rebuildsBefore = runtime.getWrapperLifetimeStats().canaryRebuilds; |
| 40 | + (function () { |
| 41 | + // A read: the rebuilt canary's copy stays current. |
| 42 | + Holder.held.get(0).probeRebuiltList.size(); |
| 43 | + })(); |
| 44 | + canaryRebuilds = runtime.getWrapperLifetimeStats().canaryRebuilds - rebuildsBefore; |
| 45 | + Holder.held = null; |
| 46 | + (function () { |
| 47 | + // A JS property referencing its own object. |
| 48 | + var selfReferencing = new java.lang.Runnable({ run: function () {} }); |
| 49 | + selfReferencing.probeSelf = selfReferencing; |
| 50 | + planted.push(new WeakRef(selfReferencing)); |
| 51 | + |
| 52 | + var twin = new java.lang.Runnable({ run: function () {} }); |
| 53 | + twin.probeSelf = twin; |
| 54 | + planted.push(new WeakRef(twin)); |
| 55 | + |
| 56 | + // A cycle through Java: the object's state holds a list that holds the object. |
| 57 | + var listed = new java.lang.Runnable({ run: function () {} }); |
| 58 | + var list = new java.util.ArrayList(); |
| 59 | + list.add(listed); |
| 60 | + listed.probeList = list; |
| 61 | + planted.push(new WeakRef(listed)); |
| 62 | + |
| 63 | + // Held by a Java static: alive for a reason of its own, not a leak. |
| 64 | + var held = new java.lang.Runnable({ run: function () {} }); |
| 65 | + held.probeHeld = held; |
| 66 | + Holder.held = held; |
| 67 | + planted.push(new WeakRef(held)); |
| 68 | + |
| 69 | + // A WeakMap value referencing its own key: in canary mode the key is the |
| 70 | + // object's lifetime key, which its record holds. |
| 71 | + var mapped = new java.lang.Runnable({ run: function () {} }); |
| 72 | + probeMap.set(mapped, { probeMapped: mapped }); |
| 73 | + planted.push(new WeakRef(mapped)); |
| 74 | + |
| 75 | + // A back reference through a WeakRef keeps nothing. |
| 76 | + var weak = new java.lang.Runnable({ run: function () {} }); |
| 77 | + weak.probeWeak = new WeakRef(weak); |
| 78 | + })(); |
| 79 | + (function () { |
| 80 | + // A method closing over its own object: the cycle runs through the object's |
| 81 | + // prototype, its implementation object. In a function of its own, so no other |
| 82 | + // planted closure shares the context that holds it. |
| 83 | + var probeClosed = new java.lang.Runnable({ |
| 84 | + run: function () { |
| 85 | + return probeClosed; |
| 86 | + } |
| 87 | + }); |
| 88 | + planted.push(new WeakRef(probeClosed)); |
| 89 | + })(); |
| 90 | + setTimeout(function () { |
| 91 | + report = runtime.findLeakCycles(); |
| 92 | + console.log("leak probe: " + JSON.stringify({ |
| 93 | + leaked: report.leaked, |
| 94 | + leakedOutsideCycles: report.leakedOutsideCycles, |
| 95 | + probed: report.probed, |
| 96 | + cycles: report.cycles.length, |
| 97 | + durationMs: report.durationMs, |
| 98 | + dumpBytes: report.dumpBytes |
| 99 | + })); |
| 100 | + report.cycles.forEach(function (cycle) { |
| 101 | + console.log("leak probe cycle: " + cycle.signature); |
| 102 | + }); |
| 103 | + done(); |
| 104 | + }, 0); |
| 105 | + }, 0); |
| 106 | + }, 20000); |
| 107 | + |
| 108 | + afterAll(function () { |
| 109 | + Holder.held = null; |
| 110 | + planted.forEach(function (ref) { |
| 111 | + var object = ref.deref(); |
| 112 | + if (object !== undefined) { |
| 113 | + __releaseNativeCounterpart(object); |
| 114 | + } |
| 115 | + }); |
| 116 | + planted = []; |
| 117 | + report = null; |
| 118 | + }); |
| 119 | + |
| 120 | + it("reports the heaps it joined (" + mode + ")", function () { |
| 121 | + expect(report.mode).toBe(mode); |
| 122 | + expect(Array.isArray(report.cycles)).toBe(true); |
| 123 | + if (!canary) { |
| 124 | + // Nothing to look for: no dump is taken. |
| 125 | + expect(report.leaked).toBe(0); |
| 126 | + expect(report.javaObjects).toBeUndefined(); |
| 127 | + return; |
| 128 | + } |
| 129 | + expect(report.javaObjects).toBeGreaterThan(0); |
| 130 | + expect(report.jsNodes).toBeGreaterThan(0); |
| 131 | + expect(report.probed).toBeGreaterThan(0); |
| 132 | + expect(report.truncated).toBe(false); |
| 133 | + expect(report.durationMs).toBeGreaterThan(0); |
| 134 | + }); |
| 135 | + |
| 136 | + it("finds an object whose JS state references it", function () { |
| 137 | + var cycles = cyclesThrough("probeSelf"); |
| 138 | + if (!canary) { |
| 139 | + // Resurrection breaks it: nothing keeps the object once JS lets go. |
| 140 | + expect(cycles.length).toBe(0); |
| 141 | + return; |
| 142 | + } |
| 143 | + expect(cycles.length).toBe(2); |
| 144 | + expect(cycles[0].path).toContain("--ns:kept-->"); |
| 145 | + expect(cycles[0].path).toContain("--ns:wrapper-->"); |
| 146 | + expect(cycles[0].members.length).toBe(1); |
| 147 | + }); |
| 148 | + |
| 149 | + it("gives the same shape the same signature, free of ids", function () { |
| 150 | + if (!canary) { |
| 151 | + return; |
| 152 | + } |
| 153 | + var cycles = cyclesThrough("probeSelf"); |
| 154 | + expect(cycles[0].signature).toBe(cycles[1].signature); |
| 155 | + }); |
| 156 | + |
| 157 | + it("finds a cycle through a Java collection", function () { |
| 158 | + var cycles = cyclesThrough("probeList"); |
| 159 | + if (!canary) { |
| 160 | + expect(cycles.length).toBe(0); |
| 161 | + return; |
| 162 | + } |
| 163 | + expect(cycles.length).toBe(1); |
| 164 | + expect(cycles[0].path).toContain("--java.util.ArrayList.elementData-->"); |
| 165 | + }); |
| 166 | + |
| 167 | + it("finds a cycle through a WeakMap value that references its key", function () { |
| 168 | + var cycles = cyclesThrough("probeMapped"); |
| 169 | + if (!canary) { |
| 170 | + expect(cycles.length).toBe(0); |
| 171 | + return; |
| 172 | + } |
| 173 | + expect(cycles.length).toBe(1); |
| 174 | + expect(cycles[0].path).toContain("--WeakMap value-->"); |
| 175 | + }); |
| 176 | + |
| 177 | + it("finds a cycle through a method that closes over its object", function () { |
| 178 | + var cycles = cyclesThrough("probeClosed"); |
| 179 | + if (!canary) { |
| 180 | + expect(cycles.length).toBe(0); |
| 181 | + return; |
| 182 | + } |
| 183 | + expect(cycles.length).toBe(1); |
| 184 | + expect(cycles[0].path).toContain("--ns:kept-->"); |
| 185 | + expect(cycles[0].path).toContain("--ns:wrapper-->"); |
| 186 | + }); |
| 187 | + |
| 188 | + it("finds a cycle through Java after the object's canary was rebuilt", function () { |
| 189 | + var cycles = cyclesThrough("probeRebuiltList"); |
| 190 | + if (!canary) { |
| 191 | + expect(cycles.length).toBe(0); |
| 192 | + return; |
| 193 | + } |
| 194 | + expect(canaryRebuilds).toBeGreaterThan(0); |
| 195 | + expect(cycles.length).toBe(1); |
| 196 | + }); |
| 197 | + |
| 198 | + it("does not report an object Java holds for a reason of its own", function () { |
| 199 | + expect(cyclesThrough("probeHeld").length).toBe(0); |
| 200 | + }); |
| 201 | + |
| 202 | + it("does not report a back reference through a WeakRef", function () { |
| 203 | + expect(cyclesThrough("probeWeak").length).toBe(0); |
| 204 | + }); |
| 205 | + |
| 206 | + it("settles the wrappers that died since the last collection first", function () { |
| 207 | + if (!canary) { |
| 208 | + return; |
| 209 | + } |
| 210 | + (function () { |
| 211 | + for (var i = 0; i < 50; i++) { |
| 212 | + new java.lang.StringBuilder("probeGarbage"); |
| 213 | + } |
| 214 | + })(); |
| 215 | + // Read as leaked if unsettled: their Java objects are still held for them. |
| 216 | + expect(runtime.findLeakCycles().leaked).toBeLessThanOrEqual(report.leaked); |
| 217 | + }, 20000); |
| 218 | +}); |
0 commit comments