go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/filelock/mutex_test.go (about)

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