예제 #1
0
파일: lock_test.go 프로젝트: imoapps/juju
func (s *flockSuite) TestUnlock(c *gc.C) {
	dir := c.MkDir()
	lock, err := filelock.NewLock(dir, "testing")
	c.Assert(err, jc.ErrorIsNil)
	err = lock.Lock("test")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(filelock.IsLocked(lock), jc.IsTrue)

	err = lock.Unlock()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(filelock.IsLocked(lock), jc.IsFalse)
}
예제 #2
0
파일: lock_test.go 프로젝트: imoapps/juju
func (s *flockSuite) TestLockBlocks(c *gc.C) {

	dir := c.MkDir()
	lock1, err := filelock.NewLock(dir, "testing")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(filelock.IsLocked(lock1), jc.IsFalse)
	lock2, err := filelock.NewLock(dir, "testing")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(filelock.IsLocked(lock2), jc.IsFalse)

	acquired := make(chan struct{})
	err = lock1.Lock("")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(filelock.IsLocked(lock1), jc.IsTrue)

	go func() {
		lock2.Lock("")
		c.Assert(filelock.IsLocked(lock2), jc.IsTrue)
		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, jc.ErrorIsNil)
	c.Assert(filelock.IsLocked(lock1), jc.IsFalse)

	select {
	case <-acquired:
		// all good
	case <-time.After(coretesting.LongWait):
		c.Fatalf("Expected lock acquisition")
	}
}