test: writeloop 包增加测试用例
This commit is contained in:
parent
307e500b82
commit
f52d73e20e
|
@ -0,0 +1,25 @@
|
||||||
|
package writeloop_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kercylan98/minotaur/server/writeloop"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BenchmarkWriteLoop_Put(b *testing.B) {
|
||||||
|
wl := writeloop.NewWriteLoop(wp, func(message *Message) error {
|
||||||
|
return nil
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
wl.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
|
for pb.Next() {
|
||||||
|
wl.Put(wp.Get())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
b.StopTimer()
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
package writeloop_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/kercylan98/minotaur/server/writeloop"
|
||||||
|
"github.com/kercylan98/minotaur/utils/concurrent"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleNewWriteLoop() {
|
||||||
|
pool := concurrent.NewPool[Message](func() *Message {
|
||||||
|
return &Message{}
|
||||||
|
}, func(data *Message) {
|
||||||
|
data.ID = 0
|
||||||
|
})
|
||||||
|
var wait sync.WaitGroup
|
||||||
|
wait.Add(10)
|
||||||
|
wl := writeloop.NewWriteLoop(pool, func(message *Message) error {
|
||||||
|
fmt.Println(message.ID)
|
||||||
|
wait.Done()
|
||||||
|
return nil
|
||||||
|
}, func(err any) {
|
||||||
|
fmt.Println(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
m := pool.Get()
|
||||||
|
m.ID = i
|
||||||
|
wl.Put(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
wait.Wait()
|
||||||
|
wl.Close()
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
|
// 4
|
||||||
|
// 5
|
||||||
|
// 6
|
||||||
|
// 7
|
||||||
|
// 8
|
||||||
|
// 9
|
||||||
|
}
|
|
@ -0,0 +1,65 @@
|
||||||
|
package writeloop_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kercylan98/minotaur/server/writeloop"
|
||||||
|
"github.com/kercylan98/minotaur/utils/concurrent"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
ID int
|
||||||
|
}
|
||||||
|
|
||||||
|
var wp = concurrent.NewPool(func() *Message {
|
||||||
|
return &Message{}
|
||||||
|
}, func(data *Message) {
|
||||||
|
data.ID = 0
|
||||||
|
})
|
||||||
|
|
||||||
|
func TestNewWriteLoop(t *testing.T) {
|
||||||
|
wl := writeloop.NewWriteLoop(wp, func(message *Message) error {
|
||||||
|
t.Log(message.ID)
|
||||||
|
return nil
|
||||||
|
}, func(err any) {
|
||||||
|
t.Log(err)
|
||||||
|
})
|
||||||
|
assert.NotNil(t, wl)
|
||||||
|
wl.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteLoop_Put(t *testing.T) {
|
||||||
|
wl := writeloop.NewWriteLoop(wp, func(message *Message) error {
|
||||||
|
t.Log(message.ID)
|
||||||
|
return nil
|
||||||
|
}, func(err any) {
|
||||||
|
t.Log(err)
|
||||||
|
})
|
||||||
|
assert.NotNil(t, wl)
|
||||||
|
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
m := wp.Get()
|
||||||
|
m.ID = i
|
||||||
|
wl.Put(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
wl.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteLoop_Close(t *testing.T) {
|
||||||
|
wl := writeloop.NewWriteLoop(wp, func(message *Message) error {
|
||||||
|
t.Log(message.ID)
|
||||||
|
return nil
|
||||||
|
}, func(err any) {
|
||||||
|
t.Log(err)
|
||||||
|
})
|
||||||
|
assert.NotNil(t, wl)
|
||||||
|
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
m := wp.Get()
|
||||||
|
m.ID = i
|
||||||
|
wl.Put(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
wl.Close()
|
||||||
|
}
|
Loading…
Reference in New Issue