geek talk backend unit testing in go language

Post on 19-Mar-2017

25 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

DefineDataUnit Testing in Go Language

UnitTestinginGoLanguageBy:HaluanMohammadIrsad

1.IntrotoGoLanguage

AllAboutGo

� CreatedatGoogle@2007byRobPike,RobGriesemer,andKenThompson

� OpenSource� Cbasedlanguage� Compiled� ConcurrentProgramming� Platform:Linux,OSX,Windows,BSD,andUnix

NotableUsers

� CloudFlare� Docker� Dropbox� Google(fordownloadserverdl.google.com)� SoundCloud� Uber (forhandlinghighvolumesofgeofence-basedqueries)

Source:https://en.wikipedia.org/wiki/Go_(programming_language)

2.GoTestingPhilosophy

Howtowrite?

� Alwaysimport"testing"package� Thetestfile,shouldendwith“_test”

� Ex:main_test.go

� Puttestfilesinseparatepackage� Ex:Ifyouwanttotestmainpackage,putmain_test.go inpackagemain_test

3.UnitTestingTools

Tools

a. Gory,createsignaturesforunittestingb. Gomega,amatcher(assertion)library

Whyweneedsignatures?

� RepresentDataStructure� Consistency� Re-usability

Gory

Gorykeywords:� Define :Toconstructafactory� Factory :Factorymapsfieldnamestovalues

Source:https://github.com/modocache/gory

Library

Implementation(1)

Implementation(2)

Whyweneedtestcases?

� Businessprocessvalidation� Dataflowvalidation� Onsuccessvalidation� Onfailurevalidation� Errorhandling

Gomega

Gomega keywords:� Describe&Context :(Expressively)Organizethebehaviorofcode� Beforeeach :Setupstateforspecs� AfterEach :Executeafterspecsexecuted� It :Specifysinglespec� Expect :Tomakeexpectationoftheresult

source:https://onsi.github.io/ginkgo/

Library

MainSuite

SubTask

Implementation(1)

Implementation(2)

Onfailureresult

OnSuccessresult

CodeCoverage

4.RaceCondition

RaceCondition

� How itcouldhappened?� How todetectit?

Isthisaracecondition?

DefinitionofRaceCondition

� A“racecondition”canbedefinedas“Anomalousbehaviorduetounexpectedcriticaldependenceontherelativetimingofevents”.

� Raceconditionsgenerallyinvolveoneormoreprocessesaccessingasharedresource(suchafileorvariable),wherethismultipleaccesshasnotbeenproperlycontrolled.

DetectRaceCondition

� Gobydefaulthascreate‘--race’todetectracecondition,simplyadditafter‘gotest’.Soitwillbe‘gotest--race’

� Isthis‘really’work?

Codethatpossiblyraisearacecondition

Beforeadd“RunnerRun” Afteradd“RunnerRun”

Rungotest

It’sokay,andseemsnoproblem

run“gotest--race”

Finally,Iwatcharacehere:))

WhatIactuallysee

Thereality

Before After

Solution

Runagain..

ChangeBackAgainandRunTest

FinFin.

top related