Skip to content

Commit

Permalink
refactor for extension
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Apr 19, 2020
1 parent 8fe9aaa commit 8bd4950
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 24 deletions.
5 changes: 1 addition & 4 deletions component/notify/change_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
. "github.com/tevid/gohamcrest"
_ "github.com/zouyx/agollo/v3/agcache/memory"
"github.com/zouyx/agollo/v3/env"
jsonFile "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/extension"
_ "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/storage"
)

Expand Down Expand Up @@ -59,7 +58,6 @@ func TestListenChangeEvent(t *testing.T) {
}

func buildNotifyResult(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
initNotifications()
server := runChangeConfigResponse()
defer server.Close()
Expand All @@ -83,7 +81,6 @@ func buildNotifyResult(t *testing.T) {
}

func TestRemoveChangeListener(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
go buildNotifyResult(t)

listener := &CustomChangeListener{}
Expand Down
3 changes: 1 addition & 2 deletions component/notify/componet_notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/env/config"
jsonConfig "github.com/zouyx/agollo/v3/env/config/json"
jsonFile "github.com/zouyx/agollo/v3/env/file/json"
_ "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/extension"
)

Expand Down Expand Up @@ -56,7 +56,6 @@ func initMockNotifyAndConfigServer() {
}

func TestSyncConfigServices(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
initMockNotifyAndConfigServer()

err := AsyncConfigs()
Expand Down
12 changes: 6 additions & 6 deletions env/file/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const Suffix = ".json"

func init() {
extension.SetFileHandler(&JSONFileHandler{})
extension.SetFileHandler(&jsonFileHandler{})
}

var (
Expand All @@ -25,17 +25,17 @@ var (
configFileMap = make(map[string]string, 1)
)

//JSONFileHandler 默认备份文件读写
type JSONFileHandler struct {
//jsonFileHandler 默认备份文件读写
type jsonFileHandler struct {
}

//WriteConfigFile write config to file
func (fileHandler *JSONFileHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error {
func (fileHandler *jsonFileHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error {
return jsonFileConfig.Write(config, fileHandler.GetConfigFile(configPath, config.NamespaceName))
}

//GetConfigFile get real config file
func (fileHandler *JSONFileHandler) GetConfigFile(configDir string, namespace string) string {
func (fileHandler *jsonFileHandler) GetConfigFile(configDir string, namespace string) string {
fullPath := configFileMap[namespace]
if fullPath == "" {
filePath := fmt.Sprintf("%s%s", namespace, Suffix)
Expand All @@ -49,7 +49,7 @@ func (fileHandler *JSONFileHandler) GetConfigFile(configDir string, namespace st
}

//LoadConfigFile load config from file
func (fileHandler *JSONFileHandler) LoadConfigFile(configDir string, namespace string) (*env.ApolloConfig, error) {
func (fileHandler *jsonFileHandler) LoadConfigFile(configDir string, namespace string) (*env.ApolloConfig, error) {
configFilePath := fileHandler.GetConfigFile(configDir, namespace)
log.Info("load config file from :", configFilePath)
c, e := jsonFileConfig.Load(configFilePath, func(b []byte) (interface{}, error) {
Expand Down
4 changes: 2 additions & 2 deletions env/file/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestJSONFileHandler_WriteConfigFile(t *testing.T) {
extension.SetFileHandler(&JSONFileHandler{})
extension.SetFileHandler(&jsonFileHandler{})
configPath := ""
jsonStr := `{
"appId": "100004458",
Expand All @@ -32,7 +32,7 @@ func TestJSONFileHandler_WriteConfigFile(t *testing.T) {
}

func TestJSONFileHandler_LoadConfigFile(t *testing.T) {
extension.SetFileHandler(&JSONFileHandler{})
extension.SetFileHandler(&jsonFileHandler{})
jsonStr := `{
"appId": "100004458",
"cluster": "default",
Expand Down
23 changes: 19 additions & 4 deletions env/file/json/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ package json
import (
"fmt"
"os"
"sync"

"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/env/file"
)

//RawHandler 写入备份文件时,同时写入原始内容和namespace类型
type RawHandler struct {
*JSONFileHandler
var (
raw file.FileHandler
rawOnce sync.Once
)

//rawFileHandler 写入备份文件时,同时写入原始内容和namespace类型
type rawFileHandler struct {
*jsonFileHandler
}

func writeWithRaw(config *env.ApolloConfig, configDir string) error {
Expand All @@ -33,7 +40,15 @@ func writeWithRaw(config *env.ApolloConfig, configDir string) error {
}

//WriteConfigFile write config to file
func (fileHandler *RawHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error {
func (fileHandler *rawFileHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error {
writeWithRaw(config, configPath)
return jsonFileConfig.Write(config, fileHandler.GetConfigFile(configPath, config.NamespaceName))
}

// GetRawFileHandler 获取 rawFileHandler 实例
func GetRawFileHandler() file.FileHandler {
rawOnce.Do(func() {
raw = &rawFileHandler{}
})
return raw
}
10 changes: 9 additions & 1 deletion env/file/json/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestRawHandler_WriteConfigFile(t *testing.T) {
extension.SetFileHandler(&RawHandler{})
extension.SetFileHandler(&rawFileHandler{})
configPath := ""
jsonStr := `{
"appId": "100004458",
Expand All @@ -30,3 +30,11 @@ func TestRawHandler_WriteConfigFile(t *testing.T) {
e := extension.GetFileHandler().WriteConfigFile(config, configPath)
Assert(t, e, NilVal())
}

func TestGetRawFileHandler(t *testing.T) {
handler := GetRawFileHandler()
Assert(t, handler, NotNilVal())

fileHandler := GetRawFileHandler()
Assert(t, handler, Equal(fileHandler))
}
3 changes: 1 addition & 2 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/component/notify"
"github.com/zouyx/agollo/v3/env"
jsonFile "github.com/zouyx/agollo/v3/env/file/json"
_ "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/extension"
"github.com/zouyx/agollo/v3/storage"
)
Expand Down Expand Up @@ -131,7 +131,6 @@ func TestGetStringValue(t *testing.T) {
}

func TestAutoSyncConfigServicesNormal2NotModified(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
server := runLongNotmodifiedConfigResponse()
newAppConfig := getTestAppConfig()
newAppConfig.IP = server.URL
Expand Down
36 changes: 36 additions & 0 deletions start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"os"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -182,12 +183,47 @@ func TestSetCache(t *testing.T) {
Assert(t, extension.GetCacheFactory(), Equal(defaultCacheFactory))
}

type TestLoadBalance struct {
}

//Load 负载均衡
func (r *TestLoadBalance) Load(servers *sync.Map) *config.ServerInfo {
return nil
}

func TestSetLoadBalance(t *testing.T) {
balance := extension.GetLoadBalance()
Assert(t, balance, NotNilVal())

t2 := &TestLoadBalance{}
SetLoadBalance(t2)
Assert(t, t2, Equal(extension.GetLoadBalance()))
}

//testFileHandler 默认备份文件读写
type testFileHandler struct {
}

// WriteConfigFile write config to file
func (fileHandler *testFileHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error {
return nil
}

// GetConfigFile get real config file
func (fileHandler *testFileHandler) GetConfigFile(configDir string, namespace string) string {
return ""
}

// LoadConfigFile load config from file
func (fileHandler *testFileHandler) LoadConfigFile(configDir string, namespace string) (*env.ApolloConfig, error) {
return nil, nil
}

func TestSetBackupFileHandler(t *testing.T) {
fileHandler := extension.GetFileHandler()
Assert(t, fileHandler, NotNilVal())

t2 := &testFileHandler{}
SetBackupFileHandler(t2)
Assert(t, t2, Equal(extension.GetFileHandler()))
}
4 changes: 1 addition & 3 deletions storage/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import (
. "github.com/tevid/gohamcrest"
_ "github.com/zouyx/agollo/v3/agcache/memory"
"github.com/zouyx/agollo/v3/env"
jsonFile "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/extension"
_ "github.com/zouyx/agollo/v3/env/file/json"
)

//init param
func init() {
}

func TestUpdateApolloConfigNull(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
time.Sleep(1 * time.Second)

configurations := make(map[string]string)
Expand Down

0 comments on commit 8bd4950

Please sign in to comment.