Skip to content

Commit

Permalink
Provide default connection string for testing
Browse files Browse the repository at this point in the history
This eliminates the need to set a repository secret for the GitHub action, which fails when the action is running as the result of a pull request from a forked repo.

Also updated the start-db.sh script to use pg_ready to wait for postgres to become ready instead of sleeping for a predefined duration.
  • Loading branch information
brianpursley committed Jan 26, 2025
1 parent 49804ad commit f7f41e5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Npgmq.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using Npgmq;
using Npgsql;

const string defaultConnectionString = "Host=localhost;Username=postgres;Database=npgmq_test;";

var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddUserSecrets(Assembly.GetExecutingAssembly())
.Build();

var connectionString = configuration.GetConnectionString("ExampleDB")!;
var connectionString = configuration.GetConnectionString("ExampleDB") ?? defaultConnectionString;

// Test Npgmq with connection string
{
Expand Down
4 changes: 3 additions & 1 deletion Npgmq.Test/NpgmqClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Npgmq.Test;

public sealed class NpgmqClientTest : IDisposable
{
private const string DefaultConnectionString = "Host=localhost;Username=postgres;Database=npgmq_test;";

private static readonly string TestQueueName = $"test_{Guid.NewGuid():N}";

private readonly string _connectionString;
Expand All @@ -28,7 +30,7 @@ public NpgmqClientTest()
.AddUserSecrets<NpgmqClientTest>()
.Build();

_connectionString = configuration.GetConnectionString("Test")!;
_connectionString = configuration.GetConnectionString("Test") ?? DefaultConnectionString;
_connection = new NpgsqlConnection(_connectionString);
_sut = new NpgmqClient(_connection);
}
Expand Down
5 changes: 4 additions & 1 deletion Npgmq.Test/scripts/start-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ set -e
PGMQ_VERSION=$1

docker run -d --name npgmq_test_db -p 5432:5432 --rm quay.io/tembo/tembo-local
sleep 4

until docker exec npgmq_test_db /bin/sh -c "pg_isready"; do
sleep 1
done

docker exec npgmq_test_db /bin/sh -c "psql -c \"CREATE DATABASE npgmq_test;\""

Expand Down

0 comments on commit f7f41e5

Please sign in to comment.