Skip to content

Commit

Permalink
helper/shadow: KeyedValue.Close
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Oct 11, 2016
1 parent d2fb630 commit 136ac47
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
23 changes: 23 additions & 0 deletions helper/shadow/keyed_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ type KeyedValue struct {
once sync.Once
values map[string]interface{}
waiters map[string]*Value
closed bool
}

// Close closes the value. This can never fail. For a definition of
// "close" see the ErrClosed docs.
func (w *KeyedValue) Close() error {
w.lock.Lock()
defer w.lock.Unlock()

// Set closed to true always
w.closed = true

// For all waiters, complete with ErrClosed
for _, w := range w.waiters {
w.SetValue(ErrClosed)
}

return nil
}

// Value returns the value that was set for the given key, or blocks
Expand Down Expand Up @@ -62,6 +80,11 @@ func (w *KeyedValue) valueWaiter(k string) (interface{}, *Value) {
return v, nil
}

// If we're closed, return that
if w.closed {
return ErrClosed, nil
}

// No pending value, check for a waiter
val := w.waiters[k]
if val == nil {
Expand Down
65 changes: 65 additions & 0 deletions helper/shadow/keyed_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,68 @@ func TestKeyedValueOk(t *testing.T) {
t.Fatalf("bad: %#v", val)
}
}

func TestKeyedValueClose(t *testing.T) {
var v KeyedValue

// Close
v.Close()

// Try again
val, ok := v.ValueOk("foo")
if !ok {
t.Fatal("should be ok")
}

// Verify
if val != ErrClosed {
t.Fatalf("bad: %#v", val)
}
}

func TestKeyedValueClose_blocked(t *testing.T) {
var v KeyedValue

// Start reading this should be blocking
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):
}

// Close
v.Close()

// Verify
val := <-valueCh
if val != ErrClosed {
t.Fatalf("bad: %#v", val)
}
}

func TestKeyedValueClose_existing(t *testing.T) {
var v KeyedValue

// 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)
}
}
31 changes: 31 additions & 0 deletions helper/shadow/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ func TestValueClose(t *testing.T) {
}
}

func TestValueClose_blocked(t *testing.T) {
var v Value

// Start trying to get the value
valueCh := make(chan interface{})
go func() {
valueCh <- v.Value()
}()

// 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.Close()
val := <-valueCh

// Verify
if val != ErrClosed {
t.Fatalf("bad: %#v", val)
}

// We should be able to ask for the value again immediately
if val := v.Value(); val != ErrClosed {
t.Fatalf("bad: %#v", val)
}
}

func TestValueClose_existing(t *testing.T) {
var v Value

Expand Down

0 comments on commit 136ac47

Please sign in to comment.