refactor: remove NoCopy struct; move RefCounter struct to goutils and update usage; remove internal/utils entirely

This commit is contained in:
yusing
2026-01-07 17:17:12 +08:00
parent 1ebba20216
commit 9ea9e62ee8
8 changed files with 4 additions and 152 deletions

View File

@@ -1,78 +0,0 @@
package utils
import (
"sync"
"testing"
"time"
expect "github.com/yusing/goutils/testing"
)
func TestRefCounterAddSub(t *testing.T) {
rc := NewRefCounter() // Count starts at 1
var wg sync.WaitGroup
rc.Add()
for range 2 {
wg.Go(rc.Sub)
}
wg.Wait()
expect.Equal(t, int(rc.refCount), 0)
select {
case <-rc.Zero():
// Expected behavior
case <-time.After(1 * time.Second):
t.Fatal("Expected Zero channel to close, but it didn't")
}
}
func TestRefCounterMultipleAddSub(t *testing.T) {
rc := NewRefCounter()
var wg sync.WaitGroup
numAdds := 5
numSubs := 5
wg.Add(numAdds)
for range numAdds {
go func() {
defer wg.Done()
rc.Add()
}()
}
wg.Wait()
expect.Equal(t, int(rc.refCount), numAdds+1)
wg.Add(numSubs)
for range numSubs {
go func() {
defer wg.Done()
rc.Sub()
}()
}
wg.Wait()
expect.Equal(t, int(rc.refCount), numAdds+1-numSubs)
rc.Sub()
select {
case <-rc.Zero():
// Expected behavior
case <-time.After(1 * time.Second):
t.Fatal("Expected Zero channel to close, but it didn't")
}
}
func TestRefCounterOneInitially(t *testing.T) {
rc := NewRefCounter()
rc.Sub() // Bring count to zero
select {
case <-rc.Zero():
// Expected behavior
case <-time.After(1 * time.Second):
t.Fatal("Expected Zero channel to close, but it didn't")
}
}