github.com/zhongdalu/gf@v1.0.0/g/os/gmlock/gmlock_unit_lock_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_Lock(t *testing.T) {
    19  	gtest.Case(t, func() {
    20  		key := "testLock"
    21  		array := garray.New()
    22  		go func() {
    23  			gmlock.Lock(key)
    24  			array.Append(1)
    25  			time.Sleep(300 * time.Millisecond)
    26  			gmlock.Unlock(key)
    27  		}()
    28  		go func() {
    29  			time.Sleep(100 * time.Millisecond)
    30  			gmlock.Lock(key)
    31  			array.Append(1)
    32  			gmlock.Unlock(key)
    33  		}()
    34  		time.Sleep(100 * time.Millisecond)
    35  		gtest.Assert(array.Len(), 1)
    36  		time.Sleep(100 * time.Millisecond)
    37  		gtest.Assert(array.Len(), 1)
    38  		time.Sleep(200 * time.Millisecond)
    39  		gtest.Assert(array.Len(), 2)
    40  		gmlock.Remove(key)
    41  	})
    42  
    43  	gtest.Case(t, func() {
    44  		key := "testLock"
    45  		array := garray.New()
    46  		lock := gmlock.New()
    47  		go func() {
    48  			lock.Lock(key)
    49  			array.Append(1)
    50  			time.Sleep(300 * time.Millisecond)
    51  			lock.Unlock(key)
    52  		}()
    53  		go func() {
    54  			time.Sleep(100 * time.Millisecond)
    55  			lock.Lock(key)
    56  			array.Append(1)
    57  			lock.Unlock(key)
    58  		}()
    59  		time.Sleep(100 * time.Millisecond)
    60  		gtest.Assert(array.Len(), 1)
    61  		time.Sleep(100 * time.Millisecond)
    62  		gtest.Assert(array.Len(), 1)
    63  		time.Sleep(200 * time.Millisecond)
    64  		gtest.Assert(array.Len(), 2)
    65  		lock.Clear()
    66  	})
    67  
    68  }
    69  
    70  func Test_Locker_TryLock(t *testing.T) {
    71  	gtest.Case(t, func() {
    72  		key := "testTryLock"
    73  		array := garray.New()
    74  		go func() {
    75  			gmlock.Lock(key)
    76  			array.Append(1)
    77  			time.Sleep(300 * time.Millisecond)
    78  			gmlock.Unlock(key)
    79  		}()
    80  		go func() {
    81  			time.Sleep(150 * time.Millisecond)
    82  			if gmlock.TryLock(key) {
    83  				array.Append(1)
    84  				gmlock.Unlock(key)
    85  			}
    86  		}()
    87  		go func() {
    88  			time.Sleep(400 * time.Millisecond)
    89  			if gmlock.TryLock(key) {
    90  				array.Append(1)
    91  				gmlock.Unlock(key)
    92  			}
    93  		}()
    94  		time.Sleep(100 * time.Millisecond)
    95  		gtest.Assert(array.Len(), 1)
    96  		time.Sleep(100 * time.Millisecond)
    97  		gtest.Assert(array.Len(), 1)
    98  		time.Sleep(300 * time.Millisecond)
    99  		gtest.Assert(array.Len(), 2)
   100  	})
   101  
   102  }
   103  
   104  func Test_Locker_LockFunc(t *testing.T) {
   105  	//no expire
   106  	gtest.Case(t, func() {
   107  		key := "testLockFunc"
   108  		array := garray.New()
   109  		go func() {
   110  			gmlock.LockFunc(key, func() {
   111  				array.Append(1)
   112  				time.Sleep(300 * time.Millisecond)
   113  			}) //
   114  		}()
   115  		go func() {
   116  			time.Sleep(100 * time.Millisecond)
   117  			gmlock.LockFunc(key, func() {
   118  				array.Append(1)
   119  			})
   120  		}()
   121  		time.Sleep(100 * time.Millisecond)
   122  		gtest.Assert(array.Len(), 1)
   123  		time.Sleep(100 * time.Millisecond)
   124  		gtest.Assert(array.Len(), 1) //
   125  		time.Sleep(200 * time.Millisecond)
   126  		gtest.Assert(array.Len(), 2)
   127  	})
   128  }
   129  func Test_Locker_TryLockFunc(t *testing.T) {
   130  	//no expire
   131  	gtest.Case(t, func() {
   132  		key := "testTryLockFunc"
   133  		array := garray.New()
   134  		go func() {
   135  			gmlock.TryLockFunc(key, func() {
   136  				array.Append(1)
   137  				time.Sleep(200 * time.Millisecond)
   138  			})
   139  		}()
   140  		go func() {
   141  			time.Sleep(100 * time.Millisecond)
   142  			gmlock.TryLockFunc(key, func() {
   143  				array.Append(1)
   144  			})
   145  		}()
   146  		go func() {
   147  			time.Sleep(300 * time.Millisecond)
   148  			gmlock.TryLockFunc(key, func() {
   149  				array.Append(1)
   150  			})
   151  		}()
   152  		time.Sleep(150 * time.Millisecond)
   153  		gtest.Assert(array.Len(), 1)
   154  		time.Sleep(400 * time.Millisecond)
   155  		gtest.Assert(array.Len(), 2)
   156  	})
   157  }