config: fix panic on multiple Close() (#1374)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-03-19 12:54:59 +03:00 committed by GitHub
parent 40ff6ddfcf
commit cbb958def5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 3 deletions

View File

@ -44,7 +44,7 @@ func (c *config) Init(opts ...Option) error {
Loader: memory.NewLoader(),
Reader: json.NewReader(),
}
c.exit = make(chan bool)
for _, o := range opts {
o(&c.opts)
}

View File

@ -1,4 +1,4 @@
package file
package file_test
import (
"fmt"
@ -6,8 +6,37 @@ import (
"path/filepath"
"testing"
"time"
"github.com/micro/go-micro/v2/config"
"github.com/micro/go-micro/v2/config/source/file"
)
func TestConfig(t *testing.T) {
data := []byte(`{"foo": "bar"}`)
path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
fh, err := os.Create(path)
if err != nil {
t.Error(err)
}
defer func() {
fh.Close()
os.Remove(path)
}()
_, err = fh.Write(data)
if err != nil {
t.Error(err)
}
conf, err := config.NewConfig()
if err != nil {
t.Fatal(err)
}
conf.Load(file.NewSource(file.WithPath(path)))
// simulate multiple close
go conf.Close()
go conf.Close()
}
func TestFile(t *testing.T) {
data := []byte(`{"foo": "bar"}`)
path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
@ -25,7 +54,7 @@ func TestFile(t *testing.T) {
t.Error(err)
}
f := NewSource(WithPath(path))
f := file.NewSource(file.WithPath(path))
c, err := f.Read()
if err != nil {
t.Error(err)