2015-12-02 18:51:32 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 Gravitational, Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2015-12-03 09:26:34 +00:00
|
|
|
package limiter
|
2015-12-02 18:51:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/gravitational/teleport/Godeps/_workspace/src/gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
2015-12-03 09:26:34 +00:00
|
|
|
func TestLimiter(t *testing.T) { TestingT(t) }
|
2015-12-02 18:51:32 +00:00
|
|
|
|
|
|
|
type LimiterSuite struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Suite(&LimiterSuite{})
|
|
|
|
|
|
|
|
func (s *LimiterSuite) SetUpSuite(c *C) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LimiterSuite) SetUpTest(c *C) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LimiterSuite) TearDownTest(c *C) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LimiterSuite) TestConnectionsLimiter(c *C) {
|
2015-12-03 09:26:34 +00:00
|
|
|
limiter, err := NewLimiter(
|
|
|
|
LimiterConfig{
|
2015-12-02 18:51:32 +00:00
|
|
|
MaxConnections: 0,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
c.Assert(limiter.AcquireConnection("token1"), IsNil)
|
|
|
|
}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
c.Assert(limiter.AcquireConnection("token2"), IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
limiter.ReleaseConnection("token1")
|
|
|
|
}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
limiter.ReleaseConnection("token2")
|
|
|
|
}
|
|
|
|
|
2015-12-03 09:26:34 +00:00
|
|
|
limiter, err = NewLimiter(
|
|
|
|
LimiterConfig{
|
2015-12-02 18:51:32 +00:00
|
|
|
MaxConnections: 5,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
c.Assert(limiter.AcquireConnection("token1"), IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
c.Assert(limiter.AcquireConnection("token2"), IsNil)
|
|
|
|
}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
c.Assert(limiter.AcquireConnection("token2"), NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
limiter.ReleaseConnection("token1")
|
|
|
|
c.Assert(limiter.AcquireConnection("token1"), IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
limiter.ReleaseConnection("token2")
|
|
|
|
}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
c.Assert(limiter.AcquireConnection("token2"), IsNil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 09:26:34 +00:00
|
|
|
func (s *LimiterSuite) TestRateLimiter(c *C) {
|
|
|
|
limiter, err := NewLimiter(
|
|
|
|
LimiterConfig{
|
2015-12-02 18:51:32 +00:00
|
|
|
Rates: []Rate{
|
|
|
|
Rate{
|
|
|
|
Period: 10 * time.Millisecond,
|
|
|
|
Average: 10,
|
|
|
|
Burst: 20,
|
|
|
|
},
|
|
|
|
Rate{
|
|
|
|
Period: 40 * time.Millisecond,
|
|
|
|
Average: 10,
|
|
|
|
Burst: 40,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-12-03 09:26:34 +00:00
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
c.Assert(limiter.RegisterRequest("token1"), IsNil)
|
2015-12-02 18:51:32 +00:00
|
|
|
}
|
2015-12-03 09:26:34 +00:00
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
c.Assert(limiter.RegisterRequest("token2"), IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Assert(limiter.RegisterRequest("token1"), NotNil)
|
2015-12-02 18:51:32 +00:00
|
|
|
|
|
|
|
time.Sleep(10010 * time.Microsecond)
|
|
|
|
for i := 0; i < 10; i++ {
|
2015-12-03 09:26:34 +00:00
|
|
|
c.Assert(limiter.RegisterRequest("token1"), IsNil)
|
2015-12-02 18:51:32 +00:00
|
|
|
}
|
2015-12-03 09:26:34 +00:00
|
|
|
c.Assert(limiter.RegisterRequest("token1"), NotNil)
|
2015-12-02 18:51:32 +00:00
|
|
|
|
|
|
|
time.Sleep(10010 * time.Microsecond)
|
|
|
|
for i := 0; i < 10; i++ {
|
2015-12-03 09:26:34 +00:00
|
|
|
c.Assert(limiter.RegisterRequest("token1"), IsNil)
|
2015-12-02 18:51:32 +00:00
|
|
|
}
|
2015-12-03 09:26:34 +00:00
|
|
|
c.Assert(limiter.RegisterRequest("token1"), NotNil)
|
2015-12-02 18:51:32 +00:00
|
|
|
|
|
|
|
time.Sleep(10010 * time.Microsecond)
|
|
|
|
// the second rate is full
|
2015-12-03 09:26:34 +00:00
|
|
|
err = nil
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
err = limiter.RegisterRequest("token1")
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Assert(err, NotNil)
|
2015-12-02 18:51:32 +00:00
|
|
|
|
|
|
|
time.Sleep(10010 * time.Microsecond)
|
|
|
|
// Now the second rate have free space
|
2015-12-03 09:26:34 +00:00
|
|
|
c.Assert(limiter.RegisterRequest("token1"), IsNil)
|
|
|
|
err = nil
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
err = limiter.RegisterRequest("token1")
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Assert(err, NotNil)
|
2015-12-02 18:51:32 +00:00
|
|
|
}
|