コード例 #1
0
ファイル: fslock_test.go プロジェクト: dooferlad/juju-utils
func (s *fslockSuite) TestTomb(c *gc.C) {
	const timeToDie = 200 * time.Millisecond
	die := tomb.Tomb{}

	dir := c.MkDir()
	lock, err := fslock.NewLock(dir, "testing", s.lockConfig)
	c.Assert(err, gc.IsNil)
	// Just use one lock, and try to lock it twice.
	err = lock.Lock("very busy")
	c.Assert(err, gc.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(errors.Cause(err), gc.Equals, tomb.ErrDying)
	msg, err := lock.Message()
	c.Assert(err, gc.IsNil)
	c.Assert(msg, gc.Equals, "very busy")
}