clock_test.go 51 KB

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