public void Args_target_null() { // ARRANGE SampleDisposable arg = null; // ACT DisposableUtil.DisposeSuppressingErrors(arg); // ASSERT // no ArgumentNullException expected }
public void General() { // ARRANGE SampleDisposable arg = new SampleDisposable(); // ACT DisposableUtil.DisposeSuppressingErrors(arg); // ASSERT Assert.Equal(1, arg.DisposedCount); }
public void Error() { // ARRANGE SampleDisposable arg = new SampleDisposable(throwException: true); TestLogMonitor logMonitor = new TestLogMonitor(); // ACT logMonitor.StartLogging(); try { DisposableUtil.DisposeSuppressingErrors(arg); // no exception expected } finally { logMonitor.StopLogging(); } // ASSERT Assert.Equal(1, arg.DisposedCount); LogEntry expectedEntry = new LogEntry(null, TraceEventType.Error, $"Fail to dispose the object at 'DisposeSuppressingErrors.Error()': {arg.DisposeErrorMessage}"); logMonitor.AssertContains(expectedEntry); }
public void SkipChunkedBody() { // state checks if (this.bodyStream != null) { throw new InvalidOperationException("It has already read body part."); } // skip the body storing its bytes Stream output = Util.CreateTempFileStream(); try { StoreChunkedBody(output); } catch { this.bodyLength = 0; DisposableUtil.DisposeSuppressingErrors(output); throw; } // set the stream as the bodyStream this.bodyStream = output; return; }
public void SkipBody(long contentLength) { // argument checks if (contentLength < 0) { throw new ArgumentOutOfRangeException(nameof(contentLength)); } // state checks HeaderBuffer headerBuffer = this.headerBuffer; if (headerBuffer == null) { throw new InvalidOperationException(); } if (this.bodyLength != 0) { throw new InvalidOperationException("This buffer has already handled a message body."); } Debug.Assert(this.bodyStream == null); // skip the body storing its bytes // The media to store body depends on its length and the current margin. Stream bodyStream = null; try { // Note that the some body bytes may be read into the header buffer. // Unread bytes in the header buffer at this point are body bytes. // That is, range [headerBuffer.Next - headerBuffer.Limit). int bodyBytesInHeaderBufferLength = headerBuffer.Limit - headerBuffer.Next; long restLen = contentLength - bodyBytesInHeaderBufferLength; if (restLen <= headerBuffer.Margin) { // The body is to be stored in the rest of header buffer. (tiny body) // read body bytes into the rest of the header buffer if (0 <= restLen) { Debug.Assert(restLen <= int.MaxValue); FillBuffer(headerBuffer, (int)restLen); Debug.Assert(contentLength == headerBuffer.Limit - headerBuffer.Next); Debug.Assert(this.MemoryBlock == null); } else { // there is data of the next message Debug.Assert(contentLength <= int.MaxValue); int offset = headerBuffer.Next + (int)contentLength; headerBuffer.SetPrefetchedBytes(offset); } } else { byte[] memoryBlock = EnsureMemoryBlockAllocated(); if (contentLength <= memoryBlock.Length) { // The body is to be stored in a memory block. (small body) // copy body bytes in the header buffer to this buffer CopyFrom(headerBuffer, headerBuffer.Next, bodyBytesInHeaderBufferLength); // read rest of body bytes Debug.Assert(restLen <= int.MaxValue); FillBuffer((int)restLen); Debug.Assert(contentLength == (this.Limit - this.Next)); } else { // The body is to be stored in a stream. // determine which medium is used to store body, memory or file if (contentLength <= BodyStreamThreshold) { // use memory stream (medium body) Debug.Assert(contentLength <= int.MaxValue); bodyStream = new MemoryStream((int)contentLength); } else { // use temp file stream (large body) bodyStream = Util.CreateTempFileStream(); } StoreBody(bodyStream, contentLength); } } // update state this.bodyLength = contentLength; this.bodyStream = bodyStream; } catch { DisposableUtil.DisposeSuppressingErrors(bodyStream); throw; } return; }
public bool Stop(bool systemSessionEnding, int millisecondsTimeout = 0) { // restore the system settings SystemSettingsSwitcher switcher = this.switcher; this.switcher = null; SystemSettings backup = this.backup; this.backup = null; if (backup != null) { Debug.Assert(switcher != null); try { switcher.Restore(backup, systemSessionEnding); this.Owner.DeleteSystemSettingsBackup(); } catch (Exception exception) { this.Owner.ShowRestoreSystemSettingsErrorMessage(exception.Message); // continue } } // stop and dispose the proxy Proxy proxy = this.proxy; this.proxy = null; bool stopConfirmed = false; if (proxy == null) { stopConfirmed = true; } else { try { stopConfirmed = proxy.Stop(millisecondsTimeout); } finally { DisposableUtil.DisposeSuppressingErrors(proxy); } } // update credentials if necessary IEnumerable <CredentialSettings> credentials = null; lock (this.credentialsLocker) { if (this.isCredentialsDirty) { this.isCredentialsDirty = false; credentials = this.dictionary.Select(pair => CloneSettings(pair.Value)).ToArray(); } this.dictionary = null; } if (credentials != null) { if (this.commandSettings != null) { this.commandSettings.Credentials = credentials; } if (this.saveCredentials) { Action saveTask = () => { try { this.Owner.UpdateSettingsFile((s) => { s.Credentials = credentials; }, null); } catch (Exception exception) { string message = string.Format(Resources.CommandBase_Message_FailToSaveCredentials, exception.Message); this.Owner.ShowErrorMessage(message); } }; // launch save task Task.Run(saveTask); } } this.saveCredentials = false; // checks Debug.Assert(this.proxy == null); Debug.Assert(this.switcher == null); Debug.Assert(this.backup == null); Debug.Assert(this.saveCredentials == false); return(stopConfirmed); }
protected override void DiscardInstance(Request instance) { DisposableUtil.DisposeSuppressingErrors(instance); }