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) }