public void Persistent_stream_connection_exception() { var pStream = new PersistentStream(null, Connecter_throws_exception().ConnectorFunc, 0); //since the stream is null, reading will cause an exception and reconnect will be invoked Assert.NotEqual(0, pStream.Read(new byte[100], 0, 100)); }
public void ReadFlakyStream() { var pStream = new PersistentStream(GetWebRequest_flaky_stream(), 0); var buffer = new byte[4096]; Assert.Equal(100, pStream.Read(buffer, 0, 100)); }
/////////////////////////////////////////////////////////////////////// // SQL BLOB Section /////////////////////////////////////////////////////////////////////// #region SQL BLOB interactions /// <summary> /// Saves stream property to SQL BLOB field /// </summary> /// <param name="objID">Stream owner object ID</param> /// <param name="propName">Name of stream property</param> /// <param name="stream">Stream property to save</param> /// <param name="isnew">Flag to check property existance.</param> private void save_blob( int objID, string propName, PersistentStream stream, bool isnew ) { #region debug info #if (DEBUG) Debug.Print( "-> ODB.imageSave( {0}, '{1}' )", objID, propName ); #endif #endregion // open connection and start new transaction if required TransactionBegin(); // create command text to create new or update existing record in th table string sql = "DECLARE @_id as int; \n"; if( !isnew ) { sql += "DELETE FROM [dbo].[_properties] WHERE [ObjectID]={0} AND [Name]='{1}'; \n" + "UPDATE [dbo].[_images] SET @_id = [ID], [Value] = {2} \n" + "WHERE @@ROWCOUNT = 0 AND [ObjectID] = {0} AND [Name] ='{1}'; \n"; } sql += "IF @_id IS NULL BEGIN \n" + " INSERT INTO [dbo].[_images] ([ObjectID], [Name], [Value]) VALUES ( {0}, '{1}', {2} ); \n" + " SET @_id = SCOPE_IDENTITY(); \n" + "END; \n" + "SELECT @Pointer = TEXTPTR([Value]) FROM [dbo].[_images] WHERE [ID] = @_id; \n"; // command that executes previous sql statement DbCommand cmd = new SqlCommand(string.Format( sql, objID, propName, (stream.Length > 0) ? "0x0" : "NULL")); cmd.Connection = m_con; cmd.Transaction = m_trans; DbParameter pointerParam = new SqlParameter( "@Pointer", SqlDbType.Binary, 16 ); pointerParam.Direction = ParameterDirection.Output; cmd.Parameters.Add( pointerParam ); try { // get pointer to image data cmd.ExecuteNonQuery(); // set up UPDATETEXT command, parameters, and open BinaryReader. cmd = new SqlCommand( "UPDATETEXT [dbo].[_images].[Value] @Pointer @Offset @Delete WITH LOG @Bytes"); cmd.Connection = m_con; cmd.Transaction = m_trans; // assign value of pointer previously recieved cmd.Parameters.Add( new SqlParameter("@Pointer", SqlDbType.Binary, 16) ); cmd.Parameters["@Pointer"].Value = pointerParam.Value; // start insertion from begin DbParameter offsetParam = new SqlParameter( "@Offset", SqlDbType.Int ); offsetParam.Value = 0; cmd.Parameters.Add( offsetParam ); //delete 0x0 character DbParameter deleteParam = new SqlParameter("@Delete", SqlDbType.Int); deleteParam.Value = 1; cmd.Parameters.Add( deleteParam ); DbParameter bytesParam = new SqlParameter( "@Bytes", SqlDbType.Binary ); cmd.Parameters.Add( bytesParam ); // save current stream position and seek to begin long pos = stream.Position; stream.Seek( 0, SeekOrigin.Begin ); // read buffer full of data and execute UPDATETEXT statement. Byte[] buffer = new Byte[BUFFER_LENGTH]; // make first read from stream int ret = stream.Read( buffer, 0, BUFFER_LENGTH ); // while something is read from stream, write to apend to BLOB field while( ret > 0 ) { // initing parameters for write bytesParam.Value = buffer; bytesParam.Size = ret; // write to BLOB field cmd.ExecuteNonQuery(); // execute iteration deleteParam.Value = 0; // don't delete any other data // prepare to next iteration offsetParam.Value = Convert.ToInt32( offsetParam.Value ) + ret; // read from stream for next iteration ret = stream.Read( buffer, 0, BUFFER_LENGTH ); } // restore stream position after reading stream.Position = pos; } catch( Exception ex ) { #region debug info #if (DEBUG) Debug.Print( "[ERROR] @ ODB.imageSave: {0}", ex.ToString() ); #endif #endregion // rollback failed transaction TransactionRollback(); throw; } // close connection and commit transaction if required TransactionCommit(); #region debug info #if (DEBUG) Debug.Print( "<- ODB.imageSave( {0}, '{1}' )", objID, propName ); #endif #endregion }