diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1897a2a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module tokopedia/debuggingworkshop + +go 1.17 diff --git a/main.go b/main.go index b37c814..a0ea7bf 100644 --- a/main.go +++ b/main.go @@ -37,12 +37,15 @@ func sender(c chan signalData) { } func receiver(c chan signalData) { + // go tool pprof -alloc_space http://localhost:6060/debug/pprof/heap + // memory leak caused by repeated NewTimer instantiation. instantiate only once. + timeout := time.NewTimer(time.Second) for { select { case signalData := <-c: signalData.status = validateSignal(signalData.signal) fmt.Println("Receiving Signal ", signalData.signal, " with status ", signalData.status) - case <-time.After(time.Second * 1): + case <-timeout.C: fmt.Println("Got timeout while receiving the signal") return } @@ -100,7 +103,7 @@ func evaluateNode(node1, node2 int) int { case node2 < node1: return 0 default: - return 0 + return 1 } } diff --git a/main_test.go b/main_test.go index 95db6fe..a784173 100644 --- a/main_test.go +++ b/main_test.go @@ -28,6 +28,20 @@ func Test_validateSignal(t *testing.T) { }, want: "bad", }, + { + name: "bad signal 2", + args: args{ + signal: []int{1, 1, 0, 0, 1, 0}, + }, + want: "bad", + }, + { + name: "good signal 2", + args: args{ + signal: []int{0, 1, 0, 0, 1, 1}, + }, + want: "good", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {