Download - Robust Software

Transcript
Page 1: 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

Page 2: Robust Software

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

Page 3: Robust Software

RobustnessRobustness

Page 4: Robust Software

Stable in the face of Stable in the face of unexpected unexpected behaviourbehaviour

Page 5: Robust Software

Pop Quiz – Exit Code?Pop Quiz – Exit Code?

int main(int argc, char* argv[]){ throw UnhandledException();}

Page 6: Robust Software

Exit Code ConventionExit Code Convention

program.exe

if %errorlevel% neq 0 ( echo ERROR: Program failed exit /b 1)

Page 7: Robust Software

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

Page 8: Robust Software

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

Page 9: Robust Software

Exception Safety Exception Safety GuaranteesGuarantees

NoneNone BasicBasic StrongStrong No ThrowNo Throw

Page 10: Robust Software

Exception Unsafe CodeException Unsafe CodeIServicePtr AcquireService(){ if (!m_service) { m_service = new Service(); m_service.CreateInstance(); }

return m_service;}

IServicePtr m_service;

Page 11: Robust Software

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;

Page 12: Robust Software

Forever is a Really Long Forever is a Really Long TimeTime

Handle completed = BeginAsyncOperation();. . .Wait(completed, INFINITE);

Page 13: Robust Software

Cancellable OperationsCancellable Operations

Handle completed = BeginAsyncOperation();Handle aborted = GetAbortHandle();Handle waitables[] = { aborted, completed };. . .Handle signalled = Wait(waitables, timeout);

if (signalled == aborted){

Page 14: Robust Software

Retries: immediate then Retries: immediate then queuedqueued

Page 15: Robust Software

Unit Testing FailuresUnit Testing Failures

Page 16: Robust Software

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(. . .);}

Page 17: Robust Software

Flexible ConfigurationFlexible Configuration

Page 18: Robust Software

Monitoring ClarityMonitoring Clarity

Page 19: Robust Software

Release It!Release It!

Page 20: Robust Software

Questions?Questions?

Page 21: Robust Software

Blog:Blog:http://chrisoldwood.blogspot.comhttp://chrisoldwood.blogspot.com

@chrisoldwood / @chrisoldwood / [email protected]@cix.co.uk