Download - Robust Software
Robust Software – Robust Software – Dotting the I’s and Dotting the I’s and
Crossing the T’sCrossing the T’sChris OldwoodChris Oldwood
ACCU Conference 2013ACCU Conference 2013
@chrisoldwood / @chrisoldwood / [email protected]@cix.co.uk
The I’s & T’sThe I’s & T’s
RobustnessRobustness Handling ErrorsHandling Errors Safely Ignoring ErrorsSafely Ignoring Errors TimeoutsTimeouts Unit Testing FailuresUnit Testing Failures Flexible ConfigurationFlexible Configuration Monitoring ClarityMonitoring Clarity
RobustnessRobustness
Stable in the face of Stable in the face of unexpected unexpected behaviourbehaviour
Pop Quiz – Exit Code?Pop Quiz – Exit Code?
int main(int argc, char* argv[]){ throw UnhandledException();}
Exit Code ConventionExit Code Convention
program.exe
if %errorlevel% neq 0 ( echo ERROR: Program failed exit /b 1)
Big Outer Try BlockBig Outer Try Blockint main(int argc, char* argv[]){ try { return DoUsefulWork(argc, argv); } catch (const std::exception& e) { /* Report failure */ } catch (…) { /* Report failure */ }
return EXIT_FAILURE;}
Module BoundariesModule BoundariesHRESULT DoSomething(...){ try { return Impl::DoSomething(...); } catch (const std::bad_alloc& e) { return E_OUTOFMEMORY; } catch (const std::exception& e) { return E_FAIL; } catch (...) { return E_UNEXPECTED; }}
Exception Safety Exception Safety GuaranteesGuarantees
NoneNone BasicBasic StrongStrong No ThrowNo Throw
Exception Unsafe CodeException Unsafe CodeIServicePtr AcquireService(){ if (!m_service) { m_service = new Service(); m_service.CreateInstance(); }
return m_service;}
IServicePtr m_service;
Exception Safe CodeException Safe CodeIServicePtr AcquireService(){ if (!m_service) { ServicePtr service = new Service(); service.CreateInstance();
m_service.swap(service); }
return m_service;}
IServicePtr m_service;
Forever is a Really Long Forever is a Really Long TimeTime
Handle completed = BeginAsyncOperation();. . .Wait(completed, INFINITE);
Cancellable OperationsCancellable Operations
Handle completed = BeginAsyncOperation();Handle aborted = GetAbortHandle();Handle waitables[] = { aborted, completed };. . .Handle signalled = Wait(waitables, timeout);
if (signalled == aborted){
Retries: immediate then Retries: immediate then queuedqueued
Unit Testing FailuresUnit Testing Failures
Testing Write+Rename Testing Write+Rename IdiomIdiom
[Test]public Void OriginalFilePreservedOnException(){ var fakeIo = new FakeIo();
fakeIo.Write = (file, buffer) => { throw new IoException(); }
var writer = new WriterService(fakeIo); var filename = “original.txt”;
Assert.Throws(() => writer.WriteFile(filename)); Assert.True(fakeIo.FileExists(filename)); Assert.That(. . .);}
Flexible ConfigurationFlexible Configuration
Monitoring ClarityMonitoring Clarity
Release It!Release It!
Questions?Questions?
Blog:Blog:http://chrisoldwood.blogspot.comhttp://chrisoldwood.blogspot.com
@chrisoldwood / @chrisoldwood / [email protected]@cix.co.uk