コード例 #1
0
func (s *interruptibleStorageSuite) TestInterruptStorageConcurrently(c *gc.C) {
	closer, stor, _ := envtesting.CreateLocalTestStorage(c)
	s.AddCleanup(func(c *gc.C) { closer.Close() })
	reader := &errorReader{
		close: make(chan struct{}),
		wait:  make(chan struct{}),
		err:   fmt.Errorf("read failed"),
	}
	istor := bootstrap.NewInterruptibleStorage(stor, reader.close)
	err := istor.Put("name", reader, 3)
	c.Assert(err, gc.ErrorMatches, ".*: interrupted")
	c.Assert(reader.called, gc.Equals, 0) // reader is blocked
	close(reader.wait)
}
コード例 #2
0
func (s *interruptibleStorageSuite) TestInterruptStorage(c *gc.C) {
	closer, stor, _ := envtesting.CreateLocalTestStorage(c)
	s.AddCleanup(func(c *gc.C) { closer.Close() })
	reader := &errorReader{
		err: fmt.Errorf("read failed"),
	}
	interrupted := make(chan struct{})
	istor := bootstrap.NewInterruptibleStorage(stor, interrupted)

	err := istor.Put("name", reader, 3)
	c.Assert(err, gc.ErrorMatches, ".*: read failed")
	c.Assert(reader.called, gc.Equals, 1)

	// If the channel is already closed, then the
	// underlying reader is never deferred to.
	close(interrupted)
	err = istor.Put("name", reader, 3)
	c.Assert(err, gc.ErrorMatches, ".*: interrupted")
	c.Assert(reader.called, gc.Equals, 1)
}