func (s *BucketSetSuite) TestConsumeMoreThenBurst(c *C) {
	rates := NewRateSet()
	rates.Add(1*time.Second, 10, 20)
	rates.Add(10*time.Second, 50, 100)
	tbs := newTokenBucketSet(rates, s.clock)
	tbs.consume(5)
	c.Assert(tbs.debugState(), Equals, "{1s: 15}, {10s: 95}")
	_, err := tbs.consume(21)
	c.Assert(tbs.debugState(), Equals, "{1s: 15}, {10s: 95}")
	c.Assert(err, NotNil)
}
An attempt to consume more tokens then the smallest bucket capacity results
in error.
package tokenbucket
import (
	"time"
	"github.com/mailgun/timetools"
	. "gopkg.in/check.v1"
)
type BucketSetSuite struct {
	clock *timetools.FreezedTime
}
var _ = Suite(&BucketSetSuite{})
func (s *BucketSetSuite) SetUpSuite(c *C) {
	s.clock = &timetools.FreezedTime{
		CurrentTime: time.Date(2012, 3, 4, 5, 6, 7, 0, time.UTC),
	}
}
func (s *BucketSetSuite) TestLongestPeriod(c *C) {
	rates := NewRateSet()
	rates.Add(1*time.Second, 10, 20)
	rates.Add(7*time.Second, 10, 20)
	rates.Add(5*time.Second, 11, 21)
	tbs := newTokenBucketSet(rates, s.clock)
	c.Assert(tbs.maxPeriod, Equals, 7*time.Second)
}
func (s *BucketSetSuite) TestConsume(c *C) {
	rates := NewRateSet()
	rates.Add(1*time.Second, 10, 20)
	rates.Add(10*time.Second, 20, 50)
	tbs := newTokenBucketSet(rates, s.clock)
	delay, err := tbs.consume(15)
	c.Assert(delay, Equals, time.Duration(0))
	c.Assert(err, IsNil)
	c.Assert(tbs.debugState(), Equals, "{1s: 5}, {10s: 35}")
}
func (s *BucketSetSuite) TestConsumeRefill(c *C) {
	rates := NewRateSet()
	rates.Add(10*time.Second, 10, 20)
	rates.Add(100*time.Second, 20, 50)
	tbs := newTokenBucketSet(rates, s.clock)
	tbs.consume(15)
	c.Assert(tbs.debugState(), Equals, "{10s: 5}, {1m40s: 35}")
	s.clock.Sleep(10 * time.Second)
	delay, err := tbs.consume(0)  
	c.Assert(delay, Equals, time.Duration(0))
	c.Assert(err, IsNil)
	c.Assert(tbs.debugState(), Equals, "{10s: 15}, {1m40s: 37}")
}
func (s *BucketSetSuite) TestConsumeLimitedBy1st(c *C) {
	rates := NewRateSet()
	rates.Add(10*time.Second, 10, 10)
	rates.Add(100*time.Second, 20, 20)
	tbs := newTokenBucketSet(rates, s.clock)
	tbs.consume(5)
	c.Assert(tbs.debugState(), Equals, "{10s: 5}, {1m40s: 15}")
	delay, err := tbs.consume(10)
	c.Assert(delay, Equals, 5*time.Second)
	c.Assert(err, IsNil)
	c.Assert(tbs.debugState(), Equals, "{10s: 5}, {1m40s: 15}")
}
func (s *BucketSetSuite) TestConsumeLimitedBy2st(c *C) {
	rates := NewRateSet()
	rates.Add(10*time.Second, 10, 10)
	rates.Add(100*time.Second, 20, 20)
	tbs := newTokenBucketSet(rates, s.clock)
	tbs.consume(10)
	s.clock.Sleep(10 * time.Second)
	tbs.consume(10)
	s.clock.Sleep(5 * time.Second)
	tbs.consume(0)
	c.Assert(tbs.debugState(), Equals, "{10s: 5}, {1m40s: 3}")
	delay, err := tbs.consume(10)
	c.Assert(delay, Equals, 7*(5*time.Second))
	c.Assert(err, IsNil)
	c.Assert(tbs.debugState(), Equals, "{10s: 5}, {1m40s: 3}")
}