Ejemplo n.º 1
0
func (s *flockSuite) TestUnlock(c *gc.C) {
	dir := c.MkDir()
	lock, err := container.NewLock(dir, "testing")
	c.Assert(err, jc.ErrorIsNil)
	err = lock.Lock("test")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(container.IsLocked(lock), jc.IsTrue)

	err = lock.Unlock()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(container.IsLocked(lock), jc.IsFalse)
}
Ejemplo n.º 2
0
func (s *flockSuite) TestLockBlocks(c *gc.C) {

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

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

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

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