From 7cd0d391bd9b1b6d4cfc6a0981b0a98d0924784b Mon Sep 17 00:00:00 2001 From: Evan Burrell Date: Wed, 19 Dec 2018 15:06:44 +0000 Subject: [PATCH] Refactor to be able to unit test --- main.go | 32 +++++++++++++++++++++++++------- main_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 main_test.go diff --git a/main.go b/main.go index 23bfab3..a758db0 100644 --- a/main.go +++ b/main.go @@ -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++ { @@ -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) +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..21a66c2 --- /dev/null +++ b/main_test.go @@ -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") + } + }) +}