Added test for CreateLocks

This commit is contained in:
vishalnayak 2016-07-20 05:37:38 -04:00
parent 331f229858
commit ba4dc638bf
2 changed files with 48 additions and 1 deletions

View File

@ -12,7 +12,7 @@ import (
func CreateLocks(p map[string]*sync.RWMutex, count int64) error {
// Since the indices of the map entries are based on 2 character
// hex values, this utility can only create upto 256 locks.
if count > 256 {
if count <= 0 || count > 256 {
return fmt.Errorf("invalid count: %d", count)
}

View File

@ -0,0 +1,47 @@
package locksutil
import (
"sync"
"testing"
)
func Test_CreateLocks(t *testing.T) {
locks := map[string]*sync.RWMutex{}
// Invalid argument
if err := CreateLocks(locks, -1); err == nil {
t.Fatal("expected an error")
}
// Invalid argument
if err := CreateLocks(locks, 0); err == nil {
t.Fatal("expected an error")
}
// Invalid argument
if err := CreateLocks(locks, 300); err == nil {
t.Fatal("expected an error")
}
// Maximum number of locks
if err := CreateLocks(locks, 256); err != nil {
t.Fatal("err: %v", err)
}
if len(locks) != 256 {
t.Fatal("bad: len(locks): expected:256 actual:%d", len(locks))
}
// Clear out the locks for testing the next case
for k, _ := range locks {
delete(locks, k)
}
// General case
if err := CreateLocks(locks, 10); err != nil {
t.Fatal("err: %v", err)
}
if len(locks) != 10 {
t.Fatal("bad: len(locks): expected:10 actual:%d", len(locks))
}
}