github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/relation/util_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package relation_test
     5  
     6  import (
     7  	"gopkg.in/juju/charm.v4/hooks"
     8  	"launchpad.net/tomb"
     9  
    10  	"github.com/juju/juju/state/multiwatcher"
    11  	"github.com/juju/juju/worker/uniter/hook"
    12  )
    13  
    14  func hookList(kinds ...hooks.Kind) []hook.Info {
    15  	result := make([]hook.Info, len(kinds))
    16  	for i, kind := range kinds {
    17  		result[i].Kind = kind
    18  	}
    19  	return result
    20  }
    21  
    22  type updateSource struct {
    23  	tomb    tomb.Tomb
    24  	empty   bool
    25  	changes chan multiwatcher.RelationUnitsChange
    26  	updates chan multiwatcher.RelationUnitsChange
    27  }
    28  
    29  func newEmptySource() *updateSource {
    30  	return newUpdateSource(true, false)
    31  }
    32  
    33  func newFullBufferedSource() *updateSource {
    34  	return newUpdateSource(false, true)
    35  }
    36  
    37  func newFullUnbufferedSource() *updateSource {
    38  	return newUpdateSource(false, false)
    39  }
    40  
    41  func newUpdateSource(empty, buffered bool) *updateSource {
    42  	var bufferSize int
    43  	if buffered {
    44  		bufferSize = 1000
    45  	}
    46  	source := &updateSource{
    47  		empty:   empty,
    48  		changes: make(chan multiwatcher.RelationUnitsChange),
    49  		updates: make(chan multiwatcher.RelationUnitsChange, bufferSize),
    50  	}
    51  	go func() {
    52  		defer source.tomb.Done()
    53  		defer close(source.changes)
    54  		<-source.tomb.Dying()
    55  	}()
    56  	return source
    57  }
    58  
    59  func (source *updateSource) Stop() error {
    60  	source.tomb.Kill(nil)
    61  	return source.tomb.Wait()
    62  }
    63  
    64  func (source *updateSource) Changes() <-chan multiwatcher.RelationUnitsChange {
    65  	return source.changes
    66  }
    67  
    68  func (source *updateSource) Update(change multiwatcher.RelationUnitsChange) error {
    69  	select {
    70  	case <-source.tomb.Dying():
    71  		return tomb.ErrDying
    72  	case source.updates <- change:
    73  	}
    74  	return nil
    75  }
    76  
    77  func (source *updateSource) Empty() bool {
    78  	return source.empty
    79  }
    80  
    81  func (source *updateSource) Next() hook.Info {
    82  	if source.empty {
    83  		panic(nil)
    84  	}
    85  	return hook.Info{Kind: hooks.Install}
    86  }
    87  
    88  func (source *updateSource) Pop() {
    89  	if source.empty {
    90  		panic(nil)
    91  	}
    92  }