public async Task TestMemoryMonitorWithSimulatedValues() { using (var monitor = new MacPlatformService.MacMemoryMonitor()) { var tcs = new TaskCompletionSource <bool> (); int statusChangedCount = 0; monitor.StatusChanged += (o, args) => { if (statusChangedCount == 0) { Assert.AreEqual(PlatformMemoryStatus.Low, args.MemoryStatus); } else if (statusChangedCount == 1) { Assert.AreEqual(PlatformMemoryStatus.Critical, args.MemoryStatus); } else if (statusChangedCount == 2) { Assert.AreEqual(PlatformMemoryStatus.Normal, args.MemoryStatus); tcs.SetResult(true); } else { throw new Exception("Should not be reached"); } statusChangedCount++; }; await SimulateMemoryPressure("warn"); await SimulateMemoryPressure("critical"); await SimulateMemoryPressure("normal"); await tcs.Task; } }
public async Task TestMacMemoryMonitorLifetime() { var tcs = new TaskCompletionSource <bool> (); using (var macMonitor = new MacPlatformService.MacMemoryMonitor()) { // Cancellation is async. macMonitor.DispatchSource.SetCancelHandler(() => tcs.SetResult(true)); } Assert.AreEqual(true, await tcs.Task, "Expected cancel handler to be called"); }
public async Task TestMemoryMonitorWithSimulatedValues() { using (var monitor = new MacPlatformService.MacMemoryMonitor()) { var countsReached = new Dictionary <PlatformMemoryStatus, int> (); monitor.StatusChanged += (o, args) => { if (!countsReached.TryGetValue(args.MemoryStatus, out int count)) { count = 0; } countsReached [args.MemoryStatus] = ++count; }; await SimulateMemoryPressure("warn"); await SimulateMemoryPressure("critical"); Assert.That(countsReached [PlatformMemoryStatus.Low], Is.GreaterThanOrEqualTo(1)); Assert.That(countsReached [PlatformMemoryStatus.Critical], Is.GreaterThanOrEqualTo(1)); } }