-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
161fbfe
commit c439ef6
Showing
3 changed files
with
118 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package hub_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/kercylan98/minotaur/utils/collection" | ||
"github.com/kercylan98/minotaur/utils/hub" | ||
) | ||
|
||
func ExampleNewObjectPool() { | ||
var p = hub.NewObjectPool[map[int]int](func() *map[int]int { | ||
return &map[int]int{} | ||
}, func(data *map[int]int) { | ||
collection.ClearMap(*data) | ||
}) | ||
|
||
m := *p.Get() | ||
m[1] = 1 | ||
p.Release(&m) | ||
fmt.Println(m) | ||
// Output: | ||
// map[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,93 @@ | ||
package hub_test | ||
|
||
import ( | ||
"github.com/kercylan98/minotaur/utils/hub" | ||
"testing" | ||
) | ||
|
||
func TestNewObjectPool(t *testing.T) { | ||
var cases = []struct { | ||
name string | ||
generator func() *map[string]int | ||
releaser func(data *map[string]int) | ||
shouldPanic bool | ||
}{ | ||
{name: "TestNewObjectPool_NilGenerator", generator: nil, releaser: func(data *map[string]int) {}, shouldPanic: true}, | ||
{name: "TestNewObjectPool_NilReleaser", generator: func() *map[string]int { return &map[string]int{} }, releaser: nil, shouldPanic: true}, | ||
{name: "TestNewObjectPool_NilGeneratorAndReleaser", generator: nil, releaser: nil, shouldPanic: true}, | ||
{name: "TestNewObjectPool_Normal", generator: func() *map[string]int { return &map[string]int{} }, releaser: func(data *map[string]int) {}, shouldPanic: false}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
defer func() { | ||
if err := recover(); c.shouldPanic && err == nil { | ||
t.Error("TestNewObjectPool should panic") | ||
} | ||
}() | ||
_ = hub.NewObjectPool[map[string]int](c.generator, c.releaser) | ||
}) | ||
} | ||
} | ||
|
||
func TestObjectPool_Get(t *testing.T) { | ||
var cases = []struct { | ||
name string | ||
generator func() *map[string]int | ||
releaser func(data *map[string]int) | ||
}{ | ||
{ | ||
name: "TestObjectPool_Get_Normal", | ||
generator: func() *map[string]int { | ||
return &map[string]int{} | ||
}, | ||
releaser: func(data *map[string]int) { | ||
for k := range *data { | ||
delete(*data, k) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
pool := hub.NewObjectPool[map[string]int](c.generator, c.releaser) | ||
if actual := pool.Get(); len(*actual) != 0 { | ||
t.Error("TestObjectPool_Get failed") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestObjectPool_Release(t *testing.T) { | ||
var cases = []struct { | ||
name string | ||
generator func() *map[string]int | ||
releaser func(data *map[string]int) | ||
}{ | ||
{ | ||
name: "TestObjectPool_Release_Normal", | ||
generator: func() *map[string]int { | ||
return &map[string]int{} | ||
}, | ||
releaser: func(data *map[string]int) { | ||
for k := range *data { | ||
delete(*data, k) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
pool := hub.NewObjectPool[map[string]int](c.generator, c.releaser) | ||
msg := pool.Get() | ||
m := *msg | ||
m["test"] = 1 | ||
pool.Release(msg) | ||
if len(m) != 0 { | ||
t.Error("TestObjectPool_Release failed") | ||
} | ||
}) | ||
} | ||
} |