~james-page/ubuntu/wily/juju-core/mir-fixes

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/environs/bootstrap/interruptiblestorage_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-03-28 08:58:42 UTC
  • mfrom: (1.1.21)
  • Revision ID: package-import@ubuntu.com-20140328085842-cyzrgc120bdfxwj0
Tags: 1.17.7-0ubuntu1
* New upstream point release, including fixes for:
  - no debug log with all providers on Ubuntu 14.04 (LP: #1294776).
* d/control: Add cpu-checker dependency to juju-local (LP: #1297077).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package bootstrap_test
 
5
 
 
6
import (
 
7
        "fmt"
 
8
 
 
9
        gc "launchpad.net/gocheck"
 
10
 
 
11
        "launchpad.net/juju-core/environs/bootstrap"
 
12
        envtesting "launchpad.net/juju-core/environs/testing"
 
13
        "launchpad.net/juju-core/testing/testbase"
 
14
)
 
15
 
 
16
type interruptibleStorageSuite struct {
 
17
        testbase.LoggingSuite
 
18
}
 
19
 
 
20
var _ = gc.Suite(&interruptibleStorageSuite{})
 
21
 
 
22
type errorReader struct {
 
23
        close  chan struct{}
 
24
        wait   chan struct{}
 
25
        called int
 
26
        err    error
 
27
}
 
28
 
 
29
func (r *errorReader) Read(buf []byte) (int, error) {
 
30
        if r.close != nil {
 
31
                close(r.close)
 
32
        }
 
33
        if r.wait != nil {
 
34
                <-r.wait
 
35
        }
 
36
        r.called++
 
37
        return 0, r.err
 
38
}
 
39
 
 
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"),
 
45
        }
 
46
        interrupted := make(chan struct{})
 
47
        istor := bootstrap.NewInterruptibleStorage(stor, interrupted)
 
48
 
 
49
        err := istor.Put("name", reader, 3)
 
50
        c.Assert(err, gc.ErrorMatches, ".*: read failed")
 
51
        c.Assert(reader.called, gc.Equals, 1)
 
52
 
 
53
        // If the channel is already closed, then the
 
54
        // underlying reader is never deferred to.
 
55
        close(interrupted)
 
56
        err = istor.Put("name", reader, 3)
 
57
        c.Assert(err, gc.ErrorMatches, ".*: interrupted")
 
58
        c.Assert(reader.called, gc.Equals, 1)
 
59
}
 
60
 
 
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"),
 
68
        }
 
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
 
73
        close(reader.wait)
 
74
}