Skip to content

Commit

Permalink
bakery: tests for MemStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
rogpeppe committed Sep 21, 2014
1 parent dfa5ee0 commit 8e90013
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
4 changes: 0 additions & 4 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ all:

macaroon:

- Implement encoding.BinaryMarshaler
and encoding.BinaryUnmarshaler on Macaroon.
These should be compatible with libmacaroons.

- change all signature calculations to correspond exactly
with libmacaroons.
11 changes: 11 additions & 0 deletions bakery/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bakery_test

import (
"testing"

gc "gopkg.in/check.v1"
)

func TestPackage(t *testing.T) {
gc.TestingT(t)
}
62 changes: 62 additions & 0 deletions bakery/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package bakery_test

import (
"fmt"

gc "gopkg.in/check.v1"

"github.com/rogpeppe/macaroon/bakery"
)

type StorageSuite struct{}

var _ = gc.Suite(&StorageSuite{})

func (*StorageSuite) TestMemStorage(c *gc.C) {
store := bakery.NewMemStorage()
err := store.Put("foo", "bar")
c.Assert(err, gc.IsNil)
item, err := store.Get("foo")
c.Assert(err, gc.IsNil)
c.Assert(item, gc.Equals, "bar")

err = store.Put("bletch", "blat")
c.Assert(err, gc.IsNil)
item, err = store.Get("bletch")
c.Assert(err, gc.IsNil)
c.Assert(item, gc.Equals, "blat")

item, err = store.Get("nothing")
c.Assert(err, gc.Equals, bakery.ErrNotFound)
c.Assert(item, gc.Equals, "")

err = store.Del("bletch")
c.Assert(err, gc.IsNil)

item, err = store.Get("bletch")
c.Assert(err, gc.Equals, bakery.ErrNotFound)
c.Assert(item, gc.Equals, "")
}

func (*StorageSuite) TestConcurrentMemStorage(c *gc.C) {
// If locking is not done right, this test will
// definitely trigger the race detector.
done := make(chan struct{})
store := bakery.NewMemStorage()
for i := 0; i < 3; i++ {
i := i
go func() {
k := fmt.Sprint(i)
err := store.Put(k, k)
c.Check(err, gc.IsNil)
v, err := store.Get(k)
c.Check(v, gc.Equals, k)
err = store.Del(k)
c.Check(err, gc.IsNil)
done <- struct{}{}
}()
}
for i := 0; i < 3; i++ {
<-done
}
}

0 comments on commit 8e90013

Please sign in to comment.