├── final-output.PNG ├── src ├── DummyService.Database │ ├── db.bak │ ├── Dockerfile │ ├── CreateDb.sql │ └── db.sh ├── .dockerignore ├── DummyService.App │ ├── Application │ │ ├── Models │ │ │ ├── MessageDataDto.cs │ │ │ └── DummyEvent.cs │ │ ├── Handlers │ │ │ ├── IDummyEventHandler.cs │ │ │ └── DummyEventHandler.cs │ │ ├── Storage │ │ │ └── IMessageDatabase.cs │ │ └── Messaging │ │ │ ├── IMessageProcessor.cs │ │ │ └── IEndpointConfiguration.cs │ ├── Infrastructure │ │ ├── Storage │ │ │ ├── Entities │ │ │ │ └── MessageData.cs │ │ │ ├── DummyDbContext.cs │ │ │ └── SqlMessageDatabase.cs │ │ └── Messaging │ │ │ ├── EndpointConfiguration.cs │ │ │ └── MessageProcessor.cs │ ├── appsettings.json │ ├── Dockerfile │ ├── DummyService.App.csproj │ ├── TimedHostedService.cs │ └── Program.cs ├── DummyService.AcceptanceTests │ ├── appsettings.json │ ├── Data │ │ ├── Entities │ │ │ └── MessageData.cs │ │ ├── DummyDbContext.cs │ │ └── DbHelper.cs │ ├── Dockerfile │ ├── ConfigurationReader.cs │ ├── DummyService.AcceptanceTests.csproj │ ├── DummyEventHandlingFeature.cs │ └── wait-for-it.sh ├── docker-compose.yml ├── DummyService.sln └── wait-for-it.sh ├── README.md ├── .vscode ├── tasks.json └── launch.json ├── LICENSE.md └── .gitignore /final-output.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iqan/service-testing-docker-poc/HEAD/final-output.PNG -------------------------------------------------------------------------------- /src/DummyService.Database/db.bak: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iqan/service-testing-docker-poc/HEAD/src/DummyService.Database/db.bak -------------------------------------------------------------------------------- /src/.dockerignore: -------------------------------------------------------------------------------- 1 | .dockerignore 2 | .env 3 | .git 4 | .gitignore 5 | .vs 6 | .vscode 7 | docker-compose.yml 8 | docker-compose.*.yml 9 | */bin 10 | */obj 11 | -------------------------------------------------------------------------------- /src/DummyService.App/Application/Models/MessageDataDto.cs: -------------------------------------------------------------------------------- 1 | namespace DummyService.App.Application.Models 2 | { 3 | public class MessageDataDto 4 | { 5 | public string MessageText { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/DummyService.App/Application/Handlers/IDummyEventHandler.cs: -------------------------------------------------------------------------------- 1 | using DummyService.App.Application.Models; 2 | 3 | namespace DummyService.App.Application.Handlers 4 | { 5 | public interface IDummyEventHandler 6 | { 7 | void Handle(DummyEvent dummyEvent); 8 | } 9 | } -------------------------------------------------------------------------------- /src/DummyService.App/Application/Storage/IMessageDatabase.cs: -------------------------------------------------------------------------------- 1 | using DummyService.App.Application.Models; 2 | 3 | namespace DummyService.App.Application.Storage 4 | { 5 | public interface IMessageDatabase 6 | { 7 | void InsertMessageData(MessageDataDto messageData); 8 | } 9 | } -------------------------------------------------------------------------------- /src/DummyService.Database/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mcr.microsoft.com/mssql/server:2022-CU13-ubuntu-22.04 2 | 3 | WORKDIR /work 4 | 5 | COPY DummyService.Database/CreateDb.sql . 6 | COPY DummyService.Database/db.sh . 7 | COPY DummyService.Database/db.bak . 8 | 9 | RUN ./db.sh CreateDb.sql 10 | -------------------------------------------------------------------------------- /src/DummyService.Database/CreateDb.sql: -------------------------------------------------------------------------------- 1 | USE master 2 | GO 3 | 4 | RESTORE DATABASE DummyDatabase 5 | FROM DISK = '/work/db.bak' WITH 6 | MOVE 'DummyDatabase' TO '/var/opt/mssql/data/DummyDatabase/DummyDatabase.mdf', 7 | MOVE 'DummyDatabase_log' TO '/var/opt/mssql/data/DummyDatabase/DummyDatabase_log.ldf' 8 | GO 9 | -------------------------------------------------------------------------------- /src/DummyService.AcceptanceTests/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "EndpointConfiguration": { 3 | "ConnectionString": "", 4 | "Topic": "topic1" 5 | }, 6 | "ConnectionStrings": { 7 | "DummyDatabase": "Server=localhost;Database=DummyDatabase;Trusted_Connection=True;" 8 | } 9 | } -------------------------------------------------------------------------------- /src/DummyService.App/Application/Messaging/IMessageProcessor.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace DummyService.App.Application.Messaging 4 | { 5 | public interface IMessageProcessor 6 | { 7 | Task StartProcessingAsync(); 8 | 9 | Task StopProcessingAsync(); 10 | } 11 | } -------------------------------------------------------------------------------- /src/DummyService.App/Application/Messaging/IEndpointConfiguration.cs: -------------------------------------------------------------------------------- 1 | namespace DummyService.App.Application.Messaging 2 | { 3 | public interface IEndpointConfiguration 4 | { 5 | string ConnectionString { get; set; } 6 | string Subscription { get; set; } 7 | string Topic { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /src/DummyService.App/Application/Models/DummyEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DummyService.App.Application.Models 6 | { 7 | public class DummyEvent 8 | { 9 | public int Id { get; set; } 10 | public string Text { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/DummyService.App/Infrastructure/Storage/Entities/MessageData.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations.Schema; 2 | 3 | namespace DummyService.App.Infrastructure.Storage.Entities 4 | { 5 | [Table("MessageData")] 6 | public class MessageData 7 | { 8 | public int MessageDataId { get; set; } 9 | public string MessageText { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/DummyService.Database/db.sh: -------------------------------------------------------------------------------- 1 | echo "Setting Environment variables." 2 | export ACCEPT_EULA=Y 3 | export MSSQL_SA_PASSWORD=SuperSecretPassword#1 4 | echo "Environment variables set." 5 | echo "Starting SqlServr" 6 | /opt/mssql/bin/sqlservr & 7 | sleep 60 | echo "Waiting for 60s to start Sql Server" 8 | echo "Restoring DB." 9 | /opt/mssql-tools/bin/sqlcmd -U sa -P SuperSecretPassword#1 -i $1 10 | echo "DB restored." -------------------------------------------------------------------------------- /src/DummyService.App/Infrastructure/Messaging/EndpointConfiguration.cs: -------------------------------------------------------------------------------- 1 | using DummyService.App.Application.Messaging; 2 | 3 | namespace DummyService.App.Infrastructure.Messaging 4 | { 5 | public class EndpointConfiguration : IEndpointConfiguration 6 | { 7 | public string ConnectionString { get; set; } 8 | 9 | public string Topic { get; set; } 10 | 11 | public string Subscription { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/DummyService.AcceptanceTests/Data/Entities/MessageData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | using System.Text; 5 | 6 | namespace DummyService.AcceptanceTests.Data.Entities 7 | { 8 | [Table("MessageData")] 9 | public class MessageData 10 | { 11 | public int MessageDataId { get; set; } 12 | public string MessageText { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/DummyService.App/Infrastructure/Storage/DummyDbContext.cs: -------------------------------------------------------------------------------- 1 | using DummyService.App.Infrastructure.Storage.Entities; 2 | using Microsoft.EntityFrameworkCore; 3 | 4 | namespace DummyService.App.Infrastructure.Storage 5 | { 6 | public class DummyDbContext : DbContext 7 | { 8 | public DummyDbContext(DbContextOptions options) : base (options) 9 | { 10 | 11 | } 12 | 13 | public DbSet MessageDatas { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/DummyService.App/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | }, 9 | "EndpointConfiguration": { 10 | "ConnectionString": "", 11 | "Topic": "topic1", 12 | "Subscription": "sub1" 13 | }, 14 | "ConnectionStrings": { 15 | "DummyDatabase": "Server=localhost;Database=DummyDatabase;Trusted_Connection=True;" 16 | } 17 | } -------------------------------------------------------------------------------- /src/DummyService.AcceptanceTests/Data/DummyDbContext.cs: -------------------------------------------------------------------------------- 1 | using DummyService.AcceptanceTests.Data.Entities; 2 | using Microsoft.EntityFrameworkCore; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace DummyService.AcceptanceTests.Data 8 | { 9 | public class DummyDbContext : DbContext 10 | { 11 | public DummyDbContext(DbContextOptions options) : base (options) 12 | { 13 | 14 | } 15 | 16 | public DbSet MessageDatas { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/DummyService.AcceptanceTests/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base 2 | WORKDIR /app 3 | 4 | 5 | FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build 6 | WORKDIR /src 7 | COPY ["DummyService.AcceptanceTests/DummyService.AcceptanceTests.csproj", "DummyService.AcceptanceTests/"] 8 | RUN dotnet restore "DummyService.AcceptanceTests/DummyService.AcceptanceTests.csproj" 9 | COPY . . 10 | WORKDIR "/src/DummyService.AcceptanceTests" 11 | RUN dotnet build "DummyService.AcceptanceTests.csproj" -c Release -o /app 12 | 13 | FROM build AS test 14 | #ENTRYPOINT ["dotnet", "test", "DummyService.AcceptanceTests.csproj"] -------------------------------------------------------------------------------- /src/DummyService.App/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base 2 | WORKDIR /app 3 | 4 | FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build 5 | WORKDIR /src 6 | COPY ["DummyService.App/DummyService.App.csproj", "DummyService.App/"] 7 | RUN dotnet restore "DummyService.App/DummyService.App.csproj" 8 | COPY . . 9 | WORKDIR "/src/DummyService.App" 10 | RUN dotnet build "DummyService.App.csproj" -c Release -o /app 11 | 12 | FROM build AS publish 13 | RUN dotnet publish "DummyService.App.csproj" -c Release -o /app 14 | 15 | FROM base AS final 16 | WORKDIR /app 17 | COPY --from=publish /app . 18 | ENTRYPOINT ["dotnet", "DummyService.App.dll"] 19 | -------------------------------------------------------------------------------- /src/DummyService.AcceptanceTests/Data/DbHelper.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace DummyService.AcceptanceTests.Data 7 | { 8 | public class DbHelper 9 | { 10 | public static string ConnectionString => ConfigurationReader.GetConnectionString("DummyDatabase"); 11 | 12 | public static DbContextOptions GetDbContextOptions() 13 | { 14 | if (string.IsNullOrEmpty(ConnectionString)) 15 | throw new Exception("Please set connection string for database"); 16 | var optionsBuilder = new DbContextOptionsBuilder(); 17 | var options = optionsBuilder.UseSqlServer(ConnectionString).Options; 18 | return options; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A POC showcasing timely running service with SQL Database storage 2 | 3 | A proof of concept showcasing .Net service running in container with SQL database and acceptance tests executing against it 4 | 5 | # Article on Medium 6 | 7 | - [How to create a timely running service in .NET 7](https://iqan.medium.com/how-to-create-a-timely-running-service-in-net-7-3af70d8494d1) 8 | 9 | - [How to create a timely running service in .NET Core](https://iqan.medium.com/how-to-create-a-timely-running-service-in-net-core-757f445035ca) 10 | 11 | ## .netcore 2.1 12 | 13 | If you are after outdated .netcore 2.1 implementation, you can refer to the tag [dotnetcore-2.1](https://github.com/iqan/service-testing-docker-poc/tree/dotnetcore-2.1) 14 | 15 | ## To run 16 | - Go to src: `cd src/` 17 | - Build docker containers `docker-compose build` 18 | - Run tests in compose `docker-compose up --exit-code-from dummyservice.tests` 19 | 20 | ## Screenshots 21 | ![final-output](./final-output.PNG) 22 | -------------------------------------------------------------------------------- /src/DummyService.AcceptanceTests/ConfigurationReader.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Configuration; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Text; 6 | 7 | namespace DummyService.AcceptanceTests 8 | { 9 | public class ConfigurationReader 10 | { 11 | private static IConfigurationRoot _configuration; 12 | 13 | static ConfigurationReader() 14 | { 15 | var builder = new ConfigurationBuilder() 16 | .SetBasePath(Directory.GetCurrentDirectory()) 17 | .AddJsonFile("appsettings.json") 18 | .AddEnvironmentVariables(); 19 | 20 | _configuration = builder.Build(); 21 | } 22 | 23 | public static string GetConnectionString(string name) 24 | { 25 | return _configuration.GetConnectionString(name); 26 | } 27 | 28 | public static string GetConfigValueFor(string key) 29 | { 30 | return _configuration[key]; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/DummyService.App/Application/Handlers/DummyEventHandler.cs: -------------------------------------------------------------------------------- 1 | using DummyService.App.Application.Models; 2 | using DummyService.App.Application.Storage; 3 | using Microsoft.Extensions.Logging; 4 | 5 | namespace DummyService.App.Application.Handlers 6 | { 7 | public class DummyEventHandler : IDummyEventHandler 8 | { 9 | private readonly ILogger _logger; 10 | private readonly IMessageDatabase _messageDatabase; 11 | 12 | public DummyEventHandler(ILogger logger, IMessageDatabase messageDatabase) 13 | { 14 | _logger = logger; 15 | _messageDatabase = messageDatabase; 16 | } 17 | 18 | public void Handle(DummyEvent dummyEvent) 19 | { 20 | _logger.LogInformation("Handling event"); 21 | 22 | _messageDatabase.InsertMessageData(new MessageDataDto 23 | { 24 | MessageText = dummyEvent.Text 25 | }); 26 | 27 | _logger.LogInformation("Event handled successfully"); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/src/DummyService.App/DummyService.App.csproj" 11 | ], 12 | "problemMatcher": "$tsc" 13 | }, 14 | { 15 | "label": "publish", 16 | "command": "dotnet", 17 | "type": "process", 18 | "args": [ 19 | "publish", 20 | "${workspaceFolder}/src/DummyService.App/DummyService.App.csproj" 21 | ], 22 | "problemMatcher": "$tsc" 23 | }, 24 | { 25 | "label": "watch", 26 | "command": "dotnet", 27 | "type": "process", 28 | "args": [ 29 | "watch", 30 | "run", 31 | "${workspaceFolder}/src/DummyService.App/DummyService.App.csproj" 32 | ], 33 | "problemMatcher": "$tsc" 34 | } 35 | ] 36 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Iqan Shaikh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/DummyService.App/Infrastructure/Storage/SqlMessageDatabase.cs: -------------------------------------------------------------------------------- 1 | using DummyService.App.Application.Models; 2 | using DummyService.App.Application.Storage; 3 | using DummyService.App.Infrastructure.Storage.Entities; 4 | using Microsoft.Extensions.Logging; 5 | 6 | namespace DummyService.App.Infrastructure.Storage 7 | { 8 | public class SqlMessageDatabase : IMessageDatabase 9 | { 10 | private readonly ILogger _logger; 11 | private readonly DummyDbContext _context; 12 | 13 | public SqlMessageDatabase(ILogger logger, DummyDbContext context) 14 | { 15 | _logger = logger; 16 | _context = context; 17 | } 18 | 19 | public void InsertMessageData(MessageDataDto messageData) 20 | { 21 | _logger.LogInformation("Inserting data in database"); 22 | 23 | var messageDataToInsert = new MessageData 24 | { 25 | MessageText = messageData.MessageText 26 | }; 27 | 28 | _context.MessageDatas.Add(messageDataToInsert); 29 | _context.SaveChanges(); 30 | 31 | _logger.LogInformation("Data successfully inserted. ID: " + messageDataToInsert.MessageDataId); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to find out which attributes exist for C# debugging 3 | // Use hover for the description of the existing attributes 4 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": ".NET Core Launch (console)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/src/DummyService.App/bin/Debug/netcoreapp2.2/DummyService.App.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}/src/DummyService.App", 16 | // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window 17 | "console": "internalConsole", 18 | "stopAtEntry": false 19 | }, 20 | { 21 | "name": ".NET Core Attach", 22 | "type": "coreclr", 23 | "request": "attach", 24 | "processId": "${command:pickProcess}" 25 | } 26 | ] 27 | } -------------------------------------------------------------------------------- /src/DummyService.App/DummyService.App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Linux 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | Always 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | dummyservice.app: 3 | image: iqan/dummyserviceapp 4 | build: 5 | context: . 6 | dockerfile: DummyService.App/Dockerfile 7 | environment: 8 | - EndpointConfiguration:ConnectionString= 9 | - EndpointConfiguration:Topic=topic1 10 | - EndpointConfiguration:Subscription=sub1 11 | - ConnectionStrings:DummyDatabase=Server=db;Database=DummyDatabase;User=sa;Password=SuperSecretPassword#1;TrustServerCertificate=true 12 | depends_on: 13 | - db 14 | db: 15 | image: iqan/dummydatabase 16 | build: 17 | context: . 18 | dockerfile: DummyService.Database/Dockerfile 19 | environment: 20 | - MSSQL_SA_PASSWORD=SuperSecretPassword#1 21 | - ACCEPT_EULA=Y 22 | ports: 23 | - "1433:1433" 24 | dummyservice.tests: 25 | image: iqan/dummyservicetests 26 | build: 27 | context: . 28 | dockerfile: DummyService.AcceptanceTests/Dockerfile 29 | environment: 30 | - EndpointConfiguration:ConnectionString= 31 | - EndpointConfiguration:Topic=topic1 32 | - EndpointConfiguration:Subscription=sub1 33 | - ConnectionStrings:DummyDatabase=Server=db;Database=DummyDatabase;User=sa;Password=SuperSecretPassword#1;TrustServerCertificate=true 34 | depends_on: 35 | - db 36 | - dummyservice.app 37 | command: ["./wait-for-it.sh", "db:1433", "-t", "30", "--", "dotnet", "test", "DummyService.AcceptanceTests.csproj"] -------------------------------------------------------------------------------- /src/DummyService.App/TimedHostedService.cs: -------------------------------------------------------------------------------- 1 | using DummyService.App.Application.Messaging; 2 | using Microsoft.Extensions.Hosting; 3 | using Microsoft.Extensions.Logging; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | 7 | namespace DummyService.App 8 | { 9 | public class TimedHostedService : BackgroundService 10 | { 11 | private readonly ILogger _logger; 12 | private readonly IMessageProcessor _messageProcessor; 13 | 14 | public TimedHostedService(ILogger logger, IMessageProcessor messageProcessor) 15 | { 16 | _logger = logger; 17 | _messageProcessor = messageProcessor; 18 | } 19 | 20 | public override Task StartAsync(CancellationToken cancellationToken) 21 | { 22 | _logger.LogInformation("Service is starting."); 23 | return _messageProcessor.StartProcessingAsync(); 24 | } 25 | 26 | protected override async Task ExecuteAsync(CancellationToken stoppingToken) 27 | { 28 | while (!stoppingToken.IsCancellationRequested) 29 | { 30 | _logger.LogInformation("Service is running."); 31 | await Task.Delay(1000, stoppingToken); 32 | } 33 | } 34 | 35 | public override Task StopAsync(CancellationToken cancellationToken) 36 | { 37 | _logger.LogInformation("Service is stopping."); 38 | return _messageProcessor.StopProcessingAsync(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/DummyService.AcceptanceTests/DummyService.AcceptanceTests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net7.0 5 | 6 | false 7 | 8 | Linux 9 | 10 | 11 | 12 | 13 | Always 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | all 31 | runtime; build; native; contentfiles; analyzers; buildtransitive 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/DummyService.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.7.34031.279 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DummyService.App", "DummyService.App\DummyService.App.csproj", "{215276E6-E80A-4F9E-BC6A-C7416FF487F3}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DummyService.AcceptanceTests", "DummyService.AcceptanceTests\DummyService.AcceptanceTests.csproj", "{C52EF7F6-19E8-4A28-910E-125999A4BC18}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DummyService.Database", "DummyService.Database", "{22596E1F-358C-47A8-A9BC-E85DC4DC3410}" 11 | ProjectSection(SolutionItems) = preProject 12 | DummyDatabase\CreateDb.sql = DummyDatabase\CreateDb.sql 13 | DummyDatabase\db.sh = DummyDatabase\db.sh 14 | DummyDatabase\Dockerfile = DummyDatabase\Dockerfile 15 | EndProjectSection 16 | EndProject 17 | Global 18 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 19 | Debug|Any CPU = Debug|Any CPU 20 | Release|Any CPU = Release|Any CPU 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {215276E6-E80A-4F9E-BC6A-C7416FF487F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 24 | {215276E6-E80A-4F9E-BC6A-C7416FF487F3}.Debug|Any CPU.Build.0 = Debug|Any CPU 25 | {215276E6-E80A-4F9E-BC6A-C7416FF487F3}.Release|Any CPU.ActiveCfg = Release|Any CPU 26 | {215276E6-E80A-4F9E-BC6A-C7416FF487F3}.Release|Any CPU.Build.0 = Release|Any CPU 27 | {C52EF7F6-19E8-4A28-910E-125999A4BC18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 28 | {C52EF7F6-19E8-4A28-910E-125999A4BC18}.Debug|Any CPU.Build.0 = Debug|Any CPU 29 | {C52EF7F6-19E8-4A28-910E-125999A4BC18}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {C52EF7F6-19E8-4A28-910E-125999A4BC18}.Release|Any CPU.Build.0 = Release|Any CPU 31 | EndGlobalSection 32 | GlobalSection(SolutionProperties) = preSolution 33 | HideSolutionNode = FALSE 34 | EndGlobalSection 35 | GlobalSection(ExtensibilityGlobals) = postSolution 36 | SolutionGuid = {CDC0624D-D14A-47F0-9141-671BD2EC6BAB} 37 | EndGlobalSection 38 | EndGlobal 39 | -------------------------------------------------------------------------------- /src/DummyService.App/Program.cs: -------------------------------------------------------------------------------- 1 | using DummyService.App.Application.Handlers; 2 | using DummyService.App.Application.Messaging; 3 | using DummyService.App.Application.Storage; 4 | using DummyService.App.Infrastructure.Messaging; 5 | using DummyService.App.Infrastructure.Storage; 6 | using Microsoft.EntityFrameworkCore; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Hosting; 10 | using Microsoft.Extensions.Logging; 11 | using System.IO; 12 | using System.Threading.Tasks; 13 | 14 | namespace DummyService.App 15 | { 16 | class Program 17 | { 18 | public static async Task Main(string[] args) 19 | { 20 | var hostBuilder = new HostBuilder() 21 | .ConfigureAppConfiguration((hostContext, configBuilder) => 22 | { 23 | configBuilder.SetBasePath(Directory.GetCurrentDirectory()); 24 | configBuilder.AddJsonFile("appsettings.json", optional: true); 25 | configBuilder.AddJsonFile( 26 | $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", 27 | optional: true); 28 | configBuilder.AddEnvironmentVariables(); 29 | }) 30 | .ConfigureLogging((hostContext, configLogging) => 31 | { 32 | configLogging.AddConfiguration(hostContext.Configuration.GetSection("Logging")); 33 | configLogging.AddConsole(); 34 | }) 35 | .ConfigureServices((hostContext, services) => 36 | { 37 | services.AddSingleton(serviceProvider => 38 | { 39 | return hostContext.Configuration.GetSection("EndpointConfiguration").Get(); 40 | }); 41 | 42 | services.AddDbContext(options => 43 | { 44 | options.UseSqlServer(hostContext.Configuration.GetConnectionString("DummyDatabase")); 45 | }); 46 | 47 | services.AddScoped(); 48 | 49 | services.AddScoped(); 50 | 51 | services.AddScoped(); 52 | 53 | services.AddHostedService(); 54 | }); 55 | 56 | await hostBuilder.RunConsoleAsync(); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/DummyService.App/Infrastructure/Messaging/MessageProcessor.cs: -------------------------------------------------------------------------------- 1 | using DummyService.App.Application.Handlers; 2 | using DummyService.App.Application.Models; 3 | using Azure.Messaging.ServiceBus; 4 | using Microsoft.Extensions.Logging; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using DummyService.App.Application.Messaging; 8 | 9 | namespace DummyService.App.Infrastructure.Messaging 10 | { 11 | public class MessageProcessor : IMessageProcessor 12 | { 13 | private ServiceBusClient _serviceBusClient; 14 | private readonly ILogger _logger; 15 | private readonly IEndpointConfiguration _endpointConfiguration; 16 | private readonly IDummyEventHandler _dummyEventHandler; 17 | private readonly ServiceBusProcessor _serviceBusProcessor; 18 | 19 | public MessageProcessor( 20 | ILogger logger, 21 | IEndpointConfiguration endpointConfiguration, 22 | IDummyEventHandler dummyEventHandler) 23 | { 24 | _logger = logger; 25 | _endpointConfiguration = endpointConfiguration; 26 | _dummyEventHandler = dummyEventHandler; 27 | _serviceBusProcessor = CreateMessageProcessor(); 28 | } 29 | 30 | public Task StartProcessingAsync() 31 | { 32 | return _serviceBusProcessor.StartProcessingAsync(); 33 | } 34 | 35 | public Task StopProcessingAsync() 36 | { 37 | return _serviceBusProcessor.StopProcessingAsync(); 38 | } 39 | 40 | private ServiceBusProcessor CreateMessageProcessor() 41 | { 42 | _logger.LogInformation("Creating servicebus client"); 43 | 44 | _serviceBusClient = new ServiceBusClient(_endpointConfiguration.ConnectionString); 45 | 46 | var options = new ServiceBusProcessorOptions 47 | { 48 | MaxConcurrentCalls = 1, 49 | AutoCompleteMessages = false 50 | }; 51 | 52 | var processor = _serviceBusClient.CreateProcessor(_endpointConfiguration.Topic, _endpointConfiguration.Subscription, options); 53 | 54 | _logger.LogInformation("Registering message handler"); 55 | processor.ProcessMessageAsync += MessageHandler; 56 | processor.ProcessErrorAsync += ErrorHandler; 57 | 58 | return processor; 59 | } 60 | 61 | private async Task MessageHandler(ProcessMessageEventArgs args) 62 | { 63 | var body = Encoding.UTF8.GetString(args.Message.Body); 64 | _logger.LogInformation($"Received message: Body:{body}"); 65 | var @event = new DummyEvent { Text = body }; 66 | _dummyEventHandler.Handle(@event); 67 | await args.CompleteMessageAsync(args.Message); 68 | } 69 | 70 | private Task ErrorHandler(ProcessErrorEventArgs args) 71 | { 72 | _logger.LogInformation($"Message handler encountered an exception {args.Exception}."); 73 | return Task.CompletedTask; 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/DummyService.AcceptanceTests/DummyEventHandlingFeature.cs: -------------------------------------------------------------------------------- 1 | using DummyService.AcceptanceTests.Data; 2 | using DummyService.AcceptanceTests.Data.Entities; 3 | using FluentAssertions; 4 | using Azure.Messaging.ServiceBus; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using TestStack.BDDfy; 9 | using Xunit; 10 | 11 | namespace DummyService.AcceptanceTests 12 | { 13 | [Story( 14 | Title = "DummyEvent is handled", 15 | AsA = "As a dummy service", 16 | IWant = "I want to handle dummy event", 17 | SoThat = "So that I can get message data inserted in database")] 18 | public class DummyEventHandlingFeature 19 | { 20 | private IDictionary _inMemoryStorage = new Dictionary(); 21 | 22 | [Fact] 23 | public void Vanilla() 24 | { 25 | this.Given(_ => IHaveServiceRunning()) 26 | .When(_ => ISendDummyEvent()) 27 | .Then(_ => IShouldGetMessageDataInsertedInDatabase()) 28 | .BDDfy("Vanilla case"); 29 | } 30 | 31 | private void IHaveServiceRunning() 32 | { 33 | DeleteAllExistingData(); 34 | } 35 | 36 | private void ISendDummyEvent() 37 | { 38 | var text = "Some dummy text"; 39 | var message = new ServiceBusMessage(System.Text.Encoding.UTF8.GetBytes(text)); 40 | SendMessage(message); 41 | _inMemoryStorage.Add("text", text); 42 | } 43 | 44 | private void SendMessage(ServiceBusMessage message) 45 | { 46 | var connectionString = ConfigurationReader.GetConfigValueFor("EndpointConfiguration:ConnectionString"); 47 | var topic = ConfigurationReader.GetConfigValueFor("EndpointConfiguration:Topic"); 48 | 49 | var client = new ServiceBusClient(connectionString).CreateSender(topic); 50 | client.SendMessageAsync(message).GetAwaiter().GetResult(); 51 | } 52 | 53 | private void IShouldGetMessageDataInsertedInDatabase() 54 | { 55 | System.Threading.Thread.Sleep(5000); 56 | 57 | var expectedText = _inMemoryStorage["text"]; 58 | 59 | var messageData = GetMessageData(); 60 | 61 | messageData.Should().NotBeNull(); 62 | messageData.MessageText.Should().Be(expectedText); 63 | 64 | DeleteAllExistingData(); 65 | } 66 | 67 | private MessageData GetMessageData() 68 | { 69 | var options = DbHelper.GetDbContextOptions(); 70 | using (var context = new DummyDbContext(options)) 71 | { 72 | return context.MessageDatas.FirstOrDefault(); 73 | } 74 | } 75 | 76 | private static void DeleteAllExistingData() 77 | { 78 | var options = DbHelper.GetDbContextOptions(); 79 | using (var context = new DummyDbContext(options)) 80 | { 81 | context.MessageDatas.RemoveRange(context.MessageDatas); 82 | context.SaveChanges(); 83 | } 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/wait-for-it.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Use this script to test if a given TCP host/port are available 3 | 4 | WAITFORIT_cmdname=${0##*/} 5 | 6 | echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } 7 | 8 | usage() 9 | { 10 | cat << USAGE >&2 11 | Usage: 12 | $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] 13 | -h HOST | --host=HOST Host or IP under test 14 | -p PORT | --port=PORT TCP port under test 15 | Alternatively, you specify the host and port as host:port 16 | -s | --strict Only execute subcommand if the test succeeds 17 | -q | --quiet Don't output any status messages 18 | -t TIMEOUT | --timeout=TIMEOUT 19 | Timeout in seconds, zero for no timeout 20 | -- COMMAND ARGS Execute command with args after the test finishes 21 | USAGE 22 | exit 1 23 | } 24 | 25 | wait_for() 26 | { 27 | if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then 28 | echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" 29 | else 30 | echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" 31 | fi 32 | WAITFORIT_start_ts=$(date +%s) 33 | while : 34 | do 35 | if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then 36 | nc -z $WAITFORIT_HOST $WAITFORIT_PORT 37 | WAITFORIT_result=$? 38 | else 39 | (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 40 | WAITFORIT_result=$? 41 | fi 42 | if [[ $WAITFORIT_result -eq 0 ]]; then 43 | WAITFORIT_end_ts=$(date +%s) 44 | echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" 45 | break 46 | fi 47 | sleep 1 48 | done 49 | return $WAITFORIT_result 50 | } 51 | 52 | wait_for_wrapper() 53 | { 54 | # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 55 | if [[ $WAITFORIT_QUIET -eq 1 ]]; then 56 | timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & 57 | else 58 | timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & 59 | fi 60 | WAITFORIT_PID=$! 61 | trap "kill -INT -$WAITFORIT_PID" INT 62 | wait $WAITFORIT_PID 63 | WAITFORIT_RESULT=$? 64 | if [[ $WAITFORIT_RESULT -ne 0 ]]; then 65 | echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" 66 | fi 67 | return $WAITFORIT_RESULT 68 | } 69 | 70 | # process arguments 71 | while [[ $# -gt 0 ]] 72 | do 73 | case "$1" in 74 | *:* ) 75 | WAITFORIT_hostport=(${1//:/ }) 76 | WAITFORIT_HOST=${WAITFORIT_hostport[0]} 77 | WAITFORIT_PORT=${WAITFORIT_hostport[1]} 78 | shift 1 79 | ;; 80 | --child) 81 | WAITFORIT_CHILD=1 82 | shift 1 83 | ;; 84 | -q | --quiet) 85 | WAITFORIT_QUIET=1 86 | shift 1 87 | ;; 88 | -s | --strict) 89 | WAITFORIT_STRICT=1 90 | shift 1 91 | ;; 92 | -h) 93 | WAITFORIT_HOST="$2" 94 | if [[ $WAITFORIT_HOST == "" ]]; then break; fi 95 | shift 2 96 | ;; 97 | --host=*) 98 | WAITFORIT_HOST="${1#*=}" 99 | shift 1 100 | ;; 101 | -p) 102 | WAITFORIT_PORT="$2" 103 | if [[ $WAITFORIT_PORT == "" ]]; then break; fi 104 | shift 2 105 | ;; 106 | --port=*) 107 | WAITFORIT_PORT="${1#*=}" 108 | shift 1 109 | ;; 110 | -t) 111 | WAITFORIT_TIMEOUT="$2" 112 | if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi 113 | shift 2 114 | ;; 115 | --timeout=*) 116 | WAITFORIT_TIMEOUT="${1#*=}" 117 | shift 1 118 | ;; 119 | --) 120 | shift 121 | WAITFORIT_CLI=("$@") 122 | break 123 | ;; 124 | --help) 125 | usage 126 | ;; 127 | *) 128 | echoerr "Unknown argument: $1" 129 | usage 130 | ;; 131 | esac 132 | done 133 | 134 | if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then 135 | echoerr "Error: you need to provide a host and port to test." 136 | usage 137 | fi 138 | 139 | WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} 140 | WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} 141 | WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} 142 | WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} 143 | 144 | # check to see if timeout is from busybox? 145 | WAITFORIT_TIMEOUT_PATH=$(type -p timeout) 146 | WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) 147 | if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then 148 | WAITFORIT_ISBUSY=1 149 | WAITFORIT_BUSYTIMEFLAG="-t" 150 | 151 | else 152 | WAITFORIT_ISBUSY=0 153 | WAITFORIT_BUSYTIMEFLAG="" 154 | fi 155 | 156 | if [[ $WAITFORIT_CHILD -gt 0 ]]; then 157 | wait_for 158 | WAITFORIT_RESULT=$? 159 | exit $WAITFORIT_RESULT 160 | else 161 | if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then 162 | wait_for_wrapper 163 | WAITFORIT_RESULT=$? 164 | else 165 | wait_for 166 | WAITFORIT_RESULT=$? 167 | fi 168 | fi 169 | 170 | if [[ $WAITFORIT_CLI != "" ]]; then 171 | if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then 172 | echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" 173 | exit $WAITFORIT_RESULT 174 | fi 175 | exec "${WAITFORIT_CLI[@]}" 176 | else 177 | exit $WAITFORIT_RESULT 178 | fi -------------------------------------------------------------------------------- /src/DummyService.AcceptanceTests/wait-for-it.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Use this script to test if a given TCP host/port are available 3 | 4 | WAITFORIT_cmdname=${0##*/} 5 | 6 | echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } 7 | 8 | usage() 9 | { 10 | cat << USAGE >&2 11 | Usage: 12 | $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] 13 | -h HOST | --host=HOST Host or IP under test 14 | -p PORT | --port=PORT TCP port under test 15 | Alternatively, you specify the host and port as host:port 16 | -s | --strict Only execute subcommand if the test succeeds 17 | -q | --quiet Don't output any status messages 18 | -t TIMEOUT | --timeout=TIMEOUT 19 | Timeout in seconds, zero for no timeout 20 | -- COMMAND ARGS Execute command with args after the test finishes 21 | USAGE 22 | exit 1 23 | } 24 | 25 | wait_for() 26 | { 27 | if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then 28 | echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" 29 | else 30 | echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" 31 | fi 32 | WAITFORIT_start_ts=$(date +%s) 33 | while : 34 | do 35 | if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then 36 | nc -z $WAITFORIT_HOST $WAITFORIT_PORT 37 | WAITFORIT_result=$? 38 | else 39 | (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 40 | WAITFORIT_result=$? 41 | fi 42 | if [[ $WAITFORIT_result -eq 0 ]]; then 43 | WAITFORIT_end_ts=$(date +%s) 44 | echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" 45 | break 46 | fi 47 | sleep 1 48 | done 49 | return $WAITFORIT_result 50 | } 51 | 52 | wait_for_wrapper() 53 | { 54 | # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 55 | if [[ $WAITFORIT_QUIET -eq 1 ]]; then 56 | timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & 57 | else 58 | timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & 59 | fi 60 | WAITFORIT_PID=$! 61 | trap "kill -INT -$WAITFORIT_PID" INT 62 | wait $WAITFORIT_PID 63 | WAITFORIT_RESULT=$? 64 | if [[ $WAITFORIT_RESULT -ne 0 ]]; then 65 | echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" 66 | fi 67 | return $WAITFORIT_RESULT 68 | } 69 | 70 | # process arguments 71 | while [[ $# -gt 0 ]] 72 | do 73 | case "$1" in 74 | *:* ) 75 | WAITFORIT_hostport=(${1//:/ }) 76 | WAITFORIT_HOST=${WAITFORIT_hostport[0]} 77 | WAITFORIT_PORT=${WAITFORIT_hostport[1]} 78 | shift 1 79 | ;; 80 | --child) 81 | WAITFORIT_CHILD=1 82 | shift 1 83 | ;; 84 | -q | --quiet) 85 | WAITFORIT_QUIET=1 86 | shift 1 87 | ;; 88 | -s | --strict) 89 | WAITFORIT_STRICT=1 90 | shift 1 91 | ;; 92 | -h) 93 | WAITFORIT_HOST="$2" 94 | if [[ $WAITFORIT_HOST == "" ]]; then break; fi 95 | shift 2 96 | ;; 97 | --host=*) 98 | WAITFORIT_HOST="${1#*=}" 99 | shift 1 100 | ;; 101 | -p) 102 | WAITFORIT_PORT="$2" 103 | if [[ $WAITFORIT_PORT == "" ]]; then break; fi 104 | shift 2 105 | ;; 106 | --port=*) 107 | WAITFORIT_PORT="${1#*=}" 108 | shift 1 109 | ;; 110 | -t) 111 | WAITFORIT_TIMEOUT="$2" 112 | if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi 113 | shift 2 114 | ;; 115 | --timeout=*) 116 | WAITFORIT_TIMEOUT="${1#*=}" 117 | shift 1 118 | ;; 119 | --) 120 | shift 121 | WAITFORIT_CLI=("$@") 122 | break 123 | ;; 124 | --help) 125 | usage 126 | ;; 127 | *) 128 | echoerr "Unknown argument: $1" 129 | usage 130 | ;; 131 | esac 132 | done 133 | 134 | if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then 135 | echoerr "Error: you need to provide a host and port to test." 136 | usage 137 | fi 138 | 139 | WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} 140 | WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} 141 | WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} 142 | WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} 143 | 144 | # check to see if timeout is from busybox? 145 | WAITFORIT_TIMEOUT_PATH=$(type -p timeout) 146 | WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) 147 | if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then 148 | WAITFORIT_ISBUSY=1 149 | WAITFORIT_BUSYTIMEFLAG="-t" 150 | 151 | else 152 | WAITFORIT_ISBUSY=0 153 | WAITFORIT_BUSYTIMEFLAG="" 154 | fi 155 | 156 | if [[ $WAITFORIT_CHILD -gt 0 ]]; then 157 | wait_for 158 | WAITFORIT_RESULT=$? 159 | exit $WAITFORIT_RESULT 160 | else 161 | if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then 162 | wait_for_wrapper 163 | WAITFORIT_RESULT=$? 164 | else 165 | wait_for 166 | WAITFORIT_RESULT=$? 167 | fi 168 | fi 169 | 170 | if [[ $WAITFORIT_CLI != "" ]]; then 171 | if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then 172 | echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" 173 | exit $WAITFORIT_RESULT 174 | fi 175 | exec "${WAITFORIT_CLI[@]}" 176 | else 177 | exit $WAITFORIT_RESULT 178 | fi -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | --------------------------------------------------------------------------------