Skip to content

Commit 70a1fd2

Browse files
authored
refactor: internal package structure (#13)
1 parent 443d886 commit 70a1fd2

File tree

11 files changed

+72
-72
lines changed

11 files changed

+72
-72
lines changed

concurrent_map_test.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,98 +7,98 @@ import (
77
"testing"
88
"time"
99

10-
"github.com/reugn/async/internal"
10+
"github.com/reugn/async/internal/assert"
1111
)
1212

1313
func TestClear(t *testing.T) {
1414
m := prepareConcurrentMap()
1515
m.Clear()
16-
internal.AssertEqual(t, m.Size(), 0)
16+
assert.Equal(t, m.Size(), 0)
1717
m.Put(1, ptr("a"))
18-
internal.AssertEqual(t, m.Size(), 1)
18+
assert.Equal(t, m.Size(), 1)
1919
}
2020

2121
func TestComputeIfAbsent(t *testing.T) {
2222
m := prepareConcurrentMap()
23-
internal.AssertEqual(
23+
assert.Equal(
2424
t,
2525
m.ComputeIfAbsent(4, func(_ int) *string { return ptr("d") }),
2626
ptr("d"),
2727
)
28-
internal.AssertEqual(t, m.Size(), 4)
29-
internal.AssertEqual(
28+
assert.Equal(t, m.Size(), 4)
29+
assert.Equal(
3030
t,
3131
m.ComputeIfAbsent(4, func(_ int) *string { return ptr("e") }),
3232
ptr("d"),
3333
)
34-
internal.AssertEqual(t, m.Size(), 4)
34+
assert.Equal(t, m.Size(), 4)
3535
}
3636

3737
func TestContainsKey(t *testing.T) {
3838
m := prepareConcurrentMap()
39-
internal.AssertEqual(t, m.ContainsKey(3), true)
40-
internal.AssertEqual(t, m.ContainsKey(4), false)
39+
assert.Equal(t, m.ContainsKey(3), true)
40+
assert.Equal(t, m.ContainsKey(4), false)
4141
}
4242

4343
func TestGet(t *testing.T) {
4444
m := prepareConcurrentMap()
45-
internal.AssertEqual(t, m.Get(1), ptr("a"))
46-
internal.AssertEqual(t, m.Get(4), nil)
45+
assert.Equal(t, m.Get(1), ptr("a"))
46+
assert.Equal(t, m.Get(4), nil)
4747
}
4848

4949
func TestGetOrDefault(t *testing.T) {
5050
m := prepareConcurrentMap()
51-
internal.AssertEqual(t, m.GetOrDefault(1, ptr("e")), ptr("a"))
52-
internal.AssertEqual(t, m.GetOrDefault(5, ptr("e")), ptr("e"))
51+
assert.Equal(t, m.GetOrDefault(1, ptr("e")), ptr("a"))
52+
assert.Equal(t, m.GetOrDefault(5, ptr("e")), ptr("e"))
5353
}
5454

5555
func TestIsEmpty(t *testing.T) {
5656
m := prepareConcurrentMap()
57-
internal.AssertEqual(t, m.IsEmpty(), false)
57+
assert.Equal(t, m.IsEmpty(), false)
5858
m.Clear()
59-
internal.AssertEqual(t, m.IsEmpty(), true)
59+
assert.Equal(t, m.IsEmpty(), true)
6060
}
6161

6262
func TestKeySet(t *testing.T) {
6363
m := prepareConcurrentMap()
64-
internal.AssertElementsMatch(t, m.KeySet(), []int{1, 2, 3})
64+
assert.ElementsMatch(t, m.KeySet(), []int{1, 2, 3})
6565
m.Put(4, ptr("d"))
66-
internal.AssertElementsMatch(t, m.KeySet(), []int{1, 2, 3, 4})
66+
assert.ElementsMatch(t, m.KeySet(), []int{1, 2, 3, 4})
6767
}
6868

6969
func TestPut(t *testing.T) {
7070
m := prepareConcurrentMap()
71-
internal.AssertEqual(t, m.Size(), 3)
71+
assert.Equal(t, m.Size(), 3)
7272
m.Put(4, ptr("d"))
73-
internal.AssertEqual(t, m.Size(), 4)
74-
internal.AssertEqual(t, m.Get(4), ptr("d"))
73+
assert.Equal(t, m.Size(), 4)
74+
assert.Equal(t, m.Get(4), ptr("d"))
7575
m.Put(4, ptr("e"))
76-
internal.AssertEqual(t, m.Size(), 4)
77-
internal.AssertEqual(t, m.Get(4), ptr("e"))
76+
assert.Equal(t, m.Size(), 4)
77+
assert.Equal(t, m.Get(4), ptr("e"))
7878
}
7979

8080
func TestRemove(t *testing.T) {
8181
m := prepareConcurrentMap()
82-
internal.AssertEqual(t, m.Remove(3), ptr("c"))
83-
internal.AssertEqual(t, m.Size(), 2)
84-
internal.AssertEqual(t, m.Remove(5), nil)
85-
internal.AssertEqual(t, m.Size(), 2)
82+
assert.Equal(t, m.Remove(3), ptr("c"))
83+
assert.Equal(t, m.Size(), 2)
84+
assert.Equal(t, m.Remove(5), nil)
85+
assert.Equal(t, m.Size(), 2)
8686
}
8787

8888
func TestSize(t *testing.T) {
8989
m := prepareConcurrentMap()
90-
internal.AssertEqual(t, m.Size(), 3)
90+
assert.Equal(t, m.Size(), 3)
9191
}
9292

9393
func TestValues(t *testing.T) {
9494
m := prepareConcurrentMap()
95-
internal.AssertElementsMatch(
95+
assert.ElementsMatch(
9696
t,
9797
m.Values(),
9898
[]*string{ptr("a"), ptr("b"), ptr("c")},
9999
)
100100
m.Put(4, ptr("d"))
101-
internal.AssertElementsMatch(
101+
assert.ElementsMatch(
102102
t,
103103
m.Values(),
104104
[]*string{ptr("a"), ptr("b"), ptr("c"), ptr("d")},
@@ -149,7 +149,7 @@ func TestMemoryLeaks(t *testing.T) {
149149
var statsAfter runtime.MemStats
150150
runtime.ReadMemStats(&statsAfter)
151151

152-
internal.AssertEqual(t, m.IsEmpty(), true)
152+
assert.Equal(t, m.IsEmpty(), true)
153153
if statsAfter.HeapObjects > statsBefore.HeapObjects+50 {
154154
t.Error("HeapObjects leak")
155155
}

future_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/reugn/async/internal"
11+
"github.com/reugn/async/internal/assert"
1212
)
1313

1414
func TestFuture(t *testing.T) {
@@ -19,8 +19,8 @@ func TestFuture(t *testing.T) {
1919
}()
2020
res, err := p.Future().Join()
2121

22-
internal.AssertEqual(t, res, true)
23-
internal.AssertEqual(t, err, nil)
22+
assert.Equal(t, res, true)
23+
assert.Equal(t, err, nil)
2424
}
2525

2626
func TestFutureUtils(t *testing.T) {
@@ -39,7 +39,7 @@ func TestFutureUtils(t *testing.T) {
3939
res := []interface{}{1, 2, 3}
4040
futRes, _ := FutureSeq(arr).Join()
4141

42-
internal.AssertEqual(t, res, futRes)
42+
assert.Equal(t, res, futRes)
4343
}
4444

4545
func TestFutureFirstCompleted(t *testing.T) {
@@ -51,7 +51,7 @@ func TestFutureFirstCompleted(t *testing.T) {
5151
timeout := FutureTimer[bool](time.Millisecond * 100)
5252
futRes, futErr := FutureFirstCompletedOf(p.Future(), timeout).Join()
5353

54-
internal.AssertEqual(t, false, futRes)
54+
assert.Equal(t, false, futRes)
5555
if futErr == nil {
5656
t.Fatalf("futErr is nil")
5757
}
@@ -75,10 +75,10 @@ func TestFutureTransform(t *testing.T) {
7575
})
7676

7777
res, _ := future.Get(time.Second * 5)
78-
internal.AssertEqual(t, 3, res)
78+
assert.Equal(t, 3, res)
7979

8080
res, _ = future.Join()
81-
internal.AssertEqual(t, 3, res)
81+
assert.Equal(t, 3, res)
8282
}
8383

8484
func TestFutureFailure(t *testing.T) {
@@ -92,7 +92,7 @@ func TestFutureFailure(t *testing.T) {
9292
}()
9393
res, _ := p1.Future().RecoverWith(p2.Future()).Join()
9494

95-
internal.AssertEqual(t, 2, res)
95+
assert.Equal(t, 2, res)
9696
}
9797

9898
func TestFutureTimeout(t *testing.T) {
@@ -104,10 +104,10 @@ func TestFutureTimeout(t *testing.T) {
104104
future := p.Future()
105105

106106
_, err := future.Get(time.Millisecond * 50)
107-
internal.AssertErrorContains(t, err, "timeout")
107+
assert.ErrorContains(t, err, "timeout")
108108

109109
_, err = future.Join()
110-
internal.AssertErrorContains(t, err, "timeout")
110+
assert.ErrorContains(t, err, "timeout")
111111
}
112112

113113
func TestFutureGoroutineLeak(t *testing.T) {

goroutine_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package async
33
import (
44
"testing"
55

6-
"github.com/reugn/async/internal"
6+
"github.com/reugn/async/internal/assert"
77
)
88

99
func TestGoroutineID(t *testing.T) {
1010
gid, err := GoroutineID()
1111

12-
internal.AssertEqual(t, nil, err)
12+
assert.Equal(t, nil, err)
1313
t.Log(gid)
1414
}
1515

internal/test_utils.go renamed to internal/assert/assertions.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
package internal
1+
package assert
22

33
import (
44
"reflect"
55
"strings"
66
"testing"
77
)
88

9-
// AssertEqual verifies equality of two objects.
10-
func AssertEqual[T any](t *testing.T, a, b T) {
9+
// Equal verifies equality of two objects.
10+
func Equal[T any](t *testing.T, a, b T) {
1111
if !reflect.DeepEqual(a, b) {
1212
t.Fatalf("%v != %v", a, b)
1313
}
1414
}
1515

16-
// AssertElementsMatch checks whether the given slices contain the same elements.
17-
func AssertElementsMatch[T any](t *testing.T, a, b []T) {
16+
// ElementsMatch checks whether the given slices contain the same elements.
17+
func ElementsMatch[T any](t *testing.T, a, b []T) {
1818
if len(a) == len(b) {
1919
count := 0
2020
for _, va := range a {
@@ -32,17 +32,17 @@ func AssertElementsMatch[T any](t *testing.T, a, b []T) {
3232
t.Fatalf("Slice elements are not equal: %v != %v", a, b)
3333
}
3434

35-
// AssertErrorContains checks whether the given error contains the specified string.
36-
func AssertErrorContains(t *testing.T, err error, str string) {
35+
// ErrorContains checks whether the given error contains the specified string.
36+
func ErrorContains(t *testing.T, err error, str string) {
3737
if err == nil {
3838
t.Fatalf("Error is nil")
3939
} else if !strings.Contains(err.Error(), str) {
4040
t.Fatalf("Error doen't contain string: %s", str)
4141
}
4242
}
4343

44-
// AssertPanic checks whether the given function panics.
45-
func AssertPanic(t *testing.T, f func()) {
44+
// Panic checks whether the given function panics.
45+
func Panic(t *testing.T, f func()) {
4646
defer func() {
4747
if r := recover(); r == nil {
4848
t.Errorf("The function did not panic")
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package internal
1+
package util
22

33
import "math/rand"
44

5-
// Cas returns compare-and-set stamp value
5+
// Cas returns compare-and-set stamp value.
66
func Cas() int64 {
77
return rand.Int63()
88
}

once_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"sync/atomic"
66
"testing"
77

8-
"github.com/reugn/async/internal"
8+
"github.com/reugn/async/internal/assert"
99
)
1010

1111
func TestOnce(t *testing.T) {
@@ -18,7 +18,7 @@ func TestOnce(t *testing.T) {
1818
return count, nil
1919
})
2020
}
21-
internal.AssertEqual(t, count, 1)
21+
assert.Equal(t, count, 1)
2222
}
2323

2424
func TestOnceConcurrent(t *testing.T) {
@@ -38,7 +38,7 @@ func TestOnceConcurrent(t *testing.T) {
3838
}()
3939
}
4040
wg.Wait()
41-
internal.AssertEqual(t, count, 1)
41+
assert.Equal(t, count, 1)
4242
}
4343

4444
func TestOncePanic(t *testing.T) {
@@ -52,6 +52,6 @@ func TestOncePanic(t *testing.T) {
5252
return count, nil
5353
})
5454
}
55-
internal.AssertEqual(t, err.Error(), "recovered runtime error: integer divide by zero")
56-
internal.AssertEqual(t, count, 0)
55+
assert.Equal(t, err.Error(), "recovered runtime error: integer divide by zero")
56+
assert.Equal(t, count, 0)
5757
}

optimistic_lock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"sync/atomic"
66
"time"
77

8-
"github.com/reugn/async/internal"
8+
"github.com/reugn/async/internal/util"
99
)
1010

1111
const (
@@ -38,7 +38,7 @@ func (o *OptimisticLock) Lock() {
3838

3939
// Unlock unlocks the resource after write.
4040
func (o *OptimisticLock) Unlock() {
41-
atomic.StoreInt64(&o.stamp, internal.Cas())
41+
atomic.StoreInt64(&o.stamp, util.Cas())
4242
atomic.StoreInt32(&o.status, lockStatusSteady)
4343
o.rw.Unlock()
4444
}

optimistic_lock_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77
"time"
88

9-
"github.com/reugn/async/internal"
9+
"github.com/reugn/async/internal/assert"
1010
)
1111

1212
type syncList struct {
@@ -59,5 +59,5 @@ func TestList1(t *testing.T) {
5959
wg.Done()
6060
}()
6161
wg.Wait()
62-
internal.AssertEqual(t, 50, list.size())
62+
assert.Equal(t, 50, list.size())
6363
}

reentrant_lock_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"sync"
55
"testing"
66

7-
"github.com/reugn/async/internal"
7+
"github.com/reugn/async/internal/assert"
88
)
99

1010
type synchronizedAdder struct {
@@ -67,7 +67,7 @@ func TestPacker1(t *testing.T) {
6767
}()
6868
}
6969
wg.Wait()
70-
internal.AssertEqual(t, 30, adder.Value())
70+
assert.Equal(t, 30, adder.Value())
7171
}
7272

7373
func TestPacker2(t *testing.T) {
@@ -81,5 +81,5 @@ func TestPacker2(t *testing.T) {
8181
}()
8282
}
8383
wg.Wait()
84-
internal.AssertEqual(t, 60, adder.Value())
84+
assert.Equal(t, 60, adder.Value())
8585
}

task_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55
"time"
66

7-
"github.com/reugn/async/internal"
7+
"github.com/reugn/async/internal/assert"
88
)
99

1010
func TestTask(t *testing.T) {
@@ -14,6 +14,6 @@ func TestTask(t *testing.T) {
1414
})
1515
res, err := task.Call().Join()
1616

17-
internal.AssertEqual(t, "ok", res)
18-
internal.AssertEqual(t, err, nil)
17+
assert.Equal(t, "ok", res)
18+
assert.Equal(t, err, nil)
1919
}

0 commit comments

Comments
 (0)