Example #1
0
func TestStatsGetBucket(t *testing.T) {
	a := NewMockAdministrable()

	testNamespace := config.NewDefaultNamespaceConfig("test")
	a.Configs().Namespaces["test"] = testNamespace
	bucket := config.NewDefaultBucketConfig("bucket")
	testNamespace.Buckets["bucket"] = bucket

	bucketResponse := make(map[string]*stats.BucketScores)
	doStatsRequest(t, a, &bucketResponse, "GET", "/api/stats/test/bucket", "")

	bucketScores, ok := bucketResponse["bucket"]

	if !ok || bucketScores.Hits != 0 || bucketScores.Misses != 0 {
		t.Errorf("Received %+v instead of [bucket=[Hits=0, Misses=0]]", bucketResponse)
	}

	a = NewMockErrorAdministrable()
	a.Configs().Namespaces["test"] = testNamespace
	testNamespace.Buckets["bucket"] = bucket

	jsonResponse := make(map[string]string)
	doStatsRequest(t, a, &jsonResponse, "GET", "/api/stats/test/bucket", "")

	if jsonResponse["description"] != "No stats listener configured" {
		t.Errorf("Received \"%s\" from %+v instead of \"No stats listener configured\"",
			jsonResponse["description"], jsonResponse)
	}
}
Example #2
0
func TestStatsGet(t *testing.T) {
	a := NewMockAdministrable()

	testNamespace := config.NewDefaultNamespaceConfig("test")
	a.Configs().Namespaces["test"] = testNamespace

	nsResponse := &bucketStats{}
	doStatsRequest(t, a, nsResponse, "GET", "/api/stats/test", "")

	if nsResponse.Ns != "test" || len(nsResponse.Hits) != 0 || len(nsResponse.Misses) != 0 {
		t.Errorf("Received %+v instead of [Ns=test, Hits=[], Misses=[]]", nsResponse)
	}

	a = NewMockErrorAdministrable()
	a.Configs().Namespaces["test"] = testNamespace

	jsonResponse := make(map[string]string)

	doStatsRequest(t, a, &jsonResponse, "GET", "/api/stats/test", "")

	if jsonResponse["description"] != "No stats listener configured" {
		t.Errorf("Received \"%s\" from %+v instead of \"No stats listener configured\"",
			jsonResponse["description"], jsonResponse)
	}
}
Example #3
0
func main() {
	cfg := config.NewDefaultServiceConfig()
	ns := config.NewDefaultNamespaceConfig("test.namespace")
	ns.DynamicBucketTemplate = config.NewDefaultBucketConfig(config.DynamicBucketTemplateName)
	ns.DynamicBucketTemplate.Size = 100000000000
	ns.DynamicBucketTemplate.FillRate = 100000000
	b := config.NewDefaultBucketConfig("xyz")
	config.AddBucket(ns, b)
	config.AddNamespace(cfg, ns)

	ns = config.NewDefaultNamespaceConfig("test.namespace2")
	ns.DefaultBucket = config.NewDefaultBucketConfig(config.DefaultBucketName)
	b = config.NewDefaultBucketConfig("xyz")
	config.AddBucket(ns, b)
	config.AddNamespace(cfg, ns)

	server := quotaservice.New(memory.NewBucketFactory(),
		config.NewMemoryConfig(cfg),
		grpc.New(GRPC_SERVER))
	server.SetStatsListener(stats.NewMemoryStatsListener())
	server.Start()

	// Serve Admin Console
	logging.Printf("Starting admin server on %v\n", ADMIN_SERVER)
	sm := http.NewServeMux()
	server.ServeAdminConsole(sm, "admin/public", true)
	go func() {
		http.ListenAndServe(ADMIN_SERVER, sm)
	}()

	// Block until SIGTERM, SIGKILL or SIGINT
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT)

	var shutdown sync.WaitGroup
	shutdown.Add(1)

	go func() {
		<-sigs
		shutdown.Done()
	}()

	shutdown.Wait()
	server.Stop()
}
Example #4
0
func setUp() {
	cfg := config.NewDefaultServiceConfig()
	cfg.GlobalDefaultBucket = config.NewDefaultBucketConfig(config.DefaultBucketName)

	// Namespace "dyn"
	ns := config.NewDefaultNamespaceConfig("dyn")
	tpl := config.NewDefaultBucketConfig("")
	tpl.MaxTokensPerRequest = 5
	tpl.MaxIdleMillis = -1
	config.SetDynamicBucketTemplate(ns, tpl)
	ns.MaxDynamicBuckets = 2
	config.AddNamespace(cfg, ns)

	// Namespace "dyn_gc"
	ns = config.NewDefaultNamespaceConfig("dyn_gc")
	tpl = config.NewDefaultBucketConfig("")
	tpl.MaxTokensPerRequest = 5
	tpl.MaxIdleMillis = 100
	config.SetDynamicBucketTemplate(ns, tpl)
	ns.MaxDynamicBuckets = 3
	config.AddNamespace(cfg, ns)

	// Namespace "nodyn"
	ns = config.NewDefaultNamespaceConfig("nodyn")
	b := config.NewDefaultBucketConfig("b")
	b.MaxTokensPerRequest = 10
	config.AddBucket(ns, b)
	config.AddNamespace(cfg, ns)

	mbf = &MockBucketFactory{}
	me := &MockEndpoint{}
	p := config.NewMemoryConfig(cfg)
	s = New(mbf, p, me)
	eventsChan = make(chan events.Event, 100)
	s.SetListener(func(e events.Event) {
		eventsChan <- e
	}, 100)
	s.Start()
	qs = me.QuotaService
	// EVENTS_BUCKET_CREATED event
	_ = <-eventsChan
}
func TestBucketsGetBucketNotFound(t *testing.T) {
	a := NewMockAdministrable()
	testNamespace := config.NewDefaultNamespaceConfig("test")
	a.Configs().Namespaces["test"] = testNamespace

	jsonResponse := make(map[string]string)
	doBucketsRequest(t, a, &jsonResponse, "GET", "/api/test/bucket", "")

	if jsonResponse["description"] != "Unable to locate bucket bucket in namespace test" {
		t.Errorf("Received \"%s\" from %+v instead of not found", jsonResponse["description"], jsonResponse)
	}
}
func TestNamespacesGet(t *testing.T) {
	a := NewMockAdministrable()

	testNamespace := config.NewDefaultNamespaceConfig("test")
	a.Configs().Namespaces["test"] = testNamespace

	configResponse := &pb.NamespaceConfig{}
	doNamespacesRequest(t, a, configResponse, "GET", "/api/test", "")

	if testNamespace.Name != configResponse.Name {
		t.Errorf("Received \"%+v\" but was expecting \"%+v\"", configResponse, testNamespace)
	}
}
Example #7
0
func setUp() {
	// Start a QuotaService server.
	cfg := config.NewDefaultServiceConfig()
	cfg.GlobalDefaultBucket = config.NewDefaultBucketConfig(config.DefaultBucketName)
	nsc := config.NewDefaultNamespaceConfig("delaying")
	bc := config.NewDefaultBucketConfig("delaying")
	bc.Size = 1 // Very small.
	bc.FillRate = 100
	config.AddBucket(nsc, bc)
	config.AddNamespace(cfg, nsc)

	server = quotaservice.New(memory.NewBucketFactory(), config.NewMemoryConfig(cfg), qsgrpc.New(target))
	server.Start()
}
func TestBucketsGet(t *testing.T) {
	a := NewMockAdministrable()

	testNamespace := config.NewDefaultNamespaceConfig("test")
	a.Configs().Namespaces["test"] = testNamespace
	bucket := config.NewDefaultBucketConfig("bucket")
	testNamespace.Buckets["bucket"] = bucket

	configResponse := &pb.BucketConfig{}
	doBucketsRequest(t, a, configResponse, "GET", "/api/test/bucket", "")

	if *bucket != *configResponse {
		t.Errorf("Received \"%+v\" but was expecting \"%+v\"", configResponse, bucket)
	}
}
Example #9
0
func TestUnmarshalNamespaceConfig(t *testing.T) {
	n := config.NewDefaultNamespaceConfig("Blah Namespace 123")
	n.MaxDynamicBuckets = 8000
	config.SetDynamicBucketTemplate(n, config.NewDefaultBucketConfig(""))

	c1 := config.NewDefaultBucketConfig("Blah 123")
	c1.FillRate = 12345
	c1.MaxDebtMillis = 54321
	c1.MaxIdleMillis = 67890
	c1.MaxTokensPerRequest = 9876
	c1.Size = 50000

	c2 := config.NewDefaultBucketConfig("Blah 456")
	c2.FillRate = 123450
	c2.MaxDebtMillis = 543210
	c2.MaxIdleMillis = 678900
	c2.MaxTokensPerRequest = 98760
	c2.Size = 5000

	c3 := config.NewDefaultBucketConfig("Blah 789")
	c3.FillRate = 1234500
	c3.MaxDebtMillis = 5432100
	c3.MaxIdleMillis = 6789000
	c3.MaxTokensPerRequest = 987600
	c3.Size = 500

	config.AddBucket(n, c1)
	config.AddBucket(n, c2)
	config.AddBucket(n, c3)

	b, e := json.Marshal(n)
	if e != nil {
		t.Fatal("Unable to JSONify proto", e)
	}

	reRead, err := getNamespaceConfig(bytes.NewReader(b))
	if err != nil {
		t.Fatal("Unable to unmarshal JSON", err)
	}

	if !reflect.DeepEqual(n, reRead) {
		t.Fatalf("Two representations aren't equal: %+v != %+v", n, reRead)
	}
}
func TestGC(t *testing.T, factory quotaservice.BucketFactory, impl string) {
	cfg := config.NewDefaultServiceConfig()
	nsCfg := config.NewDefaultNamespaceConfig("n")
	tpl := config.NewDefaultBucketConfig("")
	// Times out every 250 millis.
	tpl.MaxIdleMillis = 250
	config.SetDynamicBucketTemplate(nsCfg, tpl)
	config.AddNamespace(cfg, nsCfg)

	eventsEmitter := &quotaservice.MockEmitter{Events: make(chan events.Event, 100)}
	container := quotaservice.NewBucketContainer(cfg, factory, eventsEmitter)

	// No GC should happen here as long as we are in use.
	for i := 0; i < 10; i++ {
		for j := 0; j < 10; j++ {
			bName := strconv.Itoa(j)
			b, _ := container.FindBucket("n", bName)
			if b == nil {
				t.Fatalf("Failed looking for bucket %v on impl %v", bName, impl)
			}

			// Check that the bucket hasn't been GC'd
			if !container.Exists("n", bName) {
				t.Fatalf("Bucket %v was GC'd when it shouldn't have on impl %v", bName, impl)
			}
		}
	}

	bucketNames := make([]string, 10)
	for i := 0; i < 10; i++ {
		bucketNames[i] = strconv.Itoa(i)
	}

	waitForGC(eventsEmitter.Events, "n", bucketNames)

	for _, bName := range bucketNames {
		// Check that the bucket has been GC'd
		if container.Exists("n", bName) {
			t.Fatalf("Bucket %v wasn't GC'd when it should have on impl %v", bName, impl)
		}
	}
}
Example #11
0
import (
	"strconv"
	"testing"

	"github.com/maniksurtani/quotaservice/config"

	pbconfig "github.com/maniksurtani/quotaservice/protos/config"
)

var cfg = func() *pbconfig.ServiceConfig {
	c := config.NewDefaultServiceConfig()
	c.GlobalDefaultBucket = config.NewDefaultBucketConfig(config.DefaultBucketName)

	// Namespace "x"
	ns := config.NewDefaultNamespaceConfig("x")
	ns.DefaultBucket = config.NewDefaultBucketConfig(config.DefaultBucketName)
	config.AddBucket(ns, config.NewDefaultBucketConfig("a"))
	config.AddNamespace(c, ns)

	// Namespace "y"
	ns = config.NewDefaultNamespaceConfig("y")
	ns.DynamicBucketTemplate = config.NewDefaultBucketConfig(config.DefaultBucketName)
	config.AddBucket(ns, config.NewDefaultBucketConfig("y"))
	config.AddNamespace(c, ns)

	// Namespace "z"
	ns = config.NewDefaultNamespaceConfig("z")
	ns.DynamicBucketTemplate = config.NewDefaultBucketConfig(config.DefaultBucketName)
	ns.MaxDynamicBuckets = 5
	config.AddBucket(ns, config.NewDefaultBucketConfig("a"))