Skip to content

Commit

Permalink
Finish v2.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Oct 31, 2019
2 parents a5bd2d5 + a5d1b0c commit 4bd44bb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
/application.json
application*.json
/abc1.json
/env_test.properties
12 changes: 10 additions & 2 deletions app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"encoding/json"
"fmt"
"net/url"
"os"
"strings"
"sync"
"time"
)

const appConfigFileName = "app.properties"
const (
APP_CONFIG_FILE_NAME = "app.properties"
ENV_CONFIG_FILE_PATH = "AGOLLO_CONF"
)

var (
long_poll_interval = 2 * time.Second //2s
Expand Down Expand Up @@ -177,7 +181,11 @@ func getLoadAppConfig(loadAppConfig func() (*AppConfig, error)) (*AppConfig, err
if loadAppConfig != nil {
return loadAppConfig()
}
return loadJsonConfig(appConfigFileName)
configPath := os.Getenv(ENV_CONFIG_FILE_PATH)
if configPath==""{
configPath=APP_CONFIG_FILE_NAME
}
return loadJsonConfig(configPath)
}

//set timer for update ip list
Expand Down
36 changes: 35 additions & 1 deletion json_config_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package agollo

import (
"encoding/json"
. "github.com/tevid/gohamcrest"
"os"
"testing"
)

func TestLoadJsonConfig(t *testing.T) {
config, err := loadJsonConfig(appConfigFileName)
config, err := loadJsonConfig(APP_CONFIG_FILE_NAME)
t.Log(config)

Assert(t, err,NilVal())
Expand All @@ -18,6 +20,38 @@ func TestLoadJsonConfig(t *testing.T) {

}


func TestLoadEnvConfig(t *testing.T) {
envConfigFile:="env_test.properties"
config, _ := loadJsonConfig(APP_CONFIG_FILE_NAME)
config.Ip="123"
config.AppId="1111"
config.NamespaceName="nsabbda"
file, err := os.Create(envConfigFile)
if err != nil {
t.Error(err)
t.FailNow()
}
defer file.Close()
err = json.NewEncoder(file).Encode(config)
if err != nil {
t.Error(err)
t.FailNow()
}

err = os.Setenv(ENV_CONFIG_FILE_PATH, envConfigFile)
envConfig, envConfigErr := getLoadAppConfig(nil)
t.Log(config)

Assert(t, envConfigErr,NilVal())
Assert(t, envConfig,NotNilVal())
Assert(t, envConfig.AppId, Equal(config.AppId))
Assert(t, envConfig.Cluster, Equal(config.Cluster))
Assert(t, envConfig.NamespaceName,Equal(config.NamespaceName))
Assert(t, envConfig.Ip, Equal(config.Ip))

}

func TestLoadJsonConfigWrongFile(t *testing.T) {
config, err := loadJsonConfig("")
Assert(t, err,NotNilVal())
Expand Down

0 comments on commit 4bd44bb

Please sign in to comment.