mirror of
https://github.com/pocketbase/pocketbase.git
synced 2026-09-08 15:41:18 +02:00
fix some of the flaky tests
This commit is contained in:
+54
-51
@@ -5,6 +5,7 @@ import (
|
||||
"slices"
|
||||
"sync"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -254,63 +255,65 @@ func TestCronJobs(t *testing.T) {
|
||||
func TestCronStartStop(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var mu sync.Mutex
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
|
||||
test1 := 0
|
||||
test2 := 0
|
||||
test1 := 0
|
||||
test2 := 0
|
||||
|
||||
c := New()
|
||||
c := New()
|
||||
|
||||
c.SetInterval(250 * time.Millisecond)
|
||||
c.SetInterval(250 * time.Millisecond)
|
||||
|
||||
c.Add("test1", "* * * * *", func() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
test1++
|
||||
})
|
||||
|
||||
c.Add("test2", "* * * * *", func() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
test2++
|
||||
})
|
||||
|
||||
// call twice Start to check if the previous ticker will be reseted
|
||||
c.Start()
|
||||
c.Start()
|
||||
|
||||
synctest.Sleep(500 * time.Millisecond)
|
||||
|
||||
// call twice Stop to ensure that the second stop is no-op
|
||||
c.Stop()
|
||||
c.Stop()
|
||||
|
||||
expectedCalls := 2
|
||||
|
||||
c.Add("test1", "* * * * *", func() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
test1++
|
||||
})
|
||||
if test1 != expectedCalls {
|
||||
t.Fatalf("Expected %d test1, got %d", expectedCalls, test1)
|
||||
}
|
||||
if test2 != expectedCalls {
|
||||
t.Fatalf("Expected %d test2, got %d", expectedCalls, test2)
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
// resume for 1 seconds
|
||||
c.Start()
|
||||
|
||||
synctest.Sleep(1000 * time.Millisecond)
|
||||
|
||||
c.Stop()
|
||||
|
||||
expectedCalls += 4
|
||||
|
||||
c.Add("test2", "* * * * *", func() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
test2++
|
||||
if test1 != expectedCalls {
|
||||
t.Fatalf("Expected %d test1, got %d", expectedCalls, test1)
|
||||
}
|
||||
if test2 != expectedCalls {
|
||||
t.Fatalf("Expected %d test2, got %d", expectedCalls, test2)
|
||||
}
|
||||
mu.Unlock()
|
||||
})
|
||||
|
||||
// call twice Start to check if the previous ticker will be reseted
|
||||
c.Start()
|
||||
c.Start()
|
||||
|
||||
time.Sleep(505 * time.Millisecond) // slightly larger to minimize flakiness
|
||||
|
||||
// call twice Stop to ensure that the second stop is no-op
|
||||
c.Stop()
|
||||
c.Stop()
|
||||
|
||||
expectedCalls := 2
|
||||
|
||||
mu.Lock()
|
||||
if test1 != expectedCalls {
|
||||
t.Fatalf("Expected %d test1, got %d", expectedCalls, test1)
|
||||
}
|
||||
if test2 != expectedCalls {
|
||||
t.Fatalf("Expected %d test2, got %d", expectedCalls, test2)
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
// resume for 1 seconds
|
||||
c.Start()
|
||||
|
||||
time.Sleep(1005 * time.Millisecond) // slightly larger to minimize flakiness
|
||||
|
||||
c.Stop()
|
||||
|
||||
expectedCalls += 4
|
||||
|
||||
mu.Lock()
|
||||
if test1 != expectedCalls {
|
||||
t.Fatalf("Expected %d test1, got %d", expectedCalls, test1)
|
||||
}
|
||||
if test2 != expectedCalls {
|
||||
t.Fatalf("Expected %d test2, got %d", expectedCalls, test2)
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
@@ -131,8 +131,8 @@ func (s *System) Close() error {
|
||||
return s.bucket.Close()
|
||||
}
|
||||
|
||||
// OnNewWriter is a low level hook that is triggered on every writer initialization
|
||||
// (aka. when attempting to create a new file with [system.NewWriter], [system.Upload], etc.).
|
||||
// OnNewWriter is a low level hook that is triggered on every new writer initialization
|
||||
// (aka. when attempting to create a new file with [system.NewWriter] or [system.Upload]).
|
||||
//
|
||||
// Note that currently it doesn't trigger on [System.Copy] but this may change in future releases.
|
||||
func (s *System) OnNewWriter() *hook.Hook[*NewWriterEvent] {
|
||||
@@ -144,6 +144,9 @@ func (s *System) OnNewWriter() *hook.Hook[*NewWriterEvent] {
|
||||
}
|
||||
|
||||
// OnDelete is a low level hook that is triggered on every [System.Delete] call.
|
||||
//
|
||||
// Note that the hook doesn't fire when a file is being overwritten
|
||||
// by a new one, because in that case [System.Delete] is not invoked.
|
||||
func (s *System) OnDelete() *hook.Hook[*DeleteEvent] {
|
||||
if s.onDelete == nil {
|
||||
s.onDelete = &hook.Hook[*DeleteEvent]{}
|
||||
|
||||
Reference in New Issue
Block a user