Skip to content

Commit

Permalink
Sarthak | Refactors main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakMakhija committed Jan 19, 2024
1 parent 02c898c commit b4c5814
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

.idea
dist/
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
numberOfRequests = flag.Uint("n", 1000, "")
concurrency = flag.Uint("c", 50, "")
connections = flag.Uint("conn", 1, "")
filePath = flag.String("f", "", "")
payloadFilePath = flag.String("f", "", "")
requestsPerSecond = flag.Float64("rps", 0, "")
loadDuration = flag.Duration("z", 20*time.Second, "")
connectTimeout = flag.Duration("t", 3*time.Second, "")
Expand Down Expand Up @@ -82,10 +82,10 @@ Options:
func main() {
logo := `{{ .Title "blast" "" 0}}`
banner.InitString(os.Stdout, true, false, logo)
fmt.Fprintf(os.Stdout, versionLabel, version)
_, _ = fmt.Fprintf(os.Stdout, versionLabel, version)

flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(usage, runtime.NumCPU()))
_, _ = fmt.Fprint(os.Stderr, fmt.Sprintf(usage, runtime.NumCPU()))
}

flag.Parse()
Expand All @@ -95,7 +95,7 @@ func main() {

url := flag.Args()[0]
assertUrl(url)
assertFilePath(*filePath)
assertPayloadFilePath(*payloadFilePath)
assertConnectTimeout(*connectTimeout)
assertRequestsPerSecond(*requestsPerSecond)
assertLoadDuration(*loadDuration)
Expand Down Expand Up @@ -132,8 +132,8 @@ func assertUrl(url string) {
}
}

// assertFilePath asserts that the filePath is not empty.
func assertFilePath(filePath string) {
// assertPayloadFilePath asserts that the payloadFilePath is not empty.
func assertPayloadFilePath(filePath string) {
if len(strings.Trim(filePath, " ")) == 0 {
exitFunction("-f cannot be blank.")
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func setUpBlast(url string) blast.Blast {
*concurrency,
*connections,
*numberOfRequests,
getFilePayload(*filePath),
getFilePayload(*payloadFilePath),
url,
*requestsPerSecond,
*connectTimeout,
Expand Down Expand Up @@ -253,10 +253,10 @@ func getFilePayload(filePath string) []byte {
// usageAndExit defines the usage of blast application and exits the application.
func usageAndExit(msg string) {
if msg != "" {
fmt.Fprintf(os.Stderr, msg)
fmt.Fprintf(os.Stderr, "\n\n")
_, _ = fmt.Fprintf(os.Stderr, msg)
_, _ = fmt.Fprintf(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprintf(os.Stderr, "\n")
_, _ = fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
12 changes: 6 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ func TestRunBlastWithEmptyUrl(t *testing.T) {
})
}

func TestRunBlastWithoutFilePath(t *testing.T) {
func TestRunBlastWithoutPayloadFilePath(t *testing.T) {
exitFunction = exitWithPanic
assert.Panics(t, func() {
assertFilePath("")
assertPayloadFilePath("")
})
}

func TestRunBlastWithEmptyFilePath(t *testing.T) {
func TestRunBlastWithEmptyPayloadFilePath(t *testing.T) {
exitFunction = exitWithPanic
assert.Panics(t, func() {
assertFilePath(" ")
assertPayloadFilePath(" ")
})
}

func TestRunBlastWithNonEmptyFilePath(t *testing.T) {
func TestRunBlastWithNonEmptyPayloadFilePath(t *testing.T) {
exitFunction = exitWithPanic
assert.NotPanics(t, func() {
assertFilePath("./filePayload")
assertPayloadFilePath("./filePayload")
})
}

Expand Down

0 comments on commit b4c5814

Please sign in to comment.