github.com/zhongdalu/gf@v1.0.0/g/internal/rwmutex/rwmutex_z_unit_test.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 package rwmutex_test 8 9 import ( 10 "github.com/zhongdalu/gf/g/container/garray" 11 "github.com/zhongdalu/gf/g/internal/rwmutex" 12 "github.com/zhongdalu/gf/g/test/gtest" 13 "testing" 14 "time" 15 ) 16 17 func TestRwmutexIsSafe(t *testing.T) { 18 gtest.Case(t, func() { 19 lock := rwmutex.New() 20 gtest.Assert(lock.IsSafe(), true) 21 22 lock = rwmutex.New(false) 23 gtest.Assert(lock.IsSafe(), true) 24 25 lock = rwmutex.New(false, false) 26 gtest.Assert(lock.IsSafe(), true) 27 28 lock = rwmutex.New(true, false) 29 gtest.Assert(lock.IsSafe(), false) 30 31 lock = rwmutex.New(true, true) 32 gtest.Assert(lock.IsSafe(), false) 33 34 lock = rwmutex.New(true) 35 gtest.Assert(lock.IsSafe(), false) 36 }) 37 } 38 39 func TestSafeRwmutex(t *testing.T) { 40 gtest.Case(t, func() { 41 safeLock := rwmutex.New() 42 array := garray.New() 43 44 go func() { 45 safeLock.Lock() 46 array.Append(1) 47 time.Sleep(100 * time.Millisecond) 48 array.Append(1) 49 safeLock.Unlock() 50 }() 51 go func() { 52 time.Sleep(10 * time.Millisecond) 53 safeLock.Lock() 54 array.Append(1) 55 time.Sleep(200 * time.Millisecond) 56 array.Append(1) 57 safeLock.Unlock() 58 }() 59 time.Sleep(50 * time.Millisecond) 60 gtest.Assert(array.Len(), 1) 61 time.Sleep(80 * time.Millisecond) 62 gtest.Assert(array.Len(), 3) 63 time.Sleep(100 * time.Millisecond) 64 gtest.Assert(array.Len(), 3) 65 time.Sleep(100 * time.Millisecond) 66 gtest.Assert(array.Len(), 4) 67 }) 68 } 69 70 func TestSafeReaderRwmutex(t *testing.T) { 71 gtest.Case(t, func() { 72 safeLock := rwmutex.New() 73 array := garray.New() 74 75 go func() { 76 safeLock.RLock() 77 array.Append(1) 78 time.Sleep(100 * time.Millisecond) 79 array.Append(1) 80 safeLock.RUnlock() 81 }() 82 go func() { 83 time.Sleep(10 * time.Millisecond) 84 safeLock.RLock() 85 array.Append(1) 86 time.Sleep(200 * time.Millisecond) 87 array.Append(1) 88 time.Sleep(100 * time.Millisecond) 89 array.Append(1) 90 safeLock.RUnlock() 91 }() 92 go func() { 93 time.Sleep(50 * time.Millisecond) 94 safeLock.Lock() 95 array.Append(1) 96 safeLock.Unlock() 97 }() 98 time.Sleep(50 * time.Millisecond) 99 gtest.Assert(array.Len(), 2) 100 time.Sleep(100 * time.Millisecond) 101 gtest.Assert(array.Len(), 3) 102 time.Sleep(100 * time.Millisecond) 103 gtest.Assert(array.Len(), 4) 104 time.Sleep(100 * time.Millisecond) 105 gtest.Assert(array.Len(), 6) 106 }) 107 } 108 109 func TestUnsafeRwmutex(t *testing.T) { 110 gtest.Case(t, func() { 111 unsafeLock := rwmutex.New(true) 112 array := garray.New() 113 114 go func() { 115 unsafeLock.Lock() 116 array.Append(1) 117 time.Sleep(100 * time.Millisecond) 118 array.Append(1) 119 unsafeLock.Unlock() 120 }() 121 go func() { 122 time.Sleep(10 * time.Millisecond) 123 unsafeLock.Lock() 124 array.Append(1) 125 time.Sleep(200 * time.Millisecond) 126 array.Append(1) 127 unsafeLock.Unlock() 128 }() 129 time.Sleep(50 * time.Millisecond) 130 gtest.Assert(array.Len(), 2) 131 time.Sleep(100 * time.Millisecond) 132 gtest.Assert(array.Len(), 3) 133 time.Sleep(50 * time.Millisecond) 134 gtest.Assert(array.Len(), 3) 135 time.Sleep(100 * time.Millisecond) 136 gtest.Assert(array.Len(), 4) 137 }) 138 }