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