From 1af257201968f99c57bd63ad4f5c04d852c434cc Mon Sep 17 00:00:00 2001 From: Alexey Kamenskiy Date: Mon, 15 Nov 2021 00:13:06 +0800 Subject: [PATCH] Use anonymous function instead of interface --- heap.go | 6 +++--- main.go | 6 ++---- main_test.go | 38 +++++++++++++++++++------------------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/heap.go b/heap.go index 9592b00..2e09bea 100644 --- a/heap.go +++ b/heap.go @@ -24,9 +24,9 @@ func (h *processInputHeap) Push(x interface{}) { *h = append(*h, x.(*processInput)) } -func (s processInputHeap) Peek() (*processInput, bool) { - if len(s) > 0 { - return s[0], true +func (h processInputHeap) Peek() (*processInput, bool) { + if len(h) > 0 { + return h[0], true } return nil, false } diff --git a/main.go b/main.go index 6658fa3..6afb4ac 100644 --- a/main.go +++ b/main.go @@ -18,9 +18,7 @@ type OrderedOutput struct { } // WorkFunction interface -type WorkFunction interface { - Run() interface{} -} +type WorkFunction func() interface{} // Process processes work function based on input. // It Accepts an WorkFunction read channel, work function and concurrent go routine pool size. @@ -69,7 +67,7 @@ func Process(inputChan <-chan WorkFunction, options *Options) <-chan OrderedOutp poolWg.Done() }() for input := range processChan { - input.value = input.workFn.Run() + input.value = input.workFn() input.workFn = nil aggregatorChan <- input } diff --git a/main_test.go b/main_test.go index 8023efa..ecc047f 100644 --- a/main_test.go +++ b/main_test.go @@ -8,17 +8,17 @@ import ( "time" ) -type zeroLoadWorker int - -func (w zeroLoadWorker) Run() interface{} { - return w * 2 +func zeroLoadWorker(w int) func() interface{} { + return func() interface{} { + return w * 2 + } } -type loadWorker int - -func (w loadWorker) Run() interface{} { - time.Sleep(time.Millisecond * time.Duration(rand.Intn(100))) - return w * 2 +func loadWorker(w int) func() interface{} { + return func() interface{} { + time.Sleep(time.Millisecond * time.Duration(rand.Intn(100))) + return w * 2 + } } func Test1(t *testing.T) { @@ -31,7 +31,7 @@ func Test1(t *testing.T) { counter := 0 go func(t *testing.T) { for out := range outChan { - if _, ok := out.Value.(loadWorker); !ok { + if _, ok := out.Value.(int); !ok { t.Error("Invalid output") } else { counter++ @@ -64,7 +64,7 @@ func Test2(t *testing.T) { counter := 0 go func(t *testing.T) { for out := range outChan { - if _, ok := out.Value.(loadWorker); !ok { + if _, ok := out.Value.(int); !ok { t.Error("Invalid output") } else { counter++ @@ -97,7 +97,7 @@ func Test3(t *testing.T) { counter := 0 go func(t *testing.T) { for out := range outChan { - if _, ok := out.Value.(zeroLoadWorker); !ok { + if _, ok := out.Value.(int); !ok { t.Error("Invalid output") } else { counter++ @@ -134,7 +134,7 @@ func Test4(t *testing.T) { }() counter := 0 for out := range output { - if _, ok := out.Value.(zeroLoadWorker); !ok { + if _, ok := out.Value.(int); !ok { t.Error("Invalid output") } else { counter++ @@ -158,9 +158,9 @@ func TestSortedData(t *testing.T) { } close(inputChan) }() - var res []loadWorker + var res []int for out := range output { - res = append(res, out.Value.(loadWorker)) + res = append(res, out.Value.(int)) } isSorted := sort.SliceIsSorted(res, func(i, j int) bool { return res[i] < res[j] @@ -184,9 +184,9 @@ func TestSortedDataMultiple(t *testing.T) { } close(inputChan) }() - var res []loadWorker + var res []int for out := range output { - res = append(res, out.Value.(loadWorker)) + res = append(res, out.Value.(int)) } isSorted := sort.SliceIsSorted(res, func(i, j int) bool { return res[i] < res[j] @@ -222,11 +222,11 @@ func TestStreamingInput(t *testing.T) { } }() - var res []zeroLoadWorker + var res []int go func() { for out := range output { - res = append(res, out.Value.(zeroLoadWorker)) + res = append(res, out.Value.(int)) wg.Done() } }()