Skip to content

Commit

Permalink
add Quick mode for gtimer (#2488)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinego authored Mar 20, 2023
1 parent e721124 commit 0b6798a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions os/gtimer/gtimer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Timer struct {
// TimerOptions is the configuration object for Timer.
type TimerOptions struct {
Interval time.Duration // Interval is the interval escaped of the timer.
Quick bool // Quick is used for quick timer, which means the timer will not wait for the first interval to be elapsed.
}

// internalPanic is the custom panic for internal usage.
Expand Down
16 changes: 13 additions & 3 deletions os/gtimer/gtimer_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func New(options ...TimerOptions) *Timer {
}
if len(options) > 0 {
t.options = options[0]
if t.options.Interval == 0 {
t.options.Interval = defaultInterval
}
} else {
t.options = DefaultOptions()
}
Expand Down Expand Up @@ -166,7 +169,8 @@ type createEntryInput struct {
// createEntry creates and adds a timing job to the timer.
func (t *Timer) createEntry(in createEntryInput) *Entry {
var (
infinite = false
infinite = false
nextTicks int64
)
if in.Times <= 0 {
infinite = true
Expand All @@ -179,9 +183,15 @@ func (t *Timer) createEntry(in createEntryInput) *Entry {
// then sets it to one tick, which means it will be run in one interval.
intervalTicksOfJob = 1
}
var (
if t.options.Quick {
// If the quick mode is enabled, which means it will be run right now.
// Don't need to wait for the first interval.
nextTicks = t.ticks.Val()
} else {
nextTicks = t.ticks.Val() + intervalTicksOfJob
entry = &Entry{
}
var (
entry = &Entry{
job: in.Job,
ctx: in.Ctx,
timer: t,
Expand Down
21 changes: 21 additions & 0 deletions os/gtimer/gtimer_z_unit_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ func TestJob_Singleton(t *testing.T) {
})
}

func TestJob_SingletonQuick(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New(gtimer.TimerOptions{
Quick: true,
})
array := garray.New(true)
job := timer.Add(ctx, 5*time.Second, func(ctx context.Context) {
array.Append(1)
time.Sleep(10 * time.Second)
})
t.Assert(job.IsSingleton(), false)
job.SetSingleton(true)
t.Assert(job.IsSingleton(), true)
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)

time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)
})
}

func TestJob_SetTimes(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
Expand Down
38 changes: 38 additions & 0 deletions os/gtimer/gtimer_z_unit_timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,44 @@ func TestTimer_AddSingleton(t *testing.T) {
})
}

func TestTimer_AddSingletonWithQuick(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New(gtimer.TimerOptions{
Interval: 100 * time.Millisecond,
Quick: true,
})
array := garray.New(true)
timer.AddSingleton(ctx, 5*time.Second, func(ctx context.Context) {
array.Append(1)
time.Sleep(10 * time.Second)
})
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)

time.Sleep(500 * time.Millisecond)
t.Assert(array.Len(), 1)
})
}

func TestTimer_AddSingletonWithoutQuick(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New(gtimer.TimerOptions{
Interval: 100 * time.Millisecond,
Quick: false,
})
array := garray.New(true)
timer.AddSingleton(ctx, 5*time.Second, func(ctx context.Context) {
array.Append(1)
time.Sleep(10 * time.Second)
})
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 0)

time.Sleep(500 * time.Millisecond)
t.Assert(array.Len(), 0)
})
}

func TestTimer_AddOnce(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
Expand Down

0 comments on commit 0b6798a

Please sign in to comment.