github.com/gogf/gf/v2@v2.7.4/os/gmutex/gmutex_z_unit_mutex_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 gmutex_test
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/gogf/gf/v2/container/garray"
    14  	"github.com/gogf/gf/v2/os/gmutex"
    15  	"github.com/gogf/gf/v2/test/gtest"
    16  )
    17  
    18  func Test_Mutex_Unlock(t *testing.T) {
    19  	gtest.C(t, func(t *gtest.T) {
    20  		mu := gmutex.Mutex{}
    21  		array := garray.New(true)
    22  		go func() {
    23  			mu.LockFunc(func() {
    24  				array.Append(1)
    25  				time.Sleep(300 * time.Millisecond)
    26  			})
    27  		}()
    28  		go func() {
    29  			time.Sleep(100 * time.Millisecond)
    30  			mu.LockFunc(func() {
    31  				array.Append(1)
    32  			})
    33  		}()
    34  		go func() {
    35  			time.Sleep(100 * time.Millisecond)
    36  			mu.LockFunc(func() {
    37  				array.Append(1)
    38  			})
    39  		}()
    40  
    41  		time.Sleep(100 * time.Millisecond)
    42  		t.Assert(array.Len(), 1)
    43  		time.Sleep(400 * time.Millisecond)
    44  		t.Assert(array.Len(), 3)
    45  	})
    46  }
    47  
    48  func Test_Mutex_LockFunc(t *testing.T) {
    49  	gtest.C(t, func(t *gtest.T) {
    50  		mu := gmutex.Mutex{}
    51  		array := garray.New(true)
    52  		go func() {
    53  			mu.LockFunc(func() {
    54  				array.Append(1)
    55  				time.Sleep(300 * time.Millisecond)
    56  			})
    57  		}()
    58  		go func() {
    59  			time.Sleep(100 * time.Millisecond)
    60  			mu.LockFunc(func() {
    61  				array.Append(1)
    62  			})
    63  		}()
    64  		time.Sleep(100 * time.Millisecond)
    65  		t.Assert(array.Len(), 1)
    66  		time.Sleep(100 * time.Millisecond)
    67  		t.Assert(array.Len(), 1)
    68  		time.Sleep(200 * time.Millisecond)
    69  		t.Assert(array.Len(), 2)
    70  	})
    71  }
    72  
    73  func Test_Mutex_TryLockFunc(t *testing.T) {
    74  	gtest.C(t, func(t *gtest.T) {
    75  		mu := gmutex.Mutex{}
    76  		array := garray.New(true)
    77  		go func() {
    78  			mu.LockFunc(func() {
    79  				array.Append(1)
    80  				time.Sleep(300 * time.Millisecond)
    81  			})
    82  		}()
    83  		go func() {
    84  			time.Sleep(100 * time.Millisecond)
    85  			mu.TryLockFunc(func() {
    86  				array.Append(1)
    87  			})
    88  		}()
    89  		go func() {
    90  			time.Sleep(400 * time.Millisecond)
    91  			mu.TryLockFunc(func() {
    92  				array.Append(1)
    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  }