mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-24 03:46:37 +02:00
fix for file_test (#486)
This commit is contained in:
parent
3f88443edf
commit
c18d7cc6ec
@ -195,7 +195,7 @@ func (f *file) reloadFile(name string) {
|
|||||||
|
|
||||||
for _, ch := range chs {
|
for _, ch := range chs {
|
||||||
select {
|
select {
|
||||||
case ch <- Event{Event: EventUpdate, Value: val.raw}:
|
case ch <- Event{Event: EventUpdate, Key: key, Value: val.raw}:
|
||||||
default:
|
default:
|
||||||
log.Printf("event channel full discard file %s update event", name)
|
log.Printf("event channel full discard file %s update event", name)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -14,14 +15,14 @@ func TestNewFile(t *testing.T) {
|
|||||||
// test data
|
// test data
|
||||||
path := "/tmp/test_conf/"
|
path := "/tmp/test_conf/"
|
||||||
assert.Nil(t, os.MkdirAll(path, 0700))
|
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"
|
text = "hello"
|
||||||
number = 100
|
number = 100
|
||||||
slice = [1, 2, 3]
|
slice = [1, 2, 3]
|
||||||
sliceStr = ["1", "2", "3"]
|
sliceStr = ["1", "2", "3"]
|
||||||
`), 0644))
|
`), 0644))
|
||||||
// test client
|
// test client
|
||||||
cli, err := NewFile(path + "test.toml")
|
cli, err := NewFile(filepath.Join(path, "test.toml"))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, cli)
|
assert.NotNil(t, cli)
|
||||||
// test map
|
// test map
|
||||||
@ -41,11 +42,11 @@ func TestNewFilePath(t *testing.T) {
|
|||||||
// test data
|
// test data
|
||||||
path := "/tmp/test_conf/"
|
path := "/tmp/test_conf/"
|
||||||
assert.Nil(t, os.MkdirAll(path, 0700))
|
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"
|
text = "hello"
|
||||||
number = 100
|
number = 100
|
||||||
`), 0644))
|
`), 0644))
|
||||||
assert.Nil(t, ioutil.WriteFile(path+"abc.toml", []byte(`
|
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "abc.toml"), []byte(`
|
||||||
text = "hello"
|
text = "hello"
|
||||||
number = 100
|
number = 100
|
||||||
`), 0644))
|
`), 0644))
|
||||||
@ -70,11 +71,11 @@ func TestFileEvent(t *testing.T) {
|
|||||||
// test data
|
// test data
|
||||||
path := "/tmp/test_conf_event/"
|
path := "/tmp/test_conf_event/"
|
||||||
assert.Nil(t, os.MkdirAll(path, 0700))
|
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"
|
text = "hello"
|
||||||
number = 100
|
number = 100
|
||||||
`), 0644))
|
`), 0644))
|
||||||
assert.Nil(t, ioutil.WriteFile(path+"abc.toml", []byte(`
|
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "abc.toml"), []byte(`
|
||||||
text = "hello"
|
text = "hello"
|
||||||
number = 100
|
number = 100
|
||||||
`), 0644))
|
`), 0644))
|
||||||
@ -84,25 +85,33 @@ func TestFileEvent(t *testing.T) {
|
|||||||
assert.NotNil(t, cli)
|
assert.NotNil(t, cli)
|
||||||
ch := cli.WatchEvent(context.Background(), "test.toml", "abc.toml")
|
ch := cli.WatchEvent(context.Background(), "test.toml", "abc.toml")
|
||||||
time.Sleep(time.Millisecond)
|
time.Sleep(time.Millisecond)
|
||||||
ioutil.WriteFile(path+"test.toml", []byte(`hello`), 0644)
|
|
||||||
timeout := time.NewTimer(time.Second)
|
timeout := time.NewTimer(time.Second)
|
||||||
|
|
||||||
|
// for file test.toml
|
||||||
|
ioutil.WriteFile(filepath.Join(path, "test.toml"), []byte(`hello`), 0644)
|
||||||
select {
|
select {
|
||||||
case <-timeout.C:
|
case <-timeout.C:
|
||||||
t.Fatalf("run test timeout")
|
t.Fatalf("run test timeout")
|
||||||
case ev := <-ch:
|
case ev := <-ch:
|
||||||
assert.Equal(t, EventUpdate, ev.Event)
|
if ev.Key == "test.toml" {
|
||||||
assert.Equal(t, "hello", ev.Value)
|
assert.Equal(t, EventUpdate, ev.Event)
|
||||||
}
|
assert.Equal(t, "hello", ev.Value)
|
||||||
ioutil.WriteFile(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)
|
|
||||||
}
|
}
|
||||||
content1, _ := cli.Get("test.toml").String()
|
content1, _ := cli.Get("test.toml").String()
|
||||||
assert.Equal(t, "hello", content1)
|
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:
|
||||||
|
if ev.Key == "abc.toml" {
|
||||||
|
assert.Equal(t, EventUpdate, ev.Event)
|
||||||
|
assert.Equal(t, "test", ev.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
content2, _ := cli.Get("abc.toml").String()
|
content2, _ := cli.Get("abc.toml").String()
|
||||||
assert.Equal(t, "test", content2)
|
assert.Equal(t, "test", content2)
|
||||||
}
|
}
|
||||||
@ -110,8 +119,8 @@ func TestFileEvent(t *testing.T) {
|
|||||||
func TestHiddenFile(t *testing.T) {
|
func TestHiddenFile(t *testing.T) {
|
||||||
path := "/tmp/test_hidden_event/"
|
path := "/tmp/test_hidden_event/"
|
||||||
assert.Nil(t, os.MkdirAll(path, 0700))
|
assert.Nil(t, os.MkdirAll(path, 0700))
|
||||||
assert.Nil(t, ioutil.WriteFile(path+"test.toml", []byte(`hello`), 0644))
|
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "test.toml"), []byte(`hello`), 0644))
|
||||||
assert.Nil(t, ioutil.WriteFile(path+".abc.toml", []byte(`
|
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "abc.toml"), []byte(`
|
||||||
text = "hello"
|
text = "hello"
|
||||||
number = 100
|
number = 100
|
||||||
`), 0644))
|
`), 0644))
|
||||||
@ -121,7 +130,7 @@ func TestHiddenFile(t *testing.T) {
|
|||||||
assert.NotNil(t, cli)
|
assert.NotNil(t, cli)
|
||||||
cli.WatchEvent(context.Background(), "test.toml")
|
cli.WatchEvent(context.Background(), "test.toml")
|
||||||
time.Sleep(time.Millisecond)
|
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)
|
time.Sleep(time.Second)
|
||||||
content1, _ := cli.Get("test.toml").String()
|
content1, _ := cli.Get("test.toml").String()
|
||||||
assert.Equal(t, "hello", content1)
|
assert.Equal(t, "hello", content1)
|
||||||
@ -133,14 +142,14 @@ func TestOneLevelSymbolicFile(t *testing.T) {
|
|||||||
path := "/tmp/test_symbolic_link/"
|
path := "/tmp/test_symbolic_link/"
|
||||||
path2 := "/tmp/test_symbolic_link/configs/"
|
path2 := "/tmp/test_symbolic_link/configs/"
|
||||||
assert.Nil(t, os.MkdirAll(path2, 0700))
|
assert.Nil(t, os.MkdirAll(path2, 0700))
|
||||||
assert.Nil(t, ioutil.WriteFile(path+"test.toml", []byte(`hello`), 0644))
|
assert.Nil(t, ioutil.WriteFile(filepath.Join(path, "test.toml"), []byte(`hello`), 0644))
|
||||||
assert.Nil(t, os.Symlink(path+"test.toml", path2+"test.toml.ln"))
|
assert.Nil(t, os.Symlink(filepath.Join(path, "test.toml"), filepath.Join(path2, "test.toml.ln")))
|
||||||
// test client
|
// test client
|
||||||
cli, err := NewFile(path2)
|
cli, err := NewFile(path2)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, cli)
|
assert.NotNil(t, cli)
|
||||||
content, _ := cli.Get("test.toml.ln").String()
|
content, _ := cli.Get("test.toml.ln").String()
|
||||||
assert.Equal(t, "hello", content)
|
assert.Equal(t, "hello", content)
|
||||||
os.Remove(path+"test.toml")
|
os.Remove(filepath.Join(path, "test.toml"))
|
||||||
os.Remove(path2+"test.toml.ln")
|
os.Remove(filepath.Join(path2, "test.toml.ln"))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user