fix for file_test (#486)

pull/463/head
Ehco 5 years ago committed by Tony
parent 3f88443edf
commit c18d7cc6ec
  1. 2
      pkg/conf/paladin/file.go
  2. 51
      pkg/conf/paladin/file_test.go

@ -195,7 +195,7 @@ func (f *file) reloadFile(name string) {
for _, ch := range chs {
select {
case ch <- Event{Event: EventUpdate, Value: val.raw}:
case ch <- Event{Event: EventUpdate, Key: key, Value: val.raw}:
default:
log.Printf("event channel full discard file %s update event", name)
}

@ -4,6 +4,7 @@ import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
@ -14,14 +15,14 @@ func TestNewFile(t *testing.T) {
// test data
path := "/tmp/test_conf/"
assert.Nil(t, os.MkdirAll(path, 0700))
assert.Nil(t, ioutil.WriteFile(path+"test.toml", []byte(`
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "test.toml"), []byte(`
text = "hello"
number = 100
slice = [1, 2, 3]
sliceStr = ["1", "2", "3"]
`), 0644))
// test client
cli, err := NewFile(path + "test.toml")
cli, err := NewFile(filepath.Join(path, "test.toml"))
assert.Nil(t, err)
assert.NotNil(t, cli)
// test map
@ -41,11 +42,11 @@ func TestNewFilePath(t *testing.T) {
// test data
path := "/tmp/test_conf/"
assert.Nil(t, os.MkdirAll(path, 0700))
assert.Nil(t, ioutil.WriteFile(path+"test.toml", []byte(`
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "test.toml"), []byte(`
text = "hello"
number = 100
`), 0644))
assert.Nil(t, ioutil.WriteFile(path+"abc.toml", []byte(`
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "abc.toml"), []byte(`
text = "hello"
number = 100
`), 0644))
@ -70,11 +71,11 @@ func TestFileEvent(t *testing.T) {
// test data
path := "/tmp/test_conf_event/"
assert.Nil(t, os.MkdirAll(path, 0700))
assert.Nil(t, ioutil.WriteFile(path+"test.toml", []byte(`
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "test.toml"), []byte(`
text = "hello"
number = 100
`), 0644))
assert.Nil(t, ioutil.WriteFile(path+"abc.toml", []byte(`
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "abc.toml"), []byte(`
text = "hello"
number = 100
`), 0644))
@ -84,25 +85,33 @@ func TestFileEvent(t *testing.T) {
assert.NotNil(t, cli)
ch := cli.WatchEvent(context.Background(), "test.toml", "abc.toml")
time.Sleep(time.Millisecond)
ioutil.WriteFile(path+"test.toml", []byte(`hello`), 0644)
timeout := time.NewTimer(time.Second)
// for file test.toml
ioutil.WriteFile(filepath.Join(path, "test.toml"), []byte(`hello`), 0644)
select {
case <-timeout.C:
t.Fatalf("run test timeout")
case ev := <-ch:
assert.Equal(t, EventUpdate, ev.Event)
assert.Equal(t, "hello", ev.Value)
if ev.Key == "test.toml" {
assert.Equal(t, EventUpdate, ev.Event)
assert.Equal(t, "hello", ev.Value)
}
}
ioutil.WriteFile(path+"abc.toml", []byte(`test`), 0644)
content1, _ := cli.Get("test.toml").String()
assert.Equal(t, "hello", content1)
// for file abc.toml
ioutil.WriteFile(filepath.Join(path, "abc.toml"), []byte(`test`), 0644)
select {
case <-timeout.C:
t.Fatalf("run test timeout")
case ev := <-ch:
assert.Equal(t, EventUpdate, ev.Event)
assert.Equal(t, "test", ev.Value)
if ev.Key == "abc.toml" {
assert.Equal(t, EventUpdate, ev.Event)
assert.Equal(t, "test", ev.Value)
}
}
content1, _ := cli.Get("test.toml").String()
assert.Equal(t, "hello", content1)
content2, _ := cli.Get("abc.toml").String()
assert.Equal(t, "test", content2)
}
@ -110,8 +119,8 @@ func TestFileEvent(t *testing.T) {
func TestHiddenFile(t *testing.T) {
path := "/tmp/test_hidden_event/"
assert.Nil(t, os.MkdirAll(path, 0700))
assert.Nil(t, ioutil.WriteFile(path+"test.toml", []byte(`hello`), 0644))
assert.Nil(t, ioutil.WriteFile(path+".abc.toml", []byte(`
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "test.toml"), []byte(`hello`), 0644))
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "abc.toml"), []byte(`
text = "hello"
number = 100
`), 0644))
@ -121,7 +130,7 @@ func TestHiddenFile(t *testing.T) {
assert.NotNil(t, cli)
cli.WatchEvent(context.Background(), "test.toml")
time.Sleep(time.Millisecond)
ioutil.WriteFile(path+".abc.toml", []byte(`hello`), 0644)
ioutil.WriteFile(filepath.Join(path, "abc.toml"), []byte(`hello`), 0644)
time.Sleep(time.Second)
content1, _ := cli.Get("test.toml").String()
assert.Equal(t, "hello", content1)
@ -133,14 +142,14 @@ func TestOneLevelSymbolicFile(t *testing.T) {
path := "/tmp/test_symbolic_link/"
path2 := "/tmp/test_symbolic_link/configs/"
assert.Nil(t, os.MkdirAll(path2, 0700))
assert.Nil(t, ioutil.WriteFile(path+"test.toml", []byte(`hello`), 0644))
assert.Nil(t, os.Symlink(path+"test.toml", path2+"test.toml.ln"))
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "test.toml"), []byte(`hello`), 0644))
assert.Nil(t, os.Symlink(filepath.Join(path, "test.toml"), filepath.Join(path2, "test.toml.ln")))
// test client
cli, err := NewFile(path2)
assert.Nil(t, err)
assert.NotNil(t, cli)
content, _ := cli.Get("test.toml.ln").String()
assert.Equal(t, "hello", content)
os.Remove(path+"test.toml")
os.Remove(path2+"test.toml.ln")
os.Remove(filepath.Join(path, "test.toml"))
os.Remove(filepath.Join(path2, "test.toml.ln"))
}

Loading…
Cancel
Save