github.com/tiagovtristao/plz@v13.4.0+incompatible/src/core/lock_test.go (about)

     1  package core
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"syscall"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestAcquireRepoLock(t *testing.T) {
    13  	// Grab the lock
    14  	AcquireRepoLock()
    15  	// Now we should be able to open the file (ie. it exists)
    16  	lockFile, err := os.Open(lockFilePath)
    17  	assert.NoError(t, err)
    18  	defer lockFile.Close()
    19  	assert.Error(t, syscall.Flock(int(lockFile.Fd()), syscall.LOCK_EX|syscall.LOCK_NB))
    20  	// Let it go again
    21  	ReleaseRepoLock()
    22  	// Now we can get it
    23  	assert.NoError(t, syscall.Flock(int(lockFile.Fd()), syscall.LOCK_EX|syscall.LOCK_NB))
    24  	// Let it go so the following tests aren't confused :)
    25  	assert.NoError(t, syscall.Flock(int(lockFile.Fd()), syscall.LOCK_UN))
    26  }
    27  
    28  func TestReadLastOperation(t *testing.T) {
    29  	assert.NoError(t, ioutil.WriteFile(lockFilePath, []byte("op plz"), 0644))
    30  	assert.Equal(t, []string{"op", "plz"}, ReadLastOperationOrDie())
    31  	// Can't really test a failure case because of the "or die" bit :(
    32  }