public async Task TimeoutSwitchConnection() { using (var conn = new NpgsqlConnection(ConnectionString)) { if (conn.CommandTimeout >= 100 && conn.CommandTimeout < 105) { TestUtil.IgnoreExceptOnBuildServer("Bad default command timeout"); } } using (var c1 = await OpenConnectionAsync(ConnectionString + ";CommandTimeout=100")) { using (var cmd = c1.CreateCommand()) { Assert.That(cmd.CommandTimeout, Is.EqualTo(100)); using (var c2 = new NpgsqlConnection(ConnectionString + ";CommandTimeout=101")) { cmd.Connection = c2; Assert.That(cmd.CommandTimeout, Is.EqualTo(101)); } cmd.CommandTimeout = 102; using (var c2 = new NpgsqlConnection(ConnectionString + ";CommandTimeout=101")) { cmd.Connection = c2; Assert.That(cmd.CommandTimeout, Is.EqualTo(102)); } } } }
public void ConnectTimeout() { var unknownIp = Environment.GetEnvironmentVariable("NPGSQL_UNKNOWN_IP"); if (unknownIp == null) { TestUtil.IgnoreExceptOnBuildServer("NPGSQL_UNKNOWN_IP isn't defined and is required for connection timeout tests"); } var csb = new NpgsqlConnectionStringBuilder(ConnectionString) { Host = unknownIp, Pooling = false, Timeout = 2 }; using (var conn = new NpgsqlConnection(csb.ToString())) { var sw = Stopwatch.StartNew(); Assert.That(() => conn.Open(), Throws.Exception.TypeOf <TimeoutException>()); Assert.That(sw.Elapsed.TotalMilliseconds, Is.GreaterThanOrEqualTo((csb.Timeout * 1000) - 100), $"Timeout was supposed to happen after {csb.Timeout} seconds, but fired after {sw.Elapsed.TotalSeconds}"); Assert.That(conn.State, Is.EqualTo(ConnectionState.Closed)); } }
protected virtual NpgsqlConnection OpenConnection(string?connectionString = null) { var conn = CreateConnection(connectionString); try { conn.Open(); } catch (PostgresException e) { if (e.SqlState == PostgresErrorCodes.InvalidCatalogName) { TestUtil.IgnoreExceptOnBuildServer("Please create a database npgsql_tests, owned by user npgsql_tests"); } else if (e.SqlState == PostgresErrorCodes.InvalidPassword && connectionString == DefaultConnectionString) { TestUtil.IgnoreExceptOnBuildServer("Please create a user npgsql_tests as follows: create user npgsql_tests with password 'npgsql_tests'"); } else { throw; } } return(conn); }
protected NpgsqlConnection OpenConnection(string connectionString = null) { if (connectionString == null) { connectionString = ConnectionString; } var conn = new NpgsqlConnection(connectionString); try { conn.Open(); } catch (PostgresException e) { if (e.SqlState == "3D000") { TestUtil.IgnoreExceptOnBuildServer("Please create a database npgsql_tests, owned by user npgsql_tests"); } else if (e.SqlState == "28P01") { TestUtil.IgnoreExceptOnBuildServer("Please create a user npgsql_tests as follows: create user npgsql_tests with password 'npgsql_tests'"); } else { throw; } } return(conn); }
async ValueTask <NpgsqlConnection> OpenConnection(string?connectionString, bool async) { var conn = CreateConnection(connectionString); try { if (async) { await conn.OpenAsync(); } else { conn.Open(); } } catch (PostgresException e) { if (e.SqlState == PostgresErrorCodes.InvalidCatalogName) { TestUtil.IgnoreExceptOnBuildServer("Please create a database npgsql_tests, owned by user npgsql_tests"); } else if (e.SqlState == PostgresErrorCodes.InvalidPassword && connectionString == TestUtil.DefaultConnectionString) { TestUtil.IgnoreExceptOnBuildServer("Please create a user npgsql_tests as follows: create user npgsql_tests with password 'npgsql_tests'"); } else { throw; } } return(conn); }
public void CheckSslSupport() { var sslSupport = (string)ExecuteScalar("SHOW ssl", Conn); if (sslSupport == "off") { TestUtil.IgnoreExceptOnBuildServer("SSL support isn't enabled at the backend"); } }
public void CheckSslSupport() { using var conn = OpenConnection(); var sslSupport = (string)conn.ExecuteScalar("SHOW ssl") !; if (sslSupport == "off") { TestUtil.IgnoreExceptOnBuildServer("SSL support isn't enabled at the backend"); } }
public void OneTimeSetUp() { using (new TransactionScope(TransactionScopeOption.RequiresNew)) { try { Transaction.Current !.EnlistPromotableSinglePhase(new FakePromotableSinglePhaseNotification()); } catch (NotImplementedException) { Assert.Ignore("Promotable single phase transactions aren't supported (mono < 3.0.0?)"); } } _controlConn = OpenConnection(); // Make sure prepared transactions are enabled in postgresql.conf (disabled by default) if (int.Parse((string)_controlConn.ExecuteScalar("SHOW max_prepared_transactions") !) == 0) { TestUtil.IgnoreExceptOnBuildServer("max_prepared_transactions is set to 0 in your postgresql.conf"); _controlConn.Close(); } // Rollback any lingering prepared transactions from failed previous runs var lingeringTrqnsqctions = new List <string>(); using (var cmd = new NpgsqlCommand("SELECT gid FROM pg_prepared_xacts WHERE database=@database", _controlConn)) { cmd.Parameters.AddWithValue("database", new NpgsqlConnectionStringBuilder(ConnectionString).Database !); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { lingeringTrqnsqctions.Add(reader.GetString(0)); } } } foreach (var xactGid in lingeringTrqnsqctions) { _controlConn.ExecuteNonQuery($"ROLLBACK PREPARED '{xactGid}'"); } // All tests in this fixture should have exclusive access to the database they're running on. // If we run these tests in parallel (i.e. two builds in parallel) they will interfere. // Solve this by taking a PostgreSQL advisory lock for the lifetime of the fixture. _controlConn.ExecuteNonQuery("SELECT pg_advisory_lock(666)"); _controlConn.ExecuteNonQuery("DROP TABLE IF EXISTS data"); _controlConn.ExecuteNonQuery("CREATE TABLE data (name TEXT)"); }
public void ConnectTimeoutAsync() { var unknownIp = Environment.GetEnvironmentVariable("NPGSQL_UNKNOWN_IP"); if (unknownIp == null) { TestUtil.IgnoreExceptOnBuildServer("NPGSQL_UNKNOWN_IP isn't defined and is required for connection timeout tests"); } var connString = new NpgsqlConnectionStringBuilder(ConnectionString) { Host = unknownIp, Pooling = false, Timeout = 2 }.ToString(); using (var conn = new NpgsqlConnection(connString)) { Assert.That(async() => await conn.OpenAsync(), Throws.Exception.TypeOf <TimeoutException>()); Assert.That(conn.State, Is.EqualTo(ConnectionState.Closed)); } }
public void UnixDomainSocket() { var port = new NpgsqlConnectionStringBuilder(ConnectionString).Port; var candidateDirectories = new[] { "/var/run/postgresql", "/tmp" }; var dir = candidateDirectories.FirstOrDefault(d => File.Exists(Path.Combine(d, $".s.PGSQL.{port}"))); if (dir == null) { TestUtil.IgnoreExceptOnBuildServer("No PostgreSQL unix domain socket was found"); return; } var csb = new NpgsqlConnectionStringBuilder(ConnectionString) { Host = dir, Username = null // Let Npgsql detect the username }; using (var conn = OpenConnection(csb)) { Assert.That(conn.ExecuteScalar("SELECT 1"), Is.EqualTo(1)); } }
protected virtual void SetUp() { Conn = new NpgsqlConnection(ConnectionString); try { Conn.Open(); } catch (NpgsqlException e) { if (e.Code == "3D000") { TestUtil.IgnoreExceptOnBuildServer("Please create a database npgsql_tests, owned by user npgsql_tests"); } else if (e.Code == "28P01") { TestUtil.IgnoreExceptOnBuildServer("Please create a user npgsql_tests as follows: create user npgsql_tests with password 'npgsql_tests'"); } else { throw; } } }
public void ConnectTimeoutCancel() { var unknownIp = Environment.GetEnvironmentVariable("NPGSQL_UNKNOWN_IP"); if (unknownIp == null) { TestUtil.IgnoreExceptOnBuildServer("NPGSQL_UNKNOWN_IP isn't defined and is required for connection cancellation tests"); } var connString = new NpgsqlConnectionStringBuilder(ConnectionString) { Host = unknownIp, Pooling = false, Timeout = 30 }.ToString(); using (var conn = new NpgsqlConnection(connString)) { var cts = new CancellationTokenSource(); cts.CancelAfter(1000); Assert.That(async() => await conn.OpenAsync(cts.Token), Throws.Exception.TypeOf <TaskCanceledException>()); Assert.That(conn.State, Is.EqualTo(ConnectionState.Closed)); } }