github.com/blend/go-sdk@v1.20220411.3/filelock/mutex_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package filelock_test
     9  
    10  import (
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  	"github.com/blend/go-sdk/filelock"
    17  	"github.com/blend/go-sdk/uuid"
    18  )
    19  
    20  func Test_Mutex_RLock(t *testing.T) {
    21  	its := assert.New(t)
    22  
    23  	tempFilePath := filepath.Join(os.TempDir(), uuid.V4().String()+".lock")
    24  	mu := filelock.MutexAt(tempFilePath)
    25  
    26  	unlock, err := mu.RLock()
    27  	its.Nil(err)
    28  
    29  	stat, err := os.Stat(tempFilePath)
    30  	its.Nil(err)
    31  	its.NotNil(stat)
    32  	its.False(stat.IsDir())
    33  
    34  	unlock()
    35  
    36  	stat, err = os.Stat(tempFilePath)
    37  	its.Nil(err)
    38  	its.NotNil(stat)
    39  	its.False(stat.IsDir())
    40  }
    41  
    42  func Test_Mutex_Lock(t *testing.T) {
    43  	its := assert.New(t)
    44  
    45  	tempFilePath := filepath.Join(os.TempDir(), uuid.V4().String()+".lock")
    46  	mu := filelock.MutexAt(tempFilePath)
    47  
    48  	unlock, err := mu.Lock()
    49  	its.Nil(err)
    50  
    51  	stat, err := os.Stat(tempFilePath)
    52  	its.Nil(err)
    53  	its.NotNil(stat)
    54  	its.False(stat.IsDir())
    55  
    56  	unlock()
    57  
    58  	stat, err = os.Stat(tempFilePath)
    59  	its.Nil(err)
    60  	its.NotNil(stat)
    61  	its.False(stat.IsDir())
    62  }