mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 00:38:33 +02:00
refactor: remove NoCopy struct; move RefCounter struct to goutils and update usage; remove internal/utils entirely
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user