Skip to content

Commit

Permalink
fix examples for pkg rename
Browse files Browse the repository at this point in the history
  • Loading branch information
wiggin77 committed Jul 24, 2019
1 parent 3b2bc77 commit a90af38
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 74 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ Supports monitoring configuration sources for changes, hot loading properties, a
## Usage

```Go
cfg := &cfg.Config{}
defer cfg.Shutdown() // stops monitoring
config := &cfg.Config{}
defer config.Shutdown() // stops monitoring

// load file via filespec string, os.File
src, err := Config.NewSrcFileFromFilespec("./myfile.conf")
if err != nil {
return err
}
// add src to top of chain, meaning first searched
Config.PrependSource(src)
cfg.PrependSource(src)

// fetch prop 'retries', default to 3 if not found
val := cfg.Int("retries", 3)
val := config.Int("retries", 3)
```

See [example](./example_test.go) for more complete example, including listening for configuration changes.
Expand Down
10 changes: 5 additions & 5 deletions config_mon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func (n *Notify) ConfigChanged(cfg *Config, src SourceMonitored) {
}

func TestConfig_Monitor(t *testing.T) {
cfg := &Config{}
defer cfg.Shutdown()
config := &Config{}
defer config.Shutdown()

mapSrc := makeSrc(time.Millisecond * 20)
cfg.AppendSource(mapSrc)
config.AppendSource(mapSrc)

notify := &Notify{}
cfg.AddChangedListener(notify)
config.AddChangedListener(notify)

ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
Expand All @@ -61,7 +61,7 @@ func TestConfig_Monitor(t *testing.T) {

// make sure listener not called after removal.
atomic.StoreInt32(&notify.count, 0)
cfg.RemoveChangedListener(notify)
config.RemoveChangedListener(notify)

mapSrc.Put("prop1", "n/a")
time.Sleep(50 * time.Millisecond)
Expand Down
120 changes: 60 additions & 60 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,48 @@ func TestConfig_PrependSource(t *testing.T) {
map3 := map[string]string{"prop3": "3", "prop2": "3", "prop1": "3"}
src3 := NewSrcMapFromMap(map3)

cfg := &Config{}
defer cfg.Shutdown()
config := &Config{}
defer config.Shutdown()

// Prepend one to empty config.
cfg.PrependSource(src1)
if len(cfg.srcs) != 1 {
t.Errorf("Prepend src to empty config; expected len=1, got len=%d", len(cfg.srcs))
config.PrependSource(src1)
if len(config.srcs) != 1 {
t.Errorf("Prepend src to empty config; expected len=1, got len=%d", len(config.srcs))
}
if val, err := cfg.String("prop1", ""); err != nil || val != "1" {
if val, err := config.String("prop1", ""); err != nil || val != "1" {
t.Errorf("Prepend src to empty config; expected err=nil, val=1; got err=%v, val=%s", err, val)
}
if _, err := cfg.String("blap", ""); err == nil {
if _, err := config.String("blap", ""); err == nil {
t.Errorf("Prepend src to empty config; expected err=false for missing prop, got err=%v", err)
}

// Prepend second
cfg.PrependSource(src2)
if len(cfg.srcs) != 2 {
t.Errorf("Prepend second src; expected len=2, got len=%d", len(cfg.srcs))
config.PrependSource(src2)
if len(config.srcs) != 2 {
t.Errorf("Prepend second src; expected len=2, got len=%d", len(config.srcs))
}
if val, err := cfg.String("prop1", ""); err != nil || val != "2" {
if val, err := config.String("prop1", ""); err != nil || val != "2" {
t.Errorf("Prepend second src; for prop1 expected err=nil, val=2; got err=%v, val=%s", err, val)
}
if val, err := cfg.String("prop2", ""); err != nil || val != "2" {
if val, err := config.String("prop2", ""); err != nil || val != "2" {
t.Errorf("Prepend second src; for prop2 expected err=nil, val=2; got err=%v, val=%s", err, val)
}

// Prepend third
cfg.PrependSource(src3)
if len(cfg.srcs) != 3 {
t.Errorf("Prepend third src; expected len=3, got len=%d", len(cfg.srcs))
config.PrependSource(src3)
if len(config.srcs) != 3 {
t.Errorf("Prepend third src; expected len=3, got len=%d", len(config.srcs))
}
if val, err := cfg.String("prop1", ""); err != nil || val != "3" {
if val, err := config.String("prop1", ""); err != nil || val != "3" {
t.Errorf("Prepend third src; for prop1 expected err=nil, val=3; got err=%v, val=%s", err, val)
}
if val, err := cfg.String("prop2", ""); err != nil || val != "3" {
if val, err := config.String("prop2", ""); err != nil || val != "3" {
t.Errorf("Prepend third src; for prop2 expected err=nil, val=3; got err=%v, val=%s", err, val)
}
if val, err := cfg.String("prop3", ""); err != nil || val != "3" {
if val, err := config.String("prop3", ""); err != nil || val != "3" {
t.Errorf("Prepend third src; for prop3 expected err=nil, val=3; got err=%v, val=%s", err, val)
}
if _, err := cfg.String("blap", ""); err == nil {
if _, err := config.String("blap", ""); err == nil {
t.Errorf("Prepend third src; expected err=false for missing prop, got err=%v", err)
}
}
Expand All @@ -72,90 +72,90 @@ func TestConfig_AppendSource(t *testing.T) {
map3 := map[string]string{"prop3": "3", "prop2": "3", "prop1": "3"}
src3 := NewSrcMapFromMap(map3)

cfg := &Config{}
defer cfg.Shutdown()
config := &Config{}
defer config.Shutdown()

// Append to empty config.
cfg.AppendSource(src1)
if len(cfg.srcs) != 1 {
t.Errorf("Append src to empty config; expected len=1, got len=%d", len(cfg.srcs))
config.AppendSource(src1)
if len(config.srcs) != 1 {
t.Errorf("Append src to empty config; expected len=1, got len=%d", len(config.srcs))
}
if val, err := cfg.String("prop1", ""); err != nil || val != "1" {
if val, err := config.String("prop1", ""); err != nil || val != "1" {
t.Errorf("Append src to empty config; expected err=nil, val=1; got err=%v, val=%s", err, val)
}
if _, err := cfg.String("blap", ""); err == nil {
if _, err := config.String("blap", ""); err == nil {
t.Errorf("Append src to empty config; expected err=false for missing prop, got err=%v", err)
}

// Append second
cfg.AppendSource(src2)
if len(cfg.srcs) != 2 {
t.Errorf("Append second src; expected len=2, got len=%d", len(cfg.srcs))
config.AppendSource(src2)
if len(config.srcs) != 2 {
t.Errorf("Append second src; expected len=2, got len=%d", len(config.srcs))
}
if val, err := cfg.String("prop1", ""); err != nil || val != "1" {
if val, err := config.String("prop1", ""); err != nil || val != "1" {
t.Errorf("Append second src; for prop1 expected err=nil, val=1; got err=%v, val=%s", err, val)
}
if val, err := cfg.String("prop2", ""); err != nil || val != "2" {
if val, err := config.String("prop2", ""); err != nil || val != "2" {
t.Errorf("Append second src; for prop2 expected err=nil, val=2; got err=%v, val=%s", err, val)
}

// Append third
cfg.AppendSource(src3)
if len(cfg.srcs) != 3 {
t.Errorf("Append third src; expected len=3, got len=%d", len(cfg.srcs))
config.AppendSource(src3)
if len(config.srcs) != 3 {
t.Errorf("Append third src; expected len=3, got len=%d", len(config.srcs))
}
if val, err := cfg.String("prop1", ""); err != nil || val != "1" {
if val, err := config.String("prop1", ""); err != nil || val != "1" {
t.Errorf("Append third src; for prop1 expected err=nil, val=1; got err=%v, val=%s", err, val)
}
if val, err := cfg.String("prop2", ""); err != nil || val != "2" {
if val, err := config.String("prop2", ""); err != nil || val != "2" {
t.Errorf("Append third src; for prop2 expected err=nil, val=2; got err=%v, val=%s", err, val)
}
if val, err := cfg.String("prop3", ""); err != nil || val != "3" {
if val, err := config.String("prop3", ""); err != nil || val != "3" {
t.Errorf("Append third src; for prop3 expected err=nil, val=3; got err=%v, val=%s", err, val)
}
if _, err := cfg.String("blap", ""); err == nil {
if _, err := config.String("blap", ""); err == nil {
t.Errorf("Append third src; expected err=false for missing prop, got err=%v", err)
}
}

func TestConfig_AddRemoveChangedListener(t *testing.T) {
map1 := map[string]string{"prop1": "1"}
src1 := NewSrcMapFromMap(map1)
cfg := &Config{}
defer cfg.Shutdown()
cfg.AppendSource(src1)
config := &Config{}
defer config.Shutdown()
config.AppendSource(src1)

tl1 := &TestListener{}
tl2 := &TestListener{}
tl3 := &TestListener{}

// Test Add
cfg.AddChangedListener(tl1)
if len(cfg.chgListeners) != 1 {
t.Errorf("AddChangeListener; expected len=1, got len=%d", len(cfg.chgListeners))
config.AddChangedListener(tl1)
if len(config.chgListeners) != 1 {
t.Errorf("AddChangeListener; expected len=1, got len=%d", len(config.chgListeners))
}
cfg.AddChangedListener(tl2)
if len(cfg.chgListeners) != 2 {
t.Errorf("AddChangeListener; expected len=2, got len=%d", len(cfg.chgListeners))
config.AddChangedListener(tl2)
if len(config.chgListeners) != 2 {
t.Errorf("AddChangeListener; expected len=2, got len=%d", len(config.chgListeners))
}
cfg.AddChangedListener(tl3)
cfg.AddChangedListener(tl1)
if len(cfg.chgListeners) != 4 {
t.Errorf("AddChangeListener; expected len=4, got len=%d", len(cfg.chgListeners))
config.AddChangedListener(tl3)
config.AddChangedListener(tl1)
if len(config.chgListeners) != 4 {
t.Errorf("AddChangeListener; expected len=4, got len=%d", len(config.chgListeners))
}

// Test remove
cfg.RemoveChangedListener(tl1)
if len(cfg.chgListeners) != 2 {
t.Errorf("RemoveChangeListener; expected len=2, got len=%d", len(cfg.chgListeners))
config.RemoveChangedListener(tl1)
if len(config.chgListeners) != 2 {
t.Errorf("RemoveChangeListener; expected len=2, got len=%d", len(config.chgListeners))
}
cfg.RemoveChangedListener(tl2)
if len(cfg.chgListeners) != 1 {
t.Errorf("RemoveChangeListener; expected len=1, got len=%d", len(cfg.chgListeners))
config.RemoveChangedListener(tl2)
if len(config.chgListeners) != 1 {
t.Errorf("RemoveChangeListener; expected len=1, got len=%d", len(config.chgListeners))
}
cfg.RemoveChangedListener(tl3)
if len(cfg.chgListeners) != 0 {
t.Errorf("RemoveChangeListener; expected len=0, got len=%d", len(cfg.chgListeners))
config.RemoveChangedListener(tl3)
if len(config.chgListeners) != 0 {
t.Errorf("RemoveChangeListener; expected len=0, got len=%d", len(config.chgListeners))
}
}

Expand Down
10 changes: 5 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ func (l *listener) ConfigChanged(cfg *cfg.Config, src cfg.SourceMonitored) {

func Example() {
// create a Config instance
cfg := &cfg.Config{}
config := &cfg.Config{}
// shutdown will stop monitoring the sources for changes
defer cfg.Shutdown()
defer config.Shutdown()

// for this sample use a source backed by a simple map
m := sampleMap()
src := cfg.NewSrcMapFromMap(m)

// add the source to the end of the searched sources
cfg.AppendSource(src)
config.AppendSource(src)

// add a source to the beginning of the searched sources,
// providing defaults for missing properties.
cfg.PrependSource(cfg.NewSrcMapFromMap(map[string]string{"maxRetries": "10"}))
config.PrependSource(cfg.NewSrcMapFromMap(map[string]string{"maxRetries": "10"}))

// listen for changes (why not use a func type here intead of interface? Because we
// need to be able to remove listeners and cannot do that with funcs).
cfg.AddChangedListener(&listener{})
config.AddChangedListener(&listener{})

// change a property every 1 seconds for 5 seconds.
ticker := time.NewTicker(1 * time.Second)
Expand Down

0 comments on commit a90af38

Please sign in to comment.