Files
exchange_go/pkg/utility/lockkey/lockkey_test.go
2025-02-06 11:14:33 +08:00

35 lines
554 B
Go

package lockkey
import (
"fmt"
"testing"
"time"
)
func TestLock(t *testing.T) {
lk := NewLockByKey()
// Lock and unlock the same key multiple times.
for i := 0; i < 5; i++ {
go func() {
lk.Lock("key")
fmt.Println("locked")
lk.Unlock("key")
fmt.Println("unlocked")
}()
}
// Lock and unlock different keys.
lk.Lock("key1")
fmt.Println("locked key1")
lk.Unlock("key1")
fmt.Println("unlocked key1")
lk.Lock("key2")
fmt.Println("locked key2")
lk.Unlock("key2")
fmt.Println("unlocked key2")
time.Sleep(3 * time.Second)
}