< Summary - pva.SuperV

Information
Class: pva.SuperV.TestContainers.TDengineContainer
Assembly: pva.SuperV.TestContainers
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.TestContainers/TDengineContainer.cs
Tag: dotnet-ubuntu_22190969454
Line coverage
77%
Covered lines: 83
Uncovered lines: 24
Coverable lines: 107
Total lines: 143
Line coverage: 77.5%
Branch coverage
77%
Covered branches: 17
Total branches: 22
Branch coverage: 77.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
WaitForPort(...)75%16850%
StartTDengineContainerAsync()100%2292.59%
WaitForTDengineToBeReady()87.5%8883.33%
StopTDengineContainerAsync()100%22100%
DisposeAsync()0%620%
DisposeAsync()100%210%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.TestContainers/TDengineContainer.cs

#LineLine coverage
 1using DotNet.Testcontainers.Builders;
 2using DotNet.Testcontainers.Containers;
 3using pva.Helpers;
 4using System.Net.NetworkInformation;
 5
 6namespace pva.SuperV.TestContainers
 7{
 8    public class TDengineContainer : IAsyncDisposable
 9    {
 10        private IContainer? tdEngineContainer;
 11        private bool disposedValue;
 12
 13        private static void WaitForPort(int port)
 1214        {
 15            const int MaxWaitIndex = 50;
 1216            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
 1217            int index = 0;
 177918            while (index < MaxWaitIndex && ipGlobalProperties.GetActiveTcpConnections().Any(pr => pr.LocalEndPoint.Port 
 019            {
 020                Thread.Sleep(100);
 021                index++;
 022            }
 1223            if (index == MaxWaitIndex)
 024            {
 025                throw new ApplicationException($"Port 6030 is in use after {MaxWaitIndex * 100} msecs");
 26            }
 1227        }
 28
 29        public async Task<string> StartTDengineContainerAsync()
 1230        {
 1231            if (tdEngineContainer is null)
 1232            {
 1233                Console.WriteLine("Starting TD engine container");
 1234                WaitForPort(6030);
 1235                tdEngineContainer = new ContainerBuilder("tdengine/tsdb:3.4.0.2")
 1236                    .WithPortBinding(6030)
 1237                    .WithPortBinding(6031)
 1238                    .WithPortBinding(6032)
 1239                    .WithPortBinding(6033)
 1240                    .WithPortBinding(6034)
 1241                    .WithPortBinding(6035)
 1242                    .WithPortBinding(6036)
 1243                    .WithPortBinding(6037)
 1244                    .WithPortBinding(6038)
 1245                    .WithPortBinding(6039)
 1246                    .WithPortBinding(6040)
 1247                    .WithPortBinding(6041)
 1248                    .WithPortBinding(6042)
 1249                    .WithPortBinding(6043)
 1250                    .WithPortBinding(6044)
 1251                    .WithPortBinding(6045)
 1252                    .WithPortBinding(6046)
 1253                    .WithPortBinding(6047)
 1254                    .WithPortBinding(6048)
 1255                    .WithPortBinding(6049)
 1256                    .WithPortBinding(6050)
 1257                    .WithPortBinding(6051)
 1258                    .WithPortBinding(6052)
 1259                    .WithPortBinding(6053)
 1260                    .WithPortBinding(6054)
 1261                    .WithPortBinding(6055)
 1262                    .WithPortBinding(6056)
 1263                    .WithPortBinding(6057)
 1264                    .WithPortBinding(6058)
 1265                    .WithPortBinding(6059)
 1266                    .WithPortBinding(6060)
 1267                    .WithExtraHost("buildkitsandbox", "127.0.0.1")
 1268                    .WithWaitStrategy(
 1269                        Wait.ForUnixContainer()
 1270                        .UntilExternalTcpPortIsAvailable(6030, strategy => strategy.WithTimeout(TimeSpan.FromSeconds(15)
 1271                    )
 1272                    .Build();
 73
 74                // Start the container.
 75                try
 1276                {
 1277                    Task tdEngineStartAsync = tdEngineContainer.StartAsync();
 1278                    await tdEngineStartAsync.WaitAsync(TimeSpan.FromSeconds(30));
 79                    // Wait to make sure the processes in container are ready and running.
 1280                    await WaitForTDengineToBeReady();
 1281                    Console.WriteLine("TD engine container started");
 1282                }
 083                catch (Exception)
 084                {
 085                    await StopTDengineContainerAsync();
 086                    throw;
 87                }
 88            }
 1289            return $"host={tdEngineContainer.Hostname};port={tdEngineContainer.GetMappedPublicPort(6030)};username=root;
 1290        }
 91
 92        private async ValueTask WaitForTDengineToBeReady()
 1293        {
 1294            bool connected = false;
 1295            int index = 0;
 4896            while (!connected && index < 10)
 3697            {
 3698                SystemCommand.Run("taos", $"-h {tdEngineContainer!.Hostname} -P {tdEngineContainer.GetMappedPublicPort(6
 3699                connected = output.Contains("ready");
 36100                if (!connected)
 24101                {
 24102                    Thread.Sleep(500);
 24103                    index++;
 24104                }
 36105            }
 12106            if (!connected)
 0107            {
 0108                var (Stdout, Stderr) = await tdEngineContainer!.GetLogsAsync();
 0109                throw new ApplicationException($"Can't connect to TDengine container {tdEngineContainer!.Hostname}! Out:
 110            }
 12111        }
 112
 113        public async Task<long> StopTDengineContainerAsync()
 146114        {
 146115            if (tdEngineContainer is not null)
 12116            {
 12117                await tdEngineContainer.StopAsync();
 12118                Console.WriteLine($"TD engine container {tdEngineContainer.Id} stopped");
 12119                long exitCode = await tdEngineContainer.GetExitCodeAsync();
 12120                Console.WriteLine($"TD engine container {tdEngineContainer.Id} exit code {exitCode}");
 12121                tdEngineContainer = null;
 12122                return exitCode;
 123            }
 134124            return 0;
 146125        }
 126
 127        protected virtual async ValueTask DisposeAsync(bool disposing)
 0128        {
 0129            if (!disposedValue)
 0130            {
 0131                await StopTDengineContainerAsync();
 0132                disposedValue = true;
 0133            }
 0134        }
 135
 136        public async ValueTask DisposeAsync()
 0137        {
 138            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
 0139            await DisposeAsync(disposing: true);
 0140            GC.SuppressFinalize(this);
 0141        }
 142    }
 143}