github.com/zhongdalu/gf@v1.0.0/g/os/gmlock/gmlock_unit_rlock_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 gmlock_test
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/zhongdalu/gf/g/container/garray"
    14  	"github.com/zhongdalu/gf/g/os/gmlock"
    15  	"github.com/zhongdalu/gf/g/test/gtest"
    16  )
    17  
    18  func Test_Locker_RLock(t *testing.T) {
    19  	//RLock before Lock
    20  	gtest.Case(t, func() {
    21  		key := "testRLockBeforeLock"
    22  		array := garray.New()
    23  		go func() {
    24  			gmlock.RLock(key)
    25  			array.Append(1)
    26  			time.Sleep(200 * time.Millisecond)
    27  			gmlock.RUnlock(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  		gtest.Assert(array.Len(), 1)
    37  		time.Sleep(200 * time.Millisecond)
    38  		gtest.Assert(array.Len(), 2)
    39  	})
    40  
    41  	//Lock before RLock
    42  	gtest.Case(t, func() {
    43  		key := "testLockBeforeRLock"
    44  		array := garray.New()
    45  		go func() {
    46  			gmlock.Lock(key)
    47  			array.Append(1)
    48  			time.Sleep(200 * time.Millisecond)
    49  			gmlock.Unlock(key)
    50  		}()
    51  		go func() {
    52  			time.Sleep(100 * time.Millisecond)
    53  			gmlock.RLock(key)
    54  			array.Append(1)
    55  			gmlock.RUnlock(key)
    56  		}()
    57  		time.Sleep(100 * time.Millisecond)
    58  		gtest.Assert(array.Len(), 1)
    59  		time.Sleep(200 * time.Millisecond)
    60  		gtest.Assert(array.Len(), 2)
    61  	})
    62  
    63  	//Lock before RLocks
    64  	gtest.Case(t, func() {
    65  		key := "testLockBeforeRLocks"
    66  		array := garray.New()
    67  		go func() {
    68  			gmlock.Lock(key)
    69  			array.Append(1)
    70  			time.Sleep(300 * time.Millisecond)
    71  			gmlock.Unlock(key)
    72  		}()
    73  		go func() {
    74  			time.Sleep(100 * time.Millisecond)
    75  			gmlock.RLock(key)
    76  			array.Append(1)
    77  			time.Sleep(200 * time.Millisecond)
    78  			gmlock.RUnlock(key)
    79  		}()
    80  		go func() {
    81  			time.Sleep(100 * time.Millisecond)
    82  			gmlock.RLock(key)
    83  			array.Append(1)
    84  			time.Sleep(200 * time.Millisecond)
    85  			gmlock.RUnlock(key)
    86  		}()
    87  		time.Sleep(200 * time.Millisecond)
    88  		gtest.Assert(array.Len(), 1)
    89  		time.Sleep(200 * time.Millisecond)
    90  		gtest.Assert(array.Len(), 3)
    91  	})
    92  }
    93  
    94  func Test_Locker_TryRLock(t *testing.T) {
    95  	//Lock before TryRLock
    96  	gtest.Case(t, func() {
    97  		key := "testLockBeforeTryRLock"
    98  		array := garray.New()
    99  		go func() {
   100  			gmlock.Lock(key)
   101  			array.Append(1)
   102  			time.Sleep(200 * time.Millisecond)
   103  			gmlock.Unlock(key)
   104  		}()
   105  		go func() {
   106  			time.Sleep(100 * time.Millisecond)
   107  			if gmlock.TryRLock(key) {
   108  				array.Append(1)
   109  				gmlock.RUnlock(key)
   110  			}
   111  		}()
   112  		time.Sleep(150 * time.Millisecond)
   113  		gtest.Assert(array.Len(), 1)
   114  		time.Sleep(200 * time.Millisecond)
   115  		gtest.Assert(array.Len(), 1)
   116  	})
   117  
   118  	//Lock before TryRLocks
   119  	gtest.Case(t, func() {
   120  		key := "testLockBeforeTryRLocks"
   121  		array := garray.New()
   122  		go func() {
   123  			gmlock.Lock(key)
   124  			array.Append(1)
   125  			time.Sleep(200 * time.Millisecond)
   126  			gmlock.Unlock(key)
   127  		}()
   128  		go func() {
   129  			time.Sleep(100 * time.Millisecond)
   130  			if gmlock.TryRLock(key) {
   131  				array.Append(1)
   132  				gmlock.RUnlock(key)
   133  			}
   134  		}()
   135  		go func() {
   136  			time.Sleep(300 * time.Millisecond)
   137  			if gmlock.TryRLock(key) {
   138  				array.Append(1)
   139  				gmlock.RUnlock(key)
   140  			}
   141  		}()
   142  		time.Sleep(150 * time.Millisecond)
   143  		gtest.Assert(array.Len(), 1)
   144  		time.Sleep(200 * time.Millisecond)
   145  		gtest.Assert(array.Len(), 2)
   146  	})
   147  }
   148  
   149  func Test_Locker_RLockFunc(t *testing.T) {
   150  	//RLockFunc before Lock
   151  	gtest.Case(t, func() {
   152  		key := "testRLockFuncBeforeLock"
   153  		array := garray.New()
   154  		go func() {
   155  			gmlock.RLockFunc(key, func() {
   156  				array.Append(1)
   157  				time.Sleep(200 * time.Millisecond)
   158  			})
   159  		}()
   160  		go func() {
   161  			time.Sleep(100 * time.Millisecond)
   162  			gmlock.Lock(key)
   163  			array.Append(1)
   164  			gmlock.Unlock(key)
   165  		}()
   166  		time.Sleep(150 * time.Millisecond)
   167  		gtest.Assert(array.Len(), 1)
   168  		time.Sleep(200 * time.Millisecond)
   169  		gtest.Assert(array.Len(), 2)
   170  	})
   171  
   172  	//Lock before RLockFunc
   173  	gtest.Case(t, func() {
   174  		key := "testLockBeforeRLockFunc"
   175  		array := garray.New()
   176  		go func() {
   177  			gmlock.Lock(key)
   178  			array.Append(1)
   179  			time.Sleep(200 * time.Millisecond)
   180  			gmlock.Unlock(key)
   181  		}()
   182  		go func() {
   183  			time.Sleep(100 * time.Millisecond)
   184  			gmlock.RLockFunc(key, func() {
   185  				array.Append(1)
   186  			})
   187  		}()
   188  		time.Sleep(100 * time.Millisecond)
   189  		gtest.Assert(array.Len(), 1)
   190  		time.Sleep(200 * time.Millisecond)
   191  		gtest.Assert(array.Len(), 2)
   192  	})
   193  
   194  	//Lock before RLockFuncs
   195  	gtest.Case(t, func() {
   196  		key := "testLockBeforeRLockFuncs"
   197  		array := garray.New()
   198  		go func() {
   199  			gmlock.Lock(key)
   200  			array.Append(1)
   201  			time.Sleep(200 * time.Millisecond)
   202  			gmlock.Unlock(key)
   203  		}()
   204  		go func() {
   205  			time.Sleep(100 * time.Millisecond)
   206  			gmlock.RLockFunc(key, func() {
   207  				array.Append(1)
   208  				time.Sleep(200 * time.Millisecond)
   209  			})
   210  		}()
   211  		go func() {
   212  			time.Sleep(100 * time.Millisecond)
   213  			gmlock.RLockFunc(key, func() {
   214  				array.Append(1)
   215  				time.Sleep(200 * time.Millisecond)
   216  			})
   217  		}()
   218  		time.Sleep(100 * time.Millisecond)
   219  		gtest.Assert(array.Len(), 1)
   220  		time.Sleep(200 * time.Millisecond)
   221  		gtest.Assert(array.Len(), 3)
   222  	})
   223  }
   224  
   225  func Test_Locker_TryRLockFunc(t *testing.T) {
   226  	//Lock before TryRLockFunc
   227  	gtest.Case(t, func() {
   228  		key := "testLockBeforeTryRLockFunc"
   229  		array := garray.New()
   230  		go func() {
   231  			gmlock.Lock(key)
   232  			array.Append(1)
   233  			time.Sleep(200 * time.Millisecond)
   234  			gmlock.Unlock(key)
   235  		}()
   236  		go func() {
   237  			time.Sleep(100 * time.Millisecond)
   238  			gmlock.TryRLockFunc(key, func() {
   239  				array.Append(1)
   240  			})
   241  		}()
   242  		time.Sleep(100 * time.Millisecond)
   243  		gtest.Assert(array.Len(), 1)
   244  		time.Sleep(200 * time.Millisecond)
   245  		gtest.Assert(array.Len(), 1)
   246  	})
   247  
   248  	//Lock before TryRLockFuncs
   249  	gtest.Case(t, func() {
   250  		key := "testLockBeforeTryRLockFuncs"
   251  		array := garray.New()
   252  		go func() {
   253  			gmlock.Lock(key)
   254  			array.Append(1)
   255  			time.Sleep(200 * time.Millisecond)
   256  			gmlock.Unlock(key)
   257  		}()
   258  		go func() {
   259  			time.Sleep(100 * time.Millisecond)
   260  			gmlock.TryRLockFunc(key, func() {
   261  				array.Append(1)
   262  			})
   263  		}()
   264  		go func() {
   265  			time.Sleep(300 * time.Millisecond)
   266  			gmlock.TryRLockFunc(key, func() {
   267  				array.Append(1)
   268  			})
   269  		}()
   270  		time.Sleep(100 * time.Millisecond)
   271  		gtest.Assert(array.Len(), 1)
   272  		time.Sleep(300 * time.Millisecond)
   273  		gtest.Assert(array.Len(), 2)
   274  	})
   275  }