Skip to content

Commit

Permalink
Merge pull request #3 from burrelle/test/refactor
Browse files Browse the repository at this point in the history
Refactor to be able to unit test
  • Loading branch information
AgentBurgundy authored Dec 20, 2018
2 parents e879bb6 + 7cd0d39 commit f8f9a3c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
32 changes: 25 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,34 @@ import (
"time"
)

func main() {

args := os.Args[1:]

func readArguments(args []string) (int64, int64) {
percentageLoad, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
fmt.Println("Invalid percentage load")
percentageLoad = 0
}

secondsToRun, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
fmt.Println("Invalid seconds to run")
secondsToRun = 0
}
return percentageLoad, secondsToRun
}

func calcuatePercentage(percentageLoad int64) float64 {
return float64((float64(percentageLoad) / 100.0))
}

var percentage float64 = float64((float64(percentageLoad) / 100.0))
var cpuCount float64 = float64(runtime.NumCPU()) * percentage
func calcuateCPUCount(percentage float64, CPU int) float64 {
return float64(CPU) * percentage
}

func outputLoadAndCount(cpuCount, percentage float64, percentageLoad int64) {
fmt.Println(fmt.Sprintf("%v * ( %v / %v ) = %v", runtime.NumCPU(), percentageLoad, 100, cpuCount))
fmt.Println(fmt.Sprintf("%v * %v = %v", runtime.NumCPU(), percentage, cpuCount))
}

func troll(cpuCount float64, secondsToRun int64) {
done := make(chan int)

for i := 0; i < int(cpuCount); i++ {
Expand All @@ -44,3 +53,12 @@ func main() {
time.Sleep(time.Second * time.Duration(secondsToRun))
close(done)
}

func main() {
args := os.Args[1:]
var percentageLoad, secondsToRun = readArguments(args)
var percentage = calcuatePercentage(percentageLoad)
var cpuCount = calcuateCPUCount(percentage, runtime.NumCPU())
outputLoadAndCount(cpuCount, percentage, percentageLoad)
troll(cpuCount, secondsToRun)
}
46 changes: 46 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import "testing"

func TestReadArguments(t *testing.T) {
t.Run("verify with correct parameters", func(t *testing.T) {
want := int64(12)
args := []string{"12", "12"}
var load, percentage = readArguments(args)
if load != want || percentage != want {
t.Errorf("should have recieved a value back")
}
})

t.Run("verify with incorrect parameters", func(t *testing.T) {
args := []string{"not-a-number", "not-a-number"}
var load, percentage = readArguments(args)
if load != 0 && percentage != 0 {
t.Errorf("should have recieved zero")
}
})
}

func TestCaluatePercentage(t *testing.T) {
t.Run("calculates the percentage based on input", func(t *testing.T) {
percentageLoad := int64(78)
want := float64(0.78)
var percentage = calcuatePercentage(percentageLoad)
if want != percentage {
t.Errorf("should have recieved a value back")
}
})
}

func TestCalculateCPUCount(t *testing.T) {
t.Run("calculates the percentage based on input", func(t *testing.T) {
percentageLoad := float64(0.78)
CPU := 4
want := float64(3.12)
var percentage = calcuateCPUCount(percentageLoad, CPU)
print(percentage)
if want != percentage {
t.Errorf("should have recieved a value back")
}
})
}

0 comments on commit f8f9a3c

Please sign in to comment.