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"); }
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 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"); }