Merge pull request #5008 from gyuho/gosimple_unused

clean up with gosimple and unused
This commit is contained in:
Gyu-Ho Lee 2016-04-07 23:31:21 -07:00
commit 4041bbe571
16 changed files with 68 additions and 58 deletions

View File

@ -22,6 +22,8 @@ env:
before_install: before_install:
- go get -v github.com/chzchzchz/goword - go get -v github.com/chzchzchz/goword
- go get -v honnef.co/go/simple/cmd/gosimple
- go get -v honnef.co/go/unused/cmd/unused
# disable godep restore override # disable godep restore override
install: install:

View File

@ -45,16 +45,6 @@ func NewUniqueKV(ctx context.Context, kv v3.KV, pfx, val string, opts ...v3.OpOp
} }
} }
func waitUpdate(ctx context.Context, client *v3.Client, key string, opts ...v3.OpOption) error {
cctx, cancel := context.WithCancel(ctx)
defer cancel()
wresp, ok := <-client.Watch(cctx, key, opts...)
if !ok {
return ctx.Err()
}
return wresp.Err()
}
func waitDelete(ctx context.Context, client *v3.Client, key string, rev int64) error { func waitDelete(ctx context.Context, client *v3.Client, key string, rev int64) error {
cctx, cancel := context.WithCancel(ctx) cctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()

View File

@ -81,9 +81,11 @@ func restoreEtcd() error {
return runCommands(10, 2*time.Second) return runCommands(10, 2*time.Second)
} }
var clusterHealthRegex = regexp.MustCompile(".*cluster is healthy.*") var (
var lineSplit = regexp.MustCompile("\n+") clusterHealthRegex = regexp.MustCompile(".*cluster is healthy.*")
var colonSplit = regexp.MustCompile("\\:") lineSplit = regexp.MustCompile("\n+")
colonSplit = regexp.MustCompile(`\:`)
)
func runCommands(maxRetry int, interval time.Duration) error { func runCommands(maxRetry int, interval time.Duration) error {
var retryCnt int var retryCnt int

View File

@ -104,7 +104,7 @@ func handleClusterHealth(c *cli.Context) {
} }
checked = true checked = true
if result.Health == "true" || nresult.Health == true { if result.Health == "true" || nresult.Health {
health = true health = true
fmt.Printf("member %s is healthy: got healthy result from %s\n", m.ID, url) fmt.Printf("member %s is healthy: got healthy result from %s\n", m.ID, url)
} else { } else {

View File

@ -42,18 +42,6 @@ var (
defaultDialTimeout = 30 * time.Second defaultDialTimeout = 30 * time.Second
) )
// trimsplit slices s into all substrings separated by sep and returns a
// slice of the substrings between the separator with all leading and trailing
// white space removed, as defined by Unicode.
func trimsplit(s, sep string) []string {
raw := strings.Split(s, ",")
trimmed := make([]string, 0)
for _, r := range raw {
trimmed = append(trimmed, strings.TrimSpace(r))
}
return trimmed
}
func argOrStdin(args []string, stdin io.Reader, i int) (string, error) { func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
if i < len(args) { if i < len(args) {
return args[i], nil return args[i], nil

View File

@ -22,10 +22,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var ( var memberPeerURLs string
memberID uint64
memberPeerURLs string
)
// NewMemberCommand returns the cobra command for "member". // NewMemberCommand returns the cobra command for "member".
func NewMemberCommand() *cobra.Command { func NewMemberCommand() *cobra.Command {

View File

@ -416,7 +416,7 @@ func TestCreateAndUpdateUser(t *testing.T) {
s := store{server: d, timeout: testTimeout, ensuredOnce: true, PasswordStore: fastPasswordStore{}} s := store{server: d, timeout: testTimeout, ensuredOnce: true, PasswordStore: fastPasswordStore{}}
out, created, err := s.CreateOrUpdateUser(user) out, created, err := s.CreateOrUpdateUser(user)
if created == false { if !created {
t.Error("Should have created user, instead updated?") t.Error("Should have created user, instead updated?")
} }
if err != nil { if err != nil {
@ -427,7 +427,7 @@ func TestCreateAndUpdateUser(t *testing.T) {
t.Error("UpdateUser doesn't match given update. Got", out, "expected", expected) t.Error("UpdateUser doesn't match given update. Got", out, "expected", expected)
} }
out, created, err = s.CreateOrUpdateUser(update) out, created, err = s.CreateOrUpdateUser(update)
if created == true { if created {
t.Error("Should have updated user, instead created?") t.Error("Should have updated user, instead created?")
} }
if err != nil { if err != nil {
@ -572,6 +572,7 @@ func TestEnableAuth(t *testing.T) {
t.Error("Unexpected error", err) t.Error("Unexpected error", err)
} }
} }
func TestDisableAuth(t *testing.T) { func TestDisableAuth(t *testing.T) {
trueval := "true" trueval := "true"
falseval := "false" falseval := "false"

View File

@ -144,9 +144,7 @@ func (c *RaftCluster) PeerURLs() []string {
defer c.Unlock() defer c.Unlock()
urls := make([]string, 0) urls := make([]string, 0)
for _, p := range c.members { for _, p := range c.members {
for _, addr := range p.PeerURLs { urls = append(urls, p.PeerURLs...)
urls = append(urls, addr)
}
} }
sort.Strings(urls) sort.Strings(urls)
return urls return urls
@ -159,9 +157,7 @@ func (c *RaftCluster) ClientURLs() []string {
defer c.Unlock() defer c.Unlock()
urls := make([]string, 0) urls := make([]string, 0)
for _, p := range c.members { for _, p := range c.members {
for _, url := range p.ClientURLs { urls = append(urls, p.ClientURLs...)
urls = append(urls, url)
}
} }
sort.Strings(urls) sort.Strings(urls)
return urls return urls

View File

@ -85,7 +85,7 @@ func (q *statsQueue) Rate() (float64, float64) {
return 0, 0 return 0, 0
} }
if time.Now().Sub(back.SendingTime) > time.Second { if time.Since(back.SendingTime) > time.Second {
q.Clear() q.Clear()
return 0, 0 return 0, 0
} }

View File

@ -57,7 +57,7 @@ func (ss *ServerStats) JSON() []byte {
ss.Lock() ss.Lock()
stats := *ss stats := *ss
ss.Unlock() ss.Unlock()
stats.LeaderInfo.Uptime = time.Now().Sub(stats.LeaderInfo.StartTime).String() stats.LeaderInfo.Uptime = time.Since(stats.LeaderInfo.StartTime).String()
stats.SendingPkgRate, stats.SendingBandwidthRate = stats.SendRates() stats.SendingPkgRate, stats.SendingBandwidthRate = stats.SendRates()
stats.RecvingPkgRate, stats.RecvingBandwidthRate = stats.RecvRates() stats.RecvingPkgRate, stats.RecvingBandwidthRate = stats.RecvRates()
b, err := json.Marshal(stats) b, err := json.Marshal(stats)

View File

@ -48,7 +48,7 @@ func CheckLeakedGoroutine() bool {
} }
stackCount := make(map[string]int) stackCount := make(map[string]int)
re := regexp.MustCompile("\\(0[0-9a-fx, ]*\\)") re := regexp.MustCompile(`\(0[0-9a-fx, ]*\)`)
for _, g := range gs { for _, g := range gs {
// strip out pointer arguments in first function of stack dump // strip out pointer arguments in first function of stack dump
normalized := string(re.ReplaceAll([]byte(g), []byte("(...)"))) normalized := string(re.ReplaceAll([]byte(g), []byte("(...)")))

View File

@ -289,7 +289,7 @@ func (s *store) Compact(rev int64) (<-chan struct{}, error) {
s.fifoSched.Schedule(j) s.fifoSched.Schedule(j)
indexCompactionPauseDurations.Observe(float64(time.Now().Sub(start) / time.Millisecond)) indexCompactionPauseDurations.Observe(float64(time.Since(start) / time.Millisecond))
return ch, nil return ch, nil
} }

View File

@ -21,7 +21,7 @@ import (
func (s *store) scheduleCompaction(compactMainRev int64, keep map[revision]struct{}) bool { func (s *store) scheduleCompaction(compactMainRev int64, keep map[revision]struct{}) bool {
totalStart := time.Now() totalStart := time.Now()
defer dbCompactionTotalDurations.Observe(float64(time.Now().Sub(totalStart) / time.Millisecond)) defer dbCompactionTotalDurations.Observe(float64(time.Since(totalStart) / time.Millisecond))
end := make([]byte, 8) end := make([]byte, 8)
binary.BigEndian.PutUint64(end, uint64(compactMainRev+1)) binary.BigEndian.PutUint64(end, uint64(compactMainRev+1))
@ -54,7 +54,7 @@ func (s *store) scheduleCompaction(compactMainRev int64, keep map[revision]struc
// update last // update last
revToBytes(revision{main: rev.main, sub: rev.sub + 1}, last) revToBytes(revision{main: rev.main, sub: rev.sub + 1}, last)
tx.Unlock() tx.Unlock()
dbCompactionPauseDurations.Observe(float64(time.Now().Sub(start) / time.Millisecond)) dbCompactionPauseDurations.Observe(float64(time.Since(start) / time.Millisecond))
select { select {
case <-time.After(100 * time.Millisecond): case <-time.After(100 * time.Millisecond):

View File

@ -117,10 +117,7 @@ func (eh *EventHistory) clone() *EventHistory {
Back: eh.Queue.Back, Back: eh.Queue.Back,
} }
for i, e := range eh.Queue.Events { copy(clonedQueue.Events, eh.Queue.Events)
clonedQueue.Events[i] = e
}
return &EventHistory{ return &EventHistory{
StartIndex: eh.StartIndex, StartIndex: eh.StartIndex,
Queue: clonedQueue, Queue: clonedQueue,

View File

@ -345,7 +345,7 @@ func (s *store) Delete(nodePath string, dir, recursive bool) (*Event, error) {
} }
// recursive implies dir // recursive implies dir
if recursive == true { if recursive {
dir = true dir = true
} }

55
test
View File

@ -10,6 +10,10 @@
# PKG=snap ./test # PKG=snap ./test
set -e set -e
# TODO: 'client' pkg fails with gosimple from generated files
# TODO: 'rafttest' is failing with unused
GOSIMPLE_UNUSED_PATHS=$(go list ./... | sed -e 's/github.com\/coreos\/etcd\///g' | grep -vE 'cmd|vendor|rafttest|github.com/coreos/etcd$|client$')
# Invoke ./cover for HTML output # Invoke ./cover for HTML output
COVER=${COVER:-"-cover"} COVER=${COVER:-"-cover"}
@ -96,15 +100,48 @@ function fmt_tests {
done done
echo "Checking goword..." echo "Checking goword..."
# get all go files to process if which goword >/dev/null; then
gofiles=`find $FMT -iname '*.go' 2>/dev/null` echo "goword is installed..."
# ignore tests and protobuf files # get all go files to process
gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"` gofiles=`find $FMT -iname '*.go' 2>/dev/null`
# only check for broken exported godocs # ignore tests and protobuf files
gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort` gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
if [ ! -z "$gowordRes" ]; then # only check for broken exported godocs
echo -e "goword checking failed:\n${gowordRes}" gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
exit 255 if [ ! -z "$gowordRes" ]; then
echo -e "goword checking failed:\n${gowordRes}"
exit 255
fi
else
echo "gowrod does not exist... skipping..."
fi
echo "Checking gosimple"
if which gosimple >/dev/null; then
echo "gosimple is installed..."
for path in $GOSIMPLE_UNUSED_PATHS; do
simplResult=$(gosimple $REPO_PATH/${path})
if [ -n "${simplResult}" ]; then
echo -e "gosimple checking ${path} failed:\n${simplResult}"
exit 255
fi
done
else
echo "gosimple does not exist... skipping..."
fi
echo "Checking unused"
if which unused >/dev/null; then
echo "unused is installed..."
for path in $GOSIMPLE_UNUSED_PATHS; do
unusedResult=$(unused $REPO_PATH/${path})
if [ -n "${unusedResult}" ]; then
echo -e "unused checking ${path} failed:\n${unusedResult}"
exit 255
fi
done
else
echo "unused does not exist... skipping..."
fi fi
echo "Checking for license header..." echo "Checking for license header..."