github.com/zhongdalu/gf@v1.0.0/g/os/gflock/gflock_unit_test.go (about) 1 // Copyright 2019 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 gflock_test 8 9 import ( 10 "testing" 11 "time" 12 13 "github.com/zhongdalu/gf/g/container/garray" 14 "github.com/zhongdalu/gf/g/os/gfile" 15 "github.com/zhongdalu/gf/g/os/gflock" 16 "github.com/zhongdalu/gf/g/test/gtest" 17 ) 18 19 func Test_GFlock_Base(t *testing.T) { 20 gtest.Case(t, func() { 21 fileName := "test" 22 lock := gflock.New(fileName) 23 gtest.Assert(lock.Path(), gfile.TempDir()+gfile.Separator+"gflock"+gfile.Separator+fileName) 24 gtest.Assert(lock.IsLocked(), false) 25 lock.Lock() 26 gtest.Assert(lock.IsLocked(), true) 27 lock.Unlock() 28 gtest.Assert(lock.IsLocked(), false) 29 }) 30 31 gtest.Case(t, func() { 32 fileName := "test" 33 lock := gflock.New(fileName) 34 gtest.Assert(lock.Path(), gfile.TempDir()+gfile.Separator+"gflock"+gfile.Separator+fileName) 35 gtest.Assert(lock.IsRLocked(), false) 36 lock.RLock() 37 gtest.Assert(lock.IsRLocked(), true) 38 lock.RUnlock() 39 gtest.Assert(lock.IsRLocked(), false) 40 }) 41 } 42 43 func Test_GFlock_Lock(t *testing.T) { 44 gtest.Case(t, func() { 45 fileName := "testLock" 46 array := garray.New() 47 lock := gflock.New(fileName) 48 lock2 := gflock.New(fileName) 49 50 go func() { 51 lock.Lock() 52 array.Append(1) 53 time.Sleep(300 * time.Millisecond) 54 lock.Unlock() 55 }() 56 57 go func() { 58 time.Sleep(100 * time.Millisecond) 59 lock2.Lock() 60 array.Append(1) 61 lock2.Unlock() 62 }() 63 64 time.Sleep(100 * time.Millisecond) 65 gtest.Assert(array.Len(), 1) 66 time.Sleep(100 * time.Millisecond) 67 gtest.Assert(array.Len(), 1) 68 time.Sleep(200 * time.Millisecond) 69 gtest.Assert(array.Len(), 2) 70 }) 71 } 72 73 func Test_GFlock_RLock(t *testing.T) { 74 gtest.Case(t, func() { 75 fileName := "testRLock" 76 array := garray.New() 77 lock := gflock.New(fileName) 78 lock2 := gflock.New(fileName) 79 80 go func() { 81 lock.RLock() 82 array.Append(1) 83 time.Sleep(400 * time.Millisecond) 84 lock.RUnlock() 85 }() 86 87 go func() { 88 time.Sleep(200 * time.Millisecond) 89 lock2.RLock() 90 array.Append(1) 91 lock2.RUnlock() 92 }() 93 94 time.Sleep(100 * time.Millisecond) 95 gtest.Assert(array.Len(), 1) 96 time.Sleep(200 * time.Millisecond) 97 gtest.Assert(array.Len(), 2) 98 }) 99 } 100 101 func Test_GFlock_TryLock(t *testing.T) { 102 gtest.Case(t, func() { 103 fileName := "testTryLock" 104 array := garray.New() 105 lock := gflock.New(fileName) 106 lock2 := gflock.New(fileName) 107 108 go func() { 109 lock.TryLock() 110 array.Append(1) 111 time.Sleep(200 * time.Millisecond) 112 lock.Unlock() 113 }() 114 115 go func() { 116 time.Sleep(100 * time.Millisecond) 117 if lock2.TryLock() { 118 array.Append(1) 119 lock2.Unlock() 120 } 121 }() 122 123 go func() { 124 time.Sleep(300 * time.Millisecond) 125 if lock2.TryLock() { 126 array.Append(1) 127 lock2.Unlock() 128 } 129 }() 130 time.Sleep(100 * time.Millisecond) 131 gtest.Assert(array.Len(), 1) 132 time.Sleep(100 * time.Millisecond) 133 gtest.Assert(array.Len(), 1) 134 time.Sleep(200 * time.Millisecond) 135 gtest.Assert(array.Len(), 2) 136 }) 137 } 138 139 func Test_GFlock_TryRLock(t *testing.T) { 140 gtest.Case(t, func() { 141 fileName := "testTryRLock" 142 array := garray.New() 143 lock := gflock.New(fileName) 144 lock2 := gflock.New(fileName) 145 go func() { 146 lock.TryRLock() 147 array.Append(1) 148 time.Sleep(300 * time.Millisecond) 149 lock.Unlock() 150 }() 151 152 go func() { 153 time.Sleep(200 * time.Millisecond) 154 if lock2.TryRLock() { 155 array.Append(1) 156 lock2.Unlock() 157 } 158 }() 159 160 go func() { 161 time.Sleep(200 * time.Millisecond) 162 if lock2.TryRLock() { 163 array.Append(1) 164 lock2.Unlock() 165 } 166 }() 167 168 go func() { 169 time.Sleep(200 * time.Millisecond) 170 if lock2.TryRLock() { 171 array.Append(1) 172 lock2.Unlock() 173 } 174 }() 175 time.Sleep(100 * time.Millisecond) 176 gtest.Assert(array.Len(), 1) 177 time.Sleep(300 * time.Millisecond) 178 gtest.Assert(array.Len(), 4) 179 }) 180 }