Skip to content

Commit

Permalink
Add examples from README to example_test.go (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunmahishi authored Oct 3, 2020
1 parent 31e21f6 commit 1621be0
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,40 @@ var task = func() {

func ExampleScheduler_StartBlocking() {
s := gocron.NewScheduler(time.UTC)
s.Every(3).Seconds().Do(task)
_, _ = s.Every(3).Seconds().Do(task)
s.StartBlocking()
}

func ExampleScheduler_StartAsync() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(3).Seconds().Do(task)
<-s.StartAsync()
}

func ExampleScheduler_StartImmediately() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Hour().StartImmediately().Do(task)
s.StartBlocking()
}

func ExampleScheduler_StartAt() {
s := gocron.NewScheduler(time.UTC)
specificTime := time.Date(2019, time.November, 10, 15, 0, 0, 0, time.UTC)
_, _ = s.Every(1).Hour().StartAt(specificTime).Do(task)
s.StartBlocking()
}

func ExampleScheduler_Stop() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().Do(task)
s.StartAsync()
s.Stop()
}

func ExampleScheduler_At() {
s := gocron.NewScheduler(time.UTC)
s.Every(1).Day().At("10:30").Do(task)
s.Every(1).Monday().At("10:30:01").Do(task)
_, _ = s.Every(1).Day().At("10:30").Do(task)
_, _ = s.Every(1).Monday().At("10:30:01").Do(task)
}

func ExampleJob_ScheduledTime() {
Expand All @@ -29,3 +55,36 @@ func ExampleJob_ScheduledTime() {
fmt.Println(job.ScheduledAtTime())
// Output: 10:30
}

func ExampleScheduler_RemoveJobByTag() {
s := gocron.NewScheduler(time.UTC)
tag1 := []string{"tag1"}
tag2 := []string{"tag2"}
_, _ = s.Every(1).Week().SetTag(tag1).Do(task)
_, _ = s.Every(1).Week().SetTag(tag2).Do(task)
s.StartAsync()
_ = s.RemoveJobByTag("tag1")
}

func ExampleScheduler_NextRun() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("10:30").Do(task)
s.StartAsync()
_, time := s.NextRun()
fmt.Println(time.Format("15:04")) // print only the hour and minute (hh:mm)
// Output: 10:30
}

func ExampleScheduler_Clear() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().Do(task)
_, _ = s.Every(1).Minute().Do(task)
_, _ = s.Every(1).Month(1).Do(task)
fmt.Println(len(s.Jobs())) // Print the number of jobs before clearing
s.Clear() // Clear all the jobs
fmt.Println(len(s.Jobs())) // Print the number of jobs after clearing
s.StartAsync()
// Output:
// 3
// 0
}

0 comments on commit 1621be0

Please sign in to comment.