Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Move all test methods to lo_test #331

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test: Move all test methods to lo_test
  • Loading branch information
miaoyin committed Mar 28, 2023
commit d64c0ae5efce935464bb41a380642d6ba61ec88e
5 changes: 3 additions & 2 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package lo
package lo_test

import (
"math/rand"
"strconv"
"testing"
"time"

"github.com/samber/lo"
lop "github.com/samber/lo/parallel"
"github.com/thoas/go-funk"
)
Expand All @@ -27,7 +28,7 @@ func BenchmarkMap(b *testing.B) {

b.Run("lo.Map", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = Map(arr, func(x int64, i int) string {
_ = lo.Map(arr, func(x int64, i int) string {
return strconv.FormatInt(x, 10)
})
}
Expand Down
16 changes: 8 additions & 8 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type DispatchingStrategy[T any] func(msg T, index uint64, channels []<-chan T) i
// Close events are propagated to children.
// Underlying channels can have a fixed buffer capacity or be unbuffered when cap is 0.
func ChannelDispatcher[T any](stream <-chan T, count int, channelBufferCap int, strategy DispatchingStrategy[T]) []<-chan T {
children := createChannels[T](count, channelBufferCap)
children := CreateChannels[T](count, channelBufferCap)

roChildren := channelsToReadOnly(children)
roChildren := ChannelsToReadOnly(children)

go func() {
// propagate channel closing to children
defer closeChannels(children)
defer CloseChannels(children)

var i uint64 = 0

Expand All @@ -38,7 +38,7 @@ func ChannelDispatcher[T any](stream <-chan T, count int, channelBufferCap int,
return roChildren
}

func createChannels[T any](count int, channelBufferCap int) []chan T {
func CreateChannels[T any](count int, channelBufferCap int) []chan T {
children := make([]chan T, 0, count)

for i := 0; i < count; i++ {
Expand All @@ -48,7 +48,7 @@ func createChannels[T any](count int, channelBufferCap int) []chan T {
return children
}

func channelsToReadOnly[T any](children []chan T) []<-chan T {
func ChannelsToReadOnly[T any](children []chan T) []<-chan T {
roChildren := make([]<-chan T, 0, len(children))

for i := range children {
Expand All @@ -58,7 +58,7 @@ func channelsToReadOnly[T any](children []chan T) []<-chan T {
return roChildren
}

func closeChannels[T any](children []chan T) {
func CloseChannels[T any](children []chan T) {
for i := 0; i < len(children); i++ {
close(children[i])
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func ChannelMerge[T any](channelBufferCap int, upstreams ...<-chan T) <-chan T {
// When upstream channel reach EOF, downstream channels close. If any downstream
// channels is full, broadcasting is paused.
func FanOut[T any](count int, channelsBufferCap int, upstream <-chan T) []<-chan T {
downstreams := createChannels[T](count, channelsBufferCap)
downstreams := CreateChannels[T](count, channelsBufferCap)

go func() {
for msg := range upstream {
Expand All @@ -305,5 +305,5 @@ func FanOut[T any](count int, channelsBufferCap int, upstream <-chan T) []<-chan
}
}()

return channelsToReadOnly(downstreams)
return ChannelsToReadOnly(downstreams)
}
121 changes: 61 additions & 60 deletions channel_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package lo
package lo_test

import (
"math/rand"
"testing"
"time"

"github.com/samber/lo"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,7 +23,7 @@ func TestChannelDispatcher(t *testing.T) {

is.Equal(4, len(ch))

children := ChannelDispatcher(ch, 5, 10, DispatchingStrategyRoundRobin[int])
children := lo.ChannelDispatcher(ch, 5, 10, lo.DispatchingStrategyRoundRobin[int])
time.Sleep(10 * time.Millisecond)

// check channels allocation
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestChannelDispatcher(t *testing.T) {
is.Equal(msg4, 0)

// unbuffered channels
children = ChannelDispatcher(ch, 5, 0, DispatchingStrategyRoundRobin[int])
children = lo.ChannelDispatcher(ch, 5, 0, lo.DispatchingStrategyRoundRobin[int])
is.Equal(0, cap(children[0]))
}

Expand All @@ -101,14 +102,14 @@ func TestDispatchingStrategyRoundRobin(t *testing.T) {
testWithTimeout(t, 10*time.Millisecond)
is := assert.New(t)

children := createChannels[int](3, 2)
rochildren := channelsToReadOnly(children)
defer closeChannels(children)
children := lo.CreateChannels[int](3, 2)
rochildren := lo.ChannelsToReadOnly(children)
defer lo.CloseChannels(children)

is.Equal(0, DispatchingStrategyRoundRobin(42, 0, rochildren))
is.Equal(1, DispatchingStrategyRoundRobin(42, 1, rochildren))
is.Equal(2, DispatchingStrategyRoundRobin(42, 2, rochildren))
is.Equal(0, DispatchingStrategyRoundRobin(42, 3, rochildren))
is.Equal(0, lo.DispatchingStrategyRoundRobin(42, 0, rochildren))
is.Equal(1, lo.DispatchingStrategyRoundRobin(42, 1, rochildren))
is.Equal(2, lo.DispatchingStrategyRoundRobin(42, 2, rochildren))
is.Equal(0, lo.DispatchingStrategyRoundRobin(42, 3, rochildren))
}

func TestDispatchingStrategyRandom(t *testing.T) {
Expand All @@ -118,27 +119,27 @@ func TestDispatchingStrategyRandom(t *testing.T) {
// with this seed, the order of random channels are: 1 - 0
rand.Seed(14)

children := createChannels[int](2, 2)
rochildren := channelsToReadOnly(children)
defer closeChannels(children)
children := lo.CreateChannels[int](2, 2)
rochildren := lo.ChannelsToReadOnly(children)
defer lo.CloseChannels(children)

for i := 0; i < 2; i++ {
children[1] <- i
}

is.Equal(0, DispatchingStrategyRandom(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyRandom(42, 0, rochildren))
}

func TestDispatchingStrategyWeightedRandom(t *testing.T) {
t.Parallel()
testWithTimeout(t, 10*time.Millisecond)
is := assert.New(t)

children := createChannels[int](2, 2)
rochildren := channelsToReadOnly(children)
defer closeChannels(children)
children := lo.CreateChannels[int](2, 2)
rochildren := lo.ChannelsToReadOnly(children)
defer lo.CloseChannels(children)

dispatcher := DispatchingStrategyWeightedRandom[int]([]int{0, 42})
dispatcher := lo.DispatchingStrategyWeightedRandom[int]([]int{0, 42})

is.Equal(1, dispatcher(42, 0, rochildren))
children[0] <- 0
Expand All @@ -152,63 +153,63 @@ func TestDispatchingStrategyFirst(t *testing.T) {
testWithTimeout(t, 10*time.Millisecond)
is := assert.New(t)

children := createChannels[int](2, 2)
rochildren := channelsToReadOnly(children)
defer closeChannels(children)
children := lo.CreateChannels[int](2, 2)
rochildren := lo.ChannelsToReadOnly(children)
defer lo.CloseChannels(children)

is.Equal(0, DispatchingStrategyFirst(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyFirst(42, 0, rochildren))
children[0] <- 0
is.Equal(0, DispatchingStrategyFirst(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyFirst(42, 0, rochildren))
children[0] <- 1
is.Equal(1, DispatchingStrategyFirst(42, 0, rochildren))
is.Equal(1, lo.DispatchingStrategyFirst(42, 0, rochildren))
}

func TestDispatchingStrategyLeast(t *testing.T) {
t.Parallel()
testWithTimeout(t, 10*time.Millisecond)
is := assert.New(t)

children := createChannels[int](2, 2)
rochildren := channelsToReadOnly(children)
defer closeChannels(children)
children := lo.CreateChannels[int](2, 2)
rochildren := lo.ChannelsToReadOnly(children)
defer lo.CloseChannels(children)

is.Equal(0, DispatchingStrategyLeast(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyLeast(42, 0, rochildren))
children[0] <- 0
is.Equal(1, DispatchingStrategyLeast(42, 0, rochildren))
is.Equal(1, lo.DispatchingStrategyLeast(42, 0, rochildren))
children[1] <- 0
is.Equal(0, DispatchingStrategyLeast(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyLeast(42, 0, rochildren))
children[0] <- 1
is.Equal(1, DispatchingStrategyLeast(42, 0, rochildren))
is.Equal(1, lo.DispatchingStrategyLeast(42, 0, rochildren))
children[1] <- 1
is.Equal(0, DispatchingStrategyLeast(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyLeast(42, 0, rochildren))
}

func TestDispatchingStrategyMost(t *testing.T) {
t.Parallel()
testWithTimeout(t, 10*time.Millisecond)
is := assert.New(t)

children := createChannels[int](2, 2)
rochildren := channelsToReadOnly(children)
defer closeChannels(children)
children := lo.CreateChannels[int](2, 2)
rochildren := lo.ChannelsToReadOnly(children)
defer lo.CloseChannels(children)

is.Equal(0, DispatchingStrategyMost(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyMost(42, 0, rochildren))
children[0] <- 0
is.Equal(0, DispatchingStrategyMost(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyMost(42, 0, rochildren))
children[1] <- 0
is.Equal(0, DispatchingStrategyMost(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyMost(42, 0, rochildren))
children[0] <- 1
is.Equal(0, DispatchingStrategyMost(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyMost(42, 0, rochildren))
children[1] <- 1
is.Equal(0, DispatchingStrategyMost(42, 0, rochildren))
is.Equal(0, lo.DispatchingStrategyMost(42, 0, rochildren))
}

func TestSliceToChannel(t *testing.T) {
t.Parallel()
testWithTimeout(t, 10*time.Millisecond)
is := assert.New(t)

ch := SliceToChannel(2, []int{1, 2, 3})
ch := lo.SliceToChannel(2, []int{1, 2, 3})

r1, ok1 := <-ch
r2, ok2 := <-ch
Expand All @@ -229,8 +230,8 @@ func TestChannelToSlice(t *testing.T) {
testWithTimeout(t, 10*time.Millisecond)
is := assert.New(t)

ch := SliceToChannel(2, []int{1, 2, 3})
items := ChannelToSlice(ch)
ch := lo.SliceToChannel(2, []int{1, 2, 3})
items := lo.ChannelToSlice(ch)

is.Equal([]int{1, 2, 3}, items)
}
Expand All @@ -249,7 +250,7 @@ func TestGenerate(t *testing.T) {

i := 0

for v := range Generator(2, generator) {
for v := range lo.Generator(2, generator) {
is.Equal(i, v)
i++
}
Expand All @@ -262,11 +263,11 @@ func TestBuffer(t *testing.T) {
testWithTimeout(t, 10*time.Millisecond)
is := assert.New(t)

ch := SliceToChannel(2, []int{1, 2, 3})
ch := lo.SliceToChannel(2, []int{1, 2, 3})

items1, length1, _, ok1 := Buffer(ch, 2)
items2, length2, _, ok2 := Buffer(ch, 2)
items3, length3, _, ok3 := Buffer(ch, 2)
items1, length1, _, ok1 := lo.Buffer(ch, 2)
items2, length2, _, ok2 := lo.Buffer(ch, 2)
items3, length3, _, ok3 := lo.Buffer(ch, 2)

is.Equal([]int{1, 2}, items1)
is.Equal(2, length1)
Expand All @@ -290,29 +291,29 @@ func TestBufferWithTimeout(t *testing.T) {
time.Sleep(10 * time.Millisecond)
}
}
ch := Generator(0, generator)
ch := lo.Generator(0, generator)

items1, length1, _, ok1 := BufferWithTimeout(ch, 20, 15*time.Millisecond)
items1, length1, _, ok1 := lo.BufferWithTimeout(ch, 20, 15*time.Millisecond)
is.Equal([]int{0, 1}, items1)
is.Equal(2, length1)
is.True(ok1)

items2, length2, _, ok2 := BufferWithTimeout(ch, 20, 2*time.Millisecond)
items2, length2, _, ok2 := lo.BufferWithTimeout(ch, 20, 2*time.Millisecond)
is.Equal([]int{}, items2)
is.Equal(0, length2)
is.True(ok2)

items3, length3, _, ok3 := BufferWithTimeout(ch, 1, 30*time.Millisecond)
items3, length3, _, ok3 := lo.BufferWithTimeout(ch, 1, 30*time.Millisecond)
is.Equal([]int{2}, items3)
is.Equal(1, length3)
is.True(ok3)

items4, length4, _, ok4 := BufferWithTimeout(ch, 2, 25*time.Millisecond)
items4, length4, _, ok4 := lo.BufferWithTimeout(ch, 2, 25*time.Millisecond)
is.Equal([]int{3, 4}, items4)
is.Equal(2, length4)
is.True(ok4)

items5, length5, _, ok5 := BufferWithTimeout(ch, 3, 25*time.Millisecond)
items5, length5, _, ok5 := lo.BufferWithTimeout(ch, 3, 25*time.Millisecond)
is.Equal([]int{}, items5)
is.Equal(0, length5)
is.False(ok5)
Expand All @@ -323,16 +324,16 @@ func TestFanIn(t *testing.T) {
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)

upstreams := createChannels[int](3, 10)
roupstreams := channelsToReadOnly(upstreams)
upstreams := lo.CreateChannels[int](3, 10)
roupstreams := lo.ChannelsToReadOnly(upstreams)
for i := range roupstreams {
go func(i int) {
upstreams[i] <- 1
upstreams[i] <- 1
close(upstreams[i])
}(i)
}
out := FanIn(10, roupstreams...)
out := lo.FanIn(10, roupstreams...)
time.Sleep(10 * time.Millisecond)

// check input channels
Expand Down Expand Up @@ -363,8 +364,8 @@ func TestFanOut(t *testing.T) {
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)

upstream := SliceToChannel(10, []int{0, 1, 2, 3, 4, 5})
rodownstreams := FanOut(3, 10, upstream)
upstream := lo.SliceToChannel(10, []int{0, 1, 2, 3, 4, 5})
rodownstreams := lo.FanOut(3, 10, upstream)

time.Sleep(10 * time.Millisecond)

Expand All @@ -375,7 +376,7 @@ func TestFanOut(t *testing.T) {
for i := range rodownstreams {
is.Equal(6, len(rodownstreams[i]))
is.Equal(10, cap(rodownstreams[i]))
is.Equal([]int{0, 1, 2, 3, 4, 5}, ChannelToSlice(rodownstreams[i]))
is.Equal([]int{0, 1, 2, 3, 4, 5}, lo.ChannelToSlice(rodownstreams[i]))
}

// check it is closed
Expand Down
Loading