Skip to content
This repository has been archived by the owner on Nov 26, 2018. It is now read-only.

Commit

Permalink
Merge pull request #27 from jiajunhuang/improve_coverage_for_config
Browse files Browse the repository at this point in the history
improve coverage for config
  • Loading branch information
jiajunhuang authored Jan 26, 2018
2 parents 422fc87 + 37dd021 commit eb30d24
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
2 changes: 1 addition & 1 deletion balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
LBMRandom = "random"
)

// Backend is the backend server, usally a app server like: gunicorn+flask
// Backend is the backend server, usually a app server like: gunicorn+flask
type Backend struct {
Weight int
URL string // cache the result
Expand Down
8 changes: 3 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,13 @@ func configKeeper() {
b := breakerConfig{make(map[string](appConfig))}

fileBytes := readFromFile(*configPath)
if err := json.Unmarshal(fileBytes, &b); err != nil && len(fileBytes) > 0 {
log.Printf("failed to unmarshal config file %s because %s", *configPath, err)
}

if len(b.APPs) > 0 {
if err := json.Unmarshal(fileBytes, &b); err == nil && len(b.APPs) > 0 {
log.Printf("loading config from config file")
for k, v := range b.APPs {
breaker.apps[k] = getAPP(&v)
}
} else {
log.Printf("failed to unmarshal config file %s because %s", *configPath, err)
}

// listen channel for sync
Expand Down
87 changes: 87 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
)

Expand Down Expand Up @@ -81,3 +83,88 @@ func TestGetBalancer(t *testing.T) {
defer shouldPanic()
getBalancer("what")
}

func TestReadFromFile(t *testing.T) {
os.Remove(*configPath)
defer os.Remove(*configPath)

f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
t.Errorf("failed to open config file: %s", err)
}
defer f.Close()
content := []byte("hello world")
f.Write(content)

readContent := readFromFile(*configPath)
if !bytes.Equal(readContent, content) {
t.Errorf("read from file return bad content: %s", string(readContent))
}
}

func TestConfigKeeper(t *testing.T) {
os.Remove(*configPath)
defer os.Remove(*configPath)

config := appConfig{
"www.example.com",
[]string{"192.168.1.1:80"},
[]int{1},
0.3,
false,
LBMWRR,
[]string{"/"},
[]string{"GET"},
}

go configKeeper()

configSync <- config
}

func TestConfigKeeperBadJSON(t *testing.T) {
os.Remove(*configPath)
defer os.Remove(*configPath)
f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
t.Errorf("failed to open config file: %s", err)
}
defer f.Close()
content := []byte("hello world")
f.Write(content)

go configKeeper()
}

func TestConfigKeeperGoodJSON(t *testing.T) {
os.Remove(*configPath)
//defer os.Remove(*configPath)
f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
t.Errorf("failed to open config file: %s", err)
}
config := appConfig{
"www.example.com",
[]string{"192.168.1.1:80"},
[]int{1},
0.3,
false,
LBMWRR,
[]string{"/"},
[]string{"GET"},
}
breakerConfig := make(map[string]appConfig)
breakerConfig[config.Name] = config
fileBytes, err := json.Marshal(breakerConfig)
if err != nil {
t.Errorf("failed to marshal breaker config: %s", err)
}
f.Truncate(0)
f.Seek(0, 0)
f.Write(fileBytes)
f.Close()

go configKeeper()

close(configSync)
}

0 comments on commit eb30d24

Please sign in to comment.