window_test.go 41 KB

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