Esempio n. 1
0
func TestUpdateConfig(t *testing.T) {
	s := New(&MockBucketFactory{}, config.NewMemoryConfigPersister(), &MockEndpoint{}).(*server)

	originalConfig := config.NewDefaultServiceConfig()
	originalConfig.Version = 2
	originalConfig.Date = time.Now().Unix() - 10
	s.createBucketContainer(originalConfig)

	s.Start()
	defer s.Stop()

	newConfig := config.NewDefaultServiceConfig()

	if err := s.UpdateConfig(newConfig, "test"); err != nil {
		t.Fatal("Error when updating config", err)
	}

	start := time.Now()

	for s.Configs() == originalConfig {
		if time.Since(start) > time.Second {
			t.Fatal("Timeout waiting for config to change!")
		}

		time.Sleep(time.Millisecond * 5)
	}

	cfg := s.Configs()

	if cfg.User != "test" {
		t.Errorf("User %+v does not match passed in user \"test\"", s.cfgs.User)
	}

	if cfg.Version != 3 {
		t.Errorf("Version %+v does not match current version: 3", s.cfgs.Version)
	}

	if cfg.Date <= originalConfig.Date {
		t.Errorf("Date %+v was not updated from %+v", s.cfgs.Date, originalConfig.Date)
	}
}
Esempio n. 2
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()
}
Esempio n. 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()
}
Esempio n. 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 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)
		}
	}
}
Esempio n. 6
0
// Licensed under the Apache License, Version 2.0
// Details: https://raw.githubusercontent.com/maniksurtani/quotaservice/master/LICENSE

package redis

import (
	"os"
	"testing"

	"github.com/maniksurtani/quotaservice"
	"github.com/maniksurtani/quotaservice/buckets"
	"github.com/maniksurtani/quotaservice/config"
	"gopkg.in/redis.v3"
)

var cfg = config.NewDefaultServiceConfig()
var factory quotaservice.BucketFactory
var bucket *redisBucket

func TestMain(m *testing.M) {
	setUp()
	r := m.Run()
	os.Exit(r)
}

func setUp() {
	factory = NewBucketFactory(&redis.Options{Addr: "localhost:6379"}, 2)
	factory.Init(cfg)
	bucket = factory.NewBucket("redis", "redis", config.NewDefaultBucketConfig(""), false).(*redisBucket)
}
Esempio n. 7
0
func NewMockAdministrable() *MockAdministrable {
	return &MockAdministrable{config.NewDefaultServiceConfig(), false}
}
Esempio n. 8
0
func NewMockErrorAdministrable() *MockAdministrable {
	return &MockAdministrable{config.NewDefaultServiceConfig(), true}
}
Esempio n. 9
0
// Details: https://raw.githubusercontent.com/maniksurtani/quotaservice/master/LICENSE

// Package buckets defines interfaces for abstractions of token buckets.
package quotaservice

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"
Esempio n. 10
0
func NewWithDefaultConfig(bucketFactory BucketFactory, rpcEndpoints ...RpcEndpoint) Server {
	return New(bucketFactory,
		config.NewMemoryConfig(config.NewDefaultServiceConfig()),
		rpcEndpoints...)
}
Esempio n. 11
0
func setUp() {
	factory.Init(config.NewDefaultServiceConfig())
}