From 08e5db952e6e7be03b4697d371f9e8435566ce90 Mon Sep 17 00:00:00 2001 From: Jeremiah Date: Sat, 3 Dec 2022 00:25:37 -0500 Subject: [PATCH 1/6] 14: implement timeslot and tests --- config.test.yml | 4 ++- content/program.go | 2 +- content/program_test.go | 66 +++++++++++++++++++++------------- content/schedule.go | 76 ++++++++++++++++++++-------------------- content/schedule_test.go | 11 +++--- content/timeslot.go | 31 ++++++++++++++++ content/timeslot_test.go | 69 ++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 5 +++ 9 files changed, 197 insertions(+), 68 deletions(-) create mode 100644 content/timeslot.go create mode 100644 content/timeslot_test.go diff --git a/config.test.yml b/config.test.yml index db43247..b441d78 100644 --- a/config.test.yml +++ b/config.test.yml @@ -3,5 +3,7 @@ content: Programs: - Name: "gettysburg10" Type: "file" - Timeslot: "afternoon" Source: "./static/gettysburg10.wav" + Timeslot: + Begin: "11:00PM" + End: "11:30PM" diff --git a/content/program.go b/content/program.go index df51fc4..24d9bb3 100644 --- a/content/program.go +++ b/content/program.go @@ -8,7 +8,7 @@ import ( type Program struct { Name string Source string - Timeslot Timeslot + Timeslot *Timeslot Type MediaType } diff --git a/content/program_test.go b/content/program_test.go index 2ad3497..c1f9c68 100644 --- a/content/program_test.go +++ b/content/program_test.go @@ -20,51 +20,69 @@ func TestProgram_GetMedia(t *testing.T) { name: "Success: returns folder", fields: fields{ program: &Program{ - Name: "David Rovics Folder", - Source: "./static/david_rovics", - Timeslot: "early", - Type: "folder", + Name: "David Rovics Folder", + Source: "./static/david_rovics", + Timeslot: &Timeslot{ + Begin: "11:00PM", + End: "11:30PM", + }, + Type: "folder", }, }, want: (&Program{ - Name: "David Rovics Folder", - Source: "./static/david_rovics", - Timeslot: "early", - Type: "folder", + Name: "David Rovics Folder", + Source: "./static/david_rovics", + Timeslot: &Timeslot{ + Begin: "11:00PM", + End: "11:30PM", + }, + Type: "folder", }).GetMedia(), }, { name: "Success: returns file", fields: fields{ program: &Program{ - Name: "Piano Six Seconds", - Source: "./static/piano_six_seconds.mp3", - Timeslot: "afternoon", - Type: "file", + Name: "Piano Six Seconds", + Source: "./static/piano_six_seconds.mp3", + Timeslot: &Timeslot{ + Begin: "11:00PM", + End: "11:30PM", + }, + Type: "file", }, }, want: (&Program{ - Name: "Piano Six Seconds", - Source: "./static/piano_six_seconds.mp3", - Timeslot: "afternoon", - Type: "file", + Name: "Piano Six Seconds", + Source: "./static/piano_six_seconds.mp3", + Timeslot: &Timeslot{ + Begin: "11:00PM", + End: "11:30PM", + }, + Type: "file", }).GetMedia(), }, { name: "Success: returns web radio", fields: fields{ program: &Program{ - Name: "Indie Pop Rocks", - Source: "https://somafm.com/indiepop.pls", - Timeslot: "any", - Type: "web_radio", + Name: "Indie Pop Rocks", + Source: "https://somafm.com/indiepop.pls", + Timeslot: &Timeslot{ + Begin: "11:00PM", + End: "11:30PM", + }, + Type: "web_radio", }, }, want: (&Program{ - Name: "Indie Pop Rocks", - Source: "https://somafm.com/indiepop.pls", - Timeslot: "any", - Type: "web_radio", + Name: "Indie Pop Rocks", + Source: "https://somafm.com/indiepop.pls", + Timeslot: &Timeslot{ + Begin: "11:00PM", + End: "11:30PM", + }, + Type: "web_radio", }).GetMedia(), }, { diff --git a/content/schedule.go b/content/schedule.go index 92b5a55..ea1cee0 100644 --- a/content/schedule.go +++ b/content/schedule.go @@ -13,40 +13,41 @@ import ( "time" ) -const ( - Early Timeslot = "early" - Morning Timeslot = "morning" - Breakfast Timeslot = "breakfast" - Midmorning Timeslot = "midmorning" - Afternoon Timeslot = "afternoon" - Commute Timeslot = "commute" - Evening Timeslot = "evening" - Late Timeslot = "late" - Overnight Timeslot = "overnight" - All Timeslot = "all" -) - -type Timeslot string - -type Slot struct { - Begin string - End string -} +//const ( +// Early Timeslot = "early" +// Morning Timeslot = "morning" +// Breakfast Timeslot = "breakfast" +// Midmorning Timeslot = "midmorning" +// Afternoon Timeslot = "afternoon" +// Commute Timeslot = "commute" +// Evening Timeslot = "evening" +// Late Timeslot = "late" +// Overnight Timeslot = "overnight" +// All Timeslot = "all" +//) +// +//type Timeslot string +// +//type Slot struct { +// Begin string +// End string +//} var Shuffled bool -var TimeslotMap = map[Timeslot]*Slot{ - Early: {"4:00 AM", "6:00 AM"}, - Morning: {"6:00 AM", "8:00 AM"}, - Breakfast: {"8:00 AM", "11:00 AM"}, - Midmorning: {"11:00 AM", "2:00 PM"}, - Afternoon: {"2:00 PM", "5:00 PM"}, - Commute: {"5:00 PM", "7:00 PM"}, - Evening: {"7:00 PM", "11:00 PM"}, - Late: {"11:00 PM", "2:00 AM"}, - Overnight: {"2:00 AM", "4:00 AM"}, - All: {"12:00 AM", "12:00 PM"}, -} +// +//var TimeslotMap = map[Timeslot]*Slot{ +// Early: {"4:00 AM", "6:00 AM"}, +// Morning: {"6:00 AM", "8:00 AM"}, +// Breakfast: {"8:00 AM", "11:00 AM"}, +// Midmorning: {"11:00 AM", "2:00 PM"}, +// Afternoon: {"2:00 PM", "5:00 PM"}, +// Commute: {"5:00 PM", "7:00 PM"}, +// Evening: {"7:00 PM", "11:00 PM"}, +// Late: {"11:00 PM", "2:00 AM"}, +// Overnight: {"2:00 AM", "4:00 AM"}, +// All: {"12:00 AM", "12:00 PM"}, +//} type Scheduler struct { Content struct { @@ -59,8 +60,7 @@ func (s *Scheduler) Run() error { log.Infof("Press ESC to quit") // set up the loop to continuously check for key entries - now := time.Now() - ts := getTimeSlot(&now) + // if randomized mode do x // setup signal listeners @@ -72,7 +72,7 @@ func (s *Scheduler) Run() error { for _, p := range s.Content.Programs { log.Debugf("program %v", formatter.StructToIndentedString(p)) // Check Timeslots - if ts == p.Timeslot || ts == All { + if p.Timeslot.IsScheduledNow() { log.Infof("getting media type: %v", p.Type) content := p.GetMedia() log.Debugf("media struct: %v", content) @@ -154,10 +154,10 @@ func (s *Scheduler) Stop(signal os.Signal, media Media) { } } -func getTimeSlot(t *time.Time) Timeslot { - // if t between certain times return Timeslot - return All -} +//func getTimeSlot(t *time.Time) Timeslot { +// // if t between certain times return Timeslot +// return All +//} func NewScheduler(file string) (*Scheduler, error) { log.Info("Loading Config File from: ", file) diff --git a/content/schedule_test.go b/content/schedule_test.go index c480192..a13ae50 100644 --- a/content/schedule_test.go +++ b/content/schedule_test.go @@ -30,10 +30,13 @@ func TestNewScheduler(t *testing.T) { scheduler: &Scheduler{ Content: struct{ Programs []*Program }{Programs: []*Program{ { - Name: "gettysburg10", - Source: "./static/gettysburg10.wav", - Timeslot: Timeslot("afternoon"), - Type: MediaType("file"), + Name: "gettysburg10", + Source: "./static/gettysburg10.wav", + Timeslot: &Timeslot{ + Begin: "11:00PM", + End: "11:30PM", + }, + Type: MediaType("file"), }, }}, }, diff --git a/content/timeslot.go b/content/timeslot.go new file mode 100644 index 0000000..d879d80 --- /dev/null +++ b/content/timeslot.go @@ -0,0 +1,31 @@ +package content + +import ( + "time" +) + +// Times represents timeslots and are parsed in a 24hour format +type Timeslot struct { + Current time.Time + Begin string + End string +} + +// IsScheduledNow checks the current time and returns a bool if the time falls within the range +func (t *Timeslot) IsScheduledNow() bool { + startTime, _ := time.Parse(time.Kitchen, t.Begin) + endTime, _ := time.Parse(time.Kitchen, t.End) + + return inTimeSpan(startTime, endTime, t.Current) + +} + +func inTimeSpan(start, end, current time.Time) bool { + + // handle scheduling that traverses days. + if end.Before(start) && current.After(start) { + return true + } + + return current.After(start) && current.Before(end) +} diff --git a/content/timeslot_test.go b/content/timeslot_test.go new file mode 100644 index 0000000..fd8ad6a --- /dev/null +++ b/content/timeslot_test.go @@ -0,0 +1,69 @@ +package content + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +type TimeProvider interface { + Now() time.Time +} + +type testTime struct { + TimeProvider +} + +func (t *testTime) Now() time.Time { + now, _ := time.Parse(time.Kitchen, "11:27PM") + return now +} + +func TestTimes_IsScheduledNow(t1 *testing.T) { + type fields struct { + Current time.Time + Begin string + End string + } + tests := []struct { + name string + fields fields + want bool + }{ + { + name: "Returns True", + fields: fields{ + Begin: "11:00PM", + End: "11:59PM", + }, + want: true, + }, + { + name: "Returns False", + fields: fields{ + Begin: "11:28PM", + End: "10:47PM", + }, + want: false, + }, + { + name: "Success: evaluates true for times that traverse days", + fields: fields{ + Begin: "11:00PM", + End: "2:30AM", + }, + want: true, + }, + } + for _, tt := range tests { + t1.Run(tt.name, func(t1 *testing.T) { + timeTest := &testTime{} + t := &Timeslot{ + Current: timeTest.Now(), + Begin: tt.fields.Begin, + End: tt.fields.End, + } + assert.Equalf(t1, tt.want, t.IsScheduledNow(), "IsScheduledNow()") + }) + } +} diff --git a/go.mod b/go.mod index 6b504bb..c5dd737 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( ) require ( + github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.4.9 // indirect diff --git a/go.sum b/go.sum index 497939e..d98d523 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= +github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhPwqqXc4/vE0f7GvRjuAsbW+HOIe8KnA= +github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= @@ -209,6 +211,7 @@ github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPK github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mewkiz/flac v1.0.7 h1:uIXEjnuXqdRaZttmSFM5v5Ukp4U6orrZsnYGGR3yow8= github.com/mewkiz/flac v1.0.7/go.mod h1:yU74UH277dBUpqxPouHSQIar3G1X/QIclVbFahSd1pU= github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2 h1:EyTNMdePWaoWsRSGQnXiSoQu0r6RS1eA557AwJhlzHU= @@ -237,11 +240,13 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= From 0d9d261ede925b23bbe9a8b1c1c8dcca227ad711 Mon Sep 17 00:00:00 2001 From: Jeremiah Date: Mon, 5 Dec 2022 21:07:11 -0500 Subject: [PATCH 2/6] 14: refactor timeslot --- content/schedule.go | 65 ++++++++++++--------------------------------- 1 file changed, 17 insertions(+), 48 deletions(-) diff --git a/content/schedule.go b/content/schedule.go index ea1cee0..2c3b9a1 100644 --- a/content/schedule.go +++ b/content/schedule.go @@ -13,55 +13,18 @@ import ( "time" ) -//const ( -// Early Timeslot = "early" -// Morning Timeslot = "morning" -// Breakfast Timeslot = "breakfast" -// Midmorning Timeslot = "midmorning" -// Afternoon Timeslot = "afternoon" -// Commute Timeslot = "commute" -// Evening Timeslot = "evening" -// Late Timeslot = "late" -// Overnight Timeslot = "overnight" -// All Timeslot = "all" -//) -// -//type Timeslot string -// -//type Slot struct { -// Begin string -// End string -//} - var Shuffled bool -// -//var TimeslotMap = map[Timeslot]*Slot{ -// Early: {"4:00 AM", "6:00 AM"}, -// Morning: {"6:00 AM", "8:00 AM"}, -// Breakfast: {"8:00 AM", "11:00 AM"}, -// Midmorning: {"11:00 AM", "2:00 PM"}, -// Afternoon: {"2:00 PM", "5:00 PM"}, -// Commute: {"5:00 PM", "7:00 PM"}, -// Evening: {"7:00 PM", "11:00 PM"}, -// Late: {"11:00 PM", "2:00 AM"}, -// Overnight: {"2:00 AM", "4:00 AM"}, -// All: {"12:00 AM", "12:00 PM"}, -//} - type Scheduler struct { Content struct { Programs []*Program } } -func (s *Scheduler) Run() error { +func (s *Scheduler) Run(currentTime time.Time) error { log.Info("Starting Daemon") log.Infof("Press ESC to quit") - // set up the loop to continuously check for key entries - - // if randomized mode do x // setup signal listeners sigchnl := make(chan os.Signal, 1) @@ -70,31 +33,37 @@ func (s *Scheduler) Run() error { // check content from scheduler and run through it for _, p := range s.Content.Programs { + now := currentTime log.Debugf("program %v", formatter.StructToIndentedString(p)) - // Check Timeslots - if p.Timeslot.IsScheduledNow() { + + if p.Timeslot.IsScheduledNow(now) { log.Infof("getting media type: %v", p.Type) content := p.GetMedia() log.Debugf("media struct: %v", content) - content.Get() + err := content.Get() // retrieve contents from file + if err != nil { + return err + } go func() { for { stop := <-sigchnl s.Stop(stop, content) } }() - err := content.Play() + err = content.Play() if err != nil { return err } // play will block until done } + + if !p.Timeslot.IsScheduledNow(now) { + log.WithField("IsScheduledNow", p.Timeslot.IsScheduledNow(now)). + WithField("current time", time.Now(). + Format(time.Kitchen)).Infof("media not scheduled") + } + // TODO make these checks run in a loop and always check if programs should be playing } - // if radio station start 30 minute counter. - // smartly allocate programs to timeslots based on length if known - // if time between TimeSlotMap do x - // play program from that slot. - // wait for program to finish - // get next + exitcode := <-exitchnl os.Exit(exitcode) return nil From 37f4abf5a1e3b864d3560ff7e6924ae629e1e13f Mon Sep 17 00:00:00 2001 From: Jeremiah Date: Mon, 5 Dec 2022 21:08:45 -0500 Subject: [PATCH 3/6] 14: refactor timeslot --- content/timeslot.go | 33 ++++++++++++++++++++++++--------- content/timeslot_test.go | 25 +++++++++++++------------ 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/content/timeslot.go b/content/timeslot.go index d879d80..e7c4af7 100644 --- a/content/timeslot.go +++ b/content/timeslot.go @@ -1,31 +1,46 @@ package content import ( + "github.com/araddon/dateparse" + log "github.com/sirupsen/logrus" + "strconv" "time" ) // Times represents timeslots and are parsed in a 24hour format type Timeslot struct { - Current time.Time - Begin string - End string + Begin string + End string } // IsScheduledNow checks the current time and returns a bool if the time falls within the range -func (t *Timeslot) IsScheduledNow() bool { - startTime, _ := time.Parse(time.Kitchen, t.Begin) - endTime, _ := time.Parse(time.Kitchen, t.End) +func (t *Timeslot) IsScheduledNow(current time.Time) bool { + // get date info for string + date := time.Date(current.Year(), current.Month(), current.Day(), 0, 0, 0, 0, time.Local) + year, month, day := date.Date() - return inTimeSpan(startTime, endTime, t.Current) + // convert ints to dateString + dateString := strconv.Itoa(year) + "-" + strconv.Itoa(int(month)) + "-" + strconv.Itoa(day) + // parse the date and the config time + // parsed times are returned in 2022-12-05 15:05:00 +0000 UTC format which doesn't appear to have a const in the time package + parsedStartTime, _ := dateparse.ParseAny(dateString + " " + t.Begin) + parsedEndTime, _ := dateparse.ParseAny(dateString + " " + t.End) + + // matched parse time to fixed zone time + startTime := time.Date(parsedStartTime.Year(), parsedStartTime.Month(), parsedStartTime.Day(), parsedStartTime.Hour(), parsedStartTime.Minute(), parsedStartTime.Second(), parsedStartTime.Nanosecond(), time.Local) + endTime := time.Date(parsedEndTime.Year(), parsedEndTime.Month(), parsedEndTime.Day(), parsedEndTime.Hour(), parsedEndTime.Minute(), parsedEndTime.Second(), parsedEndTime.Nanosecond(), time.Local) + + return inTimeSpan(startTime, endTime, current) } func inTimeSpan(start, end, current time.Time) bool { - + log.WithField("start", start).WithField("current", current).WithField("end", end).Info("timeslot::inTimeSpan: configured times") // handle scheduling that traverses days. + tz, _ := time.LoadLocation("UTC") if end.Before(start) && current.After(start) { return true } - return current.After(start) && current.Before(end) + return current.In(tz).After(start) && current.In(tz).Before(end) } diff --git a/content/timeslot_test.go b/content/timeslot_test.go index fd8ad6a..b8d689a 100644 --- a/content/timeslot_test.go +++ b/content/timeslot_test.go @@ -1,6 +1,7 @@ package content import ( + log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "testing" "time" @@ -15,7 +16,9 @@ type testTime struct { } func (t *testTime) Now() time.Time { - now, _ := time.Parse(time.Kitchen, "11:27PM") + tz, _ := time.LoadLocation("EST") + now := time.Date(2022, 12, 05, 23, 27, 0, 0, tz) + log.Infof("time %v", now) return now } @@ -33,37 +36,35 @@ func TestTimes_IsScheduledNow(t1 *testing.T) { { name: "Returns True", fields: fields{ - Begin: "11:00PM", - End: "11:59PM", + Begin: "11:00 PM", + End: "11:59 PM", }, want: true, }, { name: "Returns False", fields: fields{ - Begin: "11:28PM", - End: "10:47PM", + Begin: "11:28 PM", + End: "10:47 PM", }, want: false, }, { name: "Success: evaluates true for times that traverse days", fields: fields{ - Begin: "11:00PM", - End: "2:30AM", + Begin: "11:00 PM", + End: "2:30 AM", }, want: true, }, } for _, tt := range tests { t1.Run(tt.name, func(t1 *testing.T) { - timeTest := &testTime{} t := &Timeslot{ - Current: timeTest.Now(), - Begin: tt.fields.Begin, - End: tt.fields.End, + Begin: tt.fields.Begin, + End: tt.fields.End, } - assert.Equalf(t1, tt.want, t.IsScheduledNow(), "IsScheduledNow()") + assert.Equalf(t1, tt.want, t.IsScheduledNow((&testTime{}).Now()), "IsScheduledNow()") }) } } From f06c078e25ba246d7c07230e661dbe8b77c77085 Mon Sep 17 00:00:00 2001 From: Jeremiah Date: Mon, 5 Dec 2022 21:09:42 -0500 Subject: [PATCH 4/6] 14: pass time.Now() into Run() --- main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index cb2c96b..b10ef55 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/urfave/cli" "os" + "time" ) const ( @@ -37,7 +38,7 @@ func main() { return } // run content normally - err = scheduler.Run() + err = scheduler.Run(time.Now()) if err != nil { log.WithError(err).Error("unable to run go-dj") } From 8023a9101c1101c57603dbc77a7ec494dd5cd47d Mon Sep 17 00:00:00 2001 From: Jeremiah Date: Mon, 5 Dec 2022 21:11:36 -0500 Subject: [PATCH 5/6] 14: add test config --- config.test.yml | 5 +++-- config_sample.yml | 7 ------- 2 files changed, 3 insertions(+), 9 deletions(-) delete mode 100644 config_sample.yml diff --git a/config.test.yml b/config.test.yml index b441d78..f47d28b 100644 --- a/config.test.yml +++ b/config.test.yml @@ -5,5 +5,6 @@ content: Type: "file" Source: "./static/gettysburg10.wav" Timeslot: - Begin: "11:00PM" - End: "11:30PM" + - Begin: "11:00PM" + - End: "11:30PM" + diff --git a/config_sample.yml b/config_sample.yml deleted file mode 100644 index 5a22f0e..0000000 --- a/config_sample.yml +++ /dev/null @@ -1,7 +0,0 @@ -version: 0.0.1 -content: - Programs: - - Name: "" - Type: "" - Slot: "" - Source: From 91e7908d6bc42302e82afe6e121ec228df04f133 Mon Sep 17 00:00:00 2001 From: Jeremiah Date: Mon, 5 Dec 2022 21:18:54 -0500 Subject: [PATCH 6/6] 14: update tests --- config.test.yml | 4 ++-- content/schedule_test.go | 4 +--- content/timeslot_test.go | 11 +++++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/config.test.yml b/config.test.yml index f47d28b..183dddd 100644 --- a/config.test.yml +++ b/config.test.yml @@ -5,6 +5,6 @@ content: Type: "file" Source: "./static/gettysburg10.wav" Timeslot: - - Begin: "11:00PM" - - End: "11:30PM" + Begin: "11:00PM" + End: "11:30PM" diff --git a/content/schedule_test.go b/content/schedule_test.go index a13ae50..073a949 100644 --- a/content/schedule_test.go +++ b/content/schedule_test.go @@ -8,7 +8,6 @@ import ( ) func TestNewScheduler(t *testing.T) { - t.Parallel() type args struct { file string } @@ -60,10 +59,9 @@ func TestNewScheduler(t *testing.T) { wantErr: true, }, } + // TODO make test pass when running in parallel and troubleshoot race condition. for _, tt := range tests { - tt := tt t.Run(tt.name, func(t *testing.T) { - t.Parallel() got, err := NewScheduler(tt.args.file) if err != nil && tt.wantErr { assert.Error(t, err) diff --git a/content/timeslot_test.go b/content/timeslot_test.go index b8d689a..f583fe3 100644 --- a/content/timeslot_test.go +++ b/content/timeslot_test.go @@ -15,14 +15,15 @@ type testTime struct { TimeProvider } -func (t *testTime) Now() time.Time { +func (testTime *testTime) Now() time.Time { tz, _ := time.LoadLocation("EST") now := time.Date(2022, 12, 05, 23, 27, 0, 0, tz) - log.Infof("time %v", now) + log.Infof("testTime %v", now) return now } -func TestTimes_IsScheduledNow(t1 *testing.T) { +func TestTimes_IsScheduledNow(t *testing.T) { + t.Parallel() type fields struct { Current time.Time Begin string @@ -59,7 +60,9 @@ func TestTimes_IsScheduledNow(t1 *testing.T) { }, } for _, tt := range tests { - t1.Run(tt.name, func(t1 *testing.T) { + tt := tt + t.Run(tt.name, func(t1 *testing.T) { + t1.Parallel() t := &Timeslot{ Begin: tt.fields.Begin, End: tt.fields.End,