window_test.go 43 KB

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