]> 127.0.0.1 Git - part/.git/commitdiff
fix
authorqydysky <32743305+qydysky@users.noreply.github.com>
Sun, 12 Mar 2023 05:46:30 +0000 (13:46 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Sun, 12 Mar 2023 05:46:30 +0000 (13:46 +0800)
.github/workflows/test.yml
file/FileWR.go
file/FileWR_test.go
funcCtrl/FuncCtrl.go
funcCtrl/FuncCtrl_test.go
msgq/Msgq.go
pool/Pool_test.go
signal/Signal.go
signal/Signal_test.go

index cd9ae2ce312fa9dc5d4c136ca154192ed9bce051..36dfc090e8e0099ac7b518469b1af419fa55e1bb 100644 (file)
@@ -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
index 367242175065a11e2a5af64845e7a3376ddb6b24..2d44ae41246392579d5c6fb519c18fce05f81da9 100644 (file)
@@ -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)
index d17c0c2f75c07cad89f746ccf9ef82f8d967703c..c836785adb0ca0766cc3e86f4a83cbf8d55514f4 100644 (file)
@@ -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 {
index 77756eb20d0977f2c4a19483dff5b57a7ad970a5..26aca3146edc14f7cd7e4aebbe0aaa9392a93bf7 100644 (file)
@@ -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
 }
 
index 7b7acc8a5b0620a2d3d099d36d8caa6f45ab84b7..13de5b9430ea6f3bc7cad8d4d4eb5b3652ef5d12 100644 (file)
@@ -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)
 }
index 572f7187caec1155b3291c2c3955e92d29129ac7..b8bf6a7c5451ecddc107818c894ac9a8477c766b 100644 (file)
@@ -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
        })
index 55cbb006b368a4452b23bfb3caec6718db33d3e5..51152dae762ed94723f0158c39a702970b31e596 100644 (file)
@@ -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()
+       }
 }
index e3cb8fc90191ec056ec9b8ccf7532f680556e342..9fdc2b31f0ec7d2870592fed7b724d5b2133b785 100644 (file)
@@ -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)
index 3b64f29a99ba03a3d1cb139a427c28816150bcad..8130243abd1341538a0e35f5cf8bd75382aaa5cd 100644 (file)
@@ -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++ {