github.com/gogf/gf/v2@v2.7.4/internal/rwmutex/rwmutex_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package rwmutex_test 8 9 import ( 10 "testing" 11 "time" 12 13 "github.com/gogf/gf/v2/container/garray" 14 "github.com/gogf/gf/v2/internal/rwmutex" 15 "github.com/gogf/gf/v2/test/gtest" 16 ) 17 18 func TestRWMutexIsSafe(t *testing.T) { 19 gtest.C(t, func(t *gtest.T) { 20 lock := rwmutex.New() 21 t.Assert(lock.IsSafe(), false) 22 23 lock = rwmutex.New(false) 24 t.Assert(lock.IsSafe(), false) 25 26 lock = rwmutex.New(false, false) 27 t.Assert(lock.IsSafe(), false) 28 29 lock = rwmutex.New(true, false) 30 t.Assert(lock.IsSafe(), true) 31 32 lock = rwmutex.New(true, true) 33 t.Assert(lock.IsSafe(), true) 34 35 lock = rwmutex.New(true) 36 t.Assert(lock.IsSafe(), true) 37 }) 38 } 39 40 func TestSafeRWMutex(t *testing.T) { 41 gtest.C(t, func(t *gtest.T) { 42 var ( 43 localSafeLock = rwmutex.New(true) 44 array = garray.New(true) 45 ) 46 47 go func() { 48 localSafeLock.Lock() 49 array.Append(1) 50 time.Sleep(1000 * time.Millisecond) 51 array.Append(1) 52 localSafeLock.Unlock() 53 }() 54 go func() { 55 time.Sleep(100 * time.Millisecond) 56 localSafeLock.Lock() 57 array.Append(1) 58 time.Sleep(2000 * time.Millisecond) 59 array.Append(1) 60 localSafeLock.Unlock() 61 }() 62 time.Sleep(500 * time.Millisecond) 63 t.Assert(array.Len(), 1) 64 time.Sleep(800 * time.Millisecond) 65 t.Assert(array.Len(), 3) 66 time.Sleep(1000 * time.Millisecond) 67 t.Assert(array.Len(), 3) 68 time.Sleep(1000 * time.Millisecond) 69 t.Assert(array.Len(), 4) 70 }) 71 } 72 73 func TestSafeReaderRWMutex(t *testing.T) { 74 gtest.C(t, func(t *gtest.T) { 75 var ( 76 localSafeLock = rwmutex.New(true) 77 array = garray.New(true) 78 ) 79 go func() { 80 localSafeLock.RLock() 81 array.Append(1) 82 time.Sleep(1000 * time.Millisecond) 83 array.Append(1) 84 localSafeLock.RUnlock() 85 }() 86 go func() { 87 time.Sleep(100 * time.Millisecond) 88 localSafeLock.RLock() 89 array.Append(1) 90 time.Sleep(2000 * time.Millisecond) 91 array.Append(1) 92 time.Sleep(1000 * time.Millisecond) 93 array.Append(1) 94 localSafeLock.RUnlock() 95 }() 96 go func() { 97 time.Sleep(500 * time.Millisecond) 98 localSafeLock.Lock() 99 array.Append(1) 100 localSafeLock.Unlock() 101 }() 102 time.Sleep(500 * time.Millisecond) 103 t.Assert(array.Len(), 2) 104 time.Sleep(1000 * time.Millisecond) 105 t.Assert(array.Len(), 3) 106 time.Sleep(1000 * time.Millisecond) 107 t.Assert(array.Len(), 4) 108 time.Sleep(1000 * time.Millisecond) 109 t.Assert(array.Len(), 6) 110 }) 111 } 112 113 func TestUnsafeRWMutex(t *testing.T) { 114 gtest.C(t, func(t *gtest.T) { 115 var ( 116 localUnsafeLock = rwmutex.New() 117 array = garray.New(true) 118 ) 119 go func() { 120 localUnsafeLock.Lock() 121 array.Append(1) 122 time.Sleep(2000 * time.Millisecond) 123 array.Append(1) 124 localUnsafeLock.Unlock() 125 }() 126 go func() { 127 time.Sleep(500 * time.Millisecond) 128 localUnsafeLock.Lock() 129 array.Append(1) 130 time.Sleep(500 * time.Millisecond) 131 array.Append(1) 132 localUnsafeLock.Unlock() 133 }() 134 time.Sleep(800 * time.Millisecond) 135 t.Assert(array.Len(), 2) 136 time.Sleep(800 * time.Millisecond) 137 t.Assert(array.Len(), 3) 138 time.Sleep(200 * time.Millisecond) 139 t.Assert(array.Len(), 3) 140 time.Sleep(500 * time.Millisecond) 141 t.Assert(array.Len(), 4) 142 }) 143 }