51 lines
892 B
Go
51 lines
892 B
Go
package telegram
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestKeyedLockerSerializesSameKey(t *testing.T) {
|
|
locker := newKeyedLocker()
|
|
unlock := locker.Lock(1)
|
|
|
|
acquired := make(chan struct{})
|
|
go func() {
|
|
unlockSecond := locker.Lock(1)
|
|
close(acquired)
|
|
unlockSecond()
|
|
}()
|
|
|
|
select {
|
|
case <-acquired:
|
|
t.Fatal("second lock acquired while first is held")
|
|
case <-time.After(20 * time.Millisecond):
|
|
}
|
|
|
|
unlock()
|
|
select {
|
|
case <-acquired:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("second lock was not acquired after release")
|
|
}
|
|
}
|
|
|
|
func TestKeyedLockerAllowsDifferentKeys(t *testing.T) {
|
|
locker := newKeyedLocker()
|
|
unlock := locker.Lock(1)
|
|
defer unlock()
|
|
|
|
acquired := make(chan struct{})
|
|
go func() {
|
|
unlockSecond := locker.Lock(2)
|
|
close(acquired)
|
|
unlockSecond()
|
|
}()
|
|
|
|
select {
|
|
case <-acquired:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("different key was blocked")
|
|
}
|
|
}
|