- name: Check out code into the Go module directory
uses: actions/checkout@v3
- - name: Get dependencies
- run: |
- go get -v -t -d ./...
- if [ -f Gopkg.toml ]; then
- curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
- dep ensure
- fi
-
- name: Test
- run: go test -v .
+ run: |
+ go test -v .
+ go test -v -race github.com/qydysky/part/signal
+ go test -v -race github.com/qydysky/part/reqf
+ go test -v -race github.com/qydysky/part/limit
+ go test -v -race github.com/qydysky/part/file
+ go test -v -race github.com/qydysky/part/pool
+ go test -v -race github.com/qydysky/part/funcCtrl
+ go test -v -race github.com/qydysky/part/msgq
func transferIO(r io.Reader, w io.Writer, byteInSec int64) (e error) {
if byteInSec > 0 {
- limit := l.New(1, 1000, -1)
+ limit := l.New(1, "1s", "-1s")
defer limit.Close()
buf := make([]byte, byteInSec)
)
func TestWriteReadDelSync(t *testing.T) {
- f := New("test/rwd.txt", -6, true)
+ f := New("rwd.txt", -6, true)
if i, e := f.Write([]byte("sssa\n"), true); i == 0 || e != nil {
t.Fatal(e)
}
t.Fatal(string(data))
}
- t.Log(f.Config.CurIndex)
-
if data, e := f.ReadUntil('\n', 5, 20); e != nil {
t.Fatal(e)
} else if !bytes.Equal(data, []byte("s99s9")) {
t.Fatal(string(data))
}
- if data, e := f.ReadUntil('\n', 5, 20); e == nil || !errors.Is(e, io.EOF) {
+ if data, e := f.ReadUntil('\n', 5, 20); e == nil || !errors.Is(e, io.EOF) || len(data) != 0 {
t.Fatal(e)
- } else {
- t.Log(string(data))
}
if e := f.Close(); e != nil {
package part
import (
- "container/list"
"runtime"
"sync"
"sync/atomic"
"unsafe"
-
- idpool "github.com/qydysky/part/idpool"
)
-type SkipFunc struct { //新的跳过
+// 新的跳过
+type SkipFunc struct {
c unsafe.Pointer
}
atomic.CompareAndSwapPointer(&t.c, atomic.LoadPointer(&t.c), nil)
}
-type FlashFunc struct { //新的替换旧的
- b *list.List
- pool *idpool.Idpool
+// 新的替换旧的
+type FlashFunc struct {
+ b atomic.Uintptr
}
func (t *FlashFunc) Flash() (current uintptr) {
- if t.pool == nil {
- t.pool = idpool.New(func() interface{} { return new(struct{}) })
- }
- if t.b == nil {
- t.b = list.New()
- }
-
- e := t.pool.Get()
- current = e.Id
- t.b.PushFront(e)
-
+ current = uintptr(unsafe.Pointer(&struct{}{}))
+ t.b.Store(current)
return
}
-func (t *FlashFunc) UnFlash() {
- t.b.Remove(t.b.Back())
-}
+func (t *FlashFunc) UnFlash() {}
func (t *FlashFunc) NeedExit(current uintptr) bool {
- return current != t.b.Front().Value.(*idpool.Id).Id
+ return t.b.Load() != current
}
-type BlockFunc struct { //新的等待旧的
+// 新的等待旧的
+type BlockFunc struct {
sync.Mutex
}
)
func Test_SkipFunc(t *testing.T) {
+ var c = make(chan int, 2)
var b SkipFunc
var a = func(i int) {
if b.NeedSkip() {
return
}
defer b.UnSet()
- t.Log(i, `.`)
+ c <- i
time.Sleep(time.Second)
- t.Log(i, `..`)
+ c <- i
}
- t.Log(`just show 1 or 2 twice`)
go a(1)
go a(2)
- time.Sleep(5 * time.Second)
+ if i0 := <-c; i0 != <-c {
+ t.Fatal()
+ }
}
func Test_FlashFunc(t *testing.T) {
+ var c = make(chan int, 2)
var b FlashFunc
var a = func(i int) {
id := b.Flash()
defer b.UnFlash()
-
- t.Log(i, `.`)
+ c <- i
time.Sleep(time.Second)
if b.NeedExit(id) {
return
}
- t.Log(i, `.`)
+ c <- i
}
- t.Log(`show 1 or 2, then show the other twice`)
go a(1)
go a(2)
- time.Sleep(5 * time.Second)
+ if i0 := <-c; i0 == <-c {
+ t.Fatal()
+ }
}
func Test_BlockFunc(t *testing.T) {
+ var c = make(chan int, 2)
var b BlockFunc
var a = func(i int) {
b.Block()
defer b.UnBlock()
- t.Log(i, `.`)
+ c <- i
time.Sleep(time.Second)
- t.Log(i, `.`)
+ c <- i
}
- t.Log(`show 1 and 2 twice`)
go a(1)
go a(2)
- time.Sleep(5 * time.Second)
+ if i0 := <-c; i0 != <-c {
+ t.Fatal()
+ }
+ if i0 := <-c; i0 != <-c {
+ t.Fatal()
+ }
}
func Test_BlockFuncN(t *testing.T) {
+ var c = make(chan string, 8)
+ var cc string
+
var b = &BlockFuncN{
Max: 2,
}
- var a = func(i int) {
- defer b.UnBlock()
+ var a = func(i string) {
b.Block()
- t.Log(i, `.`)
+ defer b.UnBlock()
+ c <- i
time.Sleep(time.Second)
- t.Log(i, `.`)
+ c <- i
}
- t.Log(`show two . at one time`)
- go a(0)
- go a(1)
- go a(2)
- go a(3)
+ go a("0")
+ time.Sleep(time.Millisecond * 20)
+ go a("1")
+ time.Sleep(time.Millisecond * 20)
+ go a("2")
+
+ for len(c) > 0 {
+ cc += <-c
+ }
+ if cc != "01" {
+ t.Fatal()
+ }
+
b.BlockAll()
b.UnBlockAll()
- t.Log(`fin`)
+
+ for len(c) > 0 {
+ cc += <-c
+ }
+ if cc != "010212" {
+ t.Fatal()
+ }
+ // t.Log(cc)
}
"sync"
"sync/atomic"
"time"
+
+ signal "github.com/qydysky/part/signal"
)
type Msgq struct {
}
func (m *MsgType[T]) Pull_tag_async_only(key string, f func(T) (disable bool)) {
- var disable = false
+ var disable = signal.Init()
m.m.Register_front(func(data any) bool {
- if disable {
+ if !disable.Islive() {
return true
}
if d, ok := data.(Msgq_tag_data); ok && d.Tag == key {
- go func(t *bool) {
- *t = f(d.Data.(T))
- }(&disable)
+ go func() {
+ if f(d.Data.(T)) {
+ disable.Done()
+ }
+ }()
}
return false
})
package part
import (
+ "bytes"
"testing"
"unsafe"
)
var b = New(newf, validf, reusef, poolf, 10)
var c1 = b.Get()
+ var c1p = uintptr(unsafe.Pointer(c1))
c1.d = append(c1.d, []byte("1")...)
- t.Log(unsafe.Pointer(c1), c1)
-
var c2 = b.Get()
+ var c2p = uintptr(unsafe.Pointer(c2))
c2.d = append(c2.d, []byte("2")...)
- t.Log(unsafe.Pointer(c2), c2)
+ if c1p == c2p || bytes.Equal(c1.d, c2.d) || b.PoolInUse() != 0 || b.PoolSum() != 0 {
+ t.Fatal()
+ }
b.Put(c1)
-
- t.Log(unsafe.Pointer(c1), c1)
-
c1.v = false
-
- t.Log(unsafe.Pointer(c1), c1)
-
var c3 = b.Get()
+ var c3p = uintptr(unsafe.Pointer(c3))
- t.Log(unsafe.Pointer(c3), c3)
+ if c1p != c3p || len(c1.d) != 0 || b.PoolInUse() != 1 || b.PoolSum() != 1 {
+ t.Fatal()
+ }
}
func (i *Signal) WaitC() (c chan struct{}, fin func()) {
if i.Islive() {
i.waitCount.Add(1)
- return i.c, func() { i.waitCount.Add(-1) }
+ return i.c, i.fin
}
return nil, func() {}
}
+func (i *Signal) fin() {
+ i.waitCount.Add(-1)
+}
+
func (i *Signal) Done() {
if i.Islive() {
close(i.c)
)
func Test_signal(t *testing.T) {
- var s *Signal
- s.Wait()
- t.Log(s.Islive())
- s.Done()
- t.Log(s.Islive())
- s = Init()
- t.Log(s.Islive())
+ var s *Signal = Init()
+ if !s.Islive() {
+ t.Fatal()
+ }
s.Done()
- t.Log(s.Islive())
+ if s.Islive() {
+ t.Fatal()
+ }
}
func Test_signal2(t *testing.T) {
s.Wait()
}
-func Test_signal3(t *testing.T) {
- var s *Signal
- go func() {
- if s != nil {
- s.Islive()
- }
- }()
- s = Init()
-}
-
func BenchmarkXxx(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {