helper/shadow: KeyedValue

This commit is contained in:
Mitchell Hashimoto 2016-09-30 18:48:53 -07:00
parent 8426cea6b0
commit bd69e41c14
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 106 additions and 1 deletions

View File

@ -1,5 +1,59 @@
package shadow package shadow
// KeyedValue is a struct that coordinates a value by key. import (
"sync"
)
// KeyedValue is a struct that coordinates a value by key. If a value is
// not available for a give key, it'll block until it is available.
type KeyedValue struct { type KeyedValue struct {
lock sync.Mutex
once sync.Once
values map[string]interface{}
waiters map[string]*Value
}
// Value returns the value that was set for the given key, or blocks
// until one is available.
func (w *KeyedValue) Value(k string) interface{} {
w.lock.Lock()
w.once.Do(w.init)
// If we have this value already, return it
if v, ok := w.values[k]; ok {
w.lock.Unlock()
return v
}
// No pending value, check for a waiter
val := w.waiters[k]
if val == nil {
val = new(Value)
w.waiters[k] = val
}
w.lock.Unlock()
// Return the value once we have it
return val.Value()
}
func (w *KeyedValue) SetValue(k string, v interface{}) {
w.lock.Lock()
defer w.lock.Unlock()
w.once.Do(w.init)
// Set the value, always
w.values[k] = v
// If we have a waiter, set it
if val, ok := w.waiters[k]; ok {
val.SetValue(v)
w.waiters[k] = nil
}
}
// Must be called with w.lock held.
func (w *KeyedValue) init() {
w.values = make(map[string]interface{})
w.waiters = make(map[string]*Value)
} }

View File

@ -0,0 +1,51 @@
package shadow
import (
"testing"
"time"
)
func TestKeyedValue(t *testing.T) {
var v KeyedValue
// Start trying to get the value
valueCh := make(chan interface{})
go func() {
valueCh <- v.Value("foo")
}()
// We should not get the value
select {
case <-valueCh:
t.Fatal("shouldn't receive value")
case <-time.After(10 * time.Millisecond):
}
// Set the value
v.SetValue("foo", 42)
val := <-valueCh
// Verify
if val != 42 {
t.Fatalf("bad: %#v", val)
}
// We should get the next value
val = v.Value("foo")
if val != 42 {
t.Fatalf("bad: %#v", val)
}
}
func TestKeyedValue_setFirst(t *testing.T) {
var v KeyedValue
// Set the value
v.SetValue("foo", 42)
val := v.Value("foo")
// Verify
if val != 42 {
t.Fatalf("bad: %#v", val)
}
}