|
| 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 | + var report; |
| 9 | + |
| 10 | + function cyclesThrough(edge) { |
| 11 | + return report.cycles.filter(function (cycle) { |
| 12 | + return cycle.path.indexOf("--" + edge + "-->") !== -1; |
| 13 | + }); |
| 14 | + } |
| 15 | + |
| 16 | + // Each probe dumps the Java heap, so every shape is planted once and probed once, on a |
| 17 | + // later task: a WeakRef keeps its target alive until the job that made it ends. |
| 18 | + beforeAll(function (done) { |
| 19 | + (function () { |
| 20 | + // A JS property referencing its own object. |
| 21 | + var selfReferencing = new java.lang.Runnable({ run: function () {} }); |
| 22 | + selfReferencing.probeSelf = selfReferencing; |
| 23 | + planted.push(new WeakRef(selfReferencing)); |
| 24 | + |
| 25 | + var twin = new java.lang.Runnable({ run: function () {} }); |
| 26 | + twin.probeSelf = twin; |
| 27 | + planted.push(new WeakRef(twin)); |
| 28 | + |
| 29 | + // A cycle through Java: the object's state holds a list that holds the object. |
| 30 | + var listed = new java.lang.Runnable({ run: function () {} }); |
| 31 | + var list = new java.util.ArrayList(); |
| 32 | + list.add(listed); |
| 33 | + listed.probeList = list; |
| 34 | + planted.push(new WeakRef(listed)); |
| 35 | + |
| 36 | + // Held by a Java static: alive for a reason of its own, not a leak. |
| 37 | + var held = new java.lang.Runnable({ run: function () {} }); |
| 38 | + held.probeHeld = held; |
| 39 | + Holder.held = held; |
| 40 | + planted.push(new WeakRef(held)); |
| 41 | + |
| 42 | + // A back reference through a WeakRef keeps nothing. |
| 43 | + var weak = new java.lang.Runnable({ run: function () {} }); |
| 44 | + weak.probeWeak = new WeakRef(weak); |
| 45 | + })(); |
| 46 | + setTimeout(function () { |
| 47 | + gc(); |
| 48 | + java.lang.System.nanoTime(); |
| 49 | + report = runtime.findLeakCycles(); |
| 50 | + console.log("leak probe: " + JSON.stringify({ |
| 51 | + leaked: report.leaked, |
| 52 | + leakedOutsideCycles: report.leakedOutsideCycles, |
| 53 | + probed: report.probed, |
| 54 | + cycles: report.cycles.length, |
| 55 | + durationMs: report.durationMs, |
| 56 | + dumpBytes: report.dumpBytes |
| 57 | + })); |
| 58 | + report.cycles.forEach(function (cycle) { |
| 59 | + console.log("leak probe cycle: " + cycle.signature); |
| 60 | + }); |
| 61 | + done(); |
| 62 | + }, 0); |
| 63 | + }); |
| 64 | + |
| 65 | + afterAll(function () { |
| 66 | + Holder.held = null; |
| 67 | + planted.forEach(function (ref) { |
| 68 | + var object = ref.deref(); |
| 69 | + if (object !== undefined) { |
| 70 | + __releaseNativeCounterpart(object); |
| 71 | + } |
| 72 | + }); |
| 73 | + planted = []; |
| 74 | + report = null; |
| 75 | + }); |
| 76 | + |
| 77 | + it("reports the heaps it joined (" + mode + ")", function () { |
| 78 | + expect(report.mode).toBe(mode); |
| 79 | + expect(Array.isArray(report.cycles)).toBe(true); |
| 80 | + expect(report.javaObjects).toBeGreaterThan(0); |
| 81 | + expect(report.jsNodes).toBeGreaterThan(0); |
| 82 | + expect(report.probed).toBeGreaterThan(0); |
| 83 | + expect(report.durationMs).toBeGreaterThan(0); |
| 84 | + }); |
| 85 | + |
| 86 | + it("finds an object whose JS state references it", function () { |
| 87 | + var cycles = cyclesThrough("probeSelf"); |
| 88 | + if (!canary) { |
| 89 | + // Resurrection breaks it: nothing keeps the object once JS lets go. |
| 90 | + expect(cycles.length).toBe(0); |
| 91 | + return; |
| 92 | + } |
| 93 | + expect(cycles.length).toBe(2); |
| 94 | + expect(cycles[0].path).toContain("--ns:record-->"); |
| 95 | + expect(cycles[0].path).toContain("--ns:wrapper-->"); |
| 96 | + expect(cycles[0].members.length).toBe(1); |
| 97 | + }); |
| 98 | + |
| 99 | + it("gives the same shape the same signature, free of ids", function () { |
| 100 | + if (!canary) { |
| 101 | + return; |
| 102 | + } |
| 103 | + var cycles = cyclesThrough("probeSelf"); |
| 104 | + expect(cycles[0].signature).toBe(cycles[1].signature); |
| 105 | + }); |
| 106 | + |
| 107 | + it("finds a cycle through a Java collection", function () { |
| 108 | + var cycles = cyclesThrough("probeList"); |
| 109 | + if (!canary) { |
| 110 | + expect(cycles.length).toBe(0); |
| 111 | + return; |
| 112 | + } |
| 113 | + expect(cycles.length).toBe(1); |
| 114 | + expect(cycles[0].path).toContain("--java.util.ArrayList.elementData-->"); |
| 115 | + }); |
| 116 | + |
| 117 | + it("does not report an object Java holds for a reason of its own", function () { |
| 118 | + expect(cyclesThrough("probeHeld").length).toBe(0); |
| 119 | + }); |
| 120 | + |
| 121 | + it("does not report a back reference through a WeakRef", function () { |
| 122 | + expect(cyclesThrough("probeWeak").length).toBe(0); |
| 123 | + }); |
| 124 | +}); |
0 commit comments