/* Ċ Ġ* ĠCopyright Ġ( c ) Ġ 2 0 1 6 . ĠK EST I Ġco , Ġl td Ċ Ġ* ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ Ġ* Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ Ġ* ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* ĠĠĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ Ġ* Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ Ġ* ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ Ġ* ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ Ġ* Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ġ*/ Ċ Ċ package Ġdeb op 4 k . core . retry Ċ Ċ import Ġdeb op 4 k . core . async s . ready Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . assertj . core . api . Assertions . fail Because Exception Was Not Thrown Ċ import Ġorg . junit . Test Ċ import Ġorg . mockito . BDD Mockito . given Ċ import Ġorg . mockito . BDD Mockito . verify Ċ import Ġjava . util . concurrent .* Ċ import Ġjava . util . concurrent . atomic .* Ċ Ċ /** Ċ Ġ* ĠAsync Retry Executor One Failure Kotlin Test Ċ Ġ* Ġ@ author Ġdeb op Ġsun gh you k . bae @ gmail . com Ċ Ġ*/ Ċ class ĠAsync Retry Executor One Failure Kotlin Test Ġ: ĠAbstract Retry Kotlin Test () Ġ{ ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġshould Not Retry If Abort Thrown () Ġ{ ĊĠĠĠ Ġ// given ĊĠĠĠ Ġval Ġexecutor Ġ= ĠAsync Retry Executor ( scheduler Mock ) ĊĠĠĠ Ġgiven < String >( service Mock . some times Fails ()). will Throw ( Abort Retry Exception :: class . java ) ĊĊĠĠĠ Ġ// when ĊĠĠĠ Ġexecutor . get With Retry Ġ{ Ġservice Mock . some times Fails () Ġ} ĊĊĠĠĠ Ġ// then ĊĠĠĠ Ġverify < Fault y Service >( service Mock ). some times Fails () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġshould Re throw Abort Exception If First Iteration Thrown It () Ġ{ ĊĠĠĠ Ġ// given ĊĠĠĠ Ġval Ġexecutor Ġ= ĠAsync Retry Executor ( scheduler Mock ). with Max Retry ( 3 ) ĊĠĠĠ Ġgiven < String >( service Mock . some times Fails ()) ĊĠĠĠĠĠĠĠ Ġ. will Throw ( RuntimeException :: class . java ) Ċ // ĠĠĠĠĠĠĠ Ġ. will Throw ( Abort Retry Exception :: class . java ) ĊĊĠĠĠ Ġ// when ĊĠĠĠ Ġval Ġpromise Ġ= Ġexecutor . get With Retry Ġ{ Ġservice Mock . some times Fails () Ġ} ĊĊĠĠĠ Ġpromise . success Ġ{ ĊĠĠĠĠĠ Ġprintln (" Success ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// then Ċ // ĠĠĠ ĠassertThat ( promise . is Failure ()). isTrue () Ċ // ĠĠĠ Ġtry Ġ{ Ċ // ĠĠĠĠĠ Ġpromise . get () Ċ // ĠĠĠĠĠ Ġfail Because Exception Was Not Thrown ( ExecutionException :: class . java ) Ċ // ĠĠĠ Ġ} Ġcatch Ġ( e : ĠExecution Exception ) Ġ{ Ċ // ĠĠĠĠĠ ĠassertThat ( e . cause ). isInstanceOf ( Abort Retry Exception :: class . java ) Ċ // ĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġshould Complete With Exception If First Iteration Thrown It () Ġ{ ĊĠĠĠ Ġ// given ĊĠĠĠ Ġval Ġexecutor Ġ= ĠAsync Retry Executor ( scheduler Mock ). dont Retry () ĊĠĠĠ Ġgiven < String >( service Mock . some times Fails ()). will Throw ( IllegalStateException ( D ON _ T _ PAN IC )) ĊĊĠĠĠ Ġ// when ĊĠĠĠ Ġval Ġpromise Ġ= Ġexecutor . get With Retry Ġ{ Ġservice Mock . some times Fails () Ġ} ĊĊĠĠĠ Ġ// then ĊĠĠĠ Ġval Ġerror Ġ= ĠAtomic Reference < Throwable >() ĊĠĠĠ Ġpromise . fail Ġ{ Ġexception Ġ-> Ġerror . set ( exception ) Ġ} ĊĠĠĠ Ġpromise . ready () ĊĠĠĠ ĠassertThat ( error . get ()) ĊĠĠĠĠĠĠĠ Ġ. isNotNull () ĊĠĠĠĠĠĠĠ Ġ. isInstanceOf ( IllegalStateException :: class . java ) ĊĠĠĠĠĠĠĠ Ġ. has Message ( D ON _ T _ PAN IC ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġshould Re throw Last Thrown Exception When Abort ed In Sub sequent Iteration () Ġ{ ĊĠĠĠ Ġ// given ĊĠĠĠ Ġval Ġexecutor Ġ= ĠAsync Retry Executor ( scheduler Mock ) ĊĠĠĠ Ġgiven < String >( service Mock . some times Fails ()). will Throw ( ĊĠĠĠĠĠĠĠ ĠIllegalArgumentException (" First "), ĊĠĠĠĠĠĠĠ ĠIllegalStateException (" Second "), ĊĠĠĠĠĠĠĠ ĠAbort Retry Exception ()) ĊĊĊĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠ Ġval Ġpromise Ġ= Ġexecutor . get With Retry Ġ{ Ġservice Mock . some times Fails () Ġ} ĊĊĠĠĠĠĠ Ġ// then ĊĠĠĠĠĠ Ġpromise . ready () ĊĠĠĠĠĠ ĠassertThat ( promise . is Failure ()). isTrue () ĊĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠ Ġpromise . get () ĊĠĠĠĠĠĠĠ Ġfail Because Exception Was Not Thrown ( ExecutionException :: class . java ) ĊĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠExecution Exception ) Ġ{ ĊĠĠĠĠĠĠĠ ĠassertThat ( e . cause ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. isInstanceOf ( IllegalStateException :: class . java ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. has Message (" Second ") ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠ Ġ} Ġcatch Ġ( e : ĠException ) Ġ{ ĊĠĠĠĠĠ Ġlog . error (" Error ", Ġe ) ĊĠĠĠ Ġ} ĊĊĠ Ġ} Ċ }