gf/os/gtimer/gtimer_z_unit_test.go

146 lines
3.7 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-01-23 11:28:57 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2019-01-23 11:28:57 +08:00
// Package functions
package gtimer_test
import (
"context"
2019-06-19 09:06:52 +08:00
"testing"
"time"
2019-07-29 21:01:19 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/os/gtimer"
"github.com/gogf/gf/v2/test/gtest"
2019-01-23 11:28:57 +08:00
)
var (
ctx = context.TODO()
)
2019-01-23 11:28:57 +08:00
func TestSetTimeout(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
gtimer.SetTimeout(ctx, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(1000 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}
func TestSetInterval(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
gtimer.SetInterval(ctx, 300*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
2021-01-18 21:20:10 +08:00
time.Sleep(1000 * time.Millisecond)
t.Assert(array.Len(), 3)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}
func TestAddEntry(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
gtimer.AddEntry(ctx, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
2020-12-14 13:26:48 +08:00
}, false, 2, gtimer.StatusReady)
2019-06-19 09:06:52 +08:00
time.Sleep(1100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}
func TestAddSingleton(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
gtimer.AddSingleton(ctx, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
time.Sleep(10000 * time.Millisecond)
})
time.Sleep(1100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}
func TestAddTimes(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
gtimer.AddTimes(ctx, 200*time.Millisecond, 2, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(1000 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}
func TestDelayAdd(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
gtimer.DelayAdd(ctx, 500*time.Millisecond, 500*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
2021-01-18 21:20:10 +08:00
time.Sleep(600 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2021-01-18 21:20:10 +08:00
time.Sleep(600 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}
func TestDelayAddEntry(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
gtimer.DelayAddEntry(ctx, 200*time.Millisecond, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
2020-12-14 13:26:48 +08:00
}, false, 2, gtimer.StatusReady)
2019-06-19 09:06:52 +08:00
time.Sleep(300 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(1000 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}
func TestDelayAddSingleton(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
gtimer.DelayAddSingleton(ctx, 500*time.Millisecond, 500*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
time.Sleep(10000 * time.Millisecond)
})
time.Sleep(300 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(1000 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}
func TestDelayAddOnce(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
gtimer.DelayAddOnce(ctx, 200*time.Millisecond, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(300 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(1000 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}
func TestDelayAddTimes(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
gtimer.DelayAddTimes(ctx, 500*time.Millisecond, 500*time.Millisecond, 2, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(300 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
time.Sleep(1500 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}