~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/gopkg.in/juju/charmstore.v5-unstable/internal/storetesting/hashtesting/hash.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2015 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
// TODO frankban: remove this package after updating entities in the production
5
 
// db with their SHA256 hash value. Entities are updated by running the
6
 
// cshash256 command.
7
 
 
8
 
package hashtesting // import "gopkg.in/juju/charmstore.v5-unstable/internal/storetesting/hashtesting"
9
 
 
10
 
import (
11
 
        "time"
12
 
 
13
 
        jujutesting "github.com/juju/testing"
14
 
        gc "gopkg.in/check.v1"
15
 
        "gopkg.in/juju/charm.v6-unstable"
16
 
        "gopkg.in/mgo.v2/bson"
17
 
 
18
 
        "gopkg.in/juju/charmstore.v5-unstable/internal/charmstore"
19
 
        "gopkg.in/juju/charmstore.v5-unstable/internal/router"
20
 
)
21
 
 
22
 
func CheckSHA256Laziness(c *gc.C, store *charmstore.Store, id *charm.URL, check func()) {
23
 
        updated := make(chan struct{}, 1)
24
 
 
25
 
        // Patch charmstore.UpdateEntitySHA256 so that we can know whether it has
26
 
        // been called or not.
27
 
        original := charmstore.UpdateEntitySHA256
28
 
        restore := jujutesting.PatchValue(
29
 
                &charmstore.UpdateEntitySHA256,
30
 
                func(store *charmstore.Store, id *router.ResolvedURL, sum256 string) {
31
 
                        original(store, id, sum256)
32
 
                        updated <- struct{}{}
33
 
                })
34
 
        defer restore()
35
 
 
36
 
        // Update the entity removing the SHA256 hash.
37
 
        store.DB.Entities().UpdateId(id, bson.D{{
38
 
                "$set", bson.D{{"blobhash256", ""}},
39
 
        }})
40
 
 
41
 
        // Run the code under test.
42
 
        check()
43
 
 
44
 
        // Ensure the db is updated asynchronously.
45
 
        select {
46
 
        case <-updated:
47
 
        case <-time.After(5 * time.Second):
48
 
                c.Fatalf("timed out waiting for update")
49
 
        }
50
 
 
51
 
        // Run the code under test. again.
52
 
        check()
53
 
 
54
 
        // We should not update the SHA256 the second time.
55
 
        select {
56
 
        case <-updated:
57
 
                c.Fatalf("update called twice")
58
 
        case <-time.After(10 * time.Millisecond):
59
 
        }
60
 
}