Skip to content

Commit

Permalink
adept test to random number generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mateo-ivc committed Apr 8, 2024
1 parent 899e3a5 commit fe74083
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions server/src/services/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func NewSchedulerService(db DB, scheduler gocron.Scheduler, path string) *Schedu
s := new(SchedulerService)
s.database = db
s.scheduler = scheduler
s.StartScheduler(path)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
s.StartScheduler(path, strconv.Itoa(r.Intn(60)))
return s
}

Expand All @@ -37,7 +38,7 @@ This methode initializes and starts the scheduler.
When adding a new job to the scheduler, extend the switch case with the job's methode
*/

func (s *SchedulerService) StartScheduler(path string) {
func (s *SchedulerService) StartScheduler(path string, delay string) {
ctx := context.Background()
log := logger.FromContext(ctx)
conf, err := parseFile(path)
Expand All @@ -51,7 +52,6 @@ func (s *SchedulerService) StartScheduler(path string) {
case "Delete Unused Boards":
params = append(params, ctx)
task = s.deleteUnusedBoards
delay := strconv.Itoa(rand.Intn(60))
job.Schedule = strings.Replace(job.Schedule, "$", delay, -1)
params = append(params, parseTaskParameters(ctx, job.Task, new(DeleteBoards))...)
}
Expand Down
16 changes: 9 additions & 7 deletions server/src/services/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scheduler

import (
"fmt"
"github.com/go-co-op/gocron/v2"
"github.com/golang/mock/gomock"
"github.com/google/uuid"
Expand Down Expand Up @@ -35,12 +36,14 @@ func (suite *SchedulerTestSuite) TestInitialization() {
s := new(SchedulerService)
ctrl := gomock.NewController(suite.T())
sched := mocks.NewMockScheduler(ctrl)

s.database = mockDB
s.scheduler = sched

sched.EXPECT().Start().Times(1)
sched.EXPECT().NewJob(gocron.CronJob("*/20 * * * * *", true), gomock.Any())
s.StartScheduler("./config.yaml")
sched.EXPECT().NewJob(gocron.CronJob(fmt.Sprintf("0 3 * * *"), false), gomock.Any())
s.StartScheduler("./config.yaml", "0")

mockDB.AssertExpectations(suite.T())
}

Expand All @@ -60,19 +63,18 @@ func (suite *SchedulerTestSuite) TestCheckMultipleIntervals() {
User: uuid.New(),
Name: "TestBoard",
}
expected := 6

expected :=
6
mockDB.On("GetSessionsOlderThan", mock.Anything, mock.Anything).
Return([]database.BoardSession{testBoard}, nil)

mockDB.On("DeleteBoard", testBoard.Board).Return(nil)

s.StartScheduler("./config.yaml")
s.StartScheduler("./config.yaml", "0")

for i := 0; i < expected; i++ {
mockDB.wg.Add(1)
fakeClock.BlockUntil(1)
fakeClock.Advance(time.Second * 20)
fakeClock.Advance(time.Hour * 24)
mockDB.wg.Wait()
}

Expand Down

0 comments on commit fe74083

Please sign in to comment.