本文整理汇总了Golang中launchpad/net/juju-core/utils/fslock.NewLock函数的典型用法代码示例。如果您正苦于以下问题:Golang NewLock函数的具体用法?Golang NewLock怎么用?Golang NewLock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewLock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestIsLockHeldTwoLocks
func (s *fslockSuite) TestIsLockHeldTwoLocks(c *C) {
dir := c.MkDir()
lock1, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
lock2, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
err = lock1.Lock("")
c.Assert(err, IsNil)
c.Assert(lock2.IsLockHeld(), Equals, false)
}
示例2: TestMessageAcrossLocks
func (s *fslockSuite) TestMessageAcrossLocks(c *C) {
dir := c.MkDir()
lock1, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
lock2, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
err = lock1.Lock("very busy")
c.Assert(err, IsNil)
c.Assert(lock2.Message(), Equals, "very busy")
}
示例3: TestLockWithTimeoutLocked
func (s *fslockSuite) TestLockWithTimeoutLocked(c *C) {
dir := c.MkDir()
lock1, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
lock2, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
err = lock1.Lock("")
c.Assert(err, IsNil)
err = lock2.LockWithTimeout(10*time.Millisecond, "")
c.Assert(err, Equals, fslock.ErrTimeout)
}
示例4: TestNewLockWithExistingDir
func (s *fslockSuite) TestNewLockWithExistingDir(c *C) {
dir := c.MkDir()
err := os.MkdirAll(dir, 0755)
c.Assert(err, IsNil)
_, err = fslock.NewLock(dir, "special")
c.Assert(err, IsNil)
}
示例5: TestTomb
func (s *fslockSuite) TestTomb(c *C) {
const timeToDie = 200 * time.Millisecond
die := tomb.Tomb{}
dir := c.MkDir()
lock, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
// Just use one lock, and try to lock it twice.
err = lock.Lock("very busy")
c.Assert(err, IsNil)
checkTomb := func() error {
select {
case <-die.Dying():
return tomb.ErrDying
default:
// no-op to fall through to return.
}
return nil
}
go func() {
time.Sleep(timeToDie)
die.Killf("time to die")
}()
err = lock.LockWithFunc("won't happen", checkTomb)
c.Assert(err, Equals, tomb.ErrDying)
c.Assert(lock.Message(), Equals, "very busy")
}
示例6: TestUnlock
func (s *fslockSuite) TestUnlock(c *C) {
dir := c.MkDir()
lock, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
err = lock.Unlock()
c.Assert(err, Equals, fslock.ErrLockNotHeld)
}
示例7: TestLockWithTimeoutUnlocked
func (s *fslockSuite) TestLockWithTimeoutUnlocked(c *C) {
dir := c.MkDir()
lock, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
err = lock.LockWithTimeout(10*time.Millisecond, "")
c.Assert(err, IsNil)
}
示例8: TestNewLockWithExistingFileInPlace
func (s *fslockSuite) TestNewLockWithExistingFileInPlace(c *C) {
dir := c.MkDir()
err := os.MkdirAll(dir, 0755)
c.Assert(err, IsNil)
path := path.Join(dir, "locks")
err = ioutil.WriteFile(path, []byte("foo"), 0644)
c.Assert(err, IsNil)
_, err = fslock.NewLock(path, "special")
c.Assert(err, ErrorMatches, `.* not a directory`)
}
示例9: TestValidNamesLockDir
// This test also happens to test that locks can get created when the parent
// lock directory doesn't exist.
func (s *fslockSuite) TestValidNamesLockDir(c *C) {
for _, name := range []string{
"a",
"longer",
"longer-with.special-characters",
} {
dir := c.MkDir()
_, err := fslock.NewLock(dir, name)
c.Assert(err, IsNil)
}
}
示例10: TestBreakLock
func (s *fslockSuite) TestBreakLock(c *C) {
dir := c.MkDir()
lock1, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
lock2, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
err = lock1.Lock("")
c.Assert(err, IsNil)
err = lock2.BreakLock()
c.Assert(err, IsNil)
c.Assert(lock2.IsLocked(), Equals, false)
// Normally locks are broken due to client crashes, not duration.
err = lock1.Unlock()
c.Assert(err, Equals, fslock.ErrLockNotHeld)
// Breaking a non-existant isn't an error
err = lock2.BreakLock()
c.Assert(err, IsNil)
}
示例11: getUniterLock
// getUniterLock grabs the "uniter-hook-execution" lock and holds it, or an error
func getUniterLock(dataDir, message string) (*fslock.Lock, error) {
// Taken from worker/uniter/uniter.go setupLocks()
lockDir := filepath.Join(dataDir, "locks")
hookLock, err := fslock.NewLock(lockDir, "uniter-hook-execution")
if err != nil {
return nil, err
}
err = hookLock.LockWithTimeout(lockTimeout, message)
if err != nil {
return nil, err
}
return hookLock, nil
}
示例12: TestLockBlocks
func (s *fslockSuite) TestLockBlocks(c *C) {
dir := c.MkDir()
lock1, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
lock2, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
acquired := make(chan struct{})
err = lock1.Lock("")
c.Assert(err, IsNil)
go func() {
lock2.Lock("")
acquired <- struct{}{}
close(acquired)
}()
// Waiting for something not to happen is inherently hard...
select {
case <-acquired:
c.Fatalf("Unexpected lock acquisition")
case <-time.After(coretesting.ShortWait):
// all good
}
err = lock1.Unlock()
c.Assert(err, IsNil)
select {
case <-acquired:
// all good
case <-time.After(coretesting.ShortWait):
c.Fatalf("Expected lock acquisition")
}
c.Assert(lock2.IsLockHeld(), Equals, true)
}
示例13: TestIsLockHeldBasics
func (s *fslockSuite) TestIsLockHeldBasics(c *C) {
dir := c.MkDir()
lock, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
c.Assert(lock.IsLockHeld(), Equals, false)
err = lock.Lock("")
c.Assert(err, IsNil)
c.Assert(lock.IsLockHeld(), Equals, true)
err = lock.Unlock()
c.Assert(err, IsNil)
c.Assert(lock.IsLockHeld(), Equals, false)
}
示例14: TestMessage
func (s *fslockSuite) TestMessage(c *C) {
dir := c.MkDir()
lock, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
c.Assert(lock.Message(), Equals, "")
err = lock.Lock("my message")
c.Assert(err, IsNil)
c.Assert(lock.Message(), Equals, "my message")
// Unlocking removes the message.
err = lock.Unlock()
c.Assert(err, IsNil)
c.Assert(lock.Message(), Equals, "")
}
示例15: TestInitialMessageWhenLocking
func (s *fslockSuite) TestInitialMessageWhenLocking(c *C) {
dir := c.MkDir()
lock, err := fslock.NewLock(dir, "testing")
c.Assert(err, IsNil)
err = lock.Lock("initial message")
c.Assert(err, IsNil)
c.Assert(lock.Message(), Equals, "initial message")
err = lock.Unlock()
c.Assert(err, IsNil)
err = lock.LockWithTimeout(10*time.Millisecond, "initial timeout message")
c.Assert(err, IsNil)
c.Assert(lock.Message(), Equals, "initial timeout message")
}