window_test.go 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. package kubecost
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/pkg/util/timeutil"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/opencost/opencost/pkg/env"
  9. )
  10. func TestRoundBack(t *testing.T) {
  11. boulder := time.FixedZone("Boulder", -7*60*60)
  12. beijing := time.FixedZone("Beijing", 8*60*60)
  13. to := time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)
  14. tb := RoundBack(to, 24*time.Hour)
  15. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  16. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  17. }
  18. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, boulder)
  19. tb = RoundBack(to, 24*time.Hour)
  20. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  21. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  22. }
  23. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, boulder)
  24. tb = RoundBack(to, 24*time.Hour)
  25. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  26. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  27. }
  28. to = time.Date(2020, time.January, 1, 23, 37, 48, 0, boulder)
  29. tb = RoundBack(to, 24*time.Hour)
  30. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  31. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  32. }
  33. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)
  34. tb = RoundBack(to, 24*time.Hour)
  35. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  36. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  37. }
  38. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, beijing)
  39. tb = RoundBack(to, 24*time.Hour)
  40. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  41. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  42. }
  43. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, beijing)
  44. tb = RoundBack(to, 24*time.Hour)
  45. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  46. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  47. }
  48. to = time.Date(2020, time.January, 1, 23, 59, 59, 0, beijing)
  49. tb = RoundBack(to, 24*time.Hour)
  50. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  51. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  52. }
  53. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
  54. tb = RoundBack(to, 24*time.Hour)
  55. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  56. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  57. }
  58. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, time.UTC)
  59. tb = RoundBack(to, 24*time.Hour)
  60. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  61. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  62. }
  63. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, time.UTC)
  64. tb = RoundBack(to, 24*time.Hour)
  65. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  66. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  67. }
  68. to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
  69. tb = RoundBack(to, 24*time.Hour)
  70. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  71. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  72. }
  73. }
  74. func TestRoundForward(t *testing.T) {
  75. boulder := time.FixedZone("Boulder", -7*60*60)
  76. beijing := time.FixedZone("Beijing", 8*60*60)
  77. to := time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)
  78. tb := RoundForward(to, 24*time.Hour)
  79. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  80. t.Fatalf("RoundForward: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  81. }
  82. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, boulder)
  83. tb = RoundForward(to, 24*time.Hour)
  84. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
  85. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
  86. }
  87. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, boulder)
  88. tb = RoundForward(to, 24*time.Hour)
  89. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
  90. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
  91. }
  92. to = time.Date(2020, time.January, 1, 23, 37, 48, 0, boulder)
  93. tb = RoundForward(to, 24*time.Hour)
  94. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
  95. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
  96. }
  97. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)
  98. tb = RoundForward(to, 24*time.Hour)
  99. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  100. t.Fatalf("RoundForward: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  101. }
  102. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, beijing)
  103. tb = RoundForward(to, 24*time.Hour)
  104. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
  105. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
  106. }
  107. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, beijing)
  108. tb = RoundForward(to, 24*time.Hour)
  109. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
  110. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
  111. }
  112. to = time.Date(2020, time.January, 1, 23, 59, 59, 0, beijing)
  113. tb = RoundForward(to, 24*time.Hour)
  114. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
  115. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
  116. }
  117. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
  118. tb = RoundForward(to, 24*time.Hour)
  119. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  120. t.Fatalf("RoundForward: expected 2020-01-01T00:00:00Z; actual %s", tb)
  121. }
  122. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, time.UTC)
  123. tb = RoundForward(to, 24*time.Hour)
  124. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
  125. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
  126. }
  127. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, time.UTC)
  128. tb = RoundForward(to, 24*time.Hour)
  129. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
  130. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
  131. }
  132. to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
  133. tb = RoundForward(to, 24*time.Hour)
  134. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
  135. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
  136. }
  137. }
  138. func TestParseWindowUTC(t *testing.T) {
  139. now := time.Now().UTC()
  140. // "today" should span Now() and not produce an error
  141. today, err := ParseWindowUTC("today")
  142. if err != nil {
  143. t.Fatalf(`unexpected error parsing "today": %s`, err)
  144. }
  145. if today.Duration().Hours() != 24 {
  146. t.Fatalf(`expect: window "today" to have duration 24 hour; actual: %f hours`, today.Duration().Hours())
  147. }
  148. if !today.Contains(time.Now().UTC()) {
  149. t.Fatalf(`expect: window "today" to contain now; actual: %s`, today)
  150. }
  151. // "yesterday" should span Now() and not produce an error
  152. yesterday, err := ParseWindowUTC("yesterday")
  153. if err != nil {
  154. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  155. }
  156. if yesterday.Duration().Hours() != 24 {
  157. t.Fatalf(`expect: window "yesterday" to have duration 24 hour; actual: %f hours`, yesterday.Duration().Hours())
  158. }
  159. if !yesterday.End().Before(time.Now().UTC()) {
  160. t.Fatalf(`expect: window "yesterday" to end before now; actual: %s ends after %s`, yesterday, time.Now().UTC())
  161. }
  162. week, err := ParseWindowUTC("week")
  163. hoursThisWeek := float64(time.Now().UTC().Weekday()) * 24.0
  164. if err != nil {
  165. t.Fatalf(`unexpected error parsing "week": %s`, err)
  166. }
  167. if week.Duration().Hours() < hoursThisWeek {
  168. t.Fatalf(`expect: window "week" to have at least %f hours; actual: %f hours`, hoursThisWeek, week.Duration().Hours())
  169. }
  170. if week.End().After(time.Now().UTC()) {
  171. t.Fatalf(`expect: window "week" to end before now; actual: %s ends after %s`, week, time.Now().UTC())
  172. }
  173. month, err := ParseWindowUTC("month")
  174. hoursThisMonth := float64(time.Now().UTC().Day()) * 24.0
  175. if err != nil {
  176. t.Fatalf(`unexpected error parsing "month": %s`, err)
  177. }
  178. if month.Duration().Hours() > hoursThisMonth || month.Duration().Hours() < (hoursThisMonth-24.0) {
  179. t.Fatalf(`expect: window "month" to have approximately %f hours; actual: %f hours`, hoursThisMonth, month.Duration().Hours())
  180. }
  181. if !month.End().Before(time.Now().UTC()) {
  182. t.Fatalf(`expect: window "month" to end before now; actual: %s ends after %s`, month, time.Now().UTC())
  183. }
  184. // TODO lastweek
  185. lastmonth, err := ParseWindowUTC("lastmonth")
  186. monthMinHours := float64(24 * 28)
  187. monthMaxHours := float64(24 * 31)
  188. firstOfMonth := now.Truncate(time.Hour * 24).Add(-24 * time.Hour * time.Duration(now.Day()-1))
  189. if err != nil {
  190. t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
  191. }
  192. if lastmonth.Duration().Hours() > monthMaxHours || lastmonth.Duration().Hours() < monthMinHours {
  193. t.Fatalf(`expect: window "lastmonth" to have approximately %f hours; actual: %f hours`, hoursThisMonth, lastmonth.Duration().Hours())
  194. }
  195. if !lastmonth.End().Equal(firstOfMonth) {
  196. t.Fatalf(`expect: window "lastmonth" to end on the first of the current month; actual: %s doesn't end on %s`, lastmonth, firstOfMonth)
  197. }
  198. ago12h := time.Now().UTC().Add(-12 * time.Hour)
  199. ago36h := time.Now().UTC().Add(-36 * time.Hour)
  200. ago60h := time.Now().UTC().Add(-60 * time.Hour)
  201. // "24h" should have 24 hour duration and not produce an error
  202. dur24h, err := ParseWindowUTC("24h")
  203. if err != nil {
  204. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  205. }
  206. if dur24h.Duration().Hours() != 24 {
  207. t.Fatalf(`expect: window "24h" to have duration 24 hour; actual: %f hours`, dur24h.Duration().Hours())
  208. }
  209. if !dur24h.Contains(ago12h) {
  210. t.Fatalf(`expect: window "24h" to contain 12 hours ago; actual: %s doesn't contain %s`, dur24h, ago12h)
  211. }
  212. if dur24h.Contains(ago36h) {
  213. t.Fatalf(`expect: window "24h" to not contain 36 hours ago; actual: %s contains %s`, dur24h, ago36h)
  214. }
  215. // "2d" should have 2 day duration and not produce an error
  216. dur2d, err := ParseWindowUTC("2d")
  217. if err != nil {
  218. t.Fatalf(`unexpected error parsing "2d": %s`, err)
  219. }
  220. if dur2d.Duration().Hours() != 48 {
  221. t.Fatalf(`expect: window "2d" to have duration 48 hour; actual: %f hours`, dur2d.Duration().Hours())
  222. }
  223. if !dur2d.Contains(ago36h) {
  224. t.Fatalf(`expect: window "2d" to contain 36 hours ago; actual: %s doesn't contain %s`, dur2d, ago36h)
  225. }
  226. if dur2d.Contains(ago60h) {
  227. t.Fatalf(`expect: window "2d" to not contain 60 hours ago; actual: %s contains %s`, dur2d, ago60h)
  228. }
  229. // "24h offset 14h" should have 24 hour duration and not produce an error
  230. dur24hOff14h, err := ParseWindowUTC("24h offset 14h")
  231. if err != nil {
  232. t.Fatalf(`unexpected error parsing "24h offset 14h": %s`, err)
  233. }
  234. if dur24hOff14h.Duration().Hours() != 24 {
  235. t.Fatalf(`expect: window "24h offset 14h" to have duration 24 hour; actual: %f hours`, dur24hOff14h.Duration().Hours())
  236. }
  237. if dur24hOff14h.Contains(ago12h) {
  238. t.Fatalf(`expect: window "24h offset 14h" not to contain 12 hours ago; actual: %s contains %s`, dur24hOff14h, ago12h)
  239. }
  240. if !dur24hOff14h.Contains(ago36h) {
  241. t.Fatalf(`expect: window "24h offset 14h" to contain 36 hours ago; actual: %s does not contain %s`, dur24hOff14h, ago36h)
  242. }
  243. april152020, _ := time.Parse(time.RFC3339, "2020-04-15T00:00:00Z")
  244. april102020, _ := time.Parse(time.RFC3339, "2020-04-10T00:00:00Z")
  245. april052020, _ := time.Parse(time.RFC3339, "2020-04-05T00:00:00Z")
  246. // "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z" should have 96 hour duration and not produce an error
  247. april8to12, err := ParseWindowUTC("2020-04-08T00:00:00Z,2020-04-12T00:00:00Z")
  248. if err != nil {
  249. t.Fatalf(`unexpected error parsing "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z": %s`, err)
  250. }
  251. if april8to12.Duration().Hours() != 96 {
  252. t.Fatalf(`expect: window %s to have duration 96 hour; actual: %f hours`, april8to12, april8to12.Duration().Hours())
  253. }
  254. if !april8to12.Contains(april102020) {
  255. t.Fatalf(`expect: window April 8-12 to contain April 10; actual: %s doesn't contain %s`, april8to12, april102020)
  256. }
  257. if april8to12.Contains(april052020) {
  258. t.Fatalf(`expect: window April 8-12 to not contain April 5; actual: %s contains %s`, april8to12, april052020)
  259. }
  260. if april8to12.Contains(april152020) {
  261. t.Fatalf(`expect: window April 8-12 to not contain April 15; actual: %s contains %s`, april8to12, april152020)
  262. }
  263. march152020, _ := time.Parse(time.RFC3339, "2020-03-15T00:00:00Z")
  264. march102020, _ := time.Parse(time.RFC3339, "2020-03-10T00:00:00Z")
  265. march052020, _ := time.Parse(time.RFC3339, "2020-03-05T00:00:00Z")
  266. // "1583712000,1583884800" should have 48 hour duration and not produce an error
  267. march9to11, err := ParseWindowUTC("1583712000,1583884800")
  268. if err != nil {
  269. t.Fatalf(`unexpected error parsing "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z": %s`, err)
  270. }
  271. if march9to11.Duration().Hours() != 48 {
  272. t.Fatalf(`expect: window %s to have duration 48 hour; actual: %f hours`, march9to11, march9to11.Duration().Hours())
  273. }
  274. if !march9to11.Contains(march102020) {
  275. t.Fatalf(`expect: window March 9-11 to contain March 10; actual: %s doesn't contain %s`, march9to11, march102020)
  276. }
  277. if march9to11.Contains(march052020) {
  278. t.Fatalf(`expect: window March 9-11 to not contain March 5; actual: %s contains %s`, march9to11, march052020)
  279. }
  280. if march9to11.Contains(march152020) {
  281. t.Fatalf(`expect: window March 9-11 to not contain March 15; actual: %s contains %s`, march9to11, march152020)
  282. }
  283. }
  284. func BenchmarkParseWindowUTC(b *testing.B) {
  285. for n := 0; n < b.N; n++ {
  286. _, err := ParseWindowUTC("2020-04-08T00:00:00Z,2020-04-12T00:00:00Z")
  287. if err != nil {
  288. b.Fatalf("error running benchmark: %s", err.Error())
  289. }
  290. }
  291. }
  292. func TestParseWindowWithOffsetString(t *testing.T) {
  293. // ParseWindowWithOffsetString should equal ParseWindowUTC when location == "UTC"
  294. // for all window string formats
  295. todayUTC, err := ParseWindowUTC("today")
  296. if err != nil {
  297. t.Fatalf(`unexpected error parsing "today": %s`, err)
  298. }
  299. todayTZ, err := ParseWindowWithOffsetString("today", "UTC")
  300. if err != nil {
  301. t.Fatalf(`unexpected error parsing "today": %s`, err)
  302. }
  303. if !todayUTC.ApproximatelyEqual(todayTZ, time.Millisecond) {
  304. t.Fatalf(`expect: window "today" UTC to equal "today" with timezone "UTC"; actual: %s not equal %s`, todayUTC, todayTZ)
  305. }
  306. yesterdayUTC, err := ParseWindowUTC("yesterday")
  307. if err != nil {
  308. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  309. }
  310. yesterdayTZ, err := ParseWindowWithOffsetString("yesterday", "UTC")
  311. if err != nil {
  312. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  313. }
  314. if !yesterdayUTC.ApproximatelyEqual(yesterdayTZ, time.Millisecond) {
  315. t.Fatalf(`expect: window "yesterday" UTC to equal "yesterday" with timezone "UTC"; actual: %s not equal %s`, yesterdayUTC, yesterdayTZ)
  316. }
  317. weekUTC, err := ParseWindowUTC("week")
  318. if err != nil {
  319. t.Fatalf(`unexpected error parsing "week": %s`, err)
  320. }
  321. weekTZ, err := ParseWindowWithOffsetString("week", "UTC")
  322. if err != nil {
  323. t.Fatalf(`unexpected error parsing "week": %s`, err)
  324. }
  325. if !weekUTC.ApproximatelyEqual(weekTZ, time.Millisecond) {
  326. t.Fatalf(`expect: window "week" UTC to equal "week" with timezone "UTC"; actual: %s not equal %s`, weekUTC, weekTZ)
  327. }
  328. monthUTC, err := ParseWindowUTC("month")
  329. if err != nil {
  330. t.Fatalf(`unexpected error parsing "month": %s`, err)
  331. }
  332. monthTZ, err := ParseWindowWithOffsetString("month", "UTC")
  333. if err != nil {
  334. t.Fatalf(`unexpected error parsing "month": %s`, err)
  335. }
  336. if !monthUTC.ApproximatelyEqual(monthTZ, time.Millisecond) {
  337. t.Fatalf(`expect: window "month" UTC to equal "month" with timezone "UTC"; actual: %s not equal %s`, monthUTC, monthTZ)
  338. }
  339. lastweekUTC, err := ParseWindowUTC("lastweek")
  340. if err != nil {
  341. t.Fatalf(`unexpected error parsing "lastweek": %s`, err)
  342. }
  343. lastweekTZ, err := ParseWindowWithOffsetString("lastweek", "UTC")
  344. if err != nil {
  345. t.Fatalf(`unexpected error parsing "lastweek": %s`, err)
  346. }
  347. if !lastweekUTC.ApproximatelyEqual(lastweekTZ, time.Millisecond) {
  348. t.Fatalf(`expect: window "lastweek" UTC to equal "lastweek" with timezone "UTC"; actual: %s not equal %s`, lastweekUTC, lastweekTZ)
  349. }
  350. lastmonthUTC, err := ParseWindowUTC("lastmonth")
  351. if err != nil {
  352. t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
  353. }
  354. lastmonthTZ, err := ParseWindowWithOffsetString("lastmonth", "UTC")
  355. if err != nil {
  356. t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
  357. }
  358. if !lastmonthUTC.ApproximatelyEqual(lastmonthTZ, time.Millisecond) {
  359. t.Fatalf(`expect: window "lastmonth" UTC to equal "lastmonth" with timezone "UTC"; actual: %s not equal %s`, lastmonthUTC, lastmonthTZ)
  360. }
  361. dur10mUTC, err := ParseWindowUTC("10m")
  362. if err != nil {
  363. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  364. }
  365. dur10mTZ, err := ParseWindowWithOffsetString("10m", "UTC")
  366. if err != nil {
  367. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  368. }
  369. if !dur10mUTC.ApproximatelyEqual(dur10mTZ, time.Millisecond) {
  370. t.Fatalf(`expect: window "10m" UTC to equal "10m" with timezone "UTC"; actual: %s not equal %s`, dur10mUTC, dur10mTZ)
  371. }
  372. dur24hUTC, err := ParseWindowUTC("24h")
  373. if err != nil {
  374. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  375. }
  376. dur24hTZ, err := ParseWindowWithOffsetString("24h", "UTC")
  377. if err != nil {
  378. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  379. }
  380. if !dur24hUTC.ApproximatelyEqual(dur24hTZ, time.Millisecond) {
  381. t.Fatalf(`expect: window "24h" UTC to equal "24h" with timezone "UTC"; actual: %s not equal %s`, dur24hUTC, dur24hTZ)
  382. }
  383. dur37dUTC, err := ParseWindowUTC("37d")
  384. if err != nil {
  385. t.Fatalf(`unexpected error parsing "37d": %s`, err)
  386. }
  387. dur37dTZ, err := ParseWindowWithOffsetString("37d", "UTC")
  388. if err != nil {
  389. t.Fatalf(`unexpected error parsing "37d": %s`, err)
  390. }
  391. if !dur37dUTC.ApproximatelyEqual(dur37dTZ, time.Millisecond) {
  392. t.Fatalf(`expect: window "37d" UTC to equal "37d" with timezone "UTC"; actual: %s not equal %s`, dur37dUTC, dur37dTZ)
  393. }
  394. // ParseWindowWithOffsetString should be the correct relative to ParseWindowUTC; i.e.
  395. // - for durations, the times should match, but the representations should differ
  396. // by the number of hours offset
  397. // - for words like "today" and "yesterday", the times may not match, in which
  398. // case, for example, "today" UTC-08:00 might equal "yesterday" UTC
  399. // fmtWindow only compares date and time to the minute, not second or
  400. // timezone. Helper for comparing timezone shifted windows.
  401. fmtWindow := func(w Window) string {
  402. s := "nil"
  403. if w.start != nil {
  404. s = w.start.Format("2006-01-02T15:04")
  405. }
  406. e := "nil"
  407. if w.end != nil {
  408. e = w.end.Format("2006-01-02T15:04")
  409. }
  410. return fmt.Sprintf("[%s, %s]", s, e)
  411. }
  412. // Test UTC-08:00 (California), UTC+03:00 (Moscow), UTC+12:00 (New Zealand), and UTC itself
  413. for _, offsetHrs := range []int{-8, 3, 12, 0} {
  414. offStr := fmt.Sprintf("+%02d:00", offsetHrs)
  415. if offsetHrs < 0 {
  416. offStr = fmt.Sprintf("-%02d:00", -offsetHrs)
  417. }
  418. off := time.Duration(offsetHrs) * time.Hour
  419. dur10mTZ, err = ParseWindowWithOffsetString("10m", offStr)
  420. if err != nil {
  421. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  422. }
  423. if !dur10mTZ.ApproximatelyEqual(dur10mUTC, time.Second) {
  424. t.Fatalf(`expect: window "10m" UTC to equal "10m" with timezone "%s"; actual: %s not equal %s`, offStr, dur10mUTC, dur10mTZ)
  425. }
  426. if fmtWindow(dur10mTZ.Shift(-off)) != fmtWindow(dur10mUTC) {
  427. t.Fatalf(`expect: date, hour, and minute of window "10m" UTC to equal that of "10m" %s shifted by %s; actual: %s not equal %s`, offStr, off, fmtWindow(dur10mUTC), fmtWindow(dur10mTZ.Shift(-off)))
  428. }
  429. dur24hTZ, err = ParseWindowWithOffsetString("24h", offStr)
  430. if err != nil {
  431. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  432. }
  433. if !dur24hTZ.ApproximatelyEqual(dur24hUTC, time.Second) {
  434. t.Fatalf(`expect: window "24h" UTC to equal "24h" with timezone "%s"; actual: %s not equal %s`, offStr, dur24hUTC, dur24hTZ)
  435. }
  436. if fmtWindow(dur24hTZ.Shift(-off)) != fmtWindow(dur24hUTC) {
  437. t.Fatalf(`expect: date, hour, and minute of window "24h" UTC to equal that of "24h" %s shifted by %s; actual: %s not equal %s`, offStr, off, fmtWindow(dur24hUTC), fmtWindow(dur24hTZ.Shift(-off)))
  438. }
  439. dur37dTZ, err = ParseWindowWithOffsetString("37d", offStr)
  440. if err != nil {
  441. t.Fatalf(`unexpected error parsing "37d": %s`, err)
  442. }
  443. if !dur37dTZ.ApproximatelyEqual(dur37dUTC, time.Second) {
  444. t.Fatalf(`expect: window "37d" UTC to equal "37d" with timezone "%s"; actual: %s not equal %s`, offStr, dur37dUTC, dur37dTZ)
  445. }
  446. if fmtWindow(dur37dTZ.Shift(-off)) != fmtWindow(dur37dUTC) {
  447. t.Fatalf(`expect: date, hour, and minute of window "37d" UTC to equal that of "37d" %s shifted by %s; actual: %s not equal %s`, offStr, off, fmtWindow(dur37dUTC), fmtWindow(dur37dTZ.Shift(-off)))
  448. }
  449. // "today" and "yesterday" should comply with the current day in each
  450. // respective timezone, depending on if it is ahead of, equal to, or
  451. // behind UTC at the given moment.
  452. todayTZ, err = ParseWindowWithOffsetString("today", offStr)
  453. if err != nil {
  454. t.Fatalf(`unexpected error parsing "today": %s`, err)
  455. }
  456. yesterdayTZ, err = ParseWindowWithOffsetString("yesterday", offStr)
  457. if err != nil {
  458. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  459. }
  460. hoursSinceYesterdayUTC := time.Now().UTC().Sub(time.Now().UTC().Truncate(24.0 * time.Hour)).Hours()
  461. hoursUntilTomorrowUTC := 24.0 - hoursSinceYesterdayUTC
  462. aheadOfUTC := float64(offsetHrs)-hoursUntilTomorrowUTC > 0
  463. behindUTC := float64(offsetHrs)+hoursSinceYesterdayUTC < 0
  464. // yesterday in this timezone should equal today UTC
  465. if aheadOfUTC {
  466. if fmtWindow(yesterdayTZ) != fmtWindow(todayUTC) {
  467. t.Fatalf(`expect: window "today" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, yesterdayTZ, todayUTC)
  468. }
  469. }
  470. // today in this timezone should equal yesterday UTC
  471. if behindUTC {
  472. if fmtWindow(todayTZ) != fmtWindow(yesterdayUTC) {
  473. t.Fatalf(`expect: window "today" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, todayTZ, yesterdayUTC)
  474. }
  475. }
  476. // today in this timezone should equal today UTC, likewise for yesterday
  477. if !aheadOfUTC && !behindUTC {
  478. if fmtWindow(todayTZ) != fmtWindow(todayUTC) {
  479. t.Fatalf(`expect: window "today" UTC to equal "today" with timezone "%s"; actual: %s not equal %s`, offStr, todayTZ, todayUTC)
  480. }
  481. // yesterday in this timezone should equal yesterday UTC
  482. if fmtWindow(yesterdayTZ) != fmtWindow(yesterdayUTC) {
  483. t.Fatalf(`expect: window "yesterday" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, yesterdayTZ, yesterdayUTC)
  484. }
  485. }
  486. }
  487. }
  488. func TestWindow_DurationOffsetStrings(t *testing.T) {
  489. w, err := ParseWindowUTC("1d")
  490. if err != nil {
  491. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  492. }
  493. dur, off := w.DurationOffsetStrings()
  494. if dur != "1d" {
  495. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  496. }
  497. if off != "" {
  498. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  499. }
  500. w, err = ParseWindowUTC("3h")
  501. if err != nil {
  502. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  503. }
  504. dur, off = w.DurationOffsetStrings()
  505. if dur != "3h" {
  506. t.Fatalf(`expect: window to be "3h"; actual: "%s"`, dur)
  507. }
  508. if off != "" {
  509. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  510. }
  511. w, err = ParseWindowUTC("10m")
  512. if err != nil {
  513. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  514. }
  515. dur, off = w.DurationOffsetStrings()
  516. if dur != "10m" {
  517. t.Fatalf(`expect: window to be "10m"; actual: "%s"`, dur)
  518. }
  519. if off != "" {
  520. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  521. }
  522. w, err = ParseWindowUTC("1589448338,1589534798")
  523. if err != nil {
  524. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  525. }
  526. dur, off = w.DurationOffsetStrings()
  527. if dur != "1441m" {
  528. t.Fatalf(`expect: window to be "1441m"; actual: "%s"`, dur)
  529. }
  530. if off == "" {
  531. t.Fatalf(`expect: offset to not be ""; actual: "%s"`, off)
  532. }
  533. w, err = ParseWindowUTC("yesterday")
  534. if err != nil {
  535. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  536. }
  537. dur, _ = w.DurationOffsetStrings()
  538. if dur != "1d" {
  539. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  540. }
  541. }
  542. func TestWindow_DurationOffsetForPrometheus(t *testing.T) {
  543. // Set-up and tear-down
  544. thanosEnabled := env.GetBool(env.ThanosEnabledEnvVar, false)
  545. defer env.SetBool(env.ThanosEnabledEnvVar, thanosEnabled)
  546. // Test for Prometheus (env.IsThanosEnabled() == false)
  547. env.SetBool(env.ThanosEnabledEnvVar, false)
  548. if env.IsThanosEnabled() {
  549. t.Fatalf("expected env.IsThanosEnabled() == false")
  550. }
  551. w, err := ParseWindowUTC("1d")
  552. if err != nil {
  553. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  554. }
  555. dur, off, err := w.DurationOffsetForPrometheus()
  556. if err != nil {
  557. t.Fatalf("unexpected error: %s", err)
  558. }
  559. if dur != "1d" {
  560. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  561. }
  562. if off != "" {
  563. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  564. }
  565. w, err = ParseWindowUTC("2h")
  566. if err != nil {
  567. t.Fatalf(`unexpected error parsing "2h": %s`, err)
  568. }
  569. dur, off, err = w.DurationOffsetForPrometheus()
  570. if err != nil {
  571. t.Fatalf("unexpected error: %s", err)
  572. }
  573. if dur != "2h" {
  574. t.Fatalf(`expect: window to be "2h"; actual: "%s"`, dur)
  575. }
  576. if off != "" {
  577. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  578. }
  579. w, err = ParseWindowUTC("10m")
  580. if err != nil {
  581. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  582. }
  583. dur, off, err = w.DurationOffsetForPrometheus()
  584. if err != nil {
  585. t.Fatalf("unexpected error: %s", err)
  586. }
  587. if dur != "10m" {
  588. t.Fatalf(`expect: window to be "10m"; actual: "%s"`, dur)
  589. }
  590. if off != "" {
  591. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  592. }
  593. w, err = ParseWindowUTC("1589448338,1589534798")
  594. if err != nil {
  595. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  596. }
  597. dur, off, err = w.DurationOffsetForPrometheus()
  598. if err != nil {
  599. t.Fatalf("unexpected error: %s", err)
  600. }
  601. if dur != "1441m" {
  602. t.Fatalf(`expect: window to be "1441m"; actual: "%s"`, dur)
  603. }
  604. if !strings.HasPrefix(off, " offset ") {
  605. t.Fatalf(`expect: offset to start with " offset "; actual: "%s"`, off)
  606. }
  607. w, err = ParseWindowUTC("yesterday")
  608. if err != nil {
  609. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  610. }
  611. dur, off, err = w.DurationOffsetForPrometheus()
  612. if err != nil {
  613. t.Fatalf("unexpected error: %s", err)
  614. }
  615. if dur != "1d" {
  616. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  617. }
  618. if !strings.HasPrefix(off, " offset ") {
  619. t.Fatalf(`expect: offset to start with " offset "; actual: "%s"`, off)
  620. }
  621. // Test for Thanos (env.IsThanosEnabled() == true)
  622. env.SetBool(env.ThanosEnabledEnvVar, true)
  623. if !env.IsThanosEnabled() {
  624. t.Fatalf("expected env.IsThanosEnabled() == true")
  625. }
  626. w, err = ParseWindowUTC("1d")
  627. if err != nil {
  628. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  629. }
  630. dur, off, err = w.DurationOffsetForPrometheus()
  631. if err != nil {
  632. t.Fatalf("unexpected error: %s", err)
  633. }
  634. if dur != "21h" {
  635. t.Fatalf(`expect: window to be "21d"; actual: "%s"`, dur)
  636. }
  637. if off != " offset 3h" {
  638. t.Fatalf(`expect: offset to be " offset 3h"; actual: "%s"`, off)
  639. }
  640. w, err = ParseWindowUTC("2h")
  641. if err != nil {
  642. t.Fatalf(`unexpected error parsing "2h": %s`, err)
  643. }
  644. dur, off, err = w.DurationOffsetForPrometheus()
  645. if err == nil {
  646. t.Fatalf(`expected error (negative duration); got ("%s", "%s")`, dur, off)
  647. }
  648. w, err = ParseWindowUTC("10m")
  649. if err != nil {
  650. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  651. }
  652. dur, off, err = w.DurationOffsetForPrometheus()
  653. if err == nil {
  654. t.Fatalf(`expected error (negative duration); got ("%s", "%s")`, dur, off)
  655. }
  656. w, err = ParseWindowUTC("1589448338,1589534798")
  657. if err != nil {
  658. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  659. }
  660. dur, off, err = w.DurationOffsetForPrometheus()
  661. if err != nil {
  662. t.Fatalf("unexpected error: %s", err)
  663. }
  664. if dur != "1441m" {
  665. t.Fatalf(`expect: window to be "1441m"; actual: "%s"`, dur)
  666. }
  667. if !strings.HasPrefix(off, " offset ") {
  668. t.Fatalf(`expect: offset to start with " offset "; actual: "%s"`, off)
  669. }
  670. }
  671. // TODO
  672. // func TestWindow_Overlaps(t *testing.T) {}
  673. // TODO
  674. // func TestWindow_Contains(t *testing.T) {}
  675. // TODO
  676. // func TestWindow_Duration(t *testing.T) {}
  677. // TODO
  678. // func TestWindow_End(t *testing.T) {}
  679. // TODO
  680. // func TestWindow_Equal(t *testing.T) {}
  681. // TODO
  682. // func TestWindow_ExpandStart(t *testing.T) {}
  683. // TODO
  684. // func TestWindow_ExpandEnd(t *testing.T) {}
  685. func TestWindow_Expand(t *testing.T) {
  686. t1 := time.Now().Round(time.Hour)
  687. t2 := t1.Add(34 * time.Minute)
  688. t3 := t1.Add(50 * time.Minute)
  689. t4 := t1.Add(84 * time.Minute)
  690. cases := []struct {
  691. windowToExpand Window
  692. windowArgument Window
  693. expected Window
  694. }{
  695. {
  696. windowToExpand: NewClosedWindow(t1, t2),
  697. windowArgument: NewClosedWindow(t3, t4),
  698. expected: NewClosedWindow(t1, t4),
  699. },
  700. {
  701. windowToExpand: NewClosedWindow(t3, t4),
  702. windowArgument: NewClosedWindow(t1, t2),
  703. expected: NewClosedWindow(t1, t4),
  704. },
  705. {
  706. windowToExpand: NewClosedWindow(t1, t3),
  707. windowArgument: NewClosedWindow(t2, t4),
  708. expected: NewClosedWindow(t1, t4),
  709. },
  710. {
  711. windowToExpand: NewClosedWindow(t2, t4),
  712. windowArgument: NewClosedWindow(t1, t3),
  713. expected: NewClosedWindow(t1, t4),
  714. },
  715. {
  716. windowToExpand: Window{},
  717. windowArgument: NewClosedWindow(t1, t2),
  718. expected: NewClosedWindow(t1, t2),
  719. },
  720. {
  721. windowToExpand: NewWindow(nil, &t2),
  722. windowArgument: NewWindow(nil, &t3),
  723. expected: NewWindow(nil, &t3),
  724. },
  725. {
  726. windowToExpand: NewWindow(&t2, nil),
  727. windowArgument: NewWindow(&t1, nil),
  728. expected: NewWindow(&t1, nil),
  729. },
  730. }
  731. for _, c := range cases {
  732. result := c.windowToExpand.Expand(c.windowArgument)
  733. if !result.Equal(c.expected) {
  734. t.Errorf("Expand %s with %s, expected %s but got %s", c.windowToExpand, c.windowArgument, c.expected, result)
  735. }
  736. }
  737. }
  738. // TODO
  739. // func TestWindow_Start(t *testing.T) {}
  740. // TODO
  741. // func TestWindow_String(t *testing.T) {}
  742. func TestWindow_GetPercentInWindow(t *testing.T) {
  743. dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
  744. dayEnd := dayStart.Add(timeutil.Day)
  745. window := NewClosedWindow(dayStart, dayEnd)
  746. testcases := map[string]struct {
  747. window Window
  748. itemStart time.Time
  749. itemEnd time.Time
  750. expected float64
  751. }{
  752. "matching start/matching end": {
  753. window: window,
  754. itemStart: dayStart,
  755. itemEnd: dayEnd,
  756. expected: 1.0,
  757. },
  758. "matching start/contained end": {
  759. window: window,
  760. itemStart: dayStart,
  761. itemEnd: dayEnd.Add(-time.Hour * 6),
  762. expected: 1.0,
  763. },
  764. "contained start/matching end": {
  765. window: window,
  766. itemStart: dayStart.Add(time.Hour * 6),
  767. itemEnd: dayEnd,
  768. expected: 1.0,
  769. },
  770. "contained start/contained end": {
  771. window: window,
  772. itemStart: dayStart.Add(time.Hour * 6),
  773. itemEnd: dayEnd.Add(-time.Hour * 6),
  774. expected: 1.0,
  775. },
  776. "before start/contained end": {
  777. window: window,
  778. itemStart: dayStart.Add(-time.Hour * 12),
  779. itemEnd: dayEnd.Add(-time.Hour * 12),
  780. expected: 0.5,
  781. },
  782. "before start/before end": {
  783. window: window,
  784. itemStart: dayStart.Add(-time.Hour * 24),
  785. itemEnd: dayEnd.Add(-time.Hour * 24),
  786. expected: 0.0,
  787. },
  788. "contained start/after end": {
  789. window: window,
  790. itemStart: dayStart.Add(time.Hour * 12),
  791. itemEnd: dayEnd.Add(time.Hour * 12),
  792. expected: 0.5,
  793. },
  794. "after start/after end": {
  795. window: window,
  796. itemStart: dayStart.Add(time.Hour * 24),
  797. itemEnd: dayEnd.Add(time.Hour * 24),
  798. expected: 0.0,
  799. },
  800. "before start/after end": {
  801. window: window,
  802. itemStart: dayStart.Add(-time.Hour * 12),
  803. itemEnd: dayEnd.Add(time.Hour * 12),
  804. expected: 0.5,
  805. },
  806. }
  807. for name, tc := range testcases {
  808. t.Run(name, func(t *testing.T) {
  809. thatWindow := NewWindow(&tc.itemStart, &tc.itemEnd)
  810. if actual := tc.window.GetPercentInWindow(thatWindow); actual != tc.expected {
  811. t.Errorf("GetPercentInWindow() = %v, want %v", actual, tc.expected)
  812. }
  813. })
  814. }
  815. }
  816. func TestWindow_GetWindows(t *testing.T) {
  817. dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
  818. dayEnd := dayStart.Add(timeutil.Day)
  819. loc, _ := time.LoadLocation("America/Vancouver")
  820. testCases := map[string]struct {
  821. start time.Time
  822. end time.Time
  823. windowSize time.Duration
  824. expected []Window
  825. expectedErr bool
  826. }{
  827. "mismatching tz": {
  828. start: dayStart,
  829. end: dayEnd.In(loc),
  830. windowSize: time.Hour,
  831. expected: nil,
  832. expectedErr: true,
  833. },
  834. "hour windows over 1 hours": {
  835. start: dayStart,
  836. end: dayStart.Add(time.Hour),
  837. windowSize: time.Hour,
  838. expected: []Window{
  839. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  840. },
  841. expectedErr: false,
  842. },
  843. "hour windows over 3 hours": {
  844. start: dayStart,
  845. end: dayStart.Add(time.Hour * 3),
  846. windowSize: time.Hour,
  847. expected: []Window{
  848. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  849. NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Hour*2)),
  850. NewClosedWindow(dayStart.Add(time.Hour*2), dayStart.Add(time.Hour*3)),
  851. },
  852. expectedErr: false,
  853. },
  854. "hour windows off hour grid": {
  855. start: dayStart.Add(time.Minute),
  856. end: dayEnd.Add(time.Minute),
  857. windowSize: time.Hour,
  858. expected: nil,
  859. expectedErr: true,
  860. },
  861. "hour windows range not divisible by hour": {
  862. start: dayStart,
  863. end: dayStart.Add(time.Minute * 90),
  864. windowSize: time.Hour,
  865. expected: nil,
  866. expectedErr: true,
  867. },
  868. "day windows over 1 day": {
  869. start: dayStart,
  870. end: dayEnd,
  871. windowSize: timeutil.Day,
  872. expected: []Window{
  873. NewClosedWindow(dayStart, dayEnd),
  874. },
  875. expectedErr: false,
  876. },
  877. "day windows over 3 days": {
  878. start: dayStart,
  879. end: dayStart.Add(timeutil.Day * 3),
  880. windowSize: timeutil.Day,
  881. expected: []Window{
  882. NewClosedWindow(dayStart, dayStart.Add(timeutil.Day)),
  883. NewClosedWindow(dayStart.Add(timeutil.Day), dayStart.Add(timeutil.Day*2)),
  884. NewClosedWindow(dayStart.Add(timeutil.Day*2), dayStart.Add(timeutil.Day*3)),
  885. },
  886. expectedErr: false,
  887. },
  888. "day windows off day grid": {
  889. start: dayStart.Add(time.Hour),
  890. end: dayEnd.Add(time.Hour),
  891. windowSize: timeutil.Day,
  892. expected: nil,
  893. expectedErr: true,
  894. },
  895. "day windows range not divisible by day": {
  896. start: dayStart,
  897. end: dayEnd.Add(time.Hour),
  898. windowSize: timeutil.Day,
  899. expected: nil,
  900. expectedErr: true,
  901. },
  902. }
  903. for name, tc := range testCases {
  904. t.Run(name, func(t *testing.T) {
  905. actual, err := GetWindows(tc.start, tc.end, tc.windowSize)
  906. if (err != nil) != tc.expectedErr {
  907. t.Errorf("GetWindows() error = %v, expectedErr %v", err, tc.expectedErr)
  908. return
  909. }
  910. if len(tc.expected) != len(actual) {
  911. t.Errorf("GetWindows() []window has incorrect length expected: %d, actual: %d", len(tc.expected), len(actual))
  912. }
  913. for i, actualWindow := range actual {
  914. expectedWindow := tc.expected[i]
  915. if !actualWindow.Equal(expectedWindow) {
  916. t.Errorf("GetWindow() window at index %d were not equal expected: %s, actual %s", i, expectedWindow.String(), actualWindow)
  917. }
  918. }
  919. })
  920. }
  921. }
  922. func TestWindow_GetWindowsForQueryWindow(t *testing.T) {
  923. dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
  924. dayEnd := dayStart.Add(timeutil.Day)
  925. loc, _ := time.LoadLocation("America/Vancouver")
  926. testCases := map[string]struct {
  927. start time.Time
  928. end time.Time
  929. windowSize time.Duration
  930. expected []Window
  931. expectedErr bool
  932. }{
  933. "mismatching tz": {
  934. start: dayStart,
  935. end: dayEnd.In(loc),
  936. windowSize: time.Hour,
  937. expected: nil,
  938. expectedErr: true,
  939. },
  940. "hour windows over 1 hours": {
  941. start: dayStart,
  942. end: dayStart.Add(time.Hour),
  943. windowSize: time.Hour,
  944. expected: []Window{
  945. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  946. },
  947. expectedErr: false,
  948. },
  949. "hour windows over 3 hours": {
  950. start: dayStart,
  951. end: dayStart.Add(time.Hour * 3),
  952. windowSize: time.Hour,
  953. expected: []Window{
  954. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  955. NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Hour*2)),
  956. NewClosedWindow(dayStart.Add(time.Hour*2), dayStart.Add(time.Hour*3)),
  957. },
  958. expectedErr: false,
  959. },
  960. "hour windows off hour grid": {
  961. start: dayStart.Add(time.Minute),
  962. end: dayStart.Add(time.Minute * 61),
  963. windowSize: time.Hour,
  964. expected: []Window{
  965. NewClosedWindow(dayStart.Add(time.Minute), dayStart.Add(time.Minute*61)),
  966. },
  967. expectedErr: false,
  968. },
  969. "hour windows range not divisible by hour": {
  970. start: dayStart,
  971. end: dayStart.Add(time.Minute * 90),
  972. windowSize: time.Hour,
  973. expected: []Window{
  974. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  975. NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Minute*90)),
  976. },
  977. expectedErr: false,
  978. },
  979. "day windows over 1 day": {
  980. start: dayStart,
  981. end: dayEnd,
  982. windowSize: timeutil.Day,
  983. expected: []Window{
  984. NewClosedWindow(dayStart, dayEnd),
  985. },
  986. expectedErr: false,
  987. },
  988. "day windows over 3 days": {
  989. start: dayStart,
  990. end: dayStart.Add(timeutil.Day * 3),
  991. windowSize: timeutil.Day,
  992. expected: []Window{
  993. NewClosedWindow(dayStart, dayStart.Add(timeutil.Day)),
  994. NewClosedWindow(dayStart.Add(timeutil.Day), dayStart.Add(timeutil.Day*2)),
  995. NewClosedWindow(dayStart.Add(timeutil.Day*2), dayStart.Add(timeutil.Day*3)),
  996. },
  997. expectedErr: false,
  998. },
  999. "day windows off day grid": {
  1000. start: dayStart.Add(time.Hour),
  1001. end: dayEnd.Add(time.Hour),
  1002. windowSize: timeutil.Day,
  1003. expected: []Window{
  1004. NewClosedWindow(dayStart.Add(time.Hour), dayEnd.Add(time.Hour)),
  1005. },
  1006. expectedErr: false,
  1007. },
  1008. "day windows range not divisible by day": {
  1009. start: dayStart,
  1010. end: dayEnd.Add(time.Hour),
  1011. windowSize: timeutil.Day,
  1012. expected: []Window{
  1013. NewClosedWindow(dayStart, dayEnd),
  1014. NewClosedWindow(dayEnd, dayEnd.Add(time.Hour)),
  1015. },
  1016. expectedErr: false,
  1017. },
  1018. }
  1019. for name, tc := range testCases {
  1020. t.Run(name, func(t *testing.T) {
  1021. actual, err := GetWindowsForQueryWindow(tc.start, tc.end, tc.windowSize)
  1022. if (err != nil) != tc.expectedErr {
  1023. t.Errorf("GetWindowsForQueryWindow() error = %v, expectedErr %v", err, tc.expectedErr)
  1024. return
  1025. }
  1026. if len(tc.expected) != len(actual) {
  1027. t.Errorf("GetWindowsForQueryWindow() []window has incorrect length expected: %d, actual: %d", len(tc.expected), len(actual))
  1028. }
  1029. for i, actualWindow := range actual {
  1030. expectedWindow := tc.expected[i]
  1031. if !actualWindow.Equal(expectedWindow) {
  1032. t.Errorf("GetWindowsForQueryWindow() window at index %d were not equal expected: %s, actual %s", i, expectedWindow.String(), actualWindow)
  1033. }
  1034. }
  1035. })
  1036. }
  1037. }