syncs: add Semaphore
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
9643d8b34d
commit
77ec80538a
+26
-1
@@ -4,7 +4,10 @@
|
||||
|
||||
package syncs
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWaitGroupChan(t *testing.T) {
|
||||
wg := NewWaitGroupChan()
|
||||
@@ -48,3 +51,25 @@ func TestClosedChan(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSemaphore(t *testing.T) {
|
||||
s := NewSemaphore(2)
|
||||
s.Acquire()
|
||||
if !s.TryAcquire() {
|
||||
t.Fatal("want true")
|
||||
}
|
||||
if s.TryAcquire() {
|
||||
t.Fatal("want false")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if s.AcquireContext(ctx) {
|
||||
t.Fatal("want false")
|
||||
}
|
||||
s.Release()
|
||||
if !s.AcquireContext(context.Background()) {
|
||||
t.Fatal("want true")
|
||||
}
|
||||
s.Release()
|
||||
s.Release()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user