window_test.go 39 KB

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