test_onGarbageCollection-01.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Test basic usage of onGarbageCollection
  2. const root = newGlobal();
  3. const dbg = new Debugger();
  4. const wrappedRoot = dbg.addDebuggee(root)
  5. const NUM_SLICES = root.NUM_SLICES = 10;
  6. let fired = false;
  7. let slicesFound = 0;
  8. dbg.memory.onGarbageCollection = data => {
  9. fired = true;
  10. print("Got onGarbageCollection: " + JSON.stringify(data, null, 2));
  11. equal(typeof data.reason, "string");
  12. equal(typeof data.nonincrementalReason == "string" || data.nonincrementalReason === null,
  13. true);
  14. let lastStartTimestamp = 0;
  15. for (let i = 0; i < data.collections.length; i++) {
  16. let slice = data.collections[i];
  17. equal(slice.startTimestamp >= lastStartTimestamp, true);
  18. equal(slice.startTimestamp <= slice.endTimestamp, true);
  19. lastStartTimestamp = slice.startTimestamp;
  20. }
  21. equal(data.collections.length >= 1, true);
  22. slicesFound += data.collections.length;
  23. }
  24. function run_test() {
  25. do_test_pending();
  26. root.eval(
  27. `
  28. this.allocs = [];
  29. // GC slices
  30. for (var i = 0; i < NUM_SLICES; i++) {
  31. this.allocs.push({});
  32. gcslice();
  33. }
  34. // Full GC
  35. this.allocs.push({});
  36. gc();
  37. `
  38. );
  39. executeSoon(() => {
  40. equal(fired, true, "The GC hook should have fired at least once");
  41. // NUM_SLICES + 1 full gc + however many were triggered naturally (due to
  42. // whatever zealousness setting).
  43. print("Found " + slicesFound + " slices");
  44. equal(slicesFound >= NUM_SLICES + 1, true);
  45. do_test_finished();
  46. });
  47. }