mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-22 21:16:10 +03:00
31 lines
697 B
Go
31 lines
697 B
Go
|
package semaphore_test
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"github.com/tmrts/go-patterns/semaphore"
|
||
|
)
|
||
|
|
||
|
func TestCreatesSemaphore(t *testing.T) {
|
||
|
tickets, timeout := 1, 3*time.Second
|
||
|
s := semaphore.New(tickets, timeout)
|
||
|
|
||
|
if err := s.Acquire(); err != nil {
|
||
|
t.Errorf("semaphore.Acquire() got errors %s", err)
|
||
|
}
|
||
|
|
||
|
if err := s.Release(); err != nil {
|
||
|
t.Errorf("semaphore.Release() got errors %s", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestCreatesNonBlockingSemaphore(t *testing.T) {
|
||
|
tickets, timeout := 0, 0
|
||
|
s := semaphore.New(tickets, timeout)
|
||
|
|
||
|
if err := s.Acquire(); err != semaphore.ErrIllegalRelease {
|
||
|
t.Errorf("non-blocking semaphore.Acquire() expected error %s got %s", semaphore.ErrIllegalRelease, err)
|
||
|
}
|
||
|
}
|