Skip to content

Commit

Permalink
Fix yurthub tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Guo, Fei committed May 28, 2020
1 parent 362a6b6 commit e365556
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 86 deletions.
16 changes: 8 additions & 8 deletions pkg/yurthub/healthchecker/health_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

func TestHealthyCheckerWithHealthyServers(t *testing.T) {
servers := []string{
"127.0.0.1:8080",
"127.0.0.1:8081",
"127.0.0.1:8082",
"127.0.0.1:18080",
"127.0.0.1:18081",
"127.0.0.1:18082",
}

// start local http server
Expand Down Expand Up @@ -77,9 +77,9 @@ func TestHealthyCheckerWithHealthyServers(t *testing.T) {

func TestHealthyCheckerWithUnhealthyServers(t *testing.T) {
servers := []string{
"127.0.0.1:8080",
"127.0.0.1:8081",
"127.0.0.1:8082",
"127.0.0.1:18080",
"127.0.0.1:18081",
"127.0.0.1:18082",
}

// start local http server
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestHealthyCheckerWithUnhealthyServers(t *testing.T) {

func TestHealthyCheckerFromHealthyToUnhealthy(t *testing.T) {
servers := []string{
"127.0.0.1:8080",
"127.0.0.1:18080",
}

reqCnt := 0
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestHealthyCheckerFromHealthyToUnhealthy(t *testing.T) {

func TestHealthyCheckerFromUnHealthyToHealthy(t *testing.T) {
servers := []string{
"127.0.0.1:8080",
"127.0.0.1:18080",
}

reqCnt := 0
Expand Down
35 changes: 19 additions & 16 deletions pkg/yurthub/storage/disk/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ type diskStorage struct {
}

// NewDiskStorage creates a storage.Store for caching data into local disk
func NewDiskStorage() (storage.Store, error) {
if _, err := os.Stat(cacheBaseDir); os.IsNotExist(err) {
if err = os.MkdirAll(cacheBaseDir, 0755); err != nil {
func NewDiskStorage(dir string) (storage.Store, error) {
if dir == "" {
dir = cacheBaseDir
}
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
}

ds := &diskStorage{
keyPendingStatus: make(map[string]struct{}, 0),
baseDir: cacheBaseDir,
baseDir: dir,
}

err := ds.Recover("")
Expand All @@ -54,7 +57,7 @@ func (ds *diskStorage) Create(key string, contents []byte) error {
}
defer ds.unLockKey(key)

absKey := filepath.Join(cacheBaseDir, key)
absKey := filepath.Join(ds.baseDir, key)
if info, err := os.Stat(absKey); err != nil {
if os.IsNotExist(err) {
dir, _ := filepath.Split(absKey)
Expand Down Expand Up @@ -130,7 +133,7 @@ func (ds *diskStorage) delete(key string) error {
}
defer ds.unLockKey(key)

absKey := filepath.Join(cacheBaseDir, key)
absKey := filepath.Join(ds.baseDir, key)
info, err := os.Stat(absKey)
if err != nil {
if os.IsNotExist(err) {
Expand All @@ -148,15 +151,15 @@ func (ds *diskStorage) delete(key string) error {

// Get get contents from the file that specified by key
func (ds *diskStorage) Get(key string) ([]byte, error) {
return ds.get(filepath.Join(cacheBaseDir, key))
return ds.get(filepath.Join(ds.baseDir, key))
}

func (ds *diskStorage) get(path string) ([]byte, error) {
if path == "" {
return nil, nil
}

key := strings.TrimPrefix(path, cacheBaseDir)
key := strings.TrimPrefix(path, ds.baseDir)
if !ds.lockKey(key) {
return nil, storage.ErrStorageAccessConflict
}
Expand All @@ -183,7 +186,7 @@ func (ds *diskStorage) get(path string) ([]byte, error) {
// ListKeys list all of keys for files
func (ds *diskStorage) ListKeys(key string) ([]string, error) {
keys := make([]string, 0)
absPath := filepath.Join(cacheBaseDir, key)
absPath := filepath.Join(ds.baseDir, key)
if info, err := os.Stat(absPath); err != nil {
if os.IsNotExist(err) {
return keys, nil
Expand All @@ -198,7 +201,7 @@ func (ds *diskStorage) ListKeys(key string) ([]string, error) {
if info.Mode().IsRegular() {
_, file := filepath.Split(path)
if !strings.HasPrefix(file, tmpPrefix) {
keys = append(keys, strings.TrimPrefix(path, cacheBaseDir))
keys = append(keys, strings.TrimPrefix(path, ds.baseDir))
}
}

Expand All @@ -221,7 +224,7 @@ func (ds *diskStorage) List(key string) ([][]byte, error) {
}

bb := make([][]byte, 0)
absKey := filepath.Join(cacheBaseDir, key)
absKey := filepath.Join(ds.baseDir, key)
info, err := os.Stat(absKey)
if err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -285,8 +288,8 @@ func (ds *diskStorage) Update(key string, contents []byte) error {
return err
}

tmpPath := filepath.Join(cacheBaseDir, tmpKey)
absKey := filepath.Join(cacheBaseDir, key)
tmpPath := filepath.Join(ds.baseDir, tmpKey)
absKey := filepath.Join(ds.baseDir, key)
info, err := os.Stat(absKey)
if err != nil {
if !os.IsNotExist(err) {
Expand All @@ -305,7 +308,7 @@ func (ds *diskStorage) Update(key string, contents []byte) error {

// Recover recover storage error
func (ds *diskStorage) Recover(key string) error {
dir := filepath.Join(cacheBaseDir, key)
dir := filepath.Join(ds.baseDir, key)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -314,9 +317,9 @@ func (ds *diskStorage) Recover(key string) error {
if info.Mode().IsRegular() {
_, file := filepath.Split(path)
if strings.HasPrefix(file, tmpPrefix) {
tmpKey := strings.TrimPrefix(path, cacheBaseDir)
tmpKey := strings.TrimPrefix(path, ds.baseDir)
key := getKey(tmpKey)
keyPath := filepath.Join(cacheBaseDir, key)
keyPath := filepath.Join(ds.baseDir, key)
if !ds.lockKey(key) {
return nil
}
Expand Down
Loading

0 comments on commit e365556

Please sign in to comment.