storage: add tests for unsafeAddWatching

This adds map operation tests for unsafeAddWatching, which
could have been failed without https://github.com/coreos/etcd/pull/3939.
It tests if unsafeAddWatching is correctly updating synced map.
This commit is contained in:
Gyu-Ho Lee 2015-12-01 15:41:55 -08:00
parent 14210cf8a7
commit ff2e8b55ae

View File

@ -69,29 +69,30 @@ func TestUnsafeAddWatching(t *testing.T) {
testValue := []byte("bar") testValue := []byte("bar")
s.Put(testKey, testValue) s.Put(testKey, testValue)
wa := &watching{ size := 10
key: testKey, ws := make([]*watching, size)
prefix: true, for i := 0; i < size; i++ {
cur: 0, ws[i] = &watching{
} key: testKey,
prefix: true,
if err := unsafeAddWatching(&s.synced, string(testKey), wa); err != nil { cur: 0,
t.Error(err)
}
if v, ok := s.synced[string(testKey)]; !ok {
// the key must have had entry in synced
t.Errorf("existence = %v, want true", ok)
} else {
if len(v) != 1 {
// the key must have ONE entry in its watching map
t.Errorf("len(v) = %d, want 1", len(v))
} }
} }
// to test if unsafeAddWatching is correctly updating
if err := unsafeAddWatching(&s.synced, string(testKey), wa); err == nil { // synced map when adding new watching.
// unsafeAddWatching should have returned error for i, wa := range ws {
// when putting the same watch twice" if err := unsafeAddWatching(&s.synced, string(testKey), wa); err != nil {
t.Error(`error = nil, want "put the same watch twice"`) t.Errorf("#%d: error = %v, want nil", i, err)
}
if v, ok := s.synced[string(testKey)]; !ok {
t.Errorf("#%d: ok = %v, want ok true", i, ok)
} else {
if len(v) != i+1 {
t.Errorf("#%d: len(v) = %d, want %d", i, len(v), i+1)
}
if _, ok := v[wa]; !ok {
t.Errorf("#%d: ok = %v, want ok true", i, ok)
}
}
} }
} }