github.com/gogf/gf@v1.16.9/os/gmlock/gmlock_unit_lock_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 gmlock_test
     8  
     9  import (
    10  	"sync"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/gogf/gf/container/garray"
    15  	"github.com/gogf/gf/os/gmlock"
    16  	"github.com/gogf/gf/test/gtest"
    17  )
    18  
    19  func Test_Locker_Lock(t *testing.T) {
    20  	gtest.C(t, func(t *gtest.T) {
    21  		key := "testLock"
    22  		array := garray.New(true)
    23  		go func() {
    24  			gmlock.Lock(key)
    25  			array.Append(1)
    26  			time.Sleep(300 * time.Millisecond)
    27  			gmlock.Unlock(key)
    28  		}()
    29  		go func() {
    30  			time.Sleep(100 * time.Millisecond)
    31  			gmlock.Lock(key)
    32  			array.Append(1)
    33  			gmlock.Unlock(key)
    34  		}()
    35  		time.Sleep(100 * time.Millisecond)
    36  		t.Assert(array.Len(), 1)
    37  		time.Sleep(100 * time.Millisecond)
    38  		t.Assert(array.Len(), 1)
    39  		time.Sleep(200 * time.Millisecond)
    40  		t.Assert(array.Len(), 2)
    41  		gmlock.Remove(key)
    42  	})
    43  
    44  	gtest.C(t, func(t *gtest.T) {
    45  		key := "testLock"
    46  		array := garray.New(true)
    47  		lock := gmlock.New()
    48  		go func() {
    49  			lock.Lock(key)
    50  			array.Append(1)
    51  			time.Sleep(300 * time.Millisecond)
    52  			lock.Unlock(key)
    53  		}()
    54  		go func() {
    55  			time.Sleep(100 * time.Millisecond)
    56  			lock.Lock(key)
    57  			array.Append(1)
    58  			lock.Unlock(key)
    59  		}()
    60  		time.Sleep(100 * time.Millisecond)
    61  		t.Assert(array.Len(), 1)
    62  		time.Sleep(100 * time.Millisecond)
    63  		t.Assert(array.Len(), 1)
    64  		time.Sleep(200 * time.Millisecond)
    65  		t.Assert(array.Len(), 2)
    66  		lock.Clear()
    67  	})
    68  
    69  }
    70  
    71  func Test_Locker_TryLock(t *testing.T) {
    72  	gtest.C(t, func(t *gtest.T) {
    73  		key := "testTryLock"
    74  		array := garray.New(true)
    75  		go func() {
    76  			gmlock.Lock(key)
    77  			array.Append(1)
    78  			time.Sleep(300 * time.Millisecond)
    79  			gmlock.Unlock(key)
    80  		}()
    81  		go func() {
    82  			time.Sleep(150 * time.Millisecond)
    83  			if gmlock.TryLock(key) {
    84  				array.Append(1)
    85  				gmlock.Unlock(key)
    86  			}
    87  		}()
    88  		go func() {
    89  			time.Sleep(400 * time.Millisecond)
    90  			if gmlock.TryLock(key) {
    91  				array.Append(1)
    92  				gmlock.Unlock(key)
    93  			}
    94  		}()
    95  		time.Sleep(100 * time.Millisecond)
    96  		t.Assert(array.Len(), 1)
    97  		time.Sleep(100 * time.Millisecond)
    98  		t.Assert(array.Len(), 1)
    99  		time.Sleep(300 * time.Millisecond)
   100  		t.Assert(array.Len(), 2)
   101  	})
   102  
   103  }
   104  
   105  func Test_Locker_LockFunc(t *testing.T) {
   106  	//no expire
   107  	gtest.C(t, func(t *gtest.T) {
   108  		key := "testLockFunc"
   109  		array := garray.New(true)
   110  		go func() {
   111  			gmlock.LockFunc(key, func() {
   112  				array.Append(1)
   113  				time.Sleep(300 * time.Millisecond)
   114  			}) //
   115  		}()
   116  		go func() {
   117  			time.Sleep(100 * time.Millisecond)
   118  			gmlock.LockFunc(key, func() {
   119  				array.Append(1)
   120  			})
   121  		}()
   122  		time.Sleep(100 * time.Millisecond)
   123  		t.Assert(array.Len(), 1)
   124  		time.Sleep(100 * time.Millisecond)
   125  		t.Assert(array.Len(), 1) //
   126  		time.Sleep(200 * time.Millisecond)
   127  		t.Assert(array.Len(), 2)
   128  	})
   129  }
   130  func Test_Locker_TryLockFunc(t *testing.T) {
   131  	//no expire
   132  	gtest.C(t, func(t *gtest.T) {
   133  		key := "testTryLockFunc"
   134  		array := garray.New(true)
   135  		go func() {
   136  			gmlock.TryLockFunc(key, func() {
   137  				array.Append(1)
   138  				time.Sleep(200 * time.Millisecond)
   139  			})
   140  		}()
   141  		go func() {
   142  			time.Sleep(100 * time.Millisecond)
   143  			gmlock.TryLockFunc(key, func() {
   144  				array.Append(1)
   145  			})
   146  		}()
   147  		go func() {
   148  			time.Sleep(300 * time.Millisecond)
   149  			gmlock.TryLockFunc(key, func() {
   150  				array.Append(1)
   151  			})
   152  		}()
   153  		time.Sleep(150 * time.Millisecond)
   154  		t.Assert(array.Len(), 1)
   155  		time.Sleep(400 * time.Millisecond)
   156  		t.Assert(array.Len(), 2)
   157  	})
   158  }
   159  
   160  func Test_Multiple_Goroutine(t *testing.T) {
   161  	gtest.C(t, func(t *gtest.T) {
   162  		ch := make(chan struct{}, 0)
   163  		num := 1000
   164  		wait := sync.WaitGroup{}
   165  		wait.Add(num)
   166  		for i := 0; i < num; i++ {
   167  			go func() {
   168  				defer wait.Done()
   169  				<-ch
   170  				gmlock.Lock("test")
   171  				defer gmlock.Unlock("test")
   172  				time.Sleep(time.Millisecond)
   173  			}()
   174  		}
   175  		close(ch)
   176  		wait.Wait()
   177  	})
   178  
   179  	gtest.C(t, func(t *gtest.T) {
   180  		ch := make(chan struct{}, 0)
   181  		num := 100
   182  		wait := sync.WaitGroup{}
   183  		wait.Add(num * 2)
   184  		for i := 0; i < num; i++ {
   185  			go func() {
   186  				defer wait.Done()
   187  				<-ch
   188  				gmlock.Lock("test")
   189  				defer gmlock.Unlock("test")
   190  				time.Sleep(time.Millisecond)
   191  			}()
   192  		}
   193  		for i := 0; i < num; i++ {
   194  			go func() {
   195  				defer wait.Done()
   196  				<-ch
   197  				gmlock.RLock("test")
   198  				defer gmlock.RUnlock("test")
   199  				time.Sleep(time.Millisecond)
   200  			}()
   201  		}
   202  		close(ch)
   203  		wait.Wait()
   204  	})
   205  }