static public void FailCopyOutToStream() { cs = new CountStream(); cs.FailAt = 2; try { cout = new NpgsqlCopyOut(new NpgsqlCommand("COPY copy1 TO STDOUT", conn), conn, cs); cout.Start(); } catch (Exception e) { if (("" + e).Contains("Test Exception handling")) { Console.Out.WriteLine("Copy to stream failed as requested."); return; } throw e; } finally { cs.Close(); cout.End(); // should silently discard rest of data } throw new Exception("Copy to stream did not fail as requested"); }
// Stream failure tests static public void FailCopyInFromStream() { cs = new CountStream(); cs.FailAt = 2; cs.WrapStream = new FileStream("test_copy.cs", FileMode.Open, FileAccess.Read); cin = new NpgsqlCopyIn(new NpgsqlCommand("COPY copy1 FROM STDIN DELIMITER '\b'", conn), conn, cs); try { cin.Start(); } catch (Exception e) { if (("" + e).Contains("Test Exception handling")) { Console.Out.WriteLine("Copy from stream failed as requested."); return; } else { Console.Out.WriteLine("Copy from stream failing failed: " + e); throw e; } } finally { cs.Close(); cin.End(); // should do nothing } Console.Out.WriteLine("Copy from stream did not fail as requested"); }
static public void CopyOutToStream() { cs = new CountStream(); // cs.WrapStream = new FileStream("test_copy.out", FileMode.Create, FileAccess.Write); cout = new NpgsqlCopyOut(new NpgsqlCommand("COPY copy1 TO STDOUT", conn), conn, cs); cout.Start(); if (cout.IsActive) { throw new Exception("Copy to stream did not complete in single pass"); } Console.Out.WriteLine("Lengths of text written to and read in single pass from database differ by " + (InLength - cs.BytesPassed)); Console.Out.WriteLine("Sums of characters written to and read in single pass from database differ by " + (InSum - cs.CheckSum)); }
static public void FailCopyInByWriting() { cs = new CountStream(); cs.FailAt = 2; cs.WrapStream = new FileStream("test_copy.cs", FileMode.Open, FileAccess.Read); cin = new NpgsqlCopyIn("COPY copy1 FROM STDIN", conn); cin.Start(); if (!cin.IsActive) { throw new Exception("Copy started inactive"); } byte[] buf = new byte[8]; int i; try { while ((i = cs.Read(buf, 0, buf.Length)) > 0) { cin.CopyStream.Write(buf, 0, i); } } catch (Exception e) { if (("" + e).Contains("Test Exception handling")) { try { cin.Cancel("Test whether copy in fails correctly"); } catch (Exception e2) { if (("" + e2).Contains("Test whether copy in fails correctly")) { Console.Out.WriteLine("Copy from writing failed as requested."); return; } throw e2; } throw new Exception("CopyIn.Cancel() didn't throw up the expected exception"); } throw e; } finally { cs.Close(); cin.End(); // should do nothing } throw new Exception("Copy from writing did not fail as requested"); }
// Stream success tests public static void CopyInFromStream() { cs = new CountStream(); cs.WrapStream = new FileStream("test_copy.cs", FileMode.Open, FileAccess.Read); cin = new NpgsqlCopyIn( new NpgsqlCommand("COPY copy1 FROM STDIN DELIMITER '\b'", conn), conn, cs ); cin.Start(); if(cin.IsActive) { throw new Exception("Copy from stream did not complete in single pass"); } InLength += cs.BytesPassed; InSum += cs.CheckSum; cs.Close(); Console.Out.WriteLine("Copy from stream ok"); }
// Stream success tests static public void CopyInFromStream() { cs = new CountStream(); cs.WrapStream = new FileStream("test_copy.cs", FileMode.Open, FileAccess.Read); cin = new NpgsqlCopyIn(new NpgsqlCommand("COPY copy1 FROM STDIN DELIMITER '\b'", conn), conn, cs); cin.Start(); if (cin.IsActive) { throw new Exception("Copy from stream did not complete in single pass"); } InLength += cs.BytesPassed; InSum += cs.CheckSum; cs.Close(); Console.Out.WriteLine("Copy from stream ok"); }
static public void CopyOutByReading() { cs = new CountStream(); cout = new NpgsqlCopyOut("COPY copy1 TO STDOUT", conn); cout.Start(); if (!cout.IsActive) { throw new Exception("Copy reading started inactive"); } byte[] buf = new byte[9]; int i; while ((i = cout.CopyStream.Read(buf, 0, buf.Length)) > 0) { cs.Write(buf, 0, i); } cs.Close(); cout.End(); Console.Out.WriteLine("Lengths of text written to and read via stream from database differ by " + (InLength - cs.BytesPassed)); Console.Out.WriteLine("Sums of characters written to and read via stream from database differ by " + (InSum - cs.CheckSum)); }
static long InSum = 0; // this really is a SUM of the bytes! comparison depends on that. #endregion Fields #region Methods public static void CopyInByWriting() { cs = new CountStream(); cs.WrapStream = new FileStream("test_copy.cs", FileMode.Open, FileAccess.Read); cin = new NpgsqlCopyIn( "COPY copy1 FROM STDIN DELIMITER '\b'", conn ); cin.Start(); if(! cin.IsActive) { throw new Exception("Copy started inactive"); } byte[] buf = new byte[8]; int i; while( (i = cs.Read(buf,0,buf.Length)) > 0 ) { cin.CopyStream.Write(buf, 0, i); } cin.End(); InLength += cs.BytesPassed; InSum += cs.CheckSum; cs.Close(); Console.Out.WriteLine("Copy from writing ok"); }
static public void CopyInByWriting() { cs = new CountStream(); cs.WrapStream = new FileStream("test_copy.cs", FileMode.Open, FileAccess.Read); cin = new NpgsqlCopyIn("COPY copy1 FROM STDIN DELIMITER '\b'", conn); cin.Start(); if (!cin.IsActive) { throw new Exception("Copy started inactive"); } byte[] buf = new byte[8]; int i; while ((i = cs.Read(buf, 0, buf.Length)) > 0) { cin.CopyStream.Write(buf, 0, i); } cin.End(); InLength += cs.BytesPassed; InSum += cs.CheckSum; cs.Close(); Console.Out.WriteLine("Copy from writing ok"); }
static public void FailCopyOutByReading() { cs = new CountStream(); cs.FailAt = 2; cout = new NpgsqlCopyOut("COPY copy1 TO STDOUT", conn); cout.Start(); if (!cout.IsActive) { throw new Exception("Copy reading started inactive"); } byte[] buf = new byte[9]; int i; try { while ((i = cout.CopyStream.Read(buf, 0, buf.Length)) > 0) { cs.Write(buf, 0, i); } } catch (Exception e) { if (("" + e).Contains("Test Exception handling")) { Console.Out.WriteLine("Copy to reading failed as requested."); return; } throw e; } finally { cs.Close(); cout.End(); } throw new Exception("Copy reading did not fail as requested"); }
public static void FailCopyOutToStream() { cs = new CountStream(); cs.FailAt = 2; try { cout = new NpgsqlCopyOut( new NpgsqlCommand("COPY copy1 TO STDOUT", conn), conn, cs ); cout.Start(); } catch(Exception e) { if( (""+e).Contains("Test Exception handling") ) { Console.Out.WriteLine("Copy to stream failed as requested."); return; } throw e; } finally { cs.Close(); cout.End(); // should silently discard rest of data } throw new Exception("Copy to stream did not fail as requested"); }
public static void FailCopyOutByReading() { cs = new CountStream(); cs.FailAt = 2; cout = new NpgsqlCopyOut( "COPY copy1 TO STDOUT", conn ); cout.Start(); if(! cout.IsActive) { throw new Exception("Copy reading started inactive"); } byte[] buf = new byte[9]; int i; try { while( (i = cout.CopyStream.Read(buf, 0, buf.Length)) > 0 ) { cs.Write(buf, 0, i); } } catch(Exception e) { if( (""+e).Contains("Test Exception handling") ) { Console.Out.WriteLine("Copy to reading failed as requested."); return; } throw e; } finally { cs.Close(); cout.End(); } throw new Exception("Copy reading did not fail as requested"); }
// Stream failure tests public static void FailCopyInFromStream() { cs = new CountStream(); cs.FailAt = 2; cs.WrapStream = new FileStream("test_copy.cs", FileMode.Open, FileAccess.Read); cin = new NpgsqlCopyIn( new NpgsqlCommand("COPY copy1 FROM STDIN DELIMITER '\b'", conn), conn, cs ); try { cin.Start(); } catch(Exception e) { if( (""+e).Contains("Test Exception handling") ) { Console.Out.WriteLine("Copy from stream failed as requested."); return; } else { Console.Out.WriteLine("Copy from stream failing failed: " + e); throw e; } } finally { cs.Close(); cin.End(); // should do nothing } Console.Out.WriteLine("Copy from stream did not fail as requested"); }
public static void FailCopyInByWriting() { cs = new CountStream(); cs.FailAt = 2; cs.WrapStream = new FileStream("test_copy.cs", FileMode.Open, FileAccess.Read); cin = new NpgsqlCopyIn( "COPY copy1 FROM STDIN", conn ); cin.Start(); if(! cin.IsActive) { throw new Exception("Copy started inactive"); } byte[] buf = new byte[8]; int i; try { while( (i = cs.Read(buf,0,buf.Length)) > 0 ) { cin.CopyStream.Write(buf, 0, i); } } catch(Exception e) { if( (""+e).Contains("Test Exception handling") ) { try { cin.Cancel("Test whether copy in fails correctly"); } catch(Exception e2) { if( (""+e2).Contains("Test whether copy in fails correctly") ) { Console.Out.WriteLine("Copy from writing failed as requested."); return; } throw e2; } throw new Exception("CopyIn.Cancel() didn't throw up the expected exception"); } throw e; } finally { cs.Close(); cin.End(); // should do nothing } throw new Exception("Copy from writing did not fail as requested"); }
public static void CopyOutToStream() { cs = new CountStream(); // cs.WrapStream = new FileStream("test_copy.out", FileMode.Create, FileAccess.Write); cout = new NpgsqlCopyOut( new NpgsqlCommand("COPY copy1 TO STDOUT", conn), conn, cs ); cout.Start(); if(cout.IsActive) { throw new Exception("Copy to stream did not complete in single pass"); } Console.Out.WriteLine("Lengths of text written to and read in single pass from database differ by " + (InLength-cs.BytesPassed)); Console.Out.WriteLine("Sums of characters written to and read in single pass from database differ by " + (InSum-cs.CheckSum)); }
public static void CopyOutByReading() { cs = new CountStream(); cout = new NpgsqlCopyOut( "COPY copy1 TO STDOUT", conn ); cout.Start(); if(! cout.IsActive) { throw new Exception("Copy reading started inactive"); } byte[] buf = new byte[9]; int i; while( (i = cout.CopyStream.Read(buf, 0, buf.Length)) > 0 ) { cs.Write(buf, 0, i); } cs.Close(); cout.End(); Console.Out.WriteLine("Lengths of text written to and read via stream from database differ by " + (InLength-cs.BytesPassed)); Console.Out.WriteLine("Sums of characters written to and read via stream from database differ by " + (InSum-cs.CheckSum)); }