Example #1
0
func TestHandleNonEvent(t *testing.T) {
	listener = stats.NewMemoryStatsListener()
	listener.HandleEvent(events.NewTimedOutEvent("test", "dyn", true, 1))
	scores := listener.Get("test", "dyn")

	if scores.Hits != 0 || scores.Misses != 0 {
		t.Fatalf("Bucket score was not accurate: %+v != [Hits=0, Misses=0]", scores)
	}
}
Example #2
0
func TestHandleIncrHitBucket(t *testing.T) {
	listener = stats.NewMemoryStatsListener()
	listener.HandleEvent(events.NewTokensServedEvent("test", "dyn", true, 1, 0))
	listener.HandleEvent(events.NewTokensServedEvent("test", "dyn", true, 3, 0))
	listener.HandleEvent(events.NewTokensServedEvent("test", "dyn", true, 1, 0))
	scores := listener.Get("test", "dyn")

	if scores.Hits != 5 || scores.Misses != 0 {
		t.Fatalf("Bucket score was not accurate: %+v != [Hits=5, Misses=0]", scores)
	}
}
Example #3
0
func TestHandleIncrMissBucket(t *testing.T) {
	listener = stats.NewMemoryStatsListener()
	listener.HandleEvent(events.NewBucketMissedEvent("test", "dyn", true))
	listener.HandleEvent(events.NewBucketMissedEvent("test", "dyn", true))
	listener.HandleEvent(events.NewBucketMissedEvent("test", "dyn", true))
	scores := listener.Get("test", "dyn")

	if scores.Hits != 0 || scores.Misses != 3 {
		t.Fatalf("Bucket score was not accurate: %+v != [Hits=0, Misses=3]", scores)
	}
}
Example #4
0
func TestTop10Hits(t *testing.T) {
	listener = stats.NewMemoryStatsListener()
	listener.HandleEvent(events.NewTokensServedEvent("test", "dyn-1", true, 3, 0))
	listener.HandleEvent(events.NewTokensServedEvent("test", "dyn-2", true, 10, 0))
	listener.HandleEvent(events.NewTokensServedEvent("test", "dyn-3", true, 1, 0))

	hits := listener.TopHits("test")
	correctHits := []*stats.BucketScore{
		&stats.BucketScore{"dyn-2", 10},
		&stats.BucketScore{"dyn-1", 3},
		&stats.BucketScore{"dyn-3", 1}}

	if !reflect.DeepEqual(hits, correctHits) {
		t.Fatalf("Hits top10 is not correct %+v", hits)
	}
}
Example #5
0
func TestHandleNewMissBucket(t *testing.T) {
	listener = stats.NewMemoryStatsListener()
	listener.HandleEvent(events.NewBucketMissedEvent("test", "dyn", true))
	scores := listener.Get("test", "dyn")

	if scores.Hits != 0 || scores.Misses != 1 {
		t.Fatalf("Bucket score was not accurate: %+v != [Hits=0, Misses=1]", scores)
	}

	listener.HandleEvent(events.NewBucketMissedEvent("test", "nondyn", false))
	scores = listener.Get("test", "nondyn")

	if scores.Hits != 0 || scores.Misses != 0 {
		t.Fatalf("Non-dynamic bucket score was not accurate: %+v != [Hits=0, Misses=0]", scores)
	}
}
Example #6
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 #7
0
func TestTop10Misses(t *testing.T) {
	listener = stats.NewMemoryStatsListener()

	listener.HandleEvent(events.NewBucketMissedEvent("test", "dyn-1", true))

	listener.HandleEvent(events.NewBucketMissedEvent("test", "dyn-2", true))
	listener.HandleEvent(events.NewBucketMissedEvent("test", "dyn-2", true))
	listener.HandleEvent(events.NewBucketMissedEvent("test", "dyn-2", true))

	listener.HandleEvent(events.NewBucketMissedEvent("test", "dyn-3", true))
	listener.HandleEvent(events.NewBucketMissedEvent("test", "dyn-3", true))

	misses := listener.TopMisses("test")
	correctMisses := []*stats.BucketScore{
		&stats.BucketScore{"dyn-2", 3},
		&stats.BucketScore{"dyn-3", 2},
		&stats.BucketScore{"dyn-1", 1}}

	if !reflect.DeepEqual(misses, correctMisses) {
		t.Fatalf("Misses top10 is not correct %+v", misses)
	}
}
Example #8
0
func TestHandleNewHitBucket(t *testing.T) {
	listener = stats.NewMemoryStatsListener()
	ev := events.NewTokensServedEvent("test", "dyn", true, 1, 0)
	listener.HandleEvent(ev)
	scores := listener.Get("test", "dyn")

	if scores.Hits != 1 || scores.Misses != 0 {
		t.Fatalf("Bucket score was not accurate: %+v != [Hits=1, Misses=0]", scores)
	}

	ev = events.NewTokensServedEvent("test", "nondyn", false, 1, 0)
	listener.HandleEvent(ev)
	scores = listener.Get("test", "nondyn")

	if scores.Hits != 0 || scores.Misses != 0 {
		t.Fatalf("Non-dynamic bucket score was not accurate: %+v != [Hits=0, Misses=0]", scores)
	}

	scores = listener.Get("nontest", "nondyn")

	if scores.Hits != 0 || scores.Misses != 0 {
		t.Fatalf("Nonexisting namespace was not accurate: %+v != [Hits=0, Misses=0]", scores)
	}
}