1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
gc "launchpad.net/gocheck"
11
"launchpad.net/juju-core/environs/bootstrap"
12
envtesting "launchpad.net/juju-core/environs/testing"
13
"launchpad.net/juju-core/testing/testbase"
16
type interruptibleStorageSuite struct {
20
var _ = gc.Suite(&interruptibleStorageSuite{})
22
type errorReader struct {
29
func (r *errorReader) Read(buf []byte) (int, error) {
40
func (s *interruptibleStorageSuite) TestInterruptStorage(c *gc.C) {
41
closer, stor, _ := envtesting.CreateLocalTestStorage(c)
42
s.AddCleanup(func(c *gc.C) { closer.Close() })
43
reader := &errorReader{
44
err: fmt.Errorf("read failed"),
46
interrupted := make(chan struct{})
47
istor := bootstrap.NewInterruptibleStorage(stor, interrupted)
49
err := istor.Put("name", reader, 3)
50
c.Assert(err, gc.ErrorMatches, ".*: read failed")
51
c.Assert(reader.called, gc.Equals, 1)
53
// If the channel is already closed, then the
54
// underlying reader is never deferred to.
56
err = istor.Put("name", reader, 3)
57
c.Assert(err, gc.ErrorMatches, ".*: interrupted")
58
c.Assert(reader.called, gc.Equals, 1)
61
func (s *interruptibleStorageSuite) TestInterruptStorageConcurrently(c *gc.C) {
62
closer, stor, _ := envtesting.CreateLocalTestStorage(c)
63
s.AddCleanup(func(c *gc.C) { closer.Close() })
64
reader := &errorReader{
65
close: make(chan struct{}),
66
wait: make(chan struct{}),
67
err: fmt.Errorf("read failed"),
69
istor := bootstrap.NewInterruptibleStorage(stor, reader.close)
70
err := istor.Put("name", reader, 3)
71
c.Assert(err, gc.ErrorMatches, ".*: interrupted")
72
c.Assert(reader.called, gc.Equals, 0) // reader is blocked