helper/shadow: KeyedValue add test case to avoid panic

This commit is contained in:
Mitchell Hashimoto 2016-10-04 20:30:28 -07:00
parent 136ac4728d
commit 47f4343bf5
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 31 additions and 1 deletions

View File

@ -60,7 +60,7 @@ func (w *KeyedValue) SetValue(k string, v interface{}) {
// If we have a waiter, set it // If we have a waiter, set it
if val, ok := w.waiters[k]; ok { if val, ok := w.waiters[k]; ok {
val.SetValue(v) val.SetValue(v)
w.waiters[k] = nil delete(w.waiters, k)
} }
} }

View File

@ -138,3 +138,33 @@ func TestKeyedValueClose_existing(t *testing.T) {
t.Fatalf("bad: %#v", val) t.Fatalf("bad: %#v", val)
} }
} }
func TestKeyedValueClose_existingBlocked(t *testing.T) {
var v KeyedValue
// Start reading this should be blocking
valueCh := make(chan interface{})
go func() {
valueCh <- v.Value("foo")
}()
// Wait
time.Sleep(10 * time.Millisecond)
// Set a value
v.SetValue("foo", "bar")
// Close
v.Close()
// Try again
val, ok := v.ValueOk("foo")
if !ok {
t.Fatal("should be ok")
}
// Verify
if val != "bar" {
t.Fatalf("bad: %#v", val)
}
}