|
9 | 9 | using System.Threading.Tasks;
|
10 | 10 | using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Test.Helpers;
|
11 | 11 | using Microsoft.Extensions.Logging.Testing;
|
| 12 | +using Microsoft.Extensions.Time.Testing; |
12 | 13 | using Microsoft.Shared.Instruments;
|
13 | 14 | using Microsoft.TestUtilities;
|
14 | 15 | using Moq;
|
@@ -272,4 +273,133 @@ public void Provider_Registers_Instruments_CgroupV2_WithoutHostCpu()
|
272 | 273 | Assert.Contains(samples, x => x.instrument.Name == ResourceUtilizationInstruments.ProcessMemoryUtilization);
|
273 | 274 | Assert.Equal(1, samples.Single(i => i.instrument.Name == ResourceUtilizationInstruments.ProcessMemoryUtilization).value);
|
274 | 275 | }
|
| 276 | + |
| 277 | + [Fact] |
| 278 | + public void Provider_GetMeasurementWithRetry_HandlesExceptionAndRecovers() |
| 279 | + { |
| 280 | + var meterName = Guid.NewGuid().ToString(); |
| 281 | + var logger = new FakeLogger<LinuxUtilizationProvider>(); |
| 282 | + var options = Options.Options.Create(new ResourceMonitoringOptions()); |
| 283 | + using var meter = new Meter(nameof(Provider_GetMeasurementWithRetry_HandlesExceptionAndRecovers)); |
| 284 | + var meterFactoryMock = new Mock<IMeterFactory>(); |
| 285 | + meterFactoryMock.Setup(x => x.Create(It.IsAny<MeterOptions>())) |
| 286 | + .Returns(meter); |
| 287 | + |
| 288 | + var callCount = 0; |
| 289 | + var parserMock = new Mock<ILinuxUtilizationParser>(); |
| 290 | + parserMock.Setup(p => p.GetMemoryUsageInBytes()).Returns(() => |
| 291 | + { |
| 292 | + callCount++; |
| 293 | + if (callCount <= 1) |
| 294 | + { |
| 295 | + throw new FileNotFoundException("Simulated failure to read file"); |
| 296 | + } |
| 297 | + |
| 298 | + return 420UL; |
| 299 | + }); |
| 300 | + parserMock.Setup(p => p.GetAvailableMemoryInBytes()).Returns(1000UL); |
| 301 | + parserMock.Setup(p => p.GetCgroupRequestCpu()).Returns(10f); |
| 302 | + parserMock.Setup(p => p.GetCgroupLimitedCpus()).Returns(12f); |
| 303 | + |
| 304 | + var fakeTime = new FakeTimeProvider(DateTimeOffset.UtcNow); |
| 305 | + var provider = new LinuxUtilizationProvider(options, parserMock.Object, meterFactoryMock.Object, logger, fakeTime); |
| 306 | + |
| 307 | + using var listener = new MeterListener |
| 308 | + { |
| 309 | + InstrumentPublished = (instrument, listener) => |
| 310 | + { |
| 311 | + if (ReferenceEquals(meter, instrument.Meter)) |
| 312 | + { |
| 313 | + listener.EnableMeasurementEvents(instrument); |
| 314 | + } |
| 315 | + } |
| 316 | + }; |
| 317 | + |
| 318 | + var samples = new List<(Instrument instrument, double value)>(); |
| 319 | + listener.SetMeasurementEventCallback<double>((instrument, value, _, _) => |
| 320 | + { |
| 321 | + if (ReferenceEquals(meter, instrument.Meter)) |
| 322 | + { |
| 323 | + samples.Add((instrument, value)); |
| 324 | + } |
| 325 | + }); |
| 326 | + |
| 327 | + listener.Start(); |
| 328 | + listener.RecordObservableInstruments(); |
| 329 | + Assert.DoesNotContain(samples, x => x.instrument.Name == ResourceUtilizationInstruments.ProcessMemoryUtilization); |
| 330 | + |
| 331 | + fakeTime.Advance(TimeSpan.FromMinutes(1)); |
| 332 | + listener.RecordObservableInstruments(); |
| 333 | + Assert.DoesNotContain(samples, x => x.instrument.Name == ResourceUtilizationInstruments.ProcessMemoryUtilization); |
| 334 | + |
| 335 | + fakeTime.Advance(TimeSpan.FromMinutes(5)); |
| 336 | + listener.RecordObservableInstruments(); |
| 337 | + var metric = samples.SingleOrDefault(x => x.instrument.Name == ResourceUtilizationInstruments.ProcessMemoryUtilization); |
| 338 | + Assert.Equal(0.42, metric.value); |
| 339 | + |
| 340 | + parserMock.Verify(p => p.GetMemoryUsageInBytes(), Times.Exactly(2)); |
| 341 | + } |
| 342 | + |
| 343 | + [Fact] |
| 344 | + public void Provider_GetMeasurementWithRetry_UnhandledException_DoesNotBlockFutureReads() |
| 345 | + { |
| 346 | + var meterName = Guid.NewGuid().ToString(); |
| 347 | + var logger = new FakeLogger<LinuxUtilizationProvider>(); |
| 348 | + var options = Options.Options.Create(new ResourceMonitoringOptions()); |
| 349 | + using var meter = new Meter(nameof(Provider_GetMeasurementWithRetry_UnhandledException_DoesNotBlockFutureReads)); |
| 350 | + var meterFactoryMock = new Mock<IMeterFactory>(); |
| 351 | + meterFactoryMock.Setup(x => x.Create(It.IsAny<MeterOptions>())) |
| 352 | + .Returns(meter); |
| 353 | + |
| 354 | + var callCount = 0; |
| 355 | + var parserMock = new Mock<ILinuxUtilizationParser>(); |
| 356 | + parserMock.Setup(p => p.GetMemoryUsageInBytes()).Returns(() => |
| 357 | + { |
| 358 | + callCount++; |
| 359 | + if (callCount <= 2) |
| 360 | + { |
| 361 | + throw new InvalidOperationException("Simulated unhandled exception"); |
| 362 | + } |
| 363 | + |
| 364 | + return 1234UL; |
| 365 | + }); |
| 366 | + parserMock.Setup(p => p.GetAvailableMemoryInBytes()).Returns(2000UL); |
| 367 | + parserMock.Setup(p => p.GetCgroupRequestCpu()).Returns(10f); |
| 368 | + parserMock.Setup(p => p.GetCgroupLimitedCpus()).Returns(12f); |
| 369 | + |
| 370 | + var fakeTime = new FakeTimeProvider(DateTimeOffset.UtcNow); |
| 371 | + var provider = new LinuxUtilizationProvider(options, parserMock.Object, meterFactoryMock.Object, logger, fakeTime); |
| 372 | + |
| 373 | + using var listener = new MeterListener |
| 374 | + { |
| 375 | + InstrumentPublished = (instrument, listener) => |
| 376 | + { |
| 377 | + if (ReferenceEquals(meter, instrument.Meter)) |
| 378 | + { |
| 379 | + listener.EnableMeasurementEvents(instrument); |
| 380 | + } |
| 381 | + } |
| 382 | + }; |
| 383 | + |
| 384 | + var samples = new List<(Instrument instrument, double value)>(); |
| 385 | + listener.SetMeasurementEventCallback<double>((instrument, value, _, _) => |
| 386 | + { |
| 387 | + if (ReferenceEquals(meter, instrument.Meter)) |
| 388 | + { |
| 389 | + samples.Add((instrument, value)); |
| 390 | + } |
| 391 | + }); |
| 392 | + |
| 393 | + listener.Start(); |
| 394 | + |
| 395 | + Assert.Throws<AggregateException>(() => listener.RecordObservableInstruments()); |
| 396 | + Assert.DoesNotContain(samples, x => x.instrument.Name == ResourceUtilizationInstruments.ProcessMemoryUtilization); |
| 397 | + |
| 398 | + fakeTime.Advance(TimeSpan.FromMinutes(1)); |
| 399 | + listener.RecordObservableInstruments(); |
| 400 | + var metric = samples.SingleOrDefault(x => x.instrument.Name == ResourceUtilizationInstruments.ProcessMemoryUtilization); |
| 401 | + Assert.Equal(1234f / 2000f, metric.value, 0.01f); |
| 402 | + |
| 403 | + parserMock.Verify(p => p.GetMemoryUsageInBytes(), Times.Exactly(3)); |
| 404 | + } |
275 | 405 | }
|
0 commit comments