init: 1.0.0

This commit is contained in:
Chang lue Tsen
2025-04-25 12:08:29 +09:00
commit 8addcc584b
1031 changed files with 76472 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
package timex
import "time"
// Use the long enough past time as start time, in case timex.Now() - lastTime equals 0.
var initTime = time.Now().AddDate(-1, -1, -1)
// Now returns a relative time duration since initTime, which is not important.
// The caller only needs to care about the relative value.
func Now() time.Duration {
return time.Since(initTime)
}
// Since returns a diff since given d.
func Since(d time.Duration) time.Duration {
return time.Since(initTime) - d
}
+32
View File
@@ -0,0 +1,32 @@
package timex
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRelativeTime(t *testing.T) {
time.Sleep(time.Millisecond)
now := Now()
assert.True(t, now > 0)
time.Sleep(time.Millisecond)
assert.True(t, Since(now) > 0)
}
func BenchmarkTimeSince(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = time.Since(time.Now())
}
}
func BenchmarkTimexSince(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = Since(Now())
}
}
+11
View File
@@ -0,0 +1,11 @@
package timex
import (
"fmt"
"time"
)
// ReprOfDuration returns the string representation of given duration in ms.
func ReprOfDuration(duration time.Duration) string {
return fmt.Sprintf("%.1fms", float32(duration)/float32(time.Millisecond))
}
+14
View File
@@ -0,0 +1,14 @@
package timex
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestReprOfDuration(t *testing.T) {
assert.Equal(t, "1000.0ms", ReprOfDuration(time.Second))
assert.Equal(t, "1111.6ms", ReprOfDuration(
time.Second+time.Millisecond*111+time.Microsecond*555))
}
+80
View File
@@ -0,0 +1,80 @@
package timex
import (
"errors"
"time"
"github.com/perfect-panel/ppanel-server/pkg/lang"
)
// errTimeout indicates a timeout.
var errTimeout = errors.New("timeout")
type (
// Ticker interface wraps the Chan and Stop methods.
Ticker interface {
Chan() <-chan time.Time
Stop()
}
// FakeTicker interface is used for unit testing.
FakeTicker interface {
Ticker
Done()
Tick()
Wait(d time.Duration) error
}
fakeTicker struct {
c chan time.Time
done chan lang.PlaceholderType
}
realTicker struct {
*time.Ticker
}
)
// NewTicker returns a Ticker.
func NewTicker(d time.Duration) Ticker {
return &realTicker{
Ticker: time.NewTicker(d),
}
}
func (rt *realTicker) Chan() <-chan time.Time {
return rt.C
}
// NewFakeTicker returns a FakeTicker.
func NewFakeTicker() FakeTicker {
return &fakeTicker{
c: make(chan time.Time, 1),
done: make(chan lang.PlaceholderType, 1),
}
}
func (ft *fakeTicker) Chan() <-chan time.Time {
return ft.c
}
func (ft *fakeTicker) Done() {
ft.done <- lang.Placeholder
}
func (ft *fakeTicker) Stop() {
close(ft.c)
}
func (ft *fakeTicker) Tick() {
ft.c <- time.Now()
}
func (ft *fakeTicker) Wait(d time.Duration) error {
select {
case <-time.After(d):
return errTimeout
case <-ft.done:
return nil
}
}
+50
View File
@@ -0,0 +1,50 @@
package timex
import (
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRealTickerDoTick(t *testing.T) {
ticker := NewTicker(time.Millisecond * 10)
defer ticker.Stop()
var count int
for range ticker.Chan() {
count++
if count > 5 {
break
}
}
}
func TestFakeTicker(t *testing.T) {
const total = 5
ticker := NewFakeTicker()
defer ticker.Stop()
var count int32
go func() {
for range ticker.Chan() {
if atomic.AddInt32(&count, 1) == total {
ticker.Done()
}
}
}()
for i := 0; i < 5; i++ {
ticker.Tick()
}
assert.Nil(t, ticker.Wait(time.Second))
assert.Equal(t, int32(total), atomic.LoadInt32(&count))
}
func TestFakeTickerTimeout(t *testing.T) {
ticker := NewFakeTicker()
defer ticker.Stop()
assert.NotNil(t, ticker.Wait(time.Millisecond))
}