Removed filewatch counter test.

This commit is contained in:
Milos Gajdos 2019-08-21 11:26:41 +01:00
parent 0aea8e3163
commit 86f0c06fac
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F

View File

@ -1,7 +1,6 @@
package config package config
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -9,7 +8,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/google/uuid"
"github.com/micro/go-micro/config/source/env" "github.com/micro/go-micro/config/source/env"
"github.com/micro/go-micro/config/source/file" "github.com/micro/go-micro/config/source/file"
) )
@ -122,74 +120,3 @@ func TestConfigMerge(t *testing.T) {
actualHost) actualHost)
} }
} }
func TestFileChange(t *testing.T) {
// create a temp file
fileName := uuid.New().String() + "testWatcher.json"
f, err := os.OpenFile("."+sep+fileName, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
t.Error(err)
}
defer f.Close()
defer os.Remove("." + sep + fileName)
// load the file
if err := Load(file.NewSource(
file.WithPath("." + sep + fileName),
)); err != nil {
t.Error(err)
}
// watch changes
watcher, err := Watch()
if err != nil {
t.Error(err)
}
changeTimes := 0
done := make(chan bool)
go func() {
defer func() {
close(done)
}()
for {
v, err := watcher.Next()
if err != nil {
if err.Error() != "watcher stopped" {
t.Error(err)
return
}
return
}
changeTimes++
t.Logf("file change%s: %d", string(v.Bytes()), changeTimes)
}
}()
content := map[int]string{}
// change the file
for i := 0; i < 5; i++ {
content[i] = time.Now().String()
bytes, _ := json.Marshal(content)
f.Truncate(0)
f.Seek(0, 0)
if _, err := f.Write(bytes); err != nil {
t.Error(err)
}
time.Sleep(500 * time.Millisecond)
}
if err := watcher.Stop(); err != nil {
t.Fatalf("failed to stop watcher: %s", err)
}
// wait for the watcher to finish
<-done
if changeTimes != 5 {
t.Errorf("watcher error: change times %d is not enough", changeTimes)
}
}