Skip to content

Commit

Permalink
refactor: use utility functions in tests (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn authored Nov 3, 2023
1 parent e4f03f3 commit 5c835a8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
51 changes: 24 additions & 27 deletions concurrent_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@ import (
"time"

"github.com/reugn/async/internal/assert"
"github.com/reugn/async/internal/util"
)

func TestClear(t *testing.T) {
m := prepareConcurrentMap()
m.Clear()
assert.Equal(t, m.Size(), 0)
m.Put(1, ptr("a"))
m.Put(1, util.Ptr("a"))
assert.Equal(t, m.Size(), 1)
}

func TestComputeIfAbsent(t *testing.T) {
m := prepareConcurrentMap()
assert.Equal(
t,
m.ComputeIfAbsent(4, func(_ int) *string { return ptr("d") }),
ptr("d"),
m.ComputeIfAbsent(4, func(_ int) *string { return util.Ptr("d") }),
util.Ptr("d"),
)
assert.Equal(t, m.Size(), 4)
assert.Equal(
t,
m.ComputeIfAbsent(4, func(_ int) *string { return ptr("e") }),
ptr("d"),
m.ComputeIfAbsent(4, func(_ int) *string { return util.Ptr("e") }),
util.Ptr("d"),
)
assert.Equal(t, m.Size(), 4)
}
Expand All @@ -42,14 +43,14 @@ func TestContainsKey(t *testing.T) {

func TestGet(t *testing.T) {
m := prepareConcurrentMap()
assert.Equal(t, m.Get(1), ptr("a"))
assert.Equal(t, m.Get(1), util.Ptr("a"))
assert.Equal(t, m.Get(4), nil)
}

func TestGetOrDefault(t *testing.T) {
m := prepareConcurrentMap()
assert.Equal(t, m.GetOrDefault(1, ptr("e")), ptr("a"))
assert.Equal(t, m.GetOrDefault(5, ptr("e")), ptr("e"))
assert.Equal(t, m.GetOrDefault(1, util.Ptr("e")), util.Ptr("a"))
assert.Equal(t, m.GetOrDefault(5, util.Ptr("e")), util.Ptr("e"))
}

func TestIsEmpty(t *testing.T) {
Expand All @@ -62,24 +63,24 @@ func TestIsEmpty(t *testing.T) {
func TestKeySet(t *testing.T) {
m := prepareConcurrentMap()
assert.ElementsMatch(t, m.KeySet(), []int{1, 2, 3})
m.Put(4, ptr("d"))
m.Put(4, util.Ptr("d"))
assert.ElementsMatch(t, m.KeySet(), []int{1, 2, 3, 4})
}

func TestPut(t *testing.T) {
m := prepareConcurrentMap()
assert.Equal(t, m.Size(), 3)
m.Put(4, ptr("d"))
m.Put(4, util.Ptr("d"))
assert.Equal(t, m.Size(), 4)
assert.Equal(t, m.Get(4), ptr("d"))
m.Put(4, ptr("e"))
assert.Equal(t, m.Get(4), util.Ptr("d"))
m.Put(4, util.Ptr("e"))
assert.Equal(t, m.Size(), 4)
assert.Equal(t, m.Get(4), ptr("e"))
assert.Equal(t, m.Get(4), util.Ptr("e"))
}

func TestRemove(t *testing.T) {
m := prepareConcurrentMap()
assert.Equal(t, m.Remove(3), ptr("c"))
assert.Equal(t, m.Remove(3), util.Ptr("c"))
assert.Equal(t, m.Size(), 2)
assert.Equal(t, m.Remove(5), nil)
assert.Equal(t, m.Size(), 2)
Expand All @@ -95,13 +96,13 @@ func TestValues(t *testing.T) {
assert.ElementsMatch(
t,
m.Values(),
[]*string{ptr("a"), ptr("b"), ptr("c")},
[]*string{util.Ptr("a"), util.Ptr("b"), util.Ptr("c")},
)
m.Put(4, ptr("d"))
m.Put(4, util.Ptr("d"))
assert.ElementsMatch(
t,
m.Values(),
[]*string{ptr("a"), ptr("b"), ptr("c"), ptr("d")},
[]*string{util.Ptr("a"), util.Ptr("b"), util.Ptr("c"), util.Ptr("d")},
)
}

Expand All @@ -116,7 +117,7 @@ func TestMemoryLeaks(t *testing.T) {
go func() {
defer wg.Done()
for i := 0; i < 1000000; i++ {
m.Put(i, ptr(strconv.Itoa(i)))
m.Put(i, util.Ptr(strconv.Itoa(i)))
time.Sleep(time.Nanosecond)
}
}()
Expand All @@ -131,14 +132,14 @@ func TestMemoryLeaks(t *testing.T) {
defer wg.Done()
for i := 0; i < 100; i++ {
m.KeySet()
time.Sleep(time.Millisecond * 10)
time.Sleep(10 * time.Millisecond)
}
}()
go func() {
defer wg.Done()
for i := 0; i < 80; i++ {
m.Values()
time.Sleep(time.Millisecond * 12)
time.Sleep(12 * time.Millisecond)
}
}()

Expand All @@ -157,12 +158,8 @@ func TestMemoryLeaks(t *testing.T) {

func prepareConcurrentMap() *ConcurrentMap[int, string] {
syncMap := NewConcurrentMap[int, string]()
syncMap.Put(1, ptr("a"))
syncMap.Put(2, ptr("b"))
syncMap.Put(3, ptr("c"))
syncMap.Put(1, util.Ptr("a"))
syncMap.Put(2, util.Ptr("b"))
syncMap.Put(3, util.Ptr("c"))
return syncMap
}

func ptr(s string) *string {
return &s
}
9 changes: 5 additions & 4 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/reugn/async/internal/assert"
"github.com/reugn/async/internal/util"
)

//nolint:funlen
Expand Down Expand Up @@ -47,14 +48,14 @@ func TestValueCompareAndSwap(t *testing.T) {
assert.Equal(t, swapped, true)
assert.Equal(t, value.Load(), "a")

stringPointer := ptr("b")
stringPointer := util.Ptr("b")
swapped = value.CompareAndSwap("a", stringPointer)
assert.Equal(t, swapped, true)
if value.Load() != stringPointer {
t.Fail()
}

swapped = value.CompareAndSwap(ptr("b"), "c")
swapped = value.CompareAndSwap(util.Ptr("b"), "c")
assert.Equal(t, swapped, false)
if value.Load() != stringPointer {
t.Fail()
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestValueStore(t *testing.T) {
value.Store("a")
assert.Equal(t, value.Load(), "a")

stringPointer := ptr("b")
stringPointer := util.Ptr("b")
value.Store(stringPointer)
if value.Load() != stringPointer {
t.Fail()
Expand All @@ -100,7 +101,7 @@ func TestValueSwap(t *testing.T) {
old = value.Swap("a")
assert.Equal(t, old, 1)

stringPointer := ptr("b")
stringPointer := util.Ptr("b")
old = value.Swap(stringPointer)
assert.Equal(t, old, "a")
if value.Load() != stringPointer {
Expand Down
14 changes: 7 additions & 7 deletions wait_group_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func TestWaitGroupContext(t *testing.T) {

go func() {
defer wgc.Done()
time.Sleep(time.Millisecond * 10)
time.Sleep(10 * time.Millisecond)
result.Add(1)
}()
go func() {
defer wgc.Done()
time.Sleep(time.Millisecond * 20)
time.Sleep(20 * time.Millisecond)
result.Add(2)
}()
go func() {
Expand All @@ -30,7 +30,7 @@ func TestWaitGroupContext(t *testing.T) {
}()

wgc.Wait()
time.Sleep(time.Millisecond * 10)
time.Sleep(10 * time.Millisecond)

assert.Equal(t, int(result.Load()), 6)
}
Expand All @@ -39,7 +39,7 @@ func TestWaitGroupContextCanceled(t *testing.T) {
var result atomic.Int32
ctx, cancelFunc := context.WithCancel(context.Background())
go func() {
time.Sleep(time.Millisecond * 100)
time.Sleep(100 * time.Millisecond)
result.Add(10)
cancelFunc()
}()
Expand All @@ -48,12 +48,12 @@ func TestWaitGroupContextCanceled(t *testing.T) {

go func() {
defer wgc.Done()
time.Sleep(time.Millisecond * 10)
time.Sleep(10 * time.Millisecond)
result.Add(1)
}()
go func() {
defer wgc.Done()
time.Sleep(time.Millisecond * 300)
time.Sleep(300 * time.Millisecond)
result.Add(2)
}()
go func() {
Expand All @@ -62,7 +62,7 @@ func TestWaitGroupContextCanceled(t *testing.T) {
}()

wgc.Wait()
time.Sleep(time.Millisecond * 10)
time.Sleep(10 * time.Millisecond)

assert.Equal(t, int(result.Load()), 111)
}
Expand Down

0 comments on commit 5c835a8

Please sign in to comment.