From 68efb3203d6d392c7b1698796b088d686dae65b7 Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Sun, 12 Mar 2023 13:46:30 +0800 Subject: [PATCH] fix --- .github/workflows/test.yml | 18 +++++----- file/FileWR.go | 2 +- file/FileWR_test.go | 8 ++--- funcCtrl/FuncCtrl.go | 34 ++++++------------ funcCtrl/FuncCtrl_test.go | 72 ++++++++++++++++++++++++++------------ msgq/Msgq.go | 14 +++++--- pool/Pool_test.go | 20 +++++------ signal/Signal.go | 6 +++- signal/Signal_test.go | 25 ++++--------- 9 files changed, 103 insertions(+), 96 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cd9ae2c..36dfc09 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,13 +21,13 @@ jobs: - 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 diff --git a/file/FileWR.go b/file/FileWR.go index 3672421..2d44ae4 100644 --- a/file/FileWR.go +++ b/file/FileWR.go @@ -381,7 +381,7 @@ func (t *File) newPath() error { 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) diff --git a/file/FileWR_test.go b/file/FileWR_test.go index d17c0c2..c836785 100644 --- a/file/FileWR_test.go +++ b/file/FileWR_test.go @@ -11,7 +11,7 @@ import ( ) 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) } @@ -146,18 +146,14 @@ func TestReadUntil(t *testing.T) { 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 { diff --git a/funcCtrl/FuncCtrl.go b/funcCtrl/FuncCtrl.go index 77756eb..26aca31 100644 --- a/funcCtrl/FuncCtrl.go +++ b/funcCtrl/FuncCtrl.go @@ -1,16 +1,14 @@ 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 } @@ -22,35 +20,25 @@ func (t *SkipFunc) UnSet() { 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 } diff --git a/funcCtrl/FuncCtrl_test.go b/funcCtrl/FuncCtrl_test.go index 7b7acc8..13de5b9 100644 --- a/funcCtrl/FuncCtrl_test.go +++ b/funcCtrl/FuncCtrl_test.go @@ -6,73 +6,99 @@ import ( ) 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) } diff --git a/msgq/Msgq.go b/msgq/Msgq.go index 572f718..b8bf6a7 100644 --- a/msgq/Msgq.go +++ b/msgq/Msgq.go @@ -6,6 +6,8 @@ import ( "sync" "sync/atomic" "time" + + signal "github.com/qydysky/part/signal" ) type Msgq struct { @@ -166,16 +168,18 @@ func (m *MsgType[T]) Pull_tag(func_map map[string]func(T) (disable bool)) { } 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 }) diff --git a/pool/Pool_test.go b/pool/Pool_test.go index 55cbb00..51152da 100644 --- a/pool/Pool_test.go +++ b/pool/Pool_test.go @@ -1,6 +1,7 @@ package part import ( + "bytes" "testing" "unsafe" ) @@ -32,24 +33,23 @@ func TestXxx(t *testing.T) { 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() + } } diff --git a/signal/Signal.go b/signal/Signal.go index e3cb8fc..9fdc2b3 100644 --- a/signal/Signal.go +++ b/signal/Signal.go @@ -26,11 +26,15 @@ func (i *Signal) Wait() { 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) diff --git a/signal/Signal_test.go b/signal/Signal_test.go index 3b64f29..8130243 100644 --- a/signal/Signal_test.go +++ b/signal/Signal_test.go @@ -5,15 +5,14 @@ import ( ) 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) { @@ -22,16 +21,6 @@ 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++ { -- 2.39.2