|
| 1 | +/* |
| 2 | +* Copyright (c) 2025 ADBC Drivers Contributors |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Concurrent; |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.Linq; |
| 21 | +using System.Threading; |
| 22 | +using System.Threading.Tasks; |
| 23 | +using AdbcDrivers.Databricks.Telemetry; |
| 24 | +using AdbcDrivers.Databricks.Telemetry.Models; |
| 25 | + |
| 26 | +namespace AdbcDrivers.Databricks.Tests.E2E.Telemetry |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// A test implementation of <see cref="ITelemetryExporter"/> that captures all exported |
| 30 | + /// <see cref="TelemetryFrontendLog"/> instances in memory for test assertions. |
| 31 | + /// </summary> |
| 32 | + /// <remarks> |
| 33 | + /// This exporter never makes HTTP calls. It stores all exported logs in a thread-safe |
| 34 | + /// collection that tests can inspect after driver operations complete. |
| 35 | + /// Supports configurable failure simulation for circuit breaker and error path testing. |
| 36 | + /// </remarks> |
| 37 | + internal sealed class CapturingTelemetryExporter : ITelemetryExporter |
| 38 | + { |
| 39 | + private readonly ConcurrentBag<TelemetryFrontendLog> _capturedLogs = new ConcurrentBag<TelemetryFrontendLog>(); |
| 40 | + private volatile bool _shouldFail; |
| 41 | + private volatile int _exportCallCount; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets all captured telemetry frontend logs. |
| 45 | + /// </summary> |
| 46 | + public IReadOnlyList<TelemetryFrontendLog> CapturedLogs => _capturedLogs.ToList().AsReadOnly(); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Gets the total number of captured logs. |
| 50 | + /// </summary> |
| 51 | + public int CapturedLogCount => _capturedLogs.Count; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets the total number of times <see cref="ExportAsync"/> was called. |
| 55 | + /// </summary> |
| 56 | + public int ExportCallCount => _exportCallCount; |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets or sets whether the exporter should simulate failures. |
| 60 | + /// When true, <see cref="ExportAsync"/> returns false to simulate export failure. |
| 61 | + /// </summary> |
| 62 | + public bool ShouldFail |
| 63 | + { |
| 64 | + get => _shouldFail; |
| 65 | + set => _shouldFail = value; |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Export telemetry frontend logs by capturing them in memory. |
| 70 | + /// </summary> |
| 71 | + /// <param name="logs">The list of telemetry frontend logs to capture.</param> |
| 72 | + /// <param name="ct">Cancellation token.</param> |
| 73 | + /// <returns> |
| 74 | + /// True if capture succeeded (and <see cref="ShouldFail"/> is false), |
| 75 | + /// false if <see cref="ShouldFail"/> is true. |
| 76 | + /// Returns true for empty/null logs. |
| 77 | + /// </returns> |
| 78 | + public Task<bool> ExportAsync(IReadOnlyList<TelemetryFrontendLog> logs, CancellationToken ct = default) |
| 79 | + { |
| 80 | + Interlocked.Increment(ref _exportCallCount); |
| 81 | + |
| 82 | + if (logs == null || logs.Count == 0) |
| 83 | + { |
| 84 | + return Task.FromResult(true); |
| 85 | + } |
| 86 | + |
| 87 | + if (_shouldFail) |
| 88 | + { |
| 89 | + return Task.FromResult(false); |
| 90 | + } |
| 91 | + |
| 92 | + foreach (TelemetryFrontendLog log in logs) |
| 93 | + { |
| 94 | + _capturedLogs.Add(log); |
| 95 | + } |
| 96 | + |
| 97 | + return Task.FromResult(true); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Clears all captured logs and resets the call counter. |
| 102 | + /// </summary> |
| 103 | + public void Reset() |
| 104 | + { |
| 105 | + while (_capturedLogs.TryTake(out _)) |
| 106 | + { |
| 107 | + // Drain the bag |
| 108 | + } |
| 109 | + _exportCallCount = 0; |
| 110 | + _shouldFail = false; |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Waits until at least the specified number of logs have been captured, |
| 115 | + /// or the timeout is exceeded. |
| 116 | + /// </summary> |
| 117 | + /// <param name="expectedCount">The minimum number of logs to wait for.</param> |
| 118 | + /// <param name="timeout">Maximum time to wait.</param> |
| 119 | + /// <returns>True if the expected count was reached; false if timed out.</returns> |
| 120 | + public async Task<bool> WaitForLogsAsync(int expectedCount, TimeSpan timeout) |
| 121 | + { |
| 122 | + DateTime deadline = DateTime.UtcNow + timeout; |
| 123 | + while (_capturedLogs.Count < expectedCount && DateTime.UtcNow < deadline) |
| 124 | + { |
| 125 | + await Task.Delay(50).ConfigureAwait(false); |
| 126 | + } |
| 127 | + return _capturedLogs.Count >= expectedCount; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments