/* Ċ Ġ* ĠCopyright Ġ( C ) Ġ 2 0 1 9 ĠSquare , ĠInc . Ċ Ġ* Ċ Ġ* Ġ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 Ġokhttp 3 . internal . concurrent Ċ Ċ import Ġjava . lang . Thread . Un caught ExceptionHandler Ċ import Ġjava . util . concurrent . Linked Blocking Deque Ċ import Ġjava . util . concurrent . Thread Factory Ċ import Ġjava . util . concurrent . TimeUnit Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . assertj . core . data . Offset Ċ import Ġorg . junit . jupiter . api . After Each Ċ import Ġorg . junit . jupiter . api . Tag Ċ import Ġorg . junit . jupiter . api . Test Ċ Ċ /** Ċ Ġ* ĠIntegration Ġtest Ġto Ġconfirm Ġthat Ġ[ Task Runner ] Ġworks Ġwith Ġa Ġreal Ġbackend . ĠBusiness Ġlogic Ġis Ġall Ċ Ġ* Ġexerc ised Ġby Ġ[ Task Runner Test ]. Ċ Ġ* Ċ Ġ* ĠThis Ġtest Ġis Ġdoing Ġreal Ġsleep ing Ġwith Ġt oler ances Ġof Ġ 2 5 0 Ġms . ĠHope fully Ġthat 's Ġenough Ġfor Ġeven Ġthe Ċ Ġ* Ġbus iest Ġof ĠCI Ġservers . Ċ Ġ*/ Ċ @ Tag (" Slow ish ") Ċ class ĠTask Runner Real Backend Test Ġ{ ĊĠ Ġprivate Ġval Ġlog Ġ= ĠLinked Blocking Deque < String >() ĊĊĠ Ġprivate Ġval Ġlogging Un caught ExceptionHandler Ġ= ĠUn caught ExceptionHandler Ġ{ Ġ_, Ġthrowable Ġ-> ĊĠĠĠ Ġlog . put (" unc a ught Ġexception : Ġ$ throwable ") ĊĠ Ġ} ĊĊĠ Ġprivate Ġval Ġthread Factory Ġ= ĠThread Factory Ġ{ Ġrunnable Ġ-> ĊĠĠĠ ĠThread ( run nable , Ġ" Task Runner Real Backend Test "). apply Ġ{ ĊĠĠĠĠĠ Ġis Daemon Ġ= Ġtrue ĊĠĠĠĠĠ Ġun caught ExceptionHandler Ġ= Ġlogging Un caught ExceptionHandler ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġval Ġbackend Ġ= ĠTask Runner . Real Backend ( thread Factory ) ĊĠ Ġprivate Ġval Ġtask Runner Ġ= ĠTask Runner ( backend ) ĊĠ Ġprivate Ġval Ġqueue Ġ= Ġtask Runner . new Queue () ĊĊĠ Ġ@ After Each Ġfun ĠtearDown () Ġ{ ĊĠĠĠ Ġbackend . shutdown () ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġtest () Ġ{ ĊĠĠĠ Ġval Ġt 1 Ġ= ĠSystem . nano Time () Ġ/ Ġ 1 e 6 ĊĊĠĠĠ Ġval Ġdelays Ġ= Ġmutable ListOf ( TimeUnit . MILLISECONDS . to Nan os ( 1 0 0 0 ), Ġ- 1 L ) ĊĠĠĠ Ġqueue . schedule (" task ", ĠTimeUnit . MILLISECONDS . to Nan os ( 7 5 0 )) Ġ{ ĊĠĠĠĠĠ Ġlog . put (" run Once Ġdelays . size =${ delay s . size }") ĊĠĠĠĠĠ Ġreturn @ schedule Ġdelays . remove At ( 0 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠassertThat ( log . take ()). isEqualTo (" run Once Ġdelays . size = 2 ") ĊĠĠĠ Ġval Ġt 2 Ġ= ĠSystem . nano Time () Ġ/ Ġ 1 e 6 Ġ- Ġt 1 ĊĠĠĠ ĠassertThat ( t 2 ). is Close To ( 7 5 0 . 0 , ĠOffset . offset ( 2 5 0 . 0 )) ĊĊĠĠĠ ĠassertThat ( log . take ()). isEqualTo (" run Once Ġdelays . size = 1 ") ĊĠĠĠ Ġval Ġt 3 Ġ= ĠSystem . nano Time () Ġ/ Ġ 1 e 6 Ġ- Ġt 1 ĊĠĠĠ ĠassertThat ( t 3 ). is Close To ( 1 7 5 0 . 0 , ĠOffset . offset ( 2 5 0 . 0 )) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġtask Fails With Unchecked Exception () Ġ{ ĊĠĠĠ Ġqueue . schedule (" task ", ĠTimeUnit . MILLISECONDS . to Nan os ( 1 0 0 )) Ġ{ ĊĠĠĠĠĠ Ġlog . put (" fail ing Ġtask Ġrunning ") ĊĠĠĠĠĠ Ġthrow ĠRuntimeException (" boom !") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġqueue . schedule (" task ", ĠTimeUnit . MILLISECONDS . to Nan os ( 2 0 0 )) Ġ{ ĊĠĠĠĠĠ Ġlog . put (" normal Ġtask Ġrunning ") ĊĠĠĠĠĠ Ġreturn @ schedule Ġ- 1 L ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġqueue . idle Latch (). await ( 5 0 0 , ĠTimeUnit . MILLISECONDS ) ĊĊĠĠĠ ĠassertThat ( log . take ()). isEqualTo (" fail ing Ġtask Ġrunning ") ĊĠĠĠ ĠassertThat ( log . take ()). isEqualTo (" unc a ught Ġexception : Ġjava . lang . RuntimeException : Ġbo om !") ĊĠĠĠ ĠassertThat ( log . take ()). isEqualTo (" normal Ġtask Ġrunning ") ĊĠĠĠ ĠassertThat ( log ). isEmpty () ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġidle Latch After Shutdown () Ġ{ ĊĠĠĠ Ġqueue . schedule (" task ") Ġ{ ĊĠĠĠĠĠ ĠThread . sleep ( 2 5 0 ) ĊĠĠĠĠĠ Ġbackend . shutdown () ĊĠĠĠĠĠ Ġreturn @ schedule Ġ- 1 L ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠassertThat ( queue . idle Latch (). await ( 5 0 0 L , ĠTimeUnit . MILLISECONDS )). isTrue () ĊĠĠĠ ĠassertThat ( queue . idle Latch (). count ). isEqualTo ( 0 ) ĊĠ Ġ} Ċ } Ċ