Skip to content

Commit

Permalink
Gosimple (#292)
Browse files Browse the repository at this point in the history
* enable gosimple

Signed-off-by: Sam Yuan <yy19902439@126.com>

* enable govet

Signed-off-by: Sam Yuan <yy19902439@126.com>

* a part of #286 gofmt goimports ineffassign

Signed-off-by: Sam Yuan <yy19902439@126.com>

---------

Signed-off-by: Sam Yuan <yy19902439@126.com>
  • Loading branch information
SamYuan1990 authored Jun 11, 2023
1 parent 3a2d8e8 commit 72a3c6d
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 52 deletions.
10 changes: 5 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ linters:
- goconst
#- gocritic
- gocyclo
#- gofmt
#- goimports
- gofmt
- goimports
- goprintffuncname
# - gosimple
#- govet
#- ineffassign
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- noctx
Expand Down
2 changes: 1 addition & 1 deletion cmd/tape/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
defer file.Close()
logger.SetOutput(file)
if customerLevel, customerSet := os.LookupEnv(loglevel); customerSet {
if lvl, err := log.ParseLevel(customerLevel); err == nil {
if lvl, err_lvl := log.ParseLevel(customerLevel); err_lvl == nil {
logger.SetLevel(lvl)
}
}
Expand Down
34 changes: 20 additions & 14 deletions e2e/bad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,36 @@ import (
)

var _ = Describe("Mock test for error input", func() {

Context("E2E with Error Cases", func() {
When("Command error", func() {
It("should return unexpected command", func() {
cmd := exec.Command(tapeBin, "wrongCommand")
tapeSession, err := gexec.Start(cmd, nil, nil)
var err error
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("tape: error: unexpected wrongCommand, try --help"))
})

It("should return required flag config", func() {
cmd := exec.Command(tapeBin, "-n", "500")
tapeSession, err := gexec.Start(cmd, nil, nil)
var err error
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("tape: error: required flag --config not provided, try --help"))
})

PIt("should return required flag number", func() {
cmd := exec.Command(tapeBin, "-c", "TestFile")
tapeSession, err := gexec.Start(cmd, nil, nil)
var err error
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("tape: error: required flag --number not provided, try --help"))
})

It("should return help info", func() {
cmd := exec.Command(tapeBin, "--help")
tapeSession, err := gexec.Start(cmd, nil, nil)
var err error
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("usage: tape .*flags.* .*command.* .*args.*"))
})
Expand All @@ -57,37 +60,41 @@ var _ = Describe("Mock test for error input", func() {
}
e2e.GenerateConfigFile(config.Name(), configValue)
cmd := exec.Command(tapeBin, "-c", config.Name(), "-n", "500", "--rate=-1")
tapeSession, err := gexec.Start(cmd, nil, nil)
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("tape: error: rate must be zero \\(unlimited\\) or positive number\n"))
})

It("should return error msg when less than 1 burst", func() {
cmd := exec.Command(tapeBin, "-c", "config", "-n", "500", "--burst", "0")
tapeSession, err := gexec.Start(cmd, nil, nil)
var err error
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("tape: error: burst at least 1\n"))
})

It("should return warning msg when rate bigger than burst", func() {
cmd := exec.Command(tapeBin, "-c", "NoExitFile", "-n", "500", "--rate=10", "--burst", "1")
tapeSession, err := gexec.Start(cmd, nil, nil)
var err error
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Out).Should(Say("As rate 10 is bigger than burst 1, real rate is burst\n"))
Eventually(tapeSession.Err).Should(Say("NoExitFile"))
})

It("should return warning msg when rate bigger than default burst", func() {
cmd := exec.Command(tapeBin, "-c", "NoExitFile", "-n", "500", "--rate", "10000")
tapeSession, err := gexec.Start(cmd, nil, nil)
var err error
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Out).Should(Say("As rate 10000 is bigger than burst 1000, real rate is burst\n"))
Eventually(tapeSession.Err).Should(Say("NoExitFile"))
})

It("should return error msg when less than 1 signerNumber", func() {
cmd := exec.Command(tapeBin, "-c", "config", "-n", "500", "--signers", "0")
tapeSession, err := gexec.Start(cmd, nil, nil)
var err error
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("tape: error: signerNumber at least 1\n"))
})
Expand All @@ -96,7 +103,8 @@ var _ = Describe("Mock test for error input", func() {
When("Config error", func() {
It("should return file not exist", func() {
cmd := exec.Command(tapeBin, "-c", "NoExitFile", "-n", "500")
tapeSession, err := gexec.Start(cmd, nil, nil)
var err error
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("NoExitFile"))
})
Expand All @@ -115,7 +123,7 @@ var _ = Describe("Mock test for error input", func() {
}
e2e.GenerateConfigFile(config.Name(), configValue)
cmd := exec.Command(tapeBin, "-c", config.Name(), "-n", "500")
tapeSession, err := gexec.Start(cmd, nil, nil)
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("error loading priv key"))
})
Expand All @@ -133,7 +141,6 @@ var _ = Describe("Mock test for error input", func() {
PolicyFile: PolicyFile.Name(),
}
e2e.GenerateConfigFile(config.Name(), configValue)

cmd := exec.Command(tapeBin, "-c", config.Name(), "-n", "500")
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -155,7 +162,6 @@ var _ = Describe("Mock test for error input", func() {
PolicyFile: PolicyFile.Name(),
}
e2e.GenerateConfigFile(config.Name(), configValue)

cmd := exec.Command(tapeBin, "-c", config.Name(), "-n", "500")
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Expand Down
3 changes: 2 additions & 1 deletion e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func TestE2e(t *testing.T) {
}

var _ = BeforeSuite(func() {
tmpDir, err := os.MkdirTemp("", "tape-e2e-")
var err error
tmpDir, err = os.MkdirTemp("", "tape-e2e-")
Expect(err).NotTo(HaveOccurred())

mtlsCertFile, err = os.CreateTemp(tmpDir, "mtls-*.crt")
Expand Down
3 changes: 2 additions & 1 deletion e2e/e2e_version_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ var _ = Describe("Mock test for version", func() {
Context("E2E with correct subcommand", func() {
When("Version subcommand", func() {
It("should return version info", func() {
var err error
cmd := exec.Command(tapeBin, "version")
tapeSession, err := gexec.Start(cmd, nil, nil)
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Out).Should(Say("tape:\n Version:.*\n Go version:.*\n Git commit:.*\n Built:.*\n OS/Arch:.*\n"))
})
Expand Down
12 changes: 6 additions & 6 deletions internal/fabric/core/comm/creds.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
///*
//Copyright IBM Corp. All Rights Reserved.
//
//SPDX-License-Identifier: Apache-2.0
//*/
//
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package comm

import (
Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/cmdImpl/fullProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Process(configPath string, num int, burst, signerNumber, parallel int, rate
fmt.Println("start prometheus")
http.Handle("/metrics", promhttp.Handler())
server := &http.Server{Addr: ":8080", Handler: nil}
err := server.ListenAndServe()
err = server.ListenAndServe()
if err != nil {
cmdConfig.ErrorCh <- err
}
Expand Down
26 changes: 12 additions & 14 deletions pkg/infra/observer/endorsementObersver.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ func (o *EndorseObserver) Start() {
o.logger.Debugf("start observer for endorsement")
i := 0
for {
select {
case e := <-o.Envs:
tapeSpan := basic.GetGlobalSpan()
tapeSpan.FinishWithMap(e.TxId, "", basic.TRANSCATIONSTART)
i++
fmt.Printf("Time %8.2fs\tTx %6d Processed\n", time.Since(o.Now).Seconds(), i)
if o.n > 0 {
if o.n == i {
// consider with multiple threads need close this channel, need a once here to avoid channel been closed in multiple times
o.once.Do(func() {
close(o.finishCh)
})
return
}
e := <-o.Envs
tapeSpan := basic.GetGlobalSpan()
tapeSpan.FinishWithMap(e.TxId, "", basic.TRANSCATIONSTART)
i++
fmt.Printf("Time %8.2fs\tTx %6d Processed\n", time.Since(o.Now).Seconds(), i)
if o.n > 0 {
if o.n == i {
// consider with multiple threads need close this channel, need a once here to avoid channel been closed in multiple times
o.once.Do(func() {
close(o.finishCh)
})
return
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/infra/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var _ = Describe("Observer", func() {

BeforeEach(func() {
logger = log.New()

tmpDir, err := os.MkdirTemp("", "tape-")
var err error
tmpDir, err = os.MkdirTemp("", "tape-")
Expect(err).NotTo(HaveOccurred())

mtlsCertFile, err = os.CreateTemp(tmpDir, "mtls-*.crt")
Expand Down
1 change: 0 additions & 1 deletion pkg/infra/trafficGenerator/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func (a *Assembler) Start() {
for _, v := range a.Signed {
v <- t
}
r = nil
case <-a.Ctx.Done():
return
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/infra/trafficGenerator/fackEnvelopGenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var _ = Describe("FackEnvelopGenerator", func() {
)

BeforeEach(func() {
tmpDir, err := os.MkdirTemp("", "tape-")
var err error
tmpDir, err = os.MkdirTemp("", "tape-")
Expect(err).NotTo(HaveOccurred())

mtlsCertFile, err := os.CreateTemp(tmpDir, "mtls-*.crt")
Expand Down
4 changes: 2 additions & 2 deletions pkg/infra/trafficGenerator/initiator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var _ = Describe("Initiator", func() {
)

BeforeEach(func() {

tmpDir, err := os.MkdirTemp("", "tape-")
var err error
tmpDir, err = os.MkdirTemp("", "tape-")
Expect(err).NotTo(HaveOccurred())

mtlsCertFile, err := os.CreateTemp(tmpDir, "mtls-*.crt")
Expand Down
1 change: 0 additions & 1 deletion pkg/infra/trafficGenerator/intgerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func (integrator *Integrator) Start() {
return
}
integrator.Envs <- e
p = nil
case <-integrator.Ctx.Done():
return
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/infra/trafficGenerator/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ func ConvertString(arg string) (string, error) {
str := fmt.Sprint(arg[v[0]:v[1]])
current_arg = strings.Replace(current_arg, str, newUUID(), 1)
}
regString, _ := regexp.Compile("randomString(\\d*)")
randomStringreg := "randomString(\\d*)"
regString, _ := regexp.Compile(randomStringreg)
finds = regString.FindAllStringIndex(current_arg, -1)
arg = current_arg
for _, v := range finds {
Expand All @@ -334,7 +335,8 @@ func ConvertString(arg string) (string, error) {
}
current_arg = strings.Replace(current_arg, str, randomString(length), 1)
}
regNumber, _ := regexp.Compile("randomNumber(\\d*)_(\\d*)")
randomIntReg := "randomNumber(\\d*)_(\\d*)"
regNumber, _ := regexp.Compile(randomIntReg)
arg = current_arg
finds = regNumber.FindAllStringIndex(current_arg, -1)
for _, v := range finds {
Expand Down

0 comments on commit 72a3c6d

Please sign in to comment.