github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/flock/flock_test.go (about)

     1  package flock
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"os"
     6  	"path/filepath"
     7  	"sync"
     8  	"sync/atomic"
     9  	"testing"
    10  )
    11  
    12  // don`t execute the test many times(>5) at once, otherwise you may get an "too many open files" err.
    13  func TestAcquireFileLock(t *testing.T) {
    14  	testFn := func(readOnly bool, times int, actual int) {
    15  		path, err := filepath.Abs(filepath.Join("/tmp", "flock-test"))
    16  		assert.Nil(t, err)
    17  		err = os.MkdirAll(path, os.ModePerm)
    18  		assert.Nil(t, err)
    19  
    20  		var count uint32
    21  		var flock *FileLockGuard
    22  
    23  		defer func() {
    24  			if flock != nil {
    25  				_ = flock.Release()
    26  			}
    27  			if err = os.RemoveAll(path); err != nil {
    28  				t.Error(err)
    29  			}
    30  		}()
    31  
    32  		wg := &sync.WaitGroup{}
    33  		wg.Add(times)
    34  		for i := 0; i < times; i++ {
    35  			go func() {
    36  				defer wg.Done()
    37  				lock, err := AcquireFileLock(filepath.Join(path, "FLOCK"), readOnly)
    38  				if err != nil {
    39  					atomic.AddUint32(&count, 1)
    40  				} else {
    41  					flock = lock
    42  				}
    43  				if readOnly && times > 1 && lock != nil {
    44  					_ = lock.Release()
    45  				}
    46  			}()
    47  		}
    48  		wg.Wait()
    49  		assert.Equal(t, count, uint32(actual))
    50  	}
    51  
    52  	t.Run("exclusive-1", func(t *testing.T) {
    53  		testFn(false, 1, 0)
    54  	})
    55  
    56  	t.Run("exclusive-2", func(t *testing.T) {
    57  		testFn(false, 10, 9)
    58  	})
    59  
    60  	t.Run("exclusive-3", func(t *testing.T) {
    61  		testFn(false, 15, 14)
    62  	})
    63  
    64  	t.Run("shared-1", func(t *testing.T) {
    65  		testFn(true, 1, 0)
    66  	})
    67  
    68  	t.Run("shared-2", func(t *testing.T) {
    69  		testFn(true, 15, 0)
    70  	})
    71  }
    72  
    73  func TestAcquireFileLock_NotExist(t *testing.T) {
    74  	path, err := filepath.Abs(filepath.Join("/tmp", "flock", "test"))
    75  	assert.Nil(t, err)
    76  	_, err = AcquireFileLock(path+string(os.PathSeparator)+"FLOCK", false)
    77  	assert.NotNil(t, err)
    78  }
    79  
    80  func TestFileLockGuard_Release(t *testing.T) {
    81  	path, err := filepath.Abs(filepath.Join("/tmp", "flock-test"))
    82  	assert.Nil(t, err)
    83  	err = os.MkdirAll(path, os.ModePerm)
    84  	assert.Nil(t, err)
    85  
    86  	assert.Nil(t, err)
    87  	defer func() {
    88  		_ = os.RemoveAll(path)
    89  	}()
    90  
    91  	lock, err := AcquireFileLock(filepath.Join(path, "FLOCK"), false)
    92  	assert.Nil(t, err)
    93  	err = lock.Release()
    94  	assert.Nil(t, err)
    95  }
    96  
    97  func TestSyncDir(t *testing.T) {
    98  	path, err := filepath.Abs(filepath.Join("/tmp", "flock-test"))
    99  	assert.Nil(t, err)
   100  	err = os.MkdirAll(path, os.ModePerm)
   101  	assert.Nil(t, err)
   102  
   103  	file, err := os.OpenFile(filepath.Join(path, "test.txt"), os.O_CREATE, 0644)
   104  	assert.Nil(t, err)
   105  	defer func() {
   106  		_ = file.Close()
   107  		_ = os.RemoveAll(path)
   108  	}()
   109  	err = SyncDir(path)
   110  	assert.Nil(t, err)
   111  }