2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-07-14 09:45:04 -04:00
|
|
|
package clean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.uber.org/goleak"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2024-01-16 11:45:35 -05:00
|
|
|
goleak.VerifyTestMain(m, goleak.IgnoreAnyFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"))
|
2022-07-14 09:45:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-10-28 08:52:39 -04:00
|
|
|
cleaner := New(&spyStopper{stopped: &atomic.Bool{}})
|
2022-07-14 09:45:04 -04:00
|
|
|
assert.NotNil(cleaner)
|
|
|
|
assert.NotEmpty(cleaner.stoppers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWith(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-10-28 08:52:39 -04:00
|
|
|
cleaner := New().With(&spyStopper{stopped: &atomic.Bool{}})
|
2022-07-14 09:45:04 -04:00
|
|
|
assert.NotEmpty(cleaner.stoppers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClean(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-10-28 08:52:39 -04:00
|
|
|
stopper := &spyStopper{stopped: &atomic.Bool{}}
|
2022-07-14 09:45:04 -04:00
|
|
|
cleaner := New(stopper)
|
|
|
|
go cleaner.Start()
|
|
|
|
cleaner.Clean()
|
|
|
|
cleaner.Done()
|
2022-10-28 08:52:39 -04:00
|
|
|
assert.True(stopper.stopped.Load())
|
2022-07-14 09:45:04 -04:00
|
|
|
// call again to make sure it doesn't panic or block or clean up again
|
|
|
|
cleaner.Clean()
|
2022-10-28 08:52:39 -04:00
|
|
|
assert.True(stopper.stopped.Load())
|
2022-07-14 09:45:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanBeforeStart(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
// calling Clean before Start should work
|
2022-10-28 08:52:39 -04:00
|
|
|
stopper := &spyStopper{stopped: &atomic.Bool{}}
|
2022-07-14 09:45:04 -04:00
|
|
|
cleaner := New(stopper)
|
|
|
|
cleaner.Clean()
|
|
|
|
cleaner.Start()
|
|
|
|
cleaner.Done()
|
2022-10-28 08:52:39 -04:00
|
|
|
assert.True(stopper.stopped.Load())
|
2022-07-14 09:45:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConcurrent(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
// calling Clean concurrently should call Stop exactly once
|
|
|
|
|
2022-10-28 08:52:39 -04:00
|
|
|
stopper := &spyStopper{stopped: &atomic.Bool{}}
|
2022-07-14 09:45:04 -04:00
|
|
|
cleaner := New(stopper)
|
|
|
|
|
|
|
|
parallelism := 10
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
|
|
|
start := func() {
|
|
|
|
defer wg.Done()
|
|
|
|
cleaner.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
clean := func() {
|
|
|
|
defer wg.Done()
|
|
|
|
cleaner.Clean()
|
|
|
|
}
|
|
|
|
|
|
|
|
done := func() {
|
|
|
|
defer wg.Done()
|
|
|
|
cleaner.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(3 * parallelism)
|
|
|
|
for i := 0; i < parallelism; i++ {
|
|
|
|
go start()
|
|
|
|
go clean()
|
|
|
|
go done()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
cleaner.Done()
|
2022-10-28 08:52:39 -04:00
|
|
|
assert.True(stopper.stopped.Load())
|
2022-07-14 09:45:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type spyStopper struct {
|
2022-10-28 08:52:39 -04:00
|
|
|
stopped *atomic.Bool
|
2022-07-14 09:45:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *spyStopper) Stop() {
|
2022-10-28 08:52:39 -04:00
|
|
|
s.stopped.Store(true)
|
2022-07-14 09:45:04 -04:00
|
|
|
}
|