clock_test.go 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package tstest
  4. import (
  5. "slices"
  6. "sync/atomic"
  7. "testing"
  8. "time"
  9. "tailscale.com/tstime"
  10. )
  11. func TestClockWithDefinedStartTime(t *testing.T) {
  12. t.Parallel()
  13. tests := []struct {
  14. name string
  15. start time.Time
  16. step time.Duration
  17. wants []time.Time // The return values of sequential calls to Now().
  18. }{
  19. {
  20. name: "increment ms",
  21. start: time.Unix(12345, 1000),
  22. step: 1000,
  23. wants: []time.Time{
  24. time.Unix(12345, 1000),
  25. time.Unix(12345, 2000),
  26. time.Unix(12345, 3000),
  27. time.Unix(12345, 4000),
  28. },
  29. },
  30. {
  31. name: "increment second",
  32. start: time.Unix(12345, 1000),
  33. step: time.Second,
  34. wants: []time.Time{
  35. time.Unix(12345, 1000),
  36. time.Unix(12346, 1000),
  37. time.Unix(12347, 1000),
  38. time.Unix(12348, 1000),
  39. },
  40. },
  41. {
  42. name: "no increment",
  43. start: time.Unix(12345, 1000),
  44. wants: []time.Time{
  45. time.Unix(12345, 1000),
  46. time.Unix(12345, 1000),
  47. time.Unix(12345, 1000),
  48. time.Unix(12345, 1000),
  49. },
  50. },
  51. }
  52. for _, tt := range tests {
  53. tt := tt
  54. t.Run(tt.name, func(t *testing.T) {
  55. t.Parallel()
  56. clock := NewClock(ClockOpts{
  57. Start: tt.start,
  58. Step: tt.step,
  59. })
  60. if start := clock.GetStart(); !start.Equal(tt.start) {
  61. t.Errorf("clock has start %v, want %v", start, tt.start)
  62. }
  63. if step := clock.GetStep(); step != tt.step {
  64. t.Errorf("clock has step %v, want %v", step, tt.step)
  65. }
  66. for i := range tt.wants {
  67. if got := clock.Now(); !got.Equal(tt.wants[i]) {
  68. t.Errorf("step %v: clock.Now() = %v, want %v", i, got, tt.wants[i])
  69. }
  70. if got := clock.PeekNow(); !got.Equal(tt.wants[i]) {
  71. t.Errorf("step %v: clock.PeekNow() = %v, want %v", i, got, tt.wants[i])
  72. }
  73. }
  74. })
  75. }
  76. }
  77. func TestClockWithDefaultStartTime(t *testing.T) {
  78. t.Parallel()
  79. tests := []struct {
  80. name string
  81. step time.Duration
  82. wants []time.Duration // The return values of sequential calls to Now() after added to Start()
  83. }{
  84. {
  85. name: "increment ms",
  86. step: 1000,
  87. wants: []time.Duration{
  88. 0,
  89. 1000,
  90. 2000,
  91. 3000,
  92. },
  93. },
  94. {
  95. name: "increment second",
  96. step: time.Second,
  97. wants: []time.Duration{
  98. 0 * time.Second,
  99. 1 * time.Second,
  100. 2 * time.Second,
  101. 3 * time.Second,
  102. },
  103. },
  104. {
  105. name: "no increment",
  106. wants: []time.Duration{0, 0, 0, 0},
  107. },
  108. }
  109. for _, tt := range tests {
  110. tt := tt
  111. t.Run(tt.name, func(t *testing.T) {
  112. t.Parallel()
  113. clock := NewClock(ClockOpts{
  114. Step: tt.step,
  115. })
  116. start := clock.GetStart()
  117. if step := clock.GetStep(); step != tt.step {
  118. t.Errorf("clock has step %v, want %v", step, tt.step)
  119. }
  120. for i := range tt.wants {
  121. want := start.Add(tt.wants[i])
  122. if got := clock.Now(); !got.Equal(want) {
  123. t.Errorf("step %v: clock.Now() = %v, want %v", i, got, tt.wants[i])
  124. }
  125. if got := clock.PeekNow(); !got.Equal(want) {
  126. t.Errorf("step %v: clock.PeekNow() = %v, want %v", i, got, tt.wants[i])
  127. }
  128. }
  129. })
  130. }
  131. }
  132. func TestZeroInitClock(t *testing.T) {
  133. t.Parallel()
  134. var clock Clock
  135. start := clock.GetStart()
  136. if step := clock.GetStep(); step != 0 {
  137. t.Errorf("clock has step %v, want 0", step)
  138. }
  139. for i := 0; i < 10; i++ {
  140. if got := clock.Now(); !got.Equal(start) {
  141. t.Errorf("step %v: clock.Now() = %v, want %v", i, got, start)
  142. }
  143. if got := clock.PeekNow(); !got.Equal(start) {
  144. t.Errorf("step %v: clock.PeekNow() = %v, want %v", i, got, start)
  145. }
  146. }
  147. }
  148. func TestClockSetStep(t *testing.T) {
  149. t.Parallel()
  150. type stepInfo struct {
  151. when int
  152. step time.Duration
  153. }
  154. tests := []struct {
  155. name string
  156. start time.Time
  157. step time.Duration
  158. stepChanges []stepInfo
  159. wants []time.Time // The return values of sequential calls to Now().
  160. }{
  161. {
  162. name: "increment ms then s",
  163. start: time.Unix(12345, 1000),
  164. step: 1000,
  165. stepChanges: []stepInfo{
  166. {
  167. when: 4,
  168. step: time.Second,
  169. },
  170. },
  171. wants: []time.Time{
  172. time.Unix(12345, 1000),
  173. time.Unix(12345, 2000),
  174. time.Unix(12345, 3000),
  175. time.Unix(12345, 4000),
  176. time.Unix(12346, 4000),
  177. time.Unix(12347, 4000),
  178. time.Unix(12348, 4000),
  179. time.Unix(12349, 4000),
  180. },
  181. },
  182. {
  183. name: "multiple changes over time",
  184. start: time.Unix(12345, 1000),
  185. step: 1,
  186. stepChanges: []stepInfo{
  187. {
  188. when: 2,
  189. step: time.Second,
  190. },
  191. {
  192. when: 4,
  193. step: 0,
  194. },
  195. {
  196. when: 6,
  197. step: 1000,
  198. },
  199. },
  200. wants: []time.Time{
  201. time.Unix(12345, 1000),
  202. time.Unix(12345, 1001),
  203. time.Unix(12346, 1001),
  204. time.Unix(12347, 1001),
  205. time.Unix(12347, 1001),
  206. time.Unix(12347, 1001),
  207. time.Unix(12347, 2001),
  208. time.Unix(12347, 3001),
  209. },
  210. },
  211. {
  212. name: "multiple changes at once",
  213. start: time.Unix(12345, 1000),
  214. step: 1,
  215. stepChanges: []stepInfo{
  216. {
  217. when: 2,
  218. step: time.Second,
  219. },
  220. {
  221. when: 2,
  222. step: 0,
  223. },
  224. {
  225. when: 2,
  226. step: 1000,
  227. },
  228. },
  229. wants: []time.Time{
  230. time.Unix(12345, 1000),
  231. time.Unix(12345, 1001),
  232. time.Unix(12345, 2001),
  233. time.Unix(12345, 3001),
  234. },
  235. },
  236. {
  237. name: "changes at start",
  238. start: time.Unix(12345, 1000),
  239. step: 0,
  240. stepChanges: []stepInfo{
  241. {
  242. when: 0,
  243. step: time.Second,
  244. },
  245. {
  246. when: 0,
  247. step: 1000,
  248. },
  249. },
  250. wants: []time.Time{
  251. time.Unix(12345, 1000),
  252. time.Unix(12345, 2000),
  253. time.Unix(12345, 3000),
  254. time.Unix(12345, 4000),
  255. },
  256. },
  257. }
  258. for _, tt := range tests {
  259. tt := tt
  260. t.Run(tt.name, func(t *testing.T) {
  261. t.Parallel()
  262. clock := NewClock(ClockOpts{
  263. Start: tt.start,
  264. Step: tt.step,
  265. })
  266. wantStep := tt.step
  267. changeIndex := 0
  268. for i := range tt.wants {
  269. for len(tt.stepChanges) > changeIndex && tt.stepChanges[changeIndex].when == i {
  270. wantStep = tt.stepChanges[changeIndex].step
  271. clock.SetStep(wantStep)
  272. changeIndex++
  273. }
  274. if start := clock.GetStart(); !start.Equal(tt.start) {
  275. t.Errorf("clock has start %v, want %v", start, tt.start)
  276. }
  277. if step := clock.GetStep(); step != wantStep {
  278. t.Errorf("clock has step %v, want %v", step, tt.step)
  279. }
  280. if got := clock.Now(); !got.Equal(tt.wants[i]) {
  281. t.Errorf("step %v: clock.Now() = %v, want %v", i, got, tt.wants[i])
  282. }
  283. if got := clock.PeekNow(); !got.Equal(tt.wants[i]) {
  284. t.Errorf("step %v: clock.PeekNow() = %v, want %v", i, got, tt.wants[i])
  285. }
  286. }
  287. })
  288. }
  289. }
  290. func TestClockAdvance(t *testing.T) {
  291. t.Parallel()
  292. type advanceInfo struct {
  293. when int
  294. advance time.Duration
  295. }
  296. tests := []struct {
  297. name string
  298. start time.Time
  299. step time.Duration
  300. advances []advanceInfo
  301. wants []time.Time // The return values of sequential calls to Now().
  302. }{
  303. {
  304. name: "increment ms then advance 1s",
  305. start: time.Unix(12345, 1000),
  306. step: 1000,
  307. advances: []advanceInfo{
  308. {
  309. when: 4,
  310. advance: time.Second,
  311. },
  312. },
  313. wants: []time.Time{
  314. time.Unix(12345, 1000),
  315. time.Unix(12345, 2000),
  316. time.Unix(12345, 3000),
  317. time.Unix(12345, 4000),
  318. time.Unix(12346, 4000),
  319. time.Unix(12346, 5000),
  320. time.Unix(12346, 6000),
  321. time.Unix(12346, 7000),
  322. },
  323. },
  324. {
  325. name: "multiple advances over time",
  326. start: time.Unix(12345, 1000),
  327. step: 1,
  328. advances: []advanceInfo{
  329. {
  330. when: 2,
  331. advance: time.Second,
  332. },
  333. {
  334. when: 4,
  335. advance: 0,
  336. },
  337. {
  338. when: 6,
  339. advance: 1000,
  340. },
  341. },
  342. wants: []time.Time{
  343. time.Unix(12345, 1000),
  344. time.Unix(12345, 1001),
  345. time.Unix(12346, 1001),
  346. time.Unix(12346, 1002),
  347. time.Unix(12346, 1002),
  348. time.Unix(12346, 1003),
  349. time.Unix(12346, 2003),
  350. time.Unix(12346, 2004),
  351. },
  352. },
  353. {
  354. name: "multiple advances at once",
  355. start: time.Unix(12345, 1000),
  356. step: 1,
  357. advances: []advanceInfo{
  358. {
  359. when: 2,
  360. advance: time.Second,
  361. },
  362. {
  363. when: 2,
  364. advance: 0,
  365. },
  366. {
  367. when: 2,
  368. advance: 1000,
  369. },
  370. },
  371. wants: []time.Time{
  372. time.Unix(12345, 1000),
  373. time.Unix(12345, 1001),
  374. time.Unix(12346, 2001),
  375. time.Unix(12346, 2002),
  376. },
  377. },
  378. {
  379. name: "changes at start",
  380. start: time.Unix(12345, 1000),
  381. step: 5,
  382. advances: []advanceInfo{
  383. {
  384. when: 0,
  385. advance: time.Second,
  386. },
  387. {
  388. when: 0,
  389. advance: 1000,
  390. },
  391. },
  392. wants: []time.Time{
  393. time.Unix(12346, 2000),
  394. time.Unix(12346, 2005),
  395. time.Unix(12346, 2010),
  396. time.Unix(12346, 2015),
  397. },
  398. },
  399. }
  400. for _, tt := range tests {
  401. tt := tt
  402. t.Run(tt.name, func(t *testing.T) {
  403. t.Parallel()
  404. clock := NewClock(ClockOpts{
  405. Start: tt.start,
  406. Step: tt.step,
  407. })
  408. wantStep := tt.step
  409. changeIndex := 0
  410. for i := range tt.wants {
  411. for len(tt.advances) > changeIndex && tt.advances[changeIndex].when == i {
  412. clock.Advance(tt.advances[changeIndex].advance)
  413. changeIndex++
  414. }
  415. if start := clock.GetStart(); !start.Equal(tt.start) {
  416. t.Errorf("clock has start %v, want %v", start, tt.start)
  417. }
  418. if step := clock.GetStep(); step != wantStep {
  419. t.Errorf("clock has step %v, want %v", step, tt.step)
  420. }
  421. if got := clock.Now(); !got.Equal(tt.wants[i]) {
  422. t.Errorf("step %v: clock.Now() = %v, want %v", i, got, tt.wants[i])
  423. }
  424. if got := clock.PeekNow(); !got.Equal(tt.wants[i]) {
  425. t.Errorf("step %v: clock.PeekNow() = %v, want %v", i, got, tt.wants[i])
  426. }
  427. }
  428. })
  429. }
  430. }
  431. func expectNoTicks(t *testing.T, tickC <-chan time.Time) {
  432. t.Helper()
  433. select {
  434. case tick := <-tickC:
  435. t.Errorf("wanted no ticks, got %v", tick)
  436. default:
  437. }
  438. }
  439. func TestSingleTicker(t *testing.T) {
  440. t.Parallel()
  441. type testStep struct {
  442. stop bool
  443. reset time.Duration
  444. resetAbsolute time.Time
  445. setStep time.Duration
  446. advance time.Duration
  447. advanceRealTime time.Duration
  448. wantTime time.Time
  449. wantTicks []time.Time
  450. }
  451. tests := []struct {
  452. name string
  453. realTimeOpts *ClockOpts
  454. start time.Time
  455. step time.Duration
  456. period time.Duration
  457. channelSize int
  458. steps []testStep
  459. }{
  460. {
  461. name: "no tick advance",
  462. start: time.Unix(12345, 0),
  463. period: time.Second,
  464. steps: []testStep{
  465. {
  466. advance: time.Second - 1,
  467. wantTime: time.Unix(12345, 999_999_999),
  468. },
  469. },
  470. },
  471. {
  472. name: "no tick step",
  473. start: time.Unix(12345, 0),
  474. step: time.Second - 1,
  475. period: time.Second,
  476. steps: []testStep{
  477. {
  478. wantTime: time.Unix(12345, 0),
  479. },
  480. {
  481. wantTime: time.Unix(12345, 999_999_999),
  482. },
  483. },
  484. },
  485. {
  486. name: "single tick advance exact",
  487. start: time.Unix(12345, 0),
  488. period: time.Second,
  489. steps: []testStep{
  490. {
  491. advance: time.Second,
  492. wantTime: time.Unix(12346, 0),
  493. wantTicks: []time.Time{time.Unix(12346, 0)},
  494. },
  495. },
  496. },
  497. {
  498. name: "single tick advance extra",
  499. start: time.Unix(12345, 0),
  500. period: time.Second,
  501. steps: []testStep{
  502. {
  503. advance: time.Second + 1,
  504. wantTime: time.Unix(12346, 1),
  505. wantTicks: []time.Time{time.Unix(12346, 0)},
  506. },
  507. },
  508. },
  509. {
  510. name: "single tick step exact",
  511. start: time.Unix(12345, 0),
  512. step: time.Second,
  513. period: time.Second,
  514. steps: []testStep{
  515. {
  516. wantTime: time.Unix(12345, 0),
  517. },
  518. {
  519. wantTime: time.Unix(12346, 0),
  520. wantTicks: []time.Time{time.Unix(12346, 0)},
  521. },
  522. },
  523. },
  524. {
  525. name: "single tick step extra",
  526. start: time.Unix(12345, 0),
  527. step: time.Second + 1,
  528. period: time.Second,
  529. steps: []testStep{
  530. {
  531. wantTime: time.Unix(12345, 0),
  532. },
  533. {
  534. wantTime: time.Unix(12346, 1),
  535. wantTicks: []time.Time{time.Unix(12346, 0)},
  536. },
  537. },
  538. },
  539. {
  540. name: "single tick per advance",
  541. start: time.Unix(12345, 0),
  542. period: 3 * time.Second,
  543. steps: []testStep{
  544. {
  545. wantTime: time.Unix(12345, 0),
  546. },
  547. {
  548. advance: 4 * time.Second,
  549. wantTime: time.Unix(12349, 0),
  550. wantTicks: []time.Time{time.Unix(12348, 0)},
  551. },
  552. {
  553. advance: 2 * time.Second,
  554. wantTime: time.Unix(12351, 0),
  555. wantTicks: []time.Time{time.Unix(12351, 0)},
  556. },
  557. {
  558. advance: 2 * time.Second,
  559. wantTime: time.Unix(12353, 0),
  560. },
  561. {
  562. advance: 2 * time.Second,
  563. wantTime: time.Unix(12355, 0),
  564. wantTicks: []time.Time{time.Unix(12354, 0)},
  565. },
  566. },
  567. },
  568. {
  569. name: "single tick per step",
  570. start: time.Unix(12345, 0),
  571. step: 2 * time.Second,
  572. period: 3 * time.Second,
  573. steps: []testStep{
  574. {
  575. wantTime: time.Unix(12345, 0),
  576. },
  577. {
  578. wantTime: time.Unix(12347, 0),
  579. },
  580. {
  581. wantTime: time.Unix(12349, 0),
  582. wantTicks: []time.Time{time.Unix(12348, 0)},
  583. },
  584. {
  585. wantTime: time.Unix(12351, 0),
  586. wantTicks: []time.Time{time.Unix(12351, 0)},
  587. },
  588. {
  589. wantTime: time.Unix(12353, 0),
  590. },
  591. {
  592. wantTime: time.Unix(12355, 0),
  593. wantTicks: []time.Time{time.Unix(12354, 0)},
  594. },
  595. },
  596. },
  597. {
  598. name: "multiple tick per advance",
  599. start: time.Unix(12345, 0),
  600. period: time.Second,
  601. channelSize: 3,
  602. steps: []testStep{
  603. {
  604. wantTime: time.Unix(12345, 0),
  605. },
  606. {
  607. advance: 2 * time.Second,
  608. wantTime: time.Unix(12347, 0),
  609. wantTicks: []time.Time{
  610. time.Unix(12346, 0),
  611. time.Unix(12347, 0),
  612. },
  613. },
  614. {
  615. advance: 4 * time.Second,
  616. wantTime: time.Unix(12351, 0),
  617. wantTicks: []time.Time{
  618. time.Unix(12348, 0),
  619. time.Unix(12349, 0),
  620. time.Unix(12350, 0),
  621. // fourth tick dropped due to channel size
  622. },
  623. },
  624. },
  625. },
  626. {
  627. name: "multiple tick per step",
  628. start: time.Unix(12345, 0),
  629. step: 3 * time.Second,
  630. period: 2 * time.Second,
  631. channelSize: 3,
  632. steps: []testStep{
  633. {
  634. wantTime: time.Unix(12345, 0),
  635. },
  636. {
  637. wantTime: time.Unix(12348, 0),
  638. wantTicks: []time.Time{
  639. time.Unix(12347, 0),
  640. },
  641. },
  642. {
  643. wantTime: time.Unix(12351, 0),
  644. wantTicks: []time.Time{
  645. time.Unix(12349, 0),
  646. time.Unix(12351, 0),
  647. },
  648. },
  649. {
  650. wantTime: time.Unix(12354, 0),
  651. wantTicks: []time.Time{
  652. time.Unix(12353, 0),
  653. },
  654. },
  655. {
  656. wantTime: time.Unix(12357, 0),
  657. wantTicks: []time.Time{
  658. time.Unix(12355, 0),
  659. time.Unix(12357, 0),
  660. },
  661. },
  662. },
  663. },
  664. {
  665. name: "stop",
  666. start: time.Unix(12345, 0),
  667. step: 2 * time.Second,
  668. period: time.Second,
  669. channelSize: 3,
  670. steps: []testStep{
  671. {
  672. wantTime: time.Unix(12345, 0),
  673. },
  674. {
  675. wantTime: time.Unix(12347, 0),
  676. wantTicks: []time.Time{
  677. time.Unix(12346, 0),
  678. time.Unix(12347, 0),
  679. },
  680. },
  681. {
  682. stop: true,
  683. wantTime: time.Unix(12349, 0),
  684. },
  685. {
  686. wantTime: time.Unix(12351, 0),
  687. },
  688. {
  689. advance: 10 * time.Second,
  690. wantTime: time.Unix(12361, 0),
  691. },
  692. },
  693. },
  694. {
  695. name: "reset while running",
  696. start: time.Unix(12345, 0),
  697. period: 2 * time.Second,
  698. steps: []testStep{
  699. {
  700. wantTime: time.Unix(12345, 0),
  701. },
  702. {
  703. advance: time.Second,
  704. wantTime: time.Unix(12346, 0),
  705. },
  706. {
  707. advance: time.Second,
  708. wantTime: time.Unix(12347, 0),
  709. wantTicks: []time.Time{
  710. time.Unix(12347, 0),
  711. },
  712. },
  713. {
  714. advance: time.Second,
  715. reset: time.Second,
  716. wantTime: time.Unix(12348, 0),
  717. wantTicks: []time.Time{
  718. time.Unix(12348, 0),
  719. },
  720. },
  721. {
  722. setStep: 5 * time.Second,
  723. reset: 10 * time.Second,
  724. wantTime: time.Unix(12353, 0),
  725. },
  726. {
  727. wantTime: time.Unix(12358, 0),
  728. wantTicks: []time.Time{
  729. time.Unix(12358, 0),
  730. },
  731. },
  732. },
  733. },
  734. {
  735. name: "reset while stopped",
  736. start: time.Unix(12345, 0),
  737. step: time.Second,
  738. period: 2 * time.Second,
  739. steps: []testStep{
  740. {
  741. wantTime: time.Unix(12345, 0),
  742. },
  743. {
  744. wantTime: time.Unix(12346, 0),
  745. },
  746. {
  747. wantTime: time.Unix(12347, 0),
  748. wantTicks: []time.Time{
  749. time.Unix(12347, 0),
  750. },
  751. },
  752. {
  753. stop: true,
  754. wantTime: time.Unix(12348, 0),
  755. },
  756. {
  757. wantTime: time.Unix(12349, 0),
  758. },
  759. {
  760. reset: time.Second,
  761. wantTime: time.Unix(12350, 0),
  762. wantTicks: []time.Time{
  763. time.Unix(12350, 0),
  764. },
  765. },
  766. {
  767. wantTime: time.Unix(12351, 0),
  768. wantTicks: []time.Time{
  769. time.Unix(12351, 0),
  770. },
  771. },
  772. },
  773. },
  774. {
  775. name: "reset absolute",
  776. start: time.Unix(12345, 0),
  777. step: time.Second,
  778. period: 2 * time.Second,
  779. steps: []testStep{
  780. {
  781. wantTime: time.Unix(12345, 0),
  782. },
  783. {
  784. wantTime: time.Unix(12346, 0),
  785. },
  786. {
  787. wantTime: time.Unix(12347, 0),
  788. wantTicks: []time.Time{
  789. time.Unix(12347, 0),
  790. },
  791. },
  792. {
  793. reset: time.Second,
  794. resetAbsolute: time.Unix(12354, 50),
  795. advance: 7 * time.Second,
  796. wantTime: time.Unix(12354, 0),
  797. },
  798. {
  799. wantTime: time.Unix(12355, 0),
  800. wantTicks: []time.Time{
  801. time.Unix(12354, 50),
  802. },
  803. },
  804. {
  805. wantTime: time.Unix(12356, 0),
  806. wantTicks: []time.Time{
  807. time.Unix(12355, 50),
  808. },
  809. },
  810. },
  811. },
  812. {
  813. name: "follow real time",
  814. realTimeOpts: new(ClockOpts),
  815. start: time.Unix(12345, 0),
  816. period: 2 * time.Second,
  817. channelSize: 3,
  818. steps: []testStep{
  819. {
  820. wantTime: time.Unix(12345, 0),
  821. },
  822. {
  823. advanceRealTime: 5 * time.Second,
  824. wantTime: time.Unix(12350, 0),
  825. wantTicks: []time.Time{
  826. time.Unix(12347, 0),
  827. time.Unix(12349, 0),
  828. },
  829. },
  830. {
  831. advance: 5 * time.Second,
  832. wantTime: time.Unix(12355, 0),
  833. wantTicks: []time.Time{
  834. time.Unix(12351, 0),
  835. time.Unix(12353, 0),
  836. time.Unix(12355, 0),
  837. },
  838. },
  839. },
  840. },
  841. }
  842. for _, tt := range tests {
  843. tt := tt
  844. t.Run(tt.name, func(t *testing.T) {
  845. t.Parallel()
  846. var realTimeClockForTestClock tstime.Clock
  847. var realTimeClock *Clock
  848. if tt.realTimeOpts != nil {
  849. realTimeClock = NewClock(*tt.realTimeOpts)
  850. // Passing realTimeClock into newClockInternal results in a
  851. // non-nil interface with a nil pointer, so this is necessary.
  852. realTimeClockForTestClock = realTimeClock
  853. }
  854. clock := newClockInternal(ClockOpts{
  855. Start: tt.start,
  856. Step: tt.step,
  857. TimerChannelSize: tt.channelSize,
  858. FollowRealTime: realTimeClock != nil,
  859. }, realTimeClockForTestClock)
  860. tc, tickC := clock.NewTicker(tt.period)
  861. tickControl := tc.(*Ticker)
  862. t.Cleanup(tickControl.Stop)
  863. expectNoTicks(t, tickC)
  864. for i, step := range tt.steps {
  865. if step.stop {
  866. tickControl.Stop()
  867. }
  868. if !step.resetAbsolute.IsZero() {
  869. tickControl.ResetAbsolute(step.resetAbsolute, step.reset)
  870. } else if step.reset > 0 {
  871. tickControl.Reset(step.reset)
  872. }
  873. if step.setStep > 0 {
  874. clock.SetStep(step.setStep)
  875. }
  876. if step.advance > 0 {
  877. clock.Advance(step.advance)
  878. }
  879. if step.advanceRealTime > 0 {
  880. realTimeClock.Advance(step.advanceRealTime)
  881. }
  882. if now := clock.Now(); !step.wantTime.IsZero() && !now.Equal(step.wantTime) {
  883. t.Errorf("step %v now = %v, want %v", i, now, step.wantTime)
  884. }
  885. for j, want := range step.wantTicks {
  886. select {
  887. case tick := <-tickC:
  888. if tick.Equal(want) {
  889. continue
  890. }
  891. t.Errorf("step %v tick %v = %v, want %v", i, j, tick, want)
  892. default:
  893. t.Errorf("step %v tick %v missing", i, j)
  894. }
  895. }
  896. expectNoTicks(t, tickC)
  897. }
  898. })
  899. }
  900. }
  901. func TestSingleTimer(t *testing.T) {
  902. t.Parallel()
  903. type testStep struct {
  904. stop bool
  905. stopReturn bool // The expected return value for Stop() if stop is true.
  906. reset time.Duration
  907. resetAbsolute time.Time
  908. resetReturn bool // The expected return value for Reset() or ResetAbsolute().
  909. setStep time.Duration
  910. advance time.Duration
  911. advanceRealTime time.Duration
  912. wantTime time.Time
  913. wantTicks []time.Time
  914. }
  915. tests := []struct {
  916. name string
  917. realTimeOpts *ClockOpts
  918. start time.Time
  919. step time.Duration
  920. delay time.Duration
  921. steps []testStep
  922. }{
  923. {
  924. name: "no tick advance",
  925. start: time.Unix(12345, 0),
  926. delay: time.Second,
  927. steps: []testStep{
  928. {
  929. advance: time.Second - 1,
  930. wantTime: time.Unix(12345, 999_999_999),
  931. },
  932. },
  933. },
  934. {
  935. name: "no tick step",
  936. start: time.Unix(12345, 0),
  937. step: time.Second - 1,
  938. delay: time.Second,
  939. steps: []testStep{
  940. {
  941. wantTime: time.Unix(12345, 0),
  942. },
  943. {
  944. wantTime: time.Unix(12345, 999_999_999),
  945. },
  946. },
  947. },
  948. {
  949. name: "single tick advance exact",
  950. start: time.Unix(12345, 0),
  951. delay: time.Second,
  952. steps: []testStep{
  953. {
  954. advance: time.Second,
  955. wantTime: time.Unix(12346, 0),
  956. wantTicks: []time.Time{time.Unix(12346, 0)},
  957. },
  958. {
  959. advance: time.Second,
  960. wantTime: time.Unix(12347, 0),
  961. },
  962. },
  963. },
  964. {
  965. name: "single tick advance extra",
  966. start: time.Unix(12345, 0),
  967. delay: time.Second,
  968. steps: []testStep{
  969. {
  970. advance: time.Second + 1,
  971. wantTime: time.Unix(12346, 1),
  972. wantTicks: []time.Time{time.Unix(12346, 0)},
  973. },
  974. {
  975. advance: time.Second,
  976. wantTime: time.Unix(12347, 1),
  977. },
  978. },
  979. },
  980. {
  981. name: "single tick step exact",
  982. start: time.Unix(12345, 0),
  983. step: time.Second,
  984. delay: time.Second,
  985. steps: []testStep{
  986. {
  987. wantTime: time.Unix(12345, 0),
  988. },
  989. {
  990. wantTime: time.Unix(12346, 0),
  991. wantTicks: []time.Time{time.Unix(12346, 0)},
  992. },
  993. {
  994. wantTime: time.Unix(12347, 0),
  995. },
  996. },
  997. },
  998. {
  999. name: "single tick step extra",
  1000. start: time.Unix(12345, 0),
  1001. step: time.Second + 1,
  1002. delay: time.Second,
  1003. steps: []testStep{
  1004. {
  1005. wantTime: time.Unix(12345, 0),
  1006. },
  1007. {
  1008. wantTime: time.Unix(12346, 1),
  1009. wantTicks: []time.Time{time.Unix(12346, 0)},
  1010. },
  1011. {
  1012. wantTime: time.Unix(12347, 2),
  1013. },
  1014. },
  1015. },
  1016. {
  1017. name: "reset for single tick per advance",
  1018. start: time.Unix(12345, 0),
  1019. delay: 3 * time.Second,
  1020. steps: []testStep{
  1021. {
  1022. wantTime: time.Unix(12345, 0),
  1023. },
  1024. {
  1025. advance: 4 * time.Second,
  1026. wantTime: time.Unix(12349, 0),
  1027. wantTicks: []time.Time{time.Unix(12348, 0)},
  1028. },
  1029. {
  1030. resetAbsolute: time.Unix(12351, 0),
  1031. advance: 2 * time.Second,
  1032. wantTime: time.Unix(12351, 0),
  1033. wantTicks: []time.Time{time.Unix(12351, 0)},
  1034. },
  1035. {
  1036. reset: 3 * time.Second,
  1037. advance: 2 * time.Second,
  1038. wantTime: time.Unix(12353, 0),
  1039. },
  1040. {
  1041. advance: 2 * time.Second,
  1042. wantTime: time.Unix(12355, 0),
  1043. wantTicks: []time.Time{time.Unix(12354, 0)},
  1044. },
  1045. {
  1046. advance: 10 * time.Second,
  1047. wantTime: time.Unix(12365, 0),
  1048. },
  1049. },
  1050. },
  1051. {
  1052. name: "reset for single tick per step",
  1053. start: time.Unix(12345, 0),
  1054. step: 2 * time.Second,
  1055. delay: 3 * time.Second,
  1056. steps: []testStep{
  1057. {
  1058. wantTime: time.Unix(12345, 0),
  1059. },
  1060. {
  1061. wantTime: time.Unix(12347, 0),
  1062. },
  1063. {
  1064. wantTime: time.Unix(12349, 0),
  1065. wantTicks: []time.Time{time.Unix(12348, 0)},
  1066. },
  1067. {
  1068. reset: time.Second,
  1069. wantTime: time.Unix(12351, 0),
  1070. wantTicks: []time.Time{time.Unix(12350, 0)},
  1071. },
  1072. {
  1073. resetAbsolute: time.Unix(12354, 0),
  1074. wantTime: time.Unix(12353, 0),
  1075. },
  1076. {
  1077. wantTime: time.Unix(12355, 0),
  1078. wantTicks: []time.Time{time.Unix(12354, 0)},
  1079. },
  1080. },
  1081. },
  1082. {
  1083. name: "reset while active",
  1084. start: time.Unix(12345, 0),
  1085. step: 2 * time.Second,
  1086. delay: 3 * time.Second,
  1087. steps: []testStep{
  1088. {
  1089. wantTime: time.Unix(12345, 0),
  1090. },
  1091. {
  1092. wantTime: time.Unix(12347, 0),
  1093. },
  1094. {
  1095. reset: 3 * time.Second,
  1096. resetReturn: true,
  1097. wantTime: time.Unix(12349, 0),
  1098. },
  1099. {
  1100. resetAbsolute: time.Unix(12354, 0),
  1101. resetReturn: true,
  1102. wantTime: time.Unix(12351, 0),
  1103. },
  1104. {
  1105. wantTime: time.Unix(12353, 0),
  1106. },
  1107. {
  1108. wantTime: time.Unix(12355, 0),
  1109. wantTicks: []time.Time{time.Unix(12354, 0)},
  1110. },
  1111. },
  1112. },
  1113. {
  1114. name: "stop after fire",
  1115. start: time.Unix(12345, 0),
  1116. step: 2 * time.Second,
  1117. delay: time.Second,
  1118. steps: []testStep{
  1119. {
  1120. wantTime: time.Unix(12345, 0),
  1121. },
  1122. {
  1123. wantTime: time.Unix(12347, 0),
  1124. wantTicks: []time.Time{time.Unix(12346, 0)},
  1125. },
  1126. {
  1127. stop: true,
  1128. wantTime: time.Unix(12349, 0),
  1129. },
  1130. {
  1131. wantTime: time.Unix(12351, 0),
  1132. },
  1133. {
  1134. advance: 10 * time.Second,
  1135. wantTime: time.Unix(12361, 0),
  1136. },
  1137. },
  1138. },
  1139. {
  1140. name: "stop before fire",
  1141. start: time.Unix(12345, 0),
  1142. step: 2 * time.Second,
  1143. delay: time.Second,
  1144. steps: []testStep{
  1145. {
  1146. stop: true,
  1147. stopReturn: true,
  1148. wantTime: time.Unix(12345, 0),
  1149. },
  1150. {
  1151. wantTime: time.Unix(12347, 0),
  1152. },
  1153. {
  1154. wantTime: time.Unix(12349, 0),
  1155. },
  1156. {
  1157. wantTime: time.Unix(12351, 0),
  1158. },
  1159. {
  1160. advance: 10 * time.Second,
  1161. wantTime: time.Unix(12361, 0),
  1162. },
  1163. },
  1164. },
  1165. {
  1166. name: "stop after reset",
  1167. start: time.Unix(12345, 0),
  1168. step: 2 * time.Second,
  1169. delay: time.Second,
  1170. steps: []testStep{
  1171. {
  1172. wantTime: time.Unix(12345, 0),
  1173. },
  1174. {
  1175. wantTime: time.Unix(12347, 0),
  1176. wantTicks: []time.Time{time.Unix(12346, 0)},
  1177. },
  1178. {
  1179. reset: 10 * time.Second,
  1180. wantTime: time.Unix(12349, 0),
  1181. },
  1182. {
  1183. stop: true,
  1184. stopReturn: true,
  1185. wantTime: time.Unix(12351, 0),
  1186. },
  1187. {
  1188. advance: 10 * time.Second,
  1189. wantTime: time.Unix(12361, 0),
  1190. },
  1191. },
  1192. },
  1193. {
  1194. name: "reset while running",
  1195. start: time.Unix(12345, 0),
  1196. delay: 2 * time.Second,
  1197. steps: []testStep{
  1198. {
  1199. wantTime: time.Unix(12345, 0),
  1200. },
  1201. {
  1202. advance: time.Second,
  1203. wantTime: time.Unix(12346, 0),
  1204. },
  1205. {
  1206. advance: time.Second,
  1207. wantTime: time.Unix(12347, 0),
  1208. wantTicks: []time.Time{
  1209. time.Unix(12347, 0),
  1210. },
  1211. },
  1212. {
  1213. advance: time.Second,
  1214. reset: time.Second,
  1215. wantTime: time.Unix(12348, 0),
  1216. wantTicks: []time.Time{
  1217. time.Unix(12348, 0),
  1218. },
  1219. },
  1220. {
  1221. setStep: 5 * time.Second,
  1222. reset: 10 * time.Second,
  1223. wantTime: time.Unix(12353, 0),
  1224. },
  1225. {
  1226. wantTime: time.Unix(12358, 0),
  1227. wantTicks: []time.Time{
  1228. time.Unix(12358, 0),
  1229. },
  1230. },
  1231. },
  1232. },
  1233. {
  1234. name: "reset while stopped",
  1235. start: time.Unix(12345, 0),
  1236. step: time.Second,
  1237. delay: 2 * time.Second,
  1238. steps: []testStep{
  1239. {
  1240. wantTime: time.Unix(12345, 0),
  1241. },
  1242. {
  1243. wantTime: time.Unix(12346, 0),
  1244. },
  1245. {
  1246. stop: true,
  1247. stopReturn: true,
  1248. wantTime: time.Unix(12347, 0),
  1249. },
  1250. {
  1251. wantTime: time.Unix(12348, 0),
  1252. },
  1253. {
  1254. wantTime: time.Unix(12349, 0),
  1255. },
  1256. {
  1257. reset: time.Second,
  1258. wantTime: time.Unix(12350, 0),
  1259. wantTicks: []time.Time{
  1260. time.Unix(12350, 0),
  1261. },
  1262. },
  1263. {
  1264. wantTime: time.Unix(12351, 0),
  1265. },
  1266. },
  1267. },
  1268. {
  1269. name: "reset absolute",
  1270. start: time.Unix(12345, 0),
  1271. step: time.Second,
  1272. delay: 2 * time.Second,
  1273. steps: []testStep{
  1274. {
  1275. wantTime: time.Unix(12345, 0),
  1276. },
  1277. {
  1278. wantTime: time.Unix(12346, 0),
  1279. },
  1280. {
  1281. wantTime: time.Unix(12347, 0),
  1282. wantTicks: []time.Time{
  1283. time.Unix(12347, 0),
  1284. },
  1285. },
  1286. {
  1287. resetAbsolute: time.Unix(12354, 50),
  1288. advance: 7 * time.Second,
  1289. wantTime: time.Unix(12354, 0),
  1290. },
  1291. {
  1292. wantTime: time.Unix(12355, 0),
  1293. wantTicks: []time.Time{
  1294. time.Unix(12354, 50),
  1295. },
  1296. },
  1297. {
  1298. wantTime: time.Unix(12356, 0),
  1299. },
  1300. },
  1301. },
  1302. {
  1303. name: "follow real time",
  1304. realTimeOpts: new(ClockOpts),
  1305. start: time.Unix(12345, 0),
  1306. delay: 2 * time.Second,
  1307. steps: []testStep{
  1308. {
  1309. wantTime: time.Unix(12345, 0),
  1310. },
  1311. {
  1312. advanceRealTime: 5 * time.Second,
  1313. wantTime: time.Unix(12350, 0),
  1314. wantTicks: []time.Time{
  1315. time.Unix(12347, 0),
  1316. },
  1317. },
  1318. {
  1319. reset: 2 * time.Second,
  1320. advance: 5 * time.Second,
  1321. wantTime: time.Unix(12355, 0),
  1322. wantTicks: []time.Time{
  1323. time.Unix(12352, 0),
  1324. },
  1325. },
  1326. },
  1327. },
  1328. }
  1329. for _, tt := range tests {
  1330. tt := tt
  1331. t.Run(tt.name, func(t *testing.T) {
  1332. t.Parallel()
  1333. var realTimeClockForTestClock tstime.Clock
  1334. var realTimeClock *Clock
  1335. if tt.realTimeOpts != nil {
  1336. realTimeClock = NewClock(*tt.realTimeOpts)
  1337. // Passing realTimeClock into newClockInternal results in a
  1338. // non-nil interface with a nil pointer, so this is necessary.
  1339. realTimeClockForTestClock = realTimeClock
  1340. }
  1341. clock := newClockInternal(ClockOpts{
  1342. Start: tt.start,
  1343. Step: tt.step,
  1344. FollowRealTime: realTimeClock != nil,
  1345. }, realTimeClockForTestClock)
  1346. tc, tickC := clock.NewTimer(tt.delay)
  1347. timerControl := tc.(*Timer)
  1348. t.Cleanup(func() { timerControl.Stop() })
  1349. expectNoTicks(t, tickC)
  1350. for i, step := range tt.steps {
  1351. if step.stop {
  1352. if got := timerControl.Stop(); got != step.stopReturn {
  1353. t.Errorf("step %v Stop returned %v, want %v", i, got, step.stopReturn)
  1354. }
  1355. }
  1356. if !step.resetAbsolute.IsZero() {
  1357. if got := timerControl.ResetAbsolute(step.resetAbsolute); got != step.resetReturn {
  1358. t.Errorf("step %v Reset returned %v, want %v", i, got, step.resetReturn)
  1359. }
  1360. }
  1361. if step.reset > 0 {
  1362. if got := timerControl.Reset(step.reset); got != step.resetReturn {
  1363. t.Errorf("step %v Reset returned %v, want %v", i, got, step.resetReturn)
  1364. }
  1365. }
  1366. if step.setStep > 0 {
  1367. clock.SetStep(step.setStep)
  1368. }
  1369. if step.advance > 0 {
  1370. clock.Advance(step.advance)
  1371. }
  1372. if step.advanceRealTime > 0 {
  1373. realTimeClock.Advance(step.advanceRealTime)
  1374. }
  1375. if now := clock.Now(); !step.wantTime.IsZero() && !now.Equal(step.wantTime) {
  1376. t.Errorf("step %v now = %v, want %v", i, now, step.wantTime)
  1377. }
  1378. for j, want := range step.wantTicks {
  1379. select {
  1380. case tick := <-tickC:
  1381. if tick.Equal(want) {
  1382. continue
  1383. }
  1384. t.Errorf("step %v tick %v = %v, want %v", i, j, tick, want)
  1385. default:
  1386. t.Errorf("step %v tick %v missing", i, j)
  1387. }
  1388. }
  1389. expectNoTicks(t, tickC)
  1390. }
  1391. })
  1392. }
  1393. }
  1394. type testEvent struct {
  1395. fireTimes []time.Time
  1396. scheduleTimes []time.Time
  1397. }
  1398. func (te *testEvent) Fire(t time.Time) time.Time {
  1399. var ret time.Time
  1400. te.fireTimes = append(te.fireTimes, t)
  1401. if len(te.scheduleTimes) > 0 {
  1402. ret = te.scheduleTimes[0]
  1403. te.scheduleTimes = te.scheduleTimes[1:]
  1404. }
  1405. return ret
  1406. }
  1407. func TestEventManager(t *testing.T) {
  1408. t.Parallel()
  1409. var em eventManager
  1410. testEvents := []testEvent{
  1411. {
  1412. scheduleTimes: []time.Time{
  1413. time.Unix(12300, 0), // step 1
  1414. time.Unix(12340, 0), // step 1
  1415. time.Unix(12345, 0), // step 1
  1416. time.Unix(12346, 0), // step 1
  1417. time.Unix(12347, 0), // step 3
  1418. time.Unix(12348, 0), // step 4
  1419. time.Unix(12349, 0), // step 4
  1420. },
  1421. },
  1422. {
  1423. scheduleTimes: []time.Time{
  1424. time.Unix(12350, 0), // step 4
  1425. time.Unix(12360, 0), // step 5
  1426. time.Unix(12370, 0), // rescheduled
  1427. time.Unix(12380, 0), // step 6
  1428. time.Unix(12381, 0), // step 6
  1429. time.Unix(12382, 0), // step 6
  1430. time.Unix(12393, 0), // stopped
  1431. },
  1432. },
  1433. {
  1434. scheduleTimes: []time.Time{
  1435. time.Unix(12350, 1), // step 4
  1436. time.Unix(12360, 1), // rescheduled
  1437. time.Unix(12370, 1), // step 6
  1438. time.Unix(12380, 1), // step 6
  1439. time.Unix(12381, 1), // step 6
  1440. time.Unix(12382, 1), // step 6
  1441. time.Unix(12383, 1), // step 6
  1442. },
  1443. },
  1444. {
  1445. scheduleTimes: []time.Time{
  1446. time.Unix(12355, 0), // step 5
  1447. time.Unix(12365, 0), // step 5
  1448. time.Unix(12370, 0), // step 6
  1449. time.Unix(12390, 0), // step 6
  1450. time.Unix(12391, 0), // step 7
  1451. time.Unix(12392, 0), // step 7
  1452. time.Unix(12393, 0), // step 7
  1453. },
  1454. },
  1455. {
  1456. scheduleTimes: []time.Time{
  1457. time.Unix(100000, 0), // step 7
  1458. },
  1459. },
  1460. {
  1461. scheduleTimes: []time.Time{
  1462. time.Unix(12346, 0), // step 1
  1463. },
  1464. },
  1465. {
  1466. scheduleTimes: []time.Time{
  1467. time.Unix(12305, 0), // step 5
  1468. },
  1469. },
  1470. {
  1471. scheduleTimes: []time.Time{
  1472. time.Unix(12372, 0), // step 6
  1473. time.Unix(12374, 0), // step 6
  1474. time.Unix(12376, 0), // step 6
  1475. time.Unix(12386, 0), // step 6
  1476. time.Unix(12396, 0), // step 7
  1477. },
  1478. },
  1479. }
  1480. steps := []struct {
  1481. reschedule []int
  1482. stop []int
  1483. advanceTo time.Time
  1484. want map[int][]time.Time
  1485. waitingEvents int
  1486. }{
  1487. {
  1488. advanceTo: time.Unix(12345, 0),
  1489. },
  1490. {
  1491. reschedule: []int{0, 1, 2, 3, 4, 5}, // add 0, 1, 2, 3, 4, 5
  1492. advanceTo: time.Unix(12346, 0),
  1493. want: map[int][]time.Time{
  1494. 0: {
  1495. time.Unix(12300, 0),
  1496. time.Unix(12340, 0),
  1497. time.Unix(12345, 0),
  1498. time.Unix(12346, 0),
  1499. },
  1500. 5: {
  1501. time.Unix(12346, 0),
  1502. },
  1503. },
  1504. waitingEvents: 5, // scheduled 0, 1, 2, 3, 4, 5; retired 5
  1505. },
  1506. {
  1507. advanceTo: time.Unix(12346, 50),
  1508. waitingEvents: 5, // no change
  1509. },
  1510. {
  1511. advanceTo: time.Unix(12347, 50),
  1512. want: map[int][]time.Time{
  1513. 0: {
  1514. time.Unix(12347, 0),
  1515. },
  1516. },
  1517. waitingEvents: 5, // no change
  1518. },
  1519. {
  1520. advanceTo: time.Unix(12350, 50),
  1521. want: map[int][]time.Time{
  1522. 0: {
  1523. time.Unix(12348, 0),
  1524. time.Unix(12349, 0),
  1525. },
  1526. 1: {
  1527. time.Unix(12350, 0),
  1528. },
  1529. 2: {
  1530. time.Unix(12350, 1),
  1531. },
  1532. },
  1533. waitingEvents: 4, // retired 0
  1534. },
  1535. {
  1536. reschedule: []int{6, 7}, // add 6, 7
  1537. stop: []int{2},
  1538. advanceTo: time.Unix(12365, 0),
  1539. want: map[int][]time.Time{
  1540. 1: {
  1541. time.Unix(12360, 0),
  1542. },
  1543. 3: {
  1544. time.Unix(12355, 0),
  1545. time.Unix(12365, 0),
  1546. },
  1547. 6: {
  1548. time.Unix(12305, 0),
  1549. },
  1550. },
  1551. waitingEvents: 4, // scheduled 6, 7; retired 2, 5
  1552. },
  1553. {
  1554. reschedule: []int{1, 2}, // update 1; add 2
  1555. stop: []int{6},
  1556. advanceTo: time.Unix(12390, 0),
  1557. want: map[int][]time.Time{
  1558. 1: {
  1559. time.Unix(12380, 0),
  1560. time.Unix(12381, 0),
  1561. time.Unix(12382, 0),
  1562. },
  1563. 2: {
  1564. time.Unix(12370, 1),
  1565. time.Unix(12380, 1),
  1566. time.Unix(12381, 1),
  1567. time.Unix(12382, 1),
  1568. time.Unix(12383, 1),
  1569. },
  1570. 3: {
  1571. time.Unix(12370, 0),
  1572. time.Unix(12390, 0),
  1573. },
  1574. 7: {
  1575. time.Unix(12372, 0),
  1576. time.Unix(12374, 0),
  1577. time.Unix(12376, 0),
  1578. time.Unix(12386, 0),
  1579. },
  1580. },
  1581. waitingEvents: 3, // scheduled 2, retired 2, stopped 6
  1582. },
  1583. {
  1584. stop: []int{1}, // no-op: already stopped
  1585. advanceTo: time.Unix(200000, 0),
  1586. want: map[int][]time.Time{
  1587. 3: {
  1588. time.Unix(12391, 0),
  1589. time.Unix(12392, 0),
  1590. time.Unix(12393, 0),
  1591. },
  1592. 4: {
  1593. time.Unix(100000, 0),
  1594. },
  1595. 7: {
  1596. time.Unix(12396, 0),
  1597. },
  1598. },
  1599. waitingEvents: 0, // retired 3, 4, 7
  1600. },
  1601. {
  1602. advanceTo: time.Unix(300000, 0),
  1603. },
  1604. }
  1605. for i, step := range steps {
  1606. for _, idx := range step.reschedule {
  1607. ev := &testEvents[idx]
  1608. t := ev.scheduleTimes[0]
  1609. ev.scheduleTimes = ev.scheduleTimes[1:]
  1610. em.Reschedule(ev, t)
  1611. }
  1612. for _, idx := range step.stop {
  1613. ev := &testEvents[idx]
  1614. em.Reschedule(ev, time.Time{})
  1615. }
  1616. em.AdvanceTo(step.advanceTo)
  1617. for j := range testEvents {
  1618. if !slices.Equal(testEvents[j].fireTimes, step.want[j]) {
  1619. t.Errorf("step %v event %v fire times = %v, want %v", i, j, testEvents[j].fireTimes, step.want[j])
  1620. }
  1621. testEvents[j].fireTimes = nil
  1622. }
  1623. }
  1624. }
  1625. func TestClockFollowRealTime(t *testing.T) {
  1626. t.Parallel()
  1627. type advanceInfo struct {
  1628. when int
  1629. advanceTestClock time.Duration
  1630. advanceTestClockTo time.Time
  1631. advanceRealTimeClock time.Duration
  1632. }
  1633. tests := []struct {
  1634. name string
  1635. start time.Time
  1636. wantStart time.Time // This may differ from start when start.IsZero().
  1637. realTimeClockOpts ClockOpts
  1638. advances []advanceInfo
  1639. wants []time.Time // The return values of sequential calls to Now().
  1640. }{
  1641. {
  1642. name: "increment ms then advance 1s",
  1643. start: time.Unix(12345, 1000),
  1644. wantStart: time.Unix(12345, 1000),
  1645. advances: []advanceInfo{
  1646. {
  1647. when: 1,
  1648. advanceRealTimeClock: 1000,
  1649. },
  1650. {
  1651. when: 2,
  1652. advanceRealTimeClock: 1000,
  1653. },
  1654. {
  1655. when: 3,
  1656. advanceRealTimeClock: 1000,
  1657. },
  1658. {
  1659. when: 4,
  1660. advanceTestClock: time.Second,
  1661. },
  1662. {
  1663. when: 5,
  1664. advanceRealTimeClock: 1000,
  1665. },
  1666. {
  1667. when: 6,
  1668. advanceRealTimeClock: 1000,
  1669. },
  1670. {
  1671. when: 7,
  1672. advanceRealTimeClock: 1000,
  1673. },
  1674. },
  1675. wants: []time.Time{
  1676. time.Unix(12345, 1000),
  1677. time.Unix(12345, 2000),
  1678. time.Unix(12345, 3000),
  1679. time.Unix(12345, 4000),
  1680. time.Unix(12346, 4000),
  1681. time.Unix(12346, 5000),
  1682. time.Unix(12346, 6000),
  1683. time.Unix(12346, 7000),
  1684. },
  1685. },
  1686. {
  1687. name: "multiple advances over time",
  1688. start: time.Unix(12345, 1000),
  1689. wantStart: time.Unix(12345, 1000),
  1690. advances: []advanceInfo{
  1691. {
  1692. when: 1,
  1693. advanceRealTimeClock: 1,
  1694. },
  1695. {
  1696. when: 2,
  1697. advanceTestClock: time.Second,
  1698. },
  1699. {
  1700. when: 3,
  1701. advanceRealTimeClock: 1,
  1702. },
  1703. {
  1704. when: 4,
  1705. advanceTestClock: 0,
  1706. },
  1707. {
  1708. when: 5,
  1709. advanceRealTimeClock: 1,
  1710. },
  1711. {
  1712. when: 6,
  1713. advanceTestClock: 1000,
  1714. },
  1715. {
  1716. when: 7,
  1717. advanceRealTimeClock: 1,
  1718. },
  1719. },
  1720. wants: []time.Time{
  1721. time.Unix(12345, 1000),
  1722. time.Unix(12345, 1001),
  1723. time.Unix(12346, 1001),
  1724. time.Unix(12346, 1002),
  1725. time.Unix(12346, 1002),
  1726. time.Unix(12346, 1003),
  1727. time.Unix(12346, 2003),
  1728. time.Unix(12346, 2004),
  1729. },
  1730. },
  1731. {
  1732. name: "multiple advances at once",
  1733. start: time.Unix(12345, 1000),
  1734. wantStart: time.Unix(12345, 1000),
  1735. advances: []advanceInfo{
  1736. {
  1737. when: 1,
  1738. advanceRealTimeClock: 1,
  1739. },
  1740. {
  1741. when: 2,
  1742. advanceTestClock: time.Second,
  1743. },
  1744. {
  1745. when: 2,
  1746. advanceTestClock: 0,
  1747. },
  1748. {
  1749. when: 2,
  1750. advanceTestClock: 1000,
  1751. },
  1752. {
  1753. when: 3,
  1754. advanceRealTimeClock: 1,
  1755. },
  1756. },
  1757. wants: []time.Time{
  1758. time.Unix(12345, 1000),
  1759. time.Unix(12345, 1001),
  1760. time.Unix(12346, 2001),
  1761. time.Unix(12346, 2002),
  1762. },
  1763. },
  1764. {
  1765. name: "changes at start",
  1766. start: time.Unix(12345, 1000),
  1767. wantStart: time.Unix(12345, 1000),
  1768. advances: []advanceInfo{
  1769. {
  1770. when: 0,
  1771. advanceTestClock: time.Second,
  1772. },
  1773. {
  1774. when: 0,
  1775. advanceTestClock: 1000,
  1776. },
  1777. {
  1778. when: 1,
  1779. advanceRealTimeClock: 5,
  1780. },
  1781. {
  1782. when: 2,
  1783. advanceRealTimeClock: 5,
  1784. },
  1785. {
  1786. when: 3,
  1787. advanceRealTimeClock: 5,
  1788. },
  1789. },
  1790. wants: []time.Time{
  1791. time.Unix(12346, 2000),
  1792. time.Unix(12346, 2005),
  1793. time.Unix(12346, 2010),
  1794. time.Unix(12346, 2015),
  1795. },
  1796. },
  1797. {
  1798. name: "start from current time",
  1799. realTimeClockOpts: ClockOpts{
  1800. Start: time.Unix(12345, 0),
  1801. },
  1802. wantStart: time.Unix(12345, 0),
  1803. advances: []advanceInfo{
  1804. {
  1805. when: 1,
  1806. advanceTestClock: time.Second,
  1807. },
  1808. {
  1809. when: 2,
  1810. advanceRealTimeClock: 10 * time.Second,
  1811. },
  1812. {
  1813. when: 3,
  1814. advanceTestClock: time.Minute,
  1815. },
  1816. {
  1817. when: 4,
  1818. advanceRealTimeClock: time.Hour,
  1819. },
  1820. {
  1821. when: 5,
  1822. advanceTestClockTo: time.Unix(100, 0),
  1823. },
  1824. {
  1825. when: 6,
  1826. advanceRealTimeClock: time.Hour,
  1827. },
  1828. },
  1829. wants: []time.Time{
  1830. time.Unix(12345, 0),
  1831. time.Unix(12346, 0),
  1832. time.Unix(12356, 0),
  1833. time.Unix(12416, 0),
  1834. time.Unix(16016, 0),
  1835. time.Unix(100, 0),
  1836. time.Unix(3700, 0),
  1837. },
  1838. },
  1839. }
  1840. for _, tt := range tests {
  1841. tt := tt
  1842. t.Run(tt.name, func(t *testing.T) {
  1843. t.Parallel()
  1844. realTimeClock := NewClock(tt.realTimeClockOpts)
  1845. clock := newClockInternal(ClockOpts{
  1846. Start: tt.start,
  1847. FollowRealTime: true,
  1848. }, realTimeClock)
  1849. changeIndex := 0
  1850. for i := range tt.wants {
  1851. for len(tt.advances) > changeIndex && tt.advances[changeIndex].when == i {
  1852. advance := tt.advances[changeIndex]
  1853. if advance.advanceTestClockTo.IsZero() {
  1854. clock.Advance(advance.advanceTestClock)
  1855. } else {
  1856. clock.AdvanceTo(advance.advanceTestClockTo)
  1857. }
  1858. realTimeClock.Advance(advance.advanceRealTimeClock)
  1859. changeIndex++
  1860. }
  1861. if start := clock.GetStart(); !start.Equal(tt.wantStart) {
  1862. t.Errorf("clock has start %v, want %v", start, tt.wantStart)
  1863. }
  1864. if got := clock.Now(); !got.Equal(tt.wants[i]) {
  1865. t.Errorf("step %v: clock.Now() = %v, want %v", i, got, tt.wants[i])
  1866. }
  1867. if got := clock.PeekNow(); !got.Equal(tt.wants[i]) {
  1868. t.Errorf("step %v: clock.PeekNow() = %v, want %v", i, got, tt.wants[i])
  1869. }
  1870. }
  1871. })
  1872. }
  1873. }
  1874. func TestAfterFunc(t *testing.T) {
  1875. t.Parallel()
  1876. type testStep struct {
  1877. stop bool
  1878. stopReturn bool // The expected return value for Stop() if stop is true.
  1879. reset time.Duration
  1880. resetAbsolute time.Time
  1881. resetReturn bool // The expected return value for Reset() or ResetAbsolute().
  1882. setStep time.Duration
  1883. advance time.Duration
  1884. advanceRealTime time.Duration
  1885. wantTime time.Time
  1886. wantTick bool
  1887. }
  1888. tests := []struct {
  1889. name string
  1890. realTimeOpts *ClockOpts
  1891. start time.Time
  1892. step time.Duration
  1893. delay time.Duration
  1894. steps []testStep
  1895. }{
  1896. {
  1897. name: "no tick advance",
  1898. start: time.Unix(12345, 0),
  1899. delay: time.Second,
  1900. steps: []testStep{
  1901. {
  1902. advance: time.Second - 1,
  1903. wantTime: time.Unix(12345, 999_999_999),
  1904. },
  1905. },
  1906. },
  1907. {
  1908. name: "no tick step",
  1909. start: time.Unix(12345, 0),
  1910. step: time.Second - 1,
  1911. delay: time.Second,
  1912. steps: []testStep{
  1913. {
  1914. wantTime: time.Unix(12345, 0),
  1915. },
  1916. {
  1917. wantTime: time.Unix(12345, 999_999_999),
  1918. },
  1919. },
  1920. },
  1921. {
  1922. name: "single tick advance exact",
  1923. start: time.Unix(12345, 0),
  1924. delay: time.Second,
  1925. steps: []testStep{
  1926. {
  1927. advance: time.Second,
  1928. wantTime: time.Unix(12346, 0),
  1929. wantTick: true,
  1930. },
  1931. {
  1932. advance: time.Second,
  1933. wantTime: time.Unix(12347, 0),
  1934. },
  1935. },
  1936. },
  1937. {
  1938. name: "single tick advance extra",
  1939. start: time.Unix(12345, 0),
  1940. delay: time.Second,
  1941. steps: []testStep{
  1942. {
  1943. advance: time.Second + 1,
  1944. wantTime: time.Unix(12346, 1),
  1945. wantTick: true,
  1946. },
  1947. {
  1948. advance: time.Second,
  1949. wantTime: time.Unix(12347, 1),
  1950. },
  1951. },
  1952. },
  1953. {
  1954. name: "single tick step exact",
  1955. start: time.Unix(12345, 0),
  1956. step: time.Second,
  1957. delay: time.Second,
  1958. steps: []testStep{
  1959. {
  1960. wantTime: time.Unix(12345, 0),
  1961. },
  1962. {
  1963. wantTime: time.Unix(12346, 0),
  1964. wantTick: true,
  1965. },
  1966. {
  1967. wantTime: time.Unix(12347, 0),
  1968. },
  1969. },
  1970. },
  1971. {
  1972. name: "single tick step extra",
  1973. start: time.Unix(12345, 0),
  1974. step: time.Second + 1,
  1975. delay: time.Second,
  1976. steps: []testStep{
  1977. {
  1978. wantTime: time.Unix(12345, 0),
  1979. },
  1980. {
  1981. wantTime: time.Unix(12346, 1),
  1982. wantTick: true,
  1983. },
  1984. {
  1985. wantTime: time.Unix(12347, 2),
  1986. },
  1987. },
  1988. },
  1989. {
  1990. name: "reset for single tick per advance",
  1991. start: time.Unix(12345, 0),
  1992. delay: 3 * time.Second,
  1993. steps: []testStep{
  1994. {
  1995. wantTime: time.Unix(12345, 0),
  1996. },
  1997. {
  1998. advance: 4 * time.Second,
  1999. wantTime: time.Unix(12349, 0),
  2000. wantTick: true,
  2001. },
  2002. {
  2003. resetAbsolute: time.Unix(12351, 0),
  2004. advance: 2 * time.Second,
  2005. wantTime: time.Unix(12351, 0),
  2006. wantTick: true,
  2007. },
  2008. {
  2009. reset: 3 * time.Second,
  2010. advance: 2 * time.Second,
  2011. wantTime: time.Unix(12353, 0),
  2012. },
  2013. {
  2014. advance: 2 * time.Second,
  2015. wantTime: time.Unix(12355, 0),
  2016. wantTick: true,
  2017. },
  2018. {
  2019. advance: 10 * time.Second,
  2020. wantTime: time.Unix(12365, 0),
  2021. },
  2022. },
  2023. },
  2024. {
  2025. name: "reset for single tick per step",
  2026. start: time.Unix(12345, 0),
  2027. step: 2 * time.Second,
  2028. delay: 3 * time.Second,
  2029. steps: []testStep{
  2030. {
  2031. wantTime: time.Unix(12345, 0),
  2032. },
  2033. {
  2034. wantTime: time.Unix(12347, 0),
  2035. },
  2036. {
  2037. wantTime: time.Unix(12349, 0),
  2038. wantTick: true,
  2039. },
  2040. {
  2041. reset: time.Second,
  2042. wantTime: time.Unix(12351, 0),
  2043. wantTick: true,
  2044. },
  2045. {
  2046. resetAbsolute: time.Unix(12354, 0),
  2047. wantTime: time.Unix(12353, 0),
  2048. },
  2049. {
  2050. wantTime: time.Unix(12355, 0),
  2051. wantTick: true,
  2052. },
  2053. },
  2054. },
  2055. {
  2056. name: "reset while active",
  2057. start: time.Unix(12345, 0),
  2058. step: 2 * time.Second,
  2059. delay: 3 * time.Second,
  2060. steps: []testStep{
  2061. {
  2062. wantTime: time.Unix(12345, 0),
  2063. },
  2064. {
  2065. wantTime: time.Unix(12347, 0),
  2066. },
  2067. {
  2068. reset: 3 * time.Second,
  2069. resetReturn: true,
  2070. wantTime: time.Unix(12349, 0),
  2071. },
  2072. {
  2073. resetAbsolute: time.Unix(12354, 0),
  2074. resetReturn: true,
  2075. wantTime: time.Unix(12351, 0),
  2076. },
  2077. {
  2078. wantTime: time.Unix(12353, 0),
  2079. },
  2080. {
  2081. wantTime: time.Unix(12355, 0),
  2082. wantTick: true,
  2083. },
  2084. },
  2085. },
  2086. {
  2087. name: "stop after fire",
  2088. start: time.Unix(12345, 0),
  2089. step: 2 * time.Second,
  2090. delay: time.Second,
  2091. steps: []testStep{
  2092. {
  2093. wantTime: time.Unix(12345, 0),
  2094. },
  2095. {
  2096. wantTime: time.Unix(12347, 0),
  2097. wantTick: true,
  2098. },
  2099. {
  2100. stop: true,
  2101. wantTime: time.Unix(12349, 0),
  2102. },
  2103. {
  2104. wantTime: time.Unix(12351, 0),
  2105. },
  2106. {
  2107. advance: 10 * time.Second,
  2108. wantTime: time.Unix(12361, 0),
  2109. },
  2110. },
  2111. },
  2112. {
  2113. name: "stop before fire",
  2114. start: time.Unix(12345, 0),
  2115. step: 2 * time.Second,
  2116. delay: time.Second,
  2117. steps: []testStep{
  2118. {
  2119. stop: true,
  2120. stopReturn: true,
  2121. wantTime: time.Unix(12345, 0),
  2122. },
  2123. {
  2124. wantTime: time.Unix(12347, 0),
  2125. },
  2126. {
  2127. wantTime: time.Unix(12349, 0),
  2128. },
  2129. {
  2130. wantTime: time.Unix(12351, 0),
  2131. },
  2132. {
  2133. advance: 10 * time.Second,
  2134. wantTime: time.Unix(12361, 0),
  2135. },
  2136. },
  2137. },
  2138. {
  2139. name: "stop after reset",
  2140. start: time.Unix(12345, 0),
  2141. step: 2 * time.Second,
  2142. delay: time.Second,
  2143. steps: []testStep{
  2144. {
  2145. wantTime: time.Unix(12345, 0),
  2146. },
  2147. {
  2148. wantTime: time.Unix(12347, 0),
  2149. wantTick: true,
  2150. },
  2151. {
  2152. reset: 10 * time.Second,
  2153. wantTime: time.Unix(12349, 0),
  2154. },
  2155. {
  2156. stop: true,
  2157. stopReturn: true,
  2158. wantTime: time.Unix(12351, 0),
  2159. },
  2160. {
  2161. advance: 10 * time.Second,
  2162. wantTime: time.Unix(12361, 0),
  2163. },
  2164. },
  2165. },
  2166. {
  2167. name: "reset while running",
  2168. start: time.Unix(12345, 0),
  2169. delay: 2 * time.Second,
  2170. steps: []testStep{
  2171. {
  2172. wantTime: time.Unix(12345, 0),
  2173. },
  2174. {
  2175. advance: time.Second,
  2176. wantTime: time.Unix(12346, 0),
  2177. },
  2178. {
  2179. advance: time.Second,
  2180. wantTime: time.Unix(12347, 0),
  2181. wantTick: true,
  2182. },
  2183. {
  2184. advance: time.Second,
  2185. reset: time.Second,
  2186. wantTime: time.Unix(12348, 0),
  2187. wantTick: true,
  2188. },
  2189. {
  2190. setStep: 5 * time.Second,
  2191. reset: 10 * time.Second,
  2192. wantTime: time.Unix(12353, 0),
  2193. },
  2194. {
  2195. wantTime: time.Unix(12358, 0),
  2196. wantTick: true,
  2197. },
  2198. },
  2199. },
  2200. {
  2201. name: "reset while stopped",
  2202. start: time.Unix(12345, 0),
  2203. step: time.Second,
  2204. delay: 2 * time.Second,
  2205. steps: []testStep{
  2206. {
  2207. wantTime: time.Unix(12345, 0),
  2208. },
  2209. {
  2210. wantTime: time.Unix(12346, 0),
  2211. },
  2212. {
  2213. stop: true,
  2214. stopReturn: true,
  2215. wantTime: time.Unix(12347, 0),
  2216. },
  2217. {
  2218. wantTime: time.Unix(12348, 0),
  2219. },
  2220. {
  2221. wantTime: time.Unix(12349, 0),
  2222. },
  2223. {
  2224. reset: time.Second,
  2225. wantTime: time.Unix(12350, 0),
  2226. wantTick: true,
  2227. },
  2228. {
  2229. wantTime: time.Unix(12351, 0),
  2230. },
  2231. },
  2232. },
  2233. {
  2234. name: "reset absolute",
  2235. start: time.Unix(12345, 0),
  2236. step: time.Second,
  2237. delay: 2 * time.Second,
  2238. steps: []testStep{
  2239. {
  2240. wantTime: time.Unix(12345, 0),
  2241. },
  2242. {
  2243. wantTime: time.Unix(12346, 0),
  2244. },
  2245. {
  2246. wantTime: time.Unix(12347, 0),
  2247. wantTick: true,
  2248. },
  2249. {
  2250. resetAbsolute: time.Unix(12354, 50),
  2251. advance: 7 * time.Second,
  2252. wantTime: time.Unix(12354, 0),
  2253. },
  2254. {
  2255. wantTime: time.Unix(12355, 0),
  2256. wantTick: true,
  2257. },
  2258. {
  2259. wantTime: time.Unix(12356, 0),
  2260. },
  2261. },
  2262. },
  2263. {
  2264. name: "follow real time",
  2265. realTimeOpts: new(ClockOpts),
  2266. start: time.Unix(12345, 0),
  2267. delay: 2 * time.Second,
  2268. steps: []testStep{
  2269. {
  2270. wantTime: time.Unix(12345, 0),
  2271. },
  2272. {
  2273. advanceRealTime: 5 * time.Second,
  2274. wantTime: time.Unix(12350, 0),
  2275. wantTick: true,
  2276. },
  2277. {
  2278. reset: 2 * time.Second,
  2279. advance: 5 * time.Second,
  2280. wantTime: time.Unix(12355, 0),
  2281. wantTick: true,
  2282. },
  2283. },
  2284. },
  2285. }
  2286. for _, tt := range tests {
  2287. tt := tt
  2288. t.Run(tt.name, func(t *testing.T) {
  2289. t.Parallel()
  2290. var realTimeClockForTestClock tstime.Clock
  2291. var realTimeClock *Clock
  2292. if tt.realTimeOpts != nil {
  2293. realTimeClock = NewClock(*tt.realTimeOpts)
  2294. // Passing realTimeClock into newClockInternal results in a
  2295. // non-nil interface with a nil pointer, so this is necessary.
  2296. realTimeClockForTestClock = realTimeClock
  2297. }
  2298. var gotTick atomic.Bool
  2299. clock := newClockInternal(ClockOpts{
  2300. Start: tt.start,
  2301. Step: tt.step,
  2302. FollowRealTime: realTimeClock != nil,
  2303. }, realTimeClockForTestClock)
  2304. tc := clock.AfterFunc(tt.delay, func() {
  2305. if gotTick.Swap(true) == true {
  2306. t.Error("multiple ticks detected")
  2307. }
  2308. })
  2309. timerControl := tc.(*Timer)
  2310. t.Cleanup(func() { timerControl.Stop() })
  2311. if gotTick.Load() {
  2312. t.Error("initial tick detected, want none")
  2313. }
  2314. for i, step := range tt.steps {
  2315. if step.stop {
  2316. if got := timerControl.Stop(); got != step.stopReturn {
  2317. t.Errorf("step %v Stop returned %v, want %v", i, got, step.stopReturn)
  2318. }
  2319. }
  2320. if !step.resetAbsolute.IsZero() {
  2321. if got := timerControl.ResetAbsolute(step.resetAbsolute); got != step.resetReturn {
  2322. t.Errorf("step %v Reset returned %v, want %v", i, got, step.resetReturn)
  2323. }
  2324. }
  2325. if step.reset > 0 {
  2326. if got := timerControl.Reset(step.reset); got != step.resetReturn {
  2327. t.Errorf("step %v Reset returned %v, want %v", i, got, step.resetReturn)
  2328. }
  2329. }
  2330. if step.setStep > 0 {
  2331. clock.SetStep(step.setStep)
  2332. }
  2333. if step.advance > 0 {
  2334. clock.Advance(step.advance)
  2335. }
  2336. if step.advanceRealTime > 0 {
  2337. realTimeClock.Advance(step.advanceRealTime)
  2338. }
  2339. if now := clock.Now(); !step.wantTime.IsZero() && !now.Equal(step.wantTime) {
  2340. t.Errorf("step %v now = %v, want %v", i, now, step.wantTime)
  2341. }
  2342. if got := gotTick.Swap(false); got != step.wantTick {
  2343. t.Errorf("step %v tick %v, want %v", i, got, step.wantTick)
  2344. }
  2345. }
  2346. })
  2347. }
  2348. }
  2349. func TestSince(t *testing.T) {
  2350. t.Parallel()
  2351. tests := []struct {
  2352. name string
  2353. start time.Time
  2354. since time.Time
  2355. want time.Duration
  2356. }{
  2357. {
  2358. name: "positive",
  2359. start: time.Unix(12345, 1000),
  2360. since: time.Unix(11111, 1000),
  2361. want: 1234 * time.Second,
  2362. },
  2363. {
  2364. name: "negative",
  2365. start: time.Unix(12345, 1000),
  2366. since: time.Unix(15436, 1000),
  2367. want: -3091 * time.Second,
  2368. },
  2369. {
  2370. name: "zero",
  2371. start: time.Unix(12345, 1000),
  2372. since: time.Unix(12345, 1000),
  2373. want: 0,
  2374. },
  2375. }
  2376. for _, tt := range tests {
  2377. tt := tt
  2378. t.Run(tt.name, func(t *testing.T) {
  2379. t.Parallel()
  2380. clock := NewClock(ClockOpts{
  2381. Start: tt.start,
  2382. })
  2383. got := clock.Since(tt.since)
  2384. if got != tt.want {
  2385. t.Errorf("Since duration %v, want %v", got, tt.want)
  2386. }
  2387. })
  2388. }
  2389. }