public void PqsqlConnectionTest4() { PqsqlConnection connection = new PqsqlConnection(connectionString); connection.Open(); Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state"); IntPtr conn_attempt1 = connection.PGConnection; int sock1 = PqsqlWrapper.PQsocket(conn_attempt1); Assert.AreNotEqual(-1, sock1, "wrong socket"); // close the underlying socket without letting Pqsql and libpq know int closed = CloseSocket(sock1); Assert.AreEqual(0, closed, "closesocket failed"); connection.Close(); Assert.AreEqual(ConnectionState.Closed, connection.State, "wrong connection state"); connection.Open(); IntPtr conn_attempt2 = connection.PGConnection; ConnStatusType status = PqsqlWrapper.PQstatus(conn_attempt2); Assert.AreEqual(ConnStatusType.CONNECTION_OK, status, "connection broken"); Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state"); int sock2 = PqsqlWrapper.PQsocket(conn_attempt2); // close the underlying socket without letting Pqsql and libpq know closed = CloseSocket(sock2); Assert.AreEqual(0, closed, "closesocket failed"); }
public void PqsqlConnectionTest2() { PqsqlConnection connection = new PqsqlConnection(connectionString); Assert.AreEqual(ConnectionState.Closed, connection.State, "wrong connection state"); Assert.AreEqual(connectionString.Replace(" ", "").Replace("\t", "").Replace("\n", ""), connection.ConnectionString, "wrong connection string"); Assert.AreEqual(3, connection.ConnectionTimeout, "wrong connection timeout"); // closed connection with service file should give us empty database name string dbname = connection.Database; Assert.AreEqual(string.Empty, dbname, "wrong connection database"); string serverVersion = connection.ServerVersion; Assert.IsFalse(string.IsNullOrEmpty(serverVersion)); Assert.AreEqual("-1", serverVersion); connection.Open(); Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state"); dbname = connection.Database; Assert.AreNotEqual(string.Empty, dbname, "wrong connection database"); serverVersion = connection.ServerVersion; Assert.IsFalse(string.IsNullOrEmpty(serverVersion)); if (serverVersion.Length >= 6) // expect 100000 or later { int version = Convert.ToInt32(serverVersion); Assert.IsTrue(version >= 100000); } else if (serverVersion.Length >= 5) // expect 90400 .. 90699 { int version = Convert.ToInt32(serverVersion); Assert.IsTrue(version >= 90400); Assert.IsTrue(version <= 90699); } else { Assert.Fail("wrong server version"); } connection.Close(); dbname = connection.Database; Assert.AreEqual(string.Empty, dbname, "wrong connection database"); serverVersion = connection.ServerVersion; Assert.IsFalse(string.IsNullOrEmpty(serverVersion)); Assert.AreEqual("-1", serverVersion); Assert.AreEqual(ConnectionState.Closed, connection.State, "wrong connection state"); }
public void PqsqlConnectionTest5() { PqsqlConnection connection = new PqsqlConnection(connectionString); connection.Open(); Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state"); PqsqlTransaction trans = connection.BeginTransaction(); Assert.AreEqual(IsolationLevel.ReadCommitted, trans.IsolationLevel, "wrong transaction isolation level"); trans.Rollback(); connection.Close(); Assert.AreEqual(ConnectionState.Closed, connection.State, "wrong connection state"); }
public void PqsqlCommandTest5() { PqsqlCommand cmd = mConnection.CreateCommand(); cmd.CommandText = "pg_authid"; cmd.CommandTimeout = 10; cmd.CommandType = CommandType.TableDirect; var r = cmd.ExecuteReader(); foreach (var o in r) { Assert.IsNotNull(o); } cmd.Cancel(); mConnection.Close(); mConnection.Open(); }
public void PqsqlConnectionTest7() { PqsqlConnection connection = new PqsqlConnection(connectionString); bool opened = false; bool closed = true; connection.StateChange += (sender, args) => { if (args.CurrentState == ConnectionState.Closed) { opened = false; closed = true; } if (args.CurrentState == ConnectionState.Open) { opened = true; closed = false; } }; connection.Open(); Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state"); Assert.AreEqual(true, opened); Assert.AreEqual(false, closed); try { PqsqlCommand cmd = connection.CreateCommand(); cmd.CommandText = "select pg_terminate_backend(pg_backend_pid()); select pg_sleep(5);"; cmd.ExecuteNonQuery(); // must execute both statements } catch (Exception) { // ignored connection.Close(); } Assert.AreEqual(false, opened); Assert.AreEqual(true, closed); }
public void PqsqlConnectionStringBuilderTest3() { PqsqlConnectionStringBuilder builder = new PqsqlConnectionStringBuilder(string.Empty); Assert.AreEqual(string.Empty, builder.ConnectionString); Assert.AreEqual(0, builder.Count); using (PqsqlConnection connection = new PqsqlConnection(builder)) { try { connection.Open(); } catch (PqsqlException) { // ignored, depends on server config whether empty connection string is valid } connection.Close(); } }
public void PqsqlConnectionTest3() { PqsqlConnectionPool.Clear(); PqsqlConnection connection = new PqsqlConnection(connectionString); connection.Open(); Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state"); IntPtr conn_attempt1 = connection.PGConnection; connection.Close(); Assert.AreEqual(ConnectionState.Closed, connection.State, "wrong connection state"); // in order to simulate broken connections in the connection pool, we can step through this unit test and turn // off postgresql before we open the connection again in the next step below: connection.Open(); Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state"); IntPtr conn_attempt2 = connection.PGConnection; Assert.AreEqual(conn_attempt1, conn_attempt2, "connection was not received from internal connection pool"); }
public void PqsqlConnectionTest10() { PqsqlConnectionPool.Clear(); PqsqlConnection connection = new PqsqlConnection(connectionString); connection.Open(); Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state"); IntPtr pgConn = connection.PGConnection; int client_encoding = PqsqlWrapper.PQclientEncoding(pgConn); Assert.AreEqual((int)PgEnc.PG_UTF8, client_encoding, "wrong client_encoding"); // setup client_encoding to sqlascii, and leave it like that byte[] encoding = PqsqlUTF8Statement.CreateUTF8Statement("sqlascii"); client_encoding = PqsqlWrapper.PQsetClientEncoding(pgConn, encoding); Assert.AreEqual(0, client_encoding, "could not set client_encoding"); client_encoding = PqsqlWrapper.PQclientEncoding(pgConn); Assert.AreEqual(0, client_encoding, "wrong client_encoding"); connection.Close(); // now try to get the same connection again connection = new PqsqlConnection(connectionString); connection.Open(); Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state"); pgConn = connection.PGConnection; client_encoding = PqsqlWrapper.PQclientEncoding(pgConn); Assert.AreEqual((int)PgEnc.PG_UTF8, client_encoding, "wrong client_encoding"); }