go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc
go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component
go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx
+ go test -count 1 -timeout 5s -v -race github.com/qydysky/part/slice
- name: Set Release Name
run: |
"errors"
)
-type Blocks[T any] struct {
+type BlocksI[T any] interface {
+ // // eg
+ //
+ // if tmpbuf, putBack, e := buf.Get(); e == nil {
+ // clear(tmpbuf)
+ // // do something with tmpbuf
+ // putBack()
+ // }
+ Get() ([]T, func(), error)
+}
+
+type blocks[T any] struct {
+ _ noCopy
free chan int
size int
buf []T
}
+type noCopy struct{}
+
+func (*noCopy) Lock() {}
+func (*noCopy) Unlock() {}
+
var ErrOverflow = errors.New("ErrOverflow")
-func NewBlocks[T any](blockSize int, blockNum int) *Blocks[T] {
- p := &Blocks[T]{
+func NewBlocks[T any](blockSize int, blockNum int) BlocksI[T] {
+ p := &blocks[T]{
size: blockSize,
free: make(chan int, blockNum+1),
buf: make([]T, blockSize*blockNum),
return p
}
-// // eg
-//
-// if tmpbuf, putBack, e := buf.Get(); e == nil {
-// clear(tmpbuf)
-// // do something with tmpbuf
-// putBack()
-// }
-func (t *Blocks[T]) Get() ([]T, func(), error) {
+func (t *blocks[T]) Get() ([]T, func(), error) {
select {
case offset := <-t.free:
offset *= t.size
package part
-import "testing"
+import (
+ "testing"
+)
func TestMain(t *testing.T) {
- buf := NewBlocks[byte](1024, 10)
+ buf := NewBlocks[byte](1024, 1)
if tmpbuf, putBack, e := buf.Get(); e == nil {
clear(tmpbuf)
// do something with tmpbuf
putBack()
+ } else {
+ t.Fail()
+ }
+ if tmpbuf, putBack, e := buf.Get(); e == nil {
+ clear(tmpbuf)
+ if tmpbuf, putBack, e := buf.Get(); e != ErrOverflow {
+ clear(tmpbuf)
+ t.Fail()
+ // do something with tmpbuf
+ putBack()
+ }
+ // do something with tmpbuf
+ putBack()
}
}