func TestIsolation(t *testing.T) {
	handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte("hello"))
	})
	rates := NewRateSet()
	err := rates.Add(time.Second, 1, 1)
	require.NoError(t, err)
	clock := testutils.GetClock()
	l, err := New(handler, headerLimit, rates, Clock(clock))
	require.NoError(t, err)
	srv := httptest.NewServer(l)
	defer srv.Close()
	re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
	require.NoError(t, err)
	assert.Equal(t, http.StatusOK, re.StatusCode)
	re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
	require.NoError(t, err)
	assert.Equal(t, 429, re.StatusCode)
	re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "b"))
	require.NoError(t, err)
	assert.Equal(t, http.StatusOK, re.StatusCode)
}
Make sure rates from different ips are controlled separately
package ratelimit
import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/vulcand/oxy/testutils"
	"github.com/vulcand/oxy/utils"
)
func TestRateSetAdd(t *testing.T) {
	rs := NewRateSet()
	err := rs.Add(0, 1, 1)
	require.Error(t, err)
	err = rs.Add(time.Second, 0, 1)
	require.Error(t, err)
	err = rs.Add(time.Second, 1, 0)
	require.Error(t, err)
	err = rs.Add(time.Second, 1, 1)
	require.NoError(t, err)
	assert.Equal(t, fmt.Sprint(rs), "map[1s:rate(1/1s, burst=1)]")
}
func TestHitLimit(t *testing.T) {
	handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte("hello"))
	})
	rates := NewRateSet()
	err := rates.Add(time.Second, 1, 1)
	require.NoError(t, err)
	clock := testutils.GetClock()
	l, err := New(handler, headerLimit, rates, Clock(clock))
	require.NoError(t, err)
	srv := httptest.NewServer(l)
	defer srv.Close()
	re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
	require.NoError(t, err)
	assert.Equal(t, http.StatusOK, re.StatusCode)
	re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
	require.NoError(t, err)
	assert.Equal(t, 429, re.StatusCode)
	clock.Sleep(time.Second)
	re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
	require.NoError(t, err)
	assert.Equal(t, http.StatusOK, re.StatusCode)
}
func TestFailure(t *testing.T) {
	handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte("hello"))
	})
	rates := NewRateSet()
	err := rates.Add(time.Second, 1, 1)
	require.NoError(t, err)
	clock := testutils.GetClock()
	l, err := New(handler, faultyExtract, rates, Clock(clock))
	require.NoError(t, err)
	srv := httptest.NewServer(l)
	defer srv.Close()
	re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
	require.NoError(t, err)
	assert.Equal(t, http.StatusInternalServerError, re.StatusCode)
}