| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194 |
- package opencost
- import (
- "encoding/json"
- "errors"
- "fmt"
- "testing"
- "time"
- "github.com/google/go-cmp/cmp"
- "github.com/opencost/opencost/core/pkg/util/timeutil"
- "github.com/opencost/opencost/core/pkg/env"
- )
- const (
- ThanosEnabledEnvVarName = "THANOS_ENABLED"
- UtcOffsetEnvVarName = "UTC_OFFSET"
- )
- func TestRoundBack(t *testing.T) {
- boulder := time.FixedZone("Boulder", -7*60*60)
- beijing := time.FixedZone("Beijing", 8*60*60)
- to := time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)
- tb := RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 0, 0, 1, 0, boulder)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 12, 37, 48, 0, boulder)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 23, 37, 48, 0, boulder)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 0, 0, 1, 0, beijing)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 12, 37, 48, 0, beijing)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 23, 59, 59, 0, beijing)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 0, 0, 1, 0, time.UTC)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 12, 37, 48, 0, time.UTC)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
- tb = RoundBack(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
- tb = RoundBack(to, timeutil.Week)
- if !tb.Equal(time.Date(2019, time.December, 29, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundForward: expected 2019-12-29T00:00:00Z; actual %s", tb)
- }
- }
- func TestRoundForward(t *testing.T) {
- boulder := time.FixedZone("Boulder", -7*60*60)
- beijing := time.FixedZone("Beijing", 8*60*60)
- to := time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)
- tb := RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
- t.Fatalf("RoundForward: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 0, 0, 1, 0, boulder)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
- t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 12, 37, 48, 0, boulder)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
- t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 23, 37, 48, 0, boulder)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
- t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
- t.Fatalf("RoundForward: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 0, 0, 1, 0, beijing)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
- t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 12, 37, 48, 0, beijing)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
- t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 23, 59, 59, 0, beijing)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
- t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundForward: expected 2020-01-01T00:00:00Z; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 0, 0, 1, 0, time.UTC)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 12, 37, 48, 0, time.UTC)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
- tb = RoundForward(to, 24*time.Hour)
- if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
- }
- to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
- tb = RoundForward(to, timeutil.Week)
- if !tb.Equal(time.Date(2020, time.January, 5, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundForward: expected 2020-01-05T00:00:00Z; actual %s", tb)
- }
- to = time.Date(2020, time.January, 5, 23, 59, 0, 0, time.UTC)
- tb = RoundForward(to, timeutil.Week)
- if !tb.Equal(time.Date(2020, time.January, 12, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundForward: expected 2020-01-05T00:00:00Z; actual %s", tb)
- }
- to = time.Date(2020, time.January, 5, 0, 0, 0, 0, time.UTC)
- tb = RoundForward(to, timeutil.Week)
- if !tb.Equal(time.Date(2020, time.January, 5, 0, 0, 0, 0, time.UTC)) {
- t.Fatalf("RoundForward: expected 2020-01-05T00:00:00Z; actual %s", tb)
- }
- }
- func TestParseWindowUTC(t *testing.T) {
- now := time.Now().UTC()
- // "today" should span Now() and not produce an error
- today, err := ParseWindowUTC("today")
- if err != nil {
- t.Fatalf(`unexpected error parsing "today": %s`, err)
- }
- if today.Duration().Hours() != 24 {
- t.Fatalf(`expect: window "today" to have duration 24 hour; actual: %f hours`, today.Duration().Hours())
- }
- if !today.Contains(time.Now().UTC()) {
- t.Fatalf(`expect: window "today" to contain now; actual: %s`, today)
- }
- // "yesterday" should span Now() and not produce an error
- yesterday, err := ParseWindowUTC("yesterday")
- if err != nil {
- t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
- }
- if yesterday.Duration().Hours() != 24 {
- t.Fatalf(`expect: window "yesterday" to have duration 24 hour; actual: %f hours`, yesterday.Duration().Hours())
- }
- if !yesterday.End().Before(time.Now().UTC()) {
- t.Fatalf(`expect: window "yesterday" to end before now; actual: %s ends after %s`, yesterday, time.Now().UTC())
- }
- week, err := ParseWindowUTC("week")
- hoursThisWeek := float64(time.Now().UTC().Weekday()) * 24.0
- if err != nil {
- t.Fatalf(`unexpected error parsing "week": %s`, err)
- }
- if week.Duration().Hours() < hoursThisWeek {
- t.Fatalf(`expect: window "week" to have at least %f hours; actual: %f hours`, hoursThisWeek, week.Duration().Hours())
- }
- if week.End().After(time.Now().UTC()) {
- t.Fatalf(`expect: window "week" to end before now; actual: %s ends after %s`, week, time.Now().UTC())
- }
- month, err := ParseWindowUTC("month")
- hoursThisMonth := float64(time.Now().UTC().Day()) * 24.0
- if err != nil {
- t.Fatalf(`unexpected error parsing "month": %s`, err)
- }
- if month.Duration().Hours() > hoursThisMonth || month.Duration().Hours() < (hoursThisMonth-24.0) {
- t.Fatalf(`expect: window "month" to have approximately %f hours; actual: %f hours`, hoursThisMonth, month.Duration().Hours())
- }
- // this test fails periodically if execution is so fast that time.Now() during the condition
- // check is the same as the end of the current month time computed by ParseWindowUTC
- // so we add one nanosecond to sure time.Now() is later than when invoked earlier
- if !month.End().Before(time.Now().UTC().Add(time.Nanosecond)) {
- t.Fatalf(`expect: window "month" to end before now; actual: %s ends after %s`, month, time.Now().UTC())
- }
- // TODO lastweek
- lastmonth, err := ParseWindowUTC("lastmonth")
- monthMinHours := float64(24 * 28)
- monthMaxHours := float64(24 * 31)
- firstOfMonth := now.Truncate(time.Hour * 24).Add(-24 * time.Hour * time.Duration(now.Day()-1))
- if err != nil {
- t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
- }
- if lastmonth.Duration().Hours() > monthMaxHours || lastmonth.Duration().Hours() < monthMinHours {
- t.Fatalf(`expect: window "lastmonth" to have approximately %f hours; actual: %f hours`, hoursThisMonth, lastmonth.Duration().Hours())
- }
- if !lastmonth.End().Equal(firstOfMonth) {
- t.Fatalf(`expect: window "lastmonth" to end on the first of the current month; actual: %s doesn't end on %s`, lastmonth, firstOfMonth)
- }
- ago12h := time.Now().UTC().Add(-12 * time.Hour)
- ago24h := time.Now().UTC().Add(-24 * time.Hour)
- ago36h := time.Now().UTC().Add(-36 * time.Hour)
- ago60h := time.Now().UTC().Add(-60 * time.Hour)
- // "24h" should have 24 hour duration and not produce an error
- dur24h, err := ParseWindowUTC("24h")
- if err != nil {
- t.Fatalf(`unexpected error parsing "24h": %s`, err)
- }
- if dur24h.Duration().Hours() != 24 {
- t.Fatalf(`expect: window "24h" to have duration 24 hour; actual: %f hours`, dur24h.Duration().Hours())
- }
- if !dur24h.Contains(ago12h) {
- t.Fatalf(`expect: window "24h" to contain 12 hours ago; actual: %s doesn't contain %s`, dur24h, ago12h)
- }
- if dur24h.Contains(ago36h) {
- t.Fatalf(`expect: window "24h" to not contain 36 hours ago; actual: %s contains %s`, dur24h, ago36h)
- }
- // "2d" should have 2 day duration and not produce an error
- dur2d, err := ParseWindowUTC("2d")
- if err != nil {
- t.Fatalf(`unexpected error parsing "2d": %s`, err)
- }
- if dur2d.Duration().Hours() != 48 {
- t.Fatalf(`expect: window "2d" to have duration 48 hour; actual: %f hours`, dur2d.Duration().Hours())
- }
- if !dur2d.Contains(ago24h) {
- t.Fatalf(`expect: window "2d" to contain 24 hours ago; actual: %s doesn't contain %s`, dur2d, ago24h)
- }
- if dur2d.Contains(ago60h) {
- t.Fatalf(`expect: window "2d" to not contain 60 hours ago; actual: %s contains %s`, dur2d, ago60h)
- }
- // "24h offset 14h" should have 24 hour duration and not produce an error
- dur24hOff14h, err := ParseWindowUTC("24h offset 14h")
- if err != nil {
- t.Fatalf(`unexpected error parsing "24h offset 14h": %s`, err)
- }
- if dur24hOff14h.Duration().Hours() != 24 {
- t.Fatalf(`expect: window "24h offset 14h" to have duration 24 hour; actual: %f hours`, dur24hOff14h.Duration().Hours())
- }
- if dur24hOff14h.Contains(ago12h) {
- t.Fatalf(`expect: window "24h offset 14h" not to contain 12 hours ago; actual: %s contains %s`, dur24hOff14h, ago12h)
- }
- if !dur24hOff14h.Contains(ago36h) {
- t.Fatalf(`expect: window "24h offset 14h" to contain 36 hours ago; actual: %s does not contain %s`, dur24hOff14h, ago36h)
- }
- april152020, _ := time.Parse(time.RFC3339, "2020-04-15T00:00:00Z")
- april102020, _ := time.Parse(time.RFC3339, "2020-04-10T00:00:00Z")
- april052020, _ := time.Parse(time.RFC3339, "2020-04-05T00:00:00Z")
- // "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z" should have 96 hour duration and not produce an error
- april8to12, err := ParseWindowUTC("2020-04-08T00:00:00Z,2020-04-12T00:00:00Z")
- if err != nil {
- t.Fatalf(`unexpected error parsing "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z": %s`, err)
- }
- if april8to12.Duration().Hours() != 96 {
- t.Fatalf(`expect: window %s to have duration 96 hour; actual: %f hours`, april8to12, april8to12.Duration().Hours())
- }
- if !april8to12.Contains(april102020) {
- t.Fatalf(`expect: window April 8-12 to contain April 10; actual: %s doesn't contain %s`, april8to12, april102020)
- }
- if april8to12.Contains(april052020) {
- t.Fatalf(`expect: window April 8-12 to not contain April 5; actual: %s contains %s`, april8to12, april052020)
- }
- if april8to12.Contains(april152020) {
- t.Fatalf(`expect: window April 8-12 to not contain April 15; actual: %s contains %s`, april8to12, april152020)
- }
- march152020, _ := time.Parse(time.RFC3339, "2020-03-15T00:00:00Z")
- march102020, _ := time.Parse(time.RFC3339, "2020-03-10T00:00:00Z")
- march052020, _ := time.Parse(time.RFC3339, "2020-03-05T00:00:00Z")
- // "1583712000,1583884800" should have 48 hour duration and not produce an error
- march9to11, err := ParseWindowUTC("1583712000,1583884800")
- if err != nil {
- t.Fatalf(`unexpected error parsing "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z": %s`, err)
- }
- if march9to11.Duration().Hours() != 48 {
- t.Fatalf(`expect: window %s to have duration 48 hour; actual: %f hours`, march9to11, march9to11.Duration().Hours())
- }
- if !march9to11.Contains(march102020) {
- t.Fatalf(`expect: window March 9-11 to contain March 10; actual: %s doesn't contain %s`, march9to11, march102020)
- }
- if march9to11.Contains(march052020) {
- t.Fatalf(`expect: window March 9-11 to not contain March 5; actual: %s contains %s`, march9to11, march052020)
- }
- if march9to11.Contains(march152020) {
- t.Fatalf(`expect: window March 9-11 to not contain March 15; actual: %s contains %s`, march9to11, march152020)
- }
- }
- func BenchmarkParseWindowUTC(b *testing.B) {
- for n := 0; n < b.N; n++ {
- _, err := ParseWindowUTC("2020-04-08T00:00:00Z,2020-04-12T00:00:00Z")
- if err != nil {
- b.Fatalf("error running benchmark: %s", err.Error())
- }
- }
- }
- func TestParseWindowWithOffsetString(t *testing.T) {
- // ParseWindowWithOffsetString should equal ParseWindowUTC when location == "UTC"
- // for all window string formats
- todayUTC, err := ParseWindowUTC("today")
- if err != nil {
- t.Fatalf(`unexpected error parsing "today": %s`, err)
- }
- todayTZ, err := ParseWindowWithOffsetString("today", "UTC")
- if err != nil {
- t.Fatalf(`unexpected error parsing "today": %s`, err)
- }
- if !todayUTC.ApproximatelyEqual(todayTZ, time.Millisecond) {
- t.Fatalf(`expect: window "today" UTC to equal "today" with timezone "UTC"; actual: %s not equal %s`, todayUTC, todayTZ)
- }
- yesterdayUTC, err := ParseWindowUTC("yesterday")
- if err != nil {
- t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
- }
- yesterdayTZ, err := ParseWindowWithOffsetString("yesterday", "UTC")
- if err != nil {
- t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
- }
- if !yesterdayUTC.ApproximatelyEqual(yesterdayTZ, time.Millisecond) {
- t.Fatalf(`expect: window "yesterday" UTC to equal "yesterday" with timezone "UTC"; actual: %s not equal %s`, yesterdayUTC, yesterdayTZ)
- }
- weekUTC, err := ParseWindowUTC("week")
- if err != nil {
- t.Fatalf(`unexpected error parsing "week": %s`, err)
- }
- weekTZ, err := ParseWindowWithOffsetString("week", "UTC")
- if err != nil {
- t.Fatalf(`unexpected error parsing "week": %s`, err)
- }
- if !weekUTC.ApproximatelyEqual(weekTZ, time.Millisecond) {
- t.Fatalf(`expect: window "week" UTC to equal "week" with timezone "UTC"; actual: %s not equal %s`, weekUTC, weekTZ)
- }
- monthUTC, err := ParseWindowUTC("month")
- if err != nil {
- t.Fatalf(`unexpected error parsing "month": %s`, err)
- }
- monthTZ, err := ParseWindowWithOffsetString("month", "UTC")
- if err != nil {
- t.Fatalf(`unexpected error parsing "month": %s`, err)
- }
- if !monthUTC.ApproximatelyEqual(monthTZ, time.Millisecond) {
- t.Fatalf(`expect: window "month" UTC to equal "month" with timezone "UTC"; actual: %s not equal %s`, monthUTC, monthTZ)
- }
- lastweekUTC, err := ParseWindowUTC("lastweek")
- if err != nil {
- t.Fatalf(`unexpected error parsing "lastweek": %s`, err)
- }
- lastweekTZ, err := ParseWindowWithOffsetString("lastweek", "UTC")
- if err != nil {
- t.Fatalf(`unexpected error parsing "lastweek": %s`, err)
- }
- if !lastweekUTC.ApproximatelyEqual(lastweekTZ, time.Millisecond) {
- t.Fatalf(`expect: window "lastweek" UTC to equal "lastweek" with timezone "UTC"; actual: %s not equal %s`, lastweekUTC, lastweekTZ)
- }
- lastmonthUTC, err := ParseWindowUTC("lastmonth")
- if err != nil {
- t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
- }
- lastmonthTZ, err := ParseWindowWithOffsetString("lastmonth", "UTC")
- if err != nil {
- t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
- }
- if !lastmonthUTC.ApproximatelyEqual(lastmonthTZ, time.Millisecond) {
- t.Fatalf(`expect: window "lastmonth" UTC to equal "lastmonth" with timezone "UTC"; actual: %s not equal %s`, lastmonthUTC, lastmonthTZ)
- }
- dur10mUTC, err := ParseWindowUTC("10m")
- if err != nil {
- t.Fatalf(`unexpected error parsing "10m": %s`, err)
- }
- dur10mTZ, err := ParseWindowWithOffsetString("10m", "UTC")
- if err != nil {
- t.Fatalf(`unexpected error parsing "10m": %s`, err)
- }
- if !dur10mUTC.ApproximatelyEqual(dur10mTZ, time.Millisecond) {
- t.Fatalf(`expect: window "10m" UTC to equal "10m" with timezone "UTC"; actual: %s not equal %s`, dur10mUTC, dur10mTZ)
- }
- dur24hUTC, err := ParseWindowUTC("24h")
- if err != nil {
- t.Fatalf(`unexpected error parsing "24h": %s`, err)
- }
- dur24hTZ, err := ParseWindowWithOffsetString("24h", "UTC")
- if err != nil {
- t.Fatalf(`unexpected error parsing "24h": %s`, err)
- }
- if !dur24hUTC.ApproximatelyEqual(dur24hTZ, time.Millisecond) {
- t.Fatalf(`expect: window "24h" UTC to equal "24h" with timezone "UTC"; actual: %s not equal %s`, dur24hUTC, dur24hTZ)
- }
- dur37dUTC, err := ParseWindowUTC("37d")
- if err != nil {
- t.Fatalf(`unexpected error parsing "37d": %s`, err)
- }
- dur37dTZ, err := ParseWindowWithOffsetString("37d", "UTC")
- if err != nil {
- t.Fatalf(`unexpected error parsing "37d": %s`, err)
- }
- if !dur37dUTC.ApproximatelyEqual(dur37dTZ, time.Millisecond) {
- t.Fatalf(`expect: window "37d" UTC to equal "37d" with timezone "UTC"; actual: %s not equal %s`, dur37dUTC, dur37dTZ)
- }
- // ParseWindowWithOffsetString should be the correct relative to ParseWindowUTC; i.e.
- // - for durations, the times should match, but the representations should differ
- // by the number of hours offset
- // - for words like "today" and "yesterday", the times may not match, in which
- // case, for example, "today" UTC-08:00 might equal "yesterday" UTC
- // fmtWindow only compares date and time to the minute, not second or
- // timezone. Helper for comparing timezone shifted windows.
- fmtWindow := func(w Window) string {
- s := "nil"
- if w.start != nil {
- s = w.start.Format("2006-01-02T15:04")
- }
- e := "nil"
- if w.end != nil {
- e = w.end.Format("2006-01-02T15:04")
- }
- return fmt.Sprintf("[%s, %s]", s, e)
- }
- // Test UTC-08:00 (California), UTC+03:00 (Moscow), UTC+12:00 (New Zealand), and UTC itself
- for _, offsetHrs := range []int{-8, 3, 12, 0} {
- offStr := fmt.Sprintf("+%02d:00", offsetHrs)
- if offsetHrs < 0 {
- offStr = fmt.Sprintf("-%02d:00", -offsetHrs)
- }
- off := time.Duration(offsetHrs) * time.Hour
- dur10mTZ, err = ParseWindowWithOffsetString("10m", offStr)
- if err != nil {
- t.Fatalf(`unexpected error parsing "10m": %s`, err)
- }
- if !dur10mTZ.ApproximatelyEqual(dur10mUTC, time.Second) {
- t.Fatalf(`expect: window "10m" UTC to equal "10m" with timezone "%s"; actual: %s not equal %s`, offStr, dur10mUTC, dur10mTZ)
- }
- if fmtWindow(dur10mTZ.Shift(-off)) != fmtWindow(dur10mUTC) {
- 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)))
- }
- dur24hTZ, err = ParseWindowWithOffsetString("24h", offStr)
- if err != nil {
- t.Fatalf(`unexpected error parsing "24h": %s`, err)
- }
- if !dur24hTZ.ApproximatelyEqual(dur24hUTC, time.Second) {
- t.Fatalf(`expect: window "24h" UTC to equal "24h" with timezone "%s"; actual: %s not equal %s`, offStr, dur24hUTC, dur24hTZ)
- }
- if fmtWindow(dur24hTZ.Shift(-off)) != fmtWindow(dur24hUTC) {
- 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)))
- }
- dur37dTZ, err = ParseWindowWithOffsetString("37d", offStr)
- if err != nil {
- t.Fatalf(`unexpected error parsing "37d": %s`, err)
- }
- if !dur37dTZ.ApproximatelyEqual(dur37dUTC, time.Second) {
- t.Fatalf(`expect: window "37d" UTC to equal "37d" with timezone "%s"; actual: %s not equal %s`, offStr, dur37dUTC, dur37dTZ)
- }
- if fmtWindow(dur37dTZ.Shift(-off)) != fmtWindow(dur37dUTC) {
- 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)))
- }
- // "today" and "yesterday" should comply with the current day in each
- // respective timezone, depending on if it is ahead of, equal to, or
- // behind UTC at the given moment.
- todayTZ, err = ParseWindowWithOffsetString("today", offStr)
- if err != nil {
- t.Fatalf(`unexpected error parsing "today": %s`, err)
- }
- yesterdayTZ, err = ParseWindowWithOffsetString("yesterday", offStr)
- if err != nil {
- t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
- }
- hoursSinceYesterdayUTC := time.Now().UTC().Sub(time.Now().UTC().Truncate(24.0 * time.Hour)).Hours()
- hoursUntilTomorrowUTC := 24.0 - hoursSinceYesterdayUTC
- aheadOfUTC := float64(offsetHrs)-hoursUntilTomorrowUTC > 0
- behindUTC := float64(offsetHrs)+hoursSinceYesterdayUTC < 0
- // yesterday in this timezone should equal today UTC
- if aheadOfUTC {
- if fmtWindow(yesterdayTZ) != fmtWindow(todayUTC) {
- t.Fatalf(`expect: window "today" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, yesterdayTZ, todayUTC)
- }
- }
- // today in this timezone should equal yesterday UTC
- if behindUTC {
- if fmtWindow(todayTZ) != fmtWindow(yesterdayUTC) {
- t.Fatalf(`expect: window "today" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, todayTZ, yesterdayUTC)
- }
- }
- // today in this timezone should equal today UTC, likewise for yesterday
- if !aheadOfUTC && !behindUTC {
- if fmtWindow(todayTZ) != fmtWindow(todayUTC) {
- t.Fatalf(`expect: window "today" UTC to equal "today" with timezone "%s"; actual: %s not equal %s`, offStr, todayTZ, todayUTC)
- }
- // yesterday in this timezone should equal yesterday UTC
- if fmtWindow(yesterdayTZ) != fmtWindow(yesterdayUTC) {
- t.Fatalf(`expect: window "yesterday" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, yesterdayTZ, yesterdayUTC)
- }
- }
- }
- }
- func TestWindow_DurationOffsetStrings(t *testing.T) {
- w, err := ParseWindowUTC("1d")
- if err != nil {
- t.Fatalf(`unexpected error parsing "1d": %s`, err)
- }
- dur, off := w.DurationOffsetStrings()
- if dur != "1d" {
- t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
- }
- if off != "" {
- t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
- }
- w, err = ParseWindowUTC("3h")
- if err != nil {
- t.Fatalf(`unexpected error parsing "1d": %s`, err)
- }
- dur, off = w.DurationOffsetStrings()
- if dur != "3h" {
- t.Fatalf(`expect: window to be "3h"; actual: "%s"`, dur)
- }
- if off != "" {
- t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
- }
- w, err = ParseWindowUTC("10m")
- if err != nil {
- t.Fatalf(`unexpected error parsing "1d": %s`, err)
- }
- dur, off = w.DurationOffsetStrings()
- if dur != "10m" {
- t.Fatalf(`expect: window to be "10m"; actual: "%s"`, dur)
- }
- if off != "" {
- t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
- }
- w, err = ParseWindowUTC("1589448338,1589534798")
- if err != nil {
- t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
- }
- dur, off = w.DurationOffsetStrings()
- if dur != "1441m" {
- t.Fatalf(`expect: window to be "1441m"; actual: "%s"`, dur)
- }
- if off == "" {
- t.Fatalf(`expect: offset to not be ""; actual: "%s"`, off)
- }
- w, err = ParseWindowUTC("yesterday")
- if err != nil {
- t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
- }
- dur, _ = w.DurationOffsetStrings()
- if dur != "1d" {
- t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
- }
- }
- func TestParse_Window(t *testing.T) {
- now := time.Date(2024, time.May, 3, 8, 1, 4, 6, time.UTC)
- win, err := parseWindow("2h", now)
- if err != nil {
- t.Fatalf(`unexpected error parsing "2h": %s`, err)
- }
- expectedStart := time.Date(2024, time.May, 3, 7, 0, 0, 0, time.UTC)
- expectedEnd := time.Date(2024, time.May, 3, 9, 0, 0, 0, time.UTC)
- if !win.start.Equal(expectedStart) {
- t.Fatalf(`expect: window start to be %s; actual: %s`, expectedStart, win.start)
- }
- if !win.end.Equal(expectedEnd) {
- t.Fatalf(`expect: window end to be %s; actual: %s`, expectedEnd, win.end)
- }
- win, err = parseWindow("3d", now)
- if err != nil {
- t.Fatalf(`unexpected error parsing "3d": %s`, err)
- }
- expectedStart = time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC)
- expectedEnd = time.Date(2024, time.May, 4, 0, 0, 0, 0, time.UTC)
- if !win.start.Equal(expectedStart) {
- t.Fatalf(`expect: window start to be %s; actual: %s`, expectedStart, win.start)
- }
- if !win.end.Equal(expectedEnd) {
- t.Fatalf(`expect: window end to be %s; actual: %s`, expectedEnd, win.end)
- }
- }
- func TestWindow_Duration(t *testing.T) {
- // Set-up and tear-down
- thanosEnabled := env.GetBool(ThanosEnabledEnvVarName, false)
- defer env.SetBool(ThanosEnabledEnvVarName, thanosEnabled)
- // Test for Prometheus (env.IsThanosEnabled() == false)
- env.SetBool(ThanosEnabledEnvVarName, false)
- if env.GetBool(ThanosEnabledEnvVarName, false) {
- t.Fatalf("expected env.IsThanosEnabled() == false")
- }
- now := time.Now().UTC()
- w, err := parseWindow("1d", now)
- if err != nil {
- t.Fatalf(`unexpected error parsing "1d": %s`, err)
- }
- if w.Duration() != 24*time.Hour {
- t.Fatalf(`expect: window to be 24 hours; actual: %s`, w.Duration())
- }
- w, err = ParseWindowUTC("2h")
- if err != nil {
- t.Fatalf(`unexpected error parsing "2h": %s`, err)
- }
- if w.Duration().String() != "2h0m0s" {
- t.Fatalf(`expect: window to be "2h"; actual: "%s"`, w.Duration().String())
- }
- w, err = ParseWindowUTC("10m")
- if err != nil {
- t.Fatalf(`unexpected error parsing "10m": %s`, err)
- }
- if w.Duration().String() != "10m0s" {
- t.Fatalf(`expect: window to be "10m"; actual: "%s"`, w.Duration().String())
- }
- w, err = ParseWindowUTC("1589448338,1589534798")
- if err != nil {
- t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
- }
- if w.Duration().String() != "24h1m0s" {
- t.Fatalf(`expect: window to be "24h1m0s"; actual: "%s"`, w.Duration().String())
- }
- w, err = ParseWindowUTC("yesterday")
- if err != nil {
- t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
- }
- if w.Duration().String() != "24h0m0s" {
- t.Fatalf(`expect: window to be "24h0m0s"; actual: "%s"`, w.Duration().String())
- }
- }
- // TODO
- // func TestWindow_Overlaps(t *testing.T) {}
- // TODO
- // func TestWindow_Contains(t *testing.T) {}
- // TODO
- // func TestWindow_Duration(t *testing.T) {}
- // TODO
- // func TestWindow_End(t *testing.T) {}
- // TODO
- // func TestWindow_Equal(t *testing.T) {}
- // TODO
- // func TestWindow_ExpandStart(t *testing.T) {}
- // TODO
- // func TestWindow_ExpandEnd(t *testing.T) {}
- func TestWindow_Expand(t *testing.T) {
- t1 := time.Now().Round(time.Hour)
- t2 := t1.Add(34 * time.Minute)
- t3 := t1.Add(50 * time.Minute)
- t4 := t1.Add(84 * time.Minute)
- cases := []struct {
- windowToExpand Window
- windowArgument Window
- expected Window
- }{
- {
- windowToExpand: NewClosedWindow(t1, t2),
- windowArgument: NewClosedWindow(t3, t4),
- expected: NewClosedWindow(t1, t4),
- },
- {
- windowToExpand: NewClosedWindow(t3, t4),
- windowArgument: NewClosedWindow(t1, t2),
- expected: NewClosedWindow(t1, t4),
- },
- {
- windowToExpand: NewClosedWindow(t1, t3),
- windowArgument: NewClosedWindow(t2, t4),
- expected: NewClosedWindow(t1, t4),
- },
- {
- windowToExpand: NewClosedWindow(t2, t4),
- windowArgument: NewClosedWindow(t1, t3),
- expected: NewClosedWindow(t1, t4),
- },
- {
- windowToExpand: Window{},
- windowArgument: NewClosedWindow(t1, t2),
- expected: NewClosedWindow(t1, t2),
- },
- {
- windowToExpand: NewWindow(nil, &t2),
- windowArgument: NewWindow(nil, &t3),
- expected: NewWindow(nil, &t3),
- },
- {
- windowToExpand: NewWindow(&t2, nil),
- windowArgument: NewWindow(&t1, nil),
- expected: NewWindow(&t1, nil),
- },
- }
- for _, c := range cases {
- result := c.windowToExpand.Expand(c.windowArgument)
- if !result.Equal(c.expected) {
- t.Errorf("Expand %s with %s, expected %s but got %s", c.windowToExpand, c.windowArgument, c.expected, result)
- }
- }
- }
- // TODO
- // func TestWindow_Start(t *testing.T) {}
- // TODO
- // func TestWindow_String(t *testing.T) {}
- func TestWindow_GetPercentInWindow(t *testing.T) {
- dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
- dayEnd := dayStart.Add(timeutil.Day)
- window := NewClosedWindow(dayStart, dayEnd)
- testcases := map[string]struct {
- window Window
- itemStart time.Time
- itemEnd time.Time
- expected float64
- }{
- "matching start/matching end": {
- window: window,
- itemStart: dayStart,
- itemEnd: dayEnd,
- expected: 1.0,
- },
- "matching start/contained end": {
- window: window,
- itemStart: dayStart,
- itemEnd: dayEnd.Add(-time.Hour * 6),
- expected: 1.0,
- },
- "contained start/matching end": {
- window: window,
- itemStart: dayStart.Add(time.Hour * 6),
- itemEnd: dayEnd,
- expected: 1.0,
- },
- "contained start/contained end": {
- window: window,
- itemStart: dayStart.Add(time.Hour * 6),
- itemEnd: dayEnd.Add(-time.Hour * 6),
- expected: 1.0,
- },
- "before start/contained end": {
- window: window,
- itemStart: dayStart.Add(-time.Hour * 12),
- itemEnd: dayEnd.Add(-time.Hour * 12),
- expected: 0.5,
- },
- "before start/before end": {
- window: window,
- itemStart: dayStart.Add(-time.Hour * 24),
- itemEnd: dayEnd.Add(-time.Hour * 24),
- expected: 0.0,
- },
- "contained start/after end": {
- window: window,
- itemStart: dayStart.Add(time.Hour * 12),
- itemEnd: dayEnd.Add(time.Hour * 12),
- expected: 0.5,
- },
- "after start/after end": {
- window: window,
- itemStart: dayStart.Add(time.Hour * 24),
- itemEnd: dayEnd.Add(time.Hour * 24),
- expected: 0.0,
- },
- "before start/after end": {
- window: window,
- itemStart: dayStart.Add(-time.Hour * 12),
- itemEnd: dayEnd.Add(time.Hour * 12),
- expected: 0.5,
- },
- }
- for name, tc := range testcases {
- t.Run(name, func(t *testing.T) {
- thatWindow := NewWindow(&tc.itemStart, &tc.itemEnd)
- if actual := tc.window.GetPercentInWindow(thatWindow); actual != tc.expected {
- t.Errorf("GetPercentInWindow() = %v, want %v", actual, tc.expected)
- }
- })
- }
- }
- func TestWindow_GetWindows(t *testing.T) {
- dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
- dayEnd := dayStart.Add(timeutil.Day)
- loc, _ := time.LoadLocation("America/Vancouver")
- testCases := map[string]struct {
- start time.Time
- end time.Time
- windowSize time.Duration
- expected []Window
- expectedErr bool
- }{
- "mismatching tz": {
- start: dayStart,
- end: dayEnd.In(loc),
- windowSize: time.Hour,
- expected: nil,
- expectedErr: true,
- },
- "hour windows over 1 hours": {
- start: dayStart,
- end: dayStart.Add(time.Hour),
- windowSize: time.Hour,
- expected: []Window{
- NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
- },
- expectedErr: false,
- },
- "hour windows over 3 hours": {
- start: dayStart,
- end: dayStart.Add(time.Hour * 3),
- windowSize: time.Hour,
- expected: []Window{
- NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
- NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Hour*2)),
- NewClosedWindow(dayStart.Add(time.Hour*2), dayStart.Add(time.Hour*3)),
- },
- expectedErr: false,
- },
- "hour windows off hour grid": {
- start: dayStart.Add(time.Minute),
- end: dayEnd.Add(time.Minute),
- windowSize: time.Hour,
- expected: nil,
- expectedErr: true,
- },
- "hour windows range not divisible by hour": {
- start: dayStart,
- end: dayStart.Add(time.Minute * 90),
- windowSize: time.Hour,
- expected: nil,
- expectedErr: true,
- },
- "day windows over 1 day": {
- start: dayStart,
- end: dayEnd,
- windowSize: timeutil.Day,
- expected: []Window{
- NewClosedWindow(dayStart, dayEnd),
- },
- expectedErr: false,
- },
- "day windows over 3 days": {
- start: dayStart,
- end: dayStart.Add(timeutil.Day * 3),
- windowSize: timeutil.Day,
- expected: []Window{
- NewClosedWindow(dayStart, dayStart.Add(timeutil.Day)),
- NewClosedWindow(dayStart.Add(timeutil.Day), dayStart.Add(timeutil.Day*2)),
- NewClosedWindow(dayStart.Add(timeutil.Day*2), dayStart.Add(timeutil.Day*3)),
- },
- expectedErr: false,
- },
- "day windows off day grid": {
- start: dayStart.Add(time.Hour),
- end: dayEnd.Add(time.Hour),
- windowSize: timeutil.Day,
- expected: nil,
- expectedErr: true,
- },
- "day windows range not divisible by day": {
- start: dayStart,
- end: dayEnd.Add(time.Hour),
- windowSize: timeutil.Day,
- expected: nil,
- expectedErr: true,
- },
- }
- for name, tc := range testCases {
- t.Run(name, func(t *testing.T) {
- actual, err := GetWindows(tc.start, tc.end, tc.windowSize)
- if (err != nil) != tc.expectedErr {
- t.Errorf("GetWindows() error = %v, expectedErr %v", err, tc.expectedErr)
- return
- }
- if len(tc.expected) != len(actual) {
- t.Errorf("GetWindows() []window has incorrect length expected: %d, actual: %d", len(tc.expected), len(actual))
- }
- for i, actualWindow := range actual {
- expectedWindow := tc.expected[i]
- if !actualWindow.Equal(expectedWindow) {
- t.Errorf("GetWindow() window at index %d were not equal expected: %s, actual %s", i, expectedWindow.String(), actualWindow)
- }
- }
- })
- }
- }
- func TestWindow_GetWindowsForQueryWindow(t *testing.T) {
- dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
- dayEnd := dayStart.Add(timeutil.Day)
- loc, _ := time.LoadLocation("America/Vancouver")
- testCases := map[string]struct {
- start time.Time
- end time.Time
- windowSize time.Duration
- expected []Window
- expectedErr bool
- }{
- "mismatching tz": {
- start: dayStart,
- end: dayEnd.In(loc),
- windowSize: time.Hour,
- expected: nil,
- expectedErr: true,
- },
- "hour windows over 1 hours": {
- start: dayStart,
- end: dayStart.Add(time.Hour),
- windowSize: time.Hour,
- expected: []Window{
- NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
- },
- expectedErr: false,
- },
- "hour windows over 3 hours": {
- start: dayStart,
- end: dayStart.Add(time.Hour * 3),
- windowSize: time.Hour,
- expected: []Window{
- NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
- NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Hour*2)),
- NewClosedWindow(dayStart.Add(time.Hour*2), dayStart.Add(time.Hour*3)),
- },
- expectedErr: false,
- },
- "hour windows off hour grid": {
- start: dayStart.Add(time.Minute),
- end: dayStart.Add(time.Minute * 61),
- windowSize: time.Hour,
- expected: []Window{
- NewClosedWindow(dayStart.Add(time.Minute), dayStart.Add(time.Minute*61)),
- },
- expectedErr: false,
- },
- "hour windows range not divisible by hour": {
- start: dayStart,
- end: dayStart.Add(time.Minute * 90),
- windowSize: time.Hour,
- expected: []Window{
- NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
- NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Minute*90)),
- },
- expectedErr: false,
- },
- "day windows over 1 day": {
- start: dayStart,
- end: dayEnd,
- windowSize: timeutil.Day,
- expected: []Window{
- NewClosedWindow(dayStart, dayEnd),
- },
- expectedErr: false,
- },
- "day windows over 3 days": {
- start: dayStart,
- end: dayStart.Add(timeutil.Day * 3),
- windowSize: timeutil.Day,
- expected: []Window{
- NewClosedWindow(dayStart, dayStart.Add(timeutil.Day)),
- NewClosedWindow(dayStart.Add(timeutil.Day), dayStart.Add(timeutil.Day*2)),
- NewClosedWindow(dayStart.Add(timeutil.Day*2), dayStart.Add(timeutil.Day*3)),
- },
- expectedErr: false,
- },
- "day windows off day grid": {
- start: dayStart.Add(time.Hour),
- end: dayEnd.Add(time.Hour),
- windowSize: timeutil.Day,
- expected: []Window{
- NewClosedWindow(dayStart.Add(time.Hour), dayEnd.Add(time.Hour)),
- },
- expectedErr: false,
- },
- "day windows range not divisible by day": {
- start: dayStart,
- end: dayEnd.Add(time.Hour),
- windowSize: timeutil.Day,
- expected: []Window{
- NewClosedWindow(dayStart, dayEnd),
- NewClosedWindow(dayEnd, dayEnd.Add(time.Hour)),
- },
- expectedErr: false,
- },
- }
- for name, tc := range testCases {
- t.Run(name, func(t *testing.T) {
- actual, err := GetWindowsForQueryWindow(tc.start, tc.end, tc.windowSize)
- if (err != nil) != tc.expectedErr {
- t.Errorf("GetWindowsForQueryWindow() error = %v, expectedErr %v", err, tc.expectedErr)
- return
- }
- if len(tc.expected) != len(actual) {
- t.Errorf("GetWindowsForQueryWindow() []window has incorrect length expected: %d, actual: %d", len(tc.expected), len(actual))
- }
- for i, actualWindow := range actual {
- expectedWindow := tc.expected[i]
- if !actualWindow.Equal(expectedWindow) {
- t.Errorf("GetWindowsForQueryWindow() window at index %d were not equal expected: %s, actual %s", i, expectedWindow.String(), actualWindow)
- }
- }
- })
- }
- }
- func TestMarshalUnmarshal(t *testing.T) {
- t1 := time.Date(2023, 03, 11, 01, 29, 15, 0, time.UTC)
- t2 := t1.Add(8 * time.Minute)
- cases := []struct {
- w Window
- }{
- {
- w: NewClosedWindow(t1, t2),
- },
- {
- w: NewWindow(&t1, nil),
- },
- {
- w: NewWindow(nil, &t2),
- },
- {
- w: NewWindow(nil, nil),
- },
- }
- for _, c := range cases {
- name := c.w.String()
- t.Run(name, func(t *testing.T) {
- marshaled, err := json.Marshal(c.w)
- if err != nil {
- t.Fatalf("marshaling: %s", err)
- }
- var unmarshaledW Window
- err = json.Unmarshal(marshaled, &unmarshaledW)
- if err != nil {
- t.Fatalf("unmarshaling: %s", err)
- }
- if diff := cmp.Diff(c.w, unmarshaledW); len(diff) > 0 {
- t.Errorf(diff)
- }
- })
- }
- }
- func TestBoundaryErrorIs(t *testing.T) {
- baseError := &BoundaryError{Message: "cde"}
- singleWrapError := fmt.Errorf("wrap: %w", &BoundaryError{Message: "abc"})
- if errors.Is(singleWrapError, baseError) {
- t.Logf("Single wrap success")
- } else {
- t.Errorf("Single wrap failure: %s != %s", baseError, singleWrapError)
- }
- multiWrapError := fmt.Errorf("wrap: %w", &BoundaryError{Message: "abc"})
- multiWrapError = fmt.Errorf("wrap x2: %w", multiWrapError)
- multiWrapError = fmt.Errorf("wrap x3: %w", multiWrapError)
- if errors.Is(multiWrapError, baseError) {
- t.Logf("Multi wrap success")
- } else {
- t.Errorf("Multi wrap failure: %s != %s", baseError, multiWrapError)
- }
- }
|