2019-05-20 22:24:09 +02:00
|
|
|
// Copyright (c) 2019 Shivaram Lingamneni
|
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTryAcquire(t *testing.T) {
|
|
|
|
count := 3
|
2021-04-07 14:44:17 +02:00
|
|
|
sem := NewSemaphore(count)
|
2019-05-20 22:24:09 +02:00
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
assertEqual(sem.TryAcquire(), true, t)
|
|
|
|
}
|
|
|
|
// used up the capacity
|
|
|
|
assertEqual(sem.TryAcquire(), false, t)
|
|
|
|
sem.Release()
|
|
|
|
// got one slot back
|
|
|
|
assertEqual(sem.TryAcquire(), true, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAcquireWithTimeout(t *testing.T) {
|
2021-04-07 14:44:17 +02:00
|
|
|
sem := NewSemaphore(1)
|
2019-05-20 22:24:09 +02:00
|
|
|
|
|
|
|
assertEqual(sem.TryAcquire(), true, t)
|
|
|
|
|
|
|
|
// cannot acquire the held semaphore
|
|
|
|
assertEqual(sem.AcquireWithTimeout(100*time.Millisecond), false, t)
|
|
|
|
|
|
|
|
sem.Release()
|
|
|
|
// can acquire the released semaphore
|
|
|
|
assertEqual(sem.AcquireWithTimeout(100*time.Millisecond), true, t)
|
|
|
|
sem.Release()
|
|
|
|
|
|
|
|
// XXX this test could fail if the machine is extremely overloaded
|
|
|
|
sem.Acquire()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
sem.Release()
|
|
|
|
}()
|
|
|
|
// we should acquire successfully after approximately 100 msec
|
|
|
|
assertEqual(sem.AcquireWithTimeout(1*time.Second), true, t)
|
|
|
|
}
|