fixed tests, added apigeesync_token header for counter service
diff --git a/api_test.go b/api_test.go index 226b3d3..ff3acbb 100644 --- a/api_test.go +++ b/api_test.go
@@ -2,10 +2,8 @@ import ( . "github.com/onsi/ginkgo" - //"net/http" "bytes" "encoding/json" - "fmt" "github.com/google/uuid" "io/ioutil" "net/http" @@ -61,10 +59,9 @@ respBody := make(map[string]interface{}) err = json.Unmarshal(respBodyBytes, &respBody) if err != nil { - fmt.Println("error: ", err) + Fail("error: " + err.Error()) } - fmt.Println(respBody) Fail("wrong status code: " + res.Status) } @@ -107,7 +104,7 @@ respBody := make(map[string]interface{}) err = json.Unmarshal(respBodyBytes, &respBody) if err != nil { - fmt.Println("error: ", err) + Fail("error: " + err.Error()) } Fail("wrong status code: " + res.Status) @@ -175,7 +172,7 @@ respBody := make(map[string]interface{}) err = json.Unmarshal(respBodyBytes, &respBody) if err != nil { - fmt.Println("error: ", err) + Fail("error: " + err.Error()) } Fail("wrong status code: " + res.Status)
diff --git a/constants/constants.go b/constants/constants.go index 6a15ea1..2b640ac 100644 --- a/constants/constants.go +++ b/constants/constants.go
@@ -3,6 +3,9 @@ import "time" const ( + //config variables. + ApigeeSyncBearerToken = "apigeesync_bearer_token" + ConfigCounterServiceBasePath = "apidquota_counterService_base_path" //add to acceptedTimeUnitList in init() if case any other new timeUnit is added TimeUnitSECOND = "second" @@ -14,9 +17,9 @@ //errors InvalidQuotaTimeUnitType = "invalidQuotaTimeUnitType" - InvalidQuotaBucketType = "invalidQuotaType" - InvalidQuotaType = "invalidQUotaType" + InvalidQuotaType = "invalidQuotaType" InvalidQuotaPeriod = "invalidQuotaPeriod" + AsyncQuotaBucketEmpty = "AsyncDetails_for_quotaBucket_are_empty" QuotaTypeCalendar = "calendar" // after start time QuotaTypeRollingWindow = "rollingwindow" // in the past "window" time @@ -33,7 +36,6 @@ ErrorCheckingQuotaLimit = "error_checking_quota_limit" QuotaBasePathDefault = "/quota" - ConfigCounterServiceBasePath = "counterService_base_path" URLCounterServiceNotSet = "url_counter_service_not_set" URLCounterServiceInvalid = "url_counter_service_invalid" MarshalJSONError = "marshal_JSON_error"
diff --git a/quotaBucket/apiUtil.go b/quotaBucket/apiUtil.go index 6a13039..600ef01 100644 --- a/quotaBucket/apiUtil.go +++ b/quotaBucket/apiUtil.go
@@ -244,6 +244,7 @@ return nil } + //for non distributed quotaBucket //retrieveFromCache. newQBucket, ok = getFromCache(cacheKey, weight)
diff --git a/quotaBucket/quotaBucket.go b/quotaBucket/quotaBucket.go index f67137c..c23f428 100644 --- a/quotaBucket/quotaBucket.go +++ b/quotaBucket/quotaBucket.go
@@ -31,16 +31,16 @@ endTime time.Time } -func (qp *quotaPeriod) GetPeriodStartTime() time.Time { - - return qp.startTime -} - func (qp *quotaPeriod) GetPeriodInputStartTime() time.Time { return qp.inputStartTime } +func (qp *quotaPeriod) GetPeriodStartTime() time.Time { + + return qp.startTime +} + func (qp *quotaPeriod) GetPeriodEndTime() time.Time { return qp.endTime @@ -65,44 +65,12 @@ qTicker *time.Ticker } -func (qAsync *aSyncQuotaBucket) getAsyncGlobalCount() (int64, error) { - - if qAsync != nil { - return qAsync.asyncGLobalCount, nil - } - return 0, errors.New("aSyncDetails for QuotaBucket are empty.") -} - -func (qAsync *aSyncQuotaBucket) getAsyncQTicker() (*time.Ticker, error) { - - if qAsync != nil { - return qAsync.qTicker, nil - } - return nil, errors.New("AsyncDetails for QuotaBucket are empty.") -} - -func (qAsync *aSyncQuotaBucket) addToAsyncLocalMessageCount(count int64) error { - - if qAsync != nil { - atomic.AddInt64(&qAsync.asyncLocalMessageCount, count) - } - return errors.New("AsyncDetails for QuotaBucket are empty. ") -} - -func (qAsync *aSyncQuotaBucket) getAsyncLocalMessageCount() (int64, error) { - - if qAsync != nil { - return qAsync.asyncLocalMessageCount, nil - } - return 0, errors.New("AsyncDetails for QuotaBucket are empty. ") -} - func (qAsync *aSyncQuotaBucket) getAsyncSyncTime() (int64, error) { if qAsync != nil { return qAsync.syncTimeInSec, nil } - return 0, errors.New("AsyncDetails for QuotaBucket are empty. ") + return 0, errors.New(constants.AsyncQuotaBucketEmpty) } func (qAsync *aSyncQuotaBucket) getAsyncSyncMessageCount() (int64, error) { @@ -110,7 +78,31 @@ if qAsync != nil { return qAsync.syncMessageCount, nil } - return 0, errors.New("AsyncDetails for QuotaBucket are empty. ") + return 0, errors.New(constants.AsyncQuotaBucketEmpty) +} + +func (qAsync *aSyncQuotaBucket) getAsyncLocalMessageCount() (int64, error) { + + if qAsync != nil { + return qAsync.asyncLocalMessageCount, nil + } + return 0, errors.New(constants.AsyncQuotaBucketEmpty) +} + +func (qAsync *aSyncQuotaBucket) addToAsyncLocalMessageCount(count int64) error { + + if qAsync != nil { + atomic.AddInt64(&qAsync.asyncLocalMessageCount, count) + } + return errors.New(constants.AsyncQuotaBucketEmpty) +} + +func (qAsync *aSyncQuotaBucket) getAsyncGlobalCount() (int64, error) { + + if qAsync != nil { + return qAsync.asyncGLobalCount, nil + } + return 0, errors.New(constants.AsyncQuotaBucketEmpty) } func (qAsync *aSyncQuotaBucket) getAsyncIsInitialized() (bool, error) { @@ -118,7 +110,33 @@ if qAsync != nil { return qAsync.initialized, nil } - return false, errors.New("AsyncDetails for QuotaBucket are empty. ") + return false, errors.New(constants.AsyncQuotaBucketEmpty) +} + +func (qAsync *aSyncQuotaBucket) getAsyncQTicker() (*time.Ticker, error) { + + if qAsync != nil { + return qAsync.qTicker, nil + } + return nil, errors.New(constants.AsyncQuotaBucketEmpty) +} + +func (qAsync *aSyncQuotaBucket) getAsyncCounter() (*[]int64, error) { + + if qAsync != nil { + return qAsync.asyncCounter, nil + } + return nil, errors.New(constants.AsyncQuotaBucketEmpty) +} + +func (aSyncbucket *aSyncQuotaBucket) addToCounter(weight int64) error { + + if aSyncbucket == nil { + return errors.New(constants.AsyncQuotaBucketEmpty) + } + + *aSyncbucket.asyncCounter = append(*aSyncbucket.asyncCounter, weight) + return nil } func (aSyncbucket *aSyncQuotaBucket) getCount(q *QuotaBucket, period *quotaPeriod) (int64, error) { @@ -137,16 +155,6 @@ return aSyncbucket.asyncGLobalCount + aSyncbucket.asyncLocalMessageCount, nil } -func (aSyncbucket *aSyncQuotaBucket) addToCounter(weight int64) error { - - if aSyncbucket == nil { - return errors.New("AsyncDetails for QuotaBucket are empty. ") - } - - *aSyncbucket.asyncCounter = append(*aSyncbucket.asyncCounter, weight) - return nil -} - type quotaBucketData struct { EdgeOrgID string ID string @@ -191,9 +199,18 @@ quotaBucketData: quotaBucketDataStruct, } + if !quotaBucket.IsDistrubuted() { + if quotaBucket.IsSynchronous(){ + return nil, errors.New("quota bucket cannot be both nonDistributed and synchronous.") + } + } //for async set AsyncQuotaDetails and start the NewTicker if distributed && !synchronous { var quotaTicker int64 + //ensure just one of syncTimeInSec and syncMessageCount is set. + if syncTimeInSec > -1 && syncMessageCount > -1 { + return nil,errors.New("both syncTimeInSec and syncMessageCount canot be set. only one of them should be set.") + } //set default syncTime for AsyncQuotaBucket. //for aSyncQuotaBucket with 'syncMessageCount' the ticker is invoked with DefaultQuotaSyncTime quotaTicker = constants.DefaultQuotaSyncTime @@ -260,7 +277,7 @@ } if ok := IsValidType(strings.ToLower(q.GetType())); !ok { - return errors.New(constants.InvalidQuotaBucketType) + return errors.New(constants.InvalidQuotaType) } //check if the period is valid
diff --git a/quotaBucket/quotaBucketType.go b/quotaBucket/quotaBucketType.go index 5b3cbbc..cb3c1b7 100644 --- a/quotaBucket/quotaBucketType.go +++ b/quotaBucket/quotaBucketType.go
@@ -3,6 +3,7 @@ import ( "errors" "github.com/30x/apidQuota/services" + "github.com/30x/apidQuota/constants" ) type QuotaBucketType interface { @@ -89,6 +90,9 @@ return nil, errors.New("error getting period: " + err.Error()) } aSyncBucket := q.GetAsyncQuotaBucket() + if aSyncBucket == nil { + return nil, errors.New(constants.AsyncQuotaBucketEmpty + " : aSyncQuotaBucket to increment cannot be empty.") + } currentCount, err := aSyncBucket.getCount(q, period) if err != nil { return nil, err @@ -114,13 +118,22 @@ } - if aSyncBucket.syncMessageCount > 0 && - aSyncBucket.asyncLocalMessageCount >= aSyncBucket.syncMessageCount { + asyncMessageCount, err := aSyncBucket.getAsyncSyncMessageCount() + if err != nil { + return nil, err + } + + asyncLocalMsgCount,err := aSyncBucket.getAsyncLocalMessageCount() + if err != nil { + return nil, err + } + + if asyncMessageCount > 0 && + asyncLocalMsgCount >= asyncMessageCount { err = internalRefresh(q, period) if err != nil { return nil, err } - } } else { exceeded = true @@ -147,11 +160,18 @@ func internalRefresh(q *QuotaBucket, period *quotaPeriod) error { var err error aSyncBucket := q.GetAsyncQuotaBucket() + if aSyncBucket == nil { + return errors.New(constants.AsyncQuotaBucketEmpty) + } + weight := int64(0) countFromCounterService := int64(0) - globalCount := aSyncBucket.asyncGLobalCount - maxCount := q.GetMaxCount() + globalCount,err := aSyncBucket.getAsyncGlobalCount() + if err != nil { + return err + } + maxCount := q.GetMaxCount() for _, counterEle := range *aSyncBucket.asyncCounter { weight += counterEle
diff --git a/quotaBucket/quotaBucket_test.go b/quotaBucket/quotaBucket_test.go index 2de6335..cba72c4 100644 --- a/quotaBucket/quotaBucket_test.go +++ b/quotaBucket/quotaBucket_test.go
@@ -1,596 +1,623 @@ package quotaBucket_test -// -//import ( -// "github.com/30x/apidQuota/constants" -// . "github.com/30x/apidQuota/quotaBucket" -// . "github.com/onsi/ginkgo" -// . "github.com/onsi/gomega" -// "strings" -// "time" -//) -// -//var _ = Describe("Test QuotaPeriod", func() { -// It("Valid NewQuotaPeriod", func() { -// //startTime before endTime -// period := NewQuotaPeriod(time.Now().UTC().AddDate(0, -1, 0).Unix(), -// time.Now().UTC().AddDate(0, 0, -1).Unix(), -// time.Now().UTC().AddDate(0, 1, 0).Unix()) -// isValid, err := period.Validate() -// -// if !isValid { -// Fail("expected isValid true but got false") -// } -// if err != nil { -// Fail("expected error <nil> but got " + err.Error()) -// } -// }) -// -// It("Invalid NewQuotaPeriod", func() { -// //startTime after endTime -// period := NewQuotaPeriod(time.Now().UTC().AddDate(0, -1, 0).Unix(), -// time.Now().UTC().AddDate(0, 1, 0).Unix(), -// time.Now().UTC().AddDate(0, 0, -1).Unix()) -// isValid, err := period.Validate() -// if isValid { -// Fail("Expected isValid false but got true") -// } -// -// if err == nil { -// Fail(" Expected error but got <nil>") -// } -// -// //startTime same as endTime -// period = NewQuotaPeriod(time.Now().UTC().AddDate(0, -1, 0).Unix(), -// time.Now().UTC().AddDate(0, 1, 0).Unix(), -// time.Now().UTC().AddDate(0, 1, 0).Unix()) -// isValid, err = period.Validate() -// if isValid { -// Fail("Expected isValid false but got true") -// } -// -// if err == nil { -// Fail(" Expected error but got <nil>") -// } -// -// }) -//}) -// -//var _ = Describe("Test AcceptedQuotaTimeUnitTypes", func() { -// It("testTimeUnit", func() { -// if !IsValidTimeUnit("second") { -// Fail("Expected true: second is a valid TimeUnit") -// } -// if !IsValidTimeUnit("minute") { -// Fail("Expected true: minute is a valid TimeUnit") -// } -// if !IsValidTimeUnit("hour") { -// Fail("Expected true: hour is a valid TimeUnit") -// } -// if !IsValidTimeUnit("day") { -// Fail("Expected true: day is a valid TimeUnit") -// } -// if !IsValidTimeUnit("week") { -// Fail("Expected true: week is a valid TimeUnit") -// } -// if !IsValidTimeUnit("month") { -// Fail("Expected true: month is a valid TimeUnit") -// } -// -// //invalid type -// if IsValidTimeUnit("invalidType") { -// Fail("Expected false: invalidType is not a valid TimeUnit") -// } -// }) -//}) -// -////Tests for QuotaBucket -//var _ = Describe("QuotaBucket", func() { -// It("Create with NewQuotaBucket", func() { -// edgeOrgID := "sampleOrg" -// id := "sampleID" -// interval := 1 -// timeUnit := "hour" -// quotaType := "calendar" -// preciseAtSecondsLevel := true -// maxCount := int64(10) -// weight := int64(1) -// distributed := true -// synchronous := true -// syncTimeInSec := int64(-1) -// syncMessageCount := int64(-1) -// -// //start time before now() -// startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() -// -// quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// now := time.Now().UTC() -// currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC) -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// //also check if all the fields are set as expected -// sTime := time.Unix(startTime, 0) -// Expect(sTime).To(Equal(quotaBucket.GetStartTime())) -// Expect(edgeOrgID).To(Equal(quotaBucket.GetEdgeOrgID())) -// Expect(id).To(Equal(quotaBucket.GetID())) -// Expect(timeUnit).To(Equal(quotaBucket.GetTimeUnit())) -// Expect(quotaType).To(Equal(quotaBucket.GetType())) -// Expect(interval).To(Equal(quotaBucket.GetInterval())) -// Expect(maxCount).To(Equal(quotaBucket.GetMaxCount())) -// Expect(preciseAtSecondsLevel).To(Equal(quotaBucket.GetIsPreciseAtSecondsLevel())) -// getPeriod, err := quotaBucket.GetPeriod() -// Expect(err).NotTo(HaveOccurred()) -// Expect(getPeriod.GetPeriodInputStartTime().String()).ShouldNot(BeEmpty()) -// Expect(getPeriod.GetPeriodStartTime().String()).ShouldNot(BeEmpty()) -// Expect(getPeriod.GetPeriodEndTime().String()).ShouldNot(BeEmpty()) -// Expect(getPeriod.GetPeriodStartTime().String()).Should(Equal(currentHour.String())) -// Expect(getPeriod.GetPeriodEndTime().String()).Should(Equal(currentHour.Add(time.Hour).String())) -// -// //start time is after now() -> should still set period. -// startTime = time.Now().UTC().AddDate(0, 1, 0).Unix() -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// now = time.Now().UTC() -// currentHour = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC) -// -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// getPeriod, err = quotaBucket.GetPeriod() -// Expect(err).NotTo(HaveOccurred()) -// Expect(getPeriod.GetPeriodInputStartTime().String()).ShouldNot(BeEmpty()) -// Expect(getPeriod.GetPeriodStartTime().String()).ShouldNot(BeEmpty()) -// Expect(getPeriod.GetPeriodEndTime().String()).ShouldNot(BeEmpty()) -// Expect(getPeriod.GetPeriodStartTime().String()).Should(Equal(currentHour.String())) -// Expect(getPeriod.GetPeriodEndTime().String()).Should(Equal(currentHour.Add(time.Hour).String())) -// -// }) -// -// //end before start -// It("Test invalid quotaPeriod", func() { -// edgeOrgID := "sampleOrg" -// id := "sampleID" -// timeUnit := "hour" -// quotaType := "calendar" -// interval := 1 -// maxCount := int64(10) -// weight := int64(1) -// preciseAtSecondsLevel := true -// startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() -// distributed := true -// synchronous := true -// syncTimeInSec := int64(-1) -// syncMessageCount := int64(-1) -// -// quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// quotaBucket.SetPeriod(time.Now().UTC().AddDate(0, 1, 0), time.Now().UTC().AddDate(0, 0, -1)) -// err = quotaBucket.Validate() -// if err == nil { -// Fail("error expected but got <nil>") -// } -// if !strings.Contains(err.Error(), constants.InvalidQuotaPeriod) { -// Fail("expected: " + constants.InvalidQuotaPeriod + " in the error but got: " + err.Error()) -// } -// -// }) -// -// It("Test invalid timeUnitType", func() { -// edgeOrgID := "sampleOrg" -// id := "sampleID" -// timeUnit := "invalidTimeUnit" -// quotaType := "calendar" -// interval := 1 -// maxCount := int64(10) -// weight := int64(1) -// preciseAtSecondsLevel := true -// startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() -// distributed := true -// synchronous := true -// syncTimeInSec := int64(-1) -// syncMessageCount := int64(-1) -// -// quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).To(HaveOccurred()) -// -// if !strings.Contains(err.Error(), constants.InvalidQuotaTimeUnitType) { -// Fail("expected: " + constants.InvalidQuotaTimeUnitType + "but got: " + err.Error()) -// } -// if quotaBucket != nil { -// Fail("quotaBucket returned should be nil.") -// } -// -// }) -// -//}) -// -//var _ = Describe("IsCurrentPeriod", func() { -// It("Test IsCurrentPeriod for RollingType Window - Valid TestCase", func() { -// -// edgeOrgID := "sampleOrg" -// id := "sampleID" -// timeUnit := "hour" -// quotaType := "rollingwindow" -// interval := 1 -// maxCount := int64(10) -// weight := int64(1) -// preciseAtSecondsLevel := true -// distributed := true -// synchronous := true -// syncTimeInSec := int64(-1) -// syncMessageCount := int64(-1) -// -// //InputStart time is before now -// startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() -// -// quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// period, err := quotaBucket.GetPeriod() -// if err != nil { -// Fail("no error expected") -// } -// if ok := period.IsCurrentPeriod(quotaBucket); !ok { -// Fail("Exprected true, returned: false") -// } -// -// //InputStart time is now -// startTime = time.Now().UTC().Unix() -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// period, err = quotaBucket.GetPeriod() -// if err != nil { -// Fail("no error expected") -// } -// period.IsCurrentPeriod(quotaBucket) -// if ok := period.IsCurrentPeriod(quotaBucket); !ok { -// Fail("Exprected true, returned: false") -// } -// }) -// -// It("Test IsCurrentPeriod for RollingType Window - InValid TestCase", func() { -// -// edgeOrgID := "sampleOrg" -// id := "sampleID" -// timeUnit := "hour" -// quotaType := "rollingwindow" -// interval := 1 -// maxCount := int64(10) -// weight := int64(1) -// preciseAtSecondsLevel := true -// //InputStart time is after now. -// startTime := time.Now().UTC().AddDate(0, 1, 0).Unix() -// distributed := true -// synchronous := true -// syncTimeInSec := int64(-1) -// syncMessageCount := int64(-1) -// -// quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// quotaBucket.SetPeriod(time.Now().UTC().AddDate(0, -1, 0), time.Now().UTC().AddDate(0, 0, 1)) -// period, err := quotaBucket.GetPeriod() -// Expect(err).NotTo(HaveOccurred()) -// if ok := period.IsCurrentPeriod(quotaBucket); ok { -// Fail("Exprected true, returned: false") -// } -// -// //endTime before startTime in period -// startTime = time.Now().UTC().AddDate(0, -1, 0).Unix() -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// quotaBucket.SetPeriod(time.Now().UTC(), time.Now().UTC().AddDate(0, -1, 0)) -// if ok := period.IsCurrentPeriod(quotaBucket); ok { -// Fail("Exprected false, returned: true") -// } -// }) -// -// It("Test IsCurrentPeriod for calendarType Window - Valid TestCases", func() { -// -// edgeOrgID := "sampleOrg" -// id := "sampleID" -// timeUnit := "hour" -// quotaType := "calendar" -// interval := 1 -// maxCount := int64(10) -// weight := int64(1) -// preciseAtSecondsLevel := true -// distributed := true -// synchronous := true -// syncTimeInSec := int64(-1) -// syncMessageCount := int64(-1) -// -// //InputStart time is before now -// startTime := time.Now().UTC().UTC().AddDate(-1, -1, 0).Unix() -// -// quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// period, err := quotaBucket.GetPeriod() -// if err != nil { -// Fail("no error expected but returned " + err.Error()) -// } -// if ok := period.IsCurrentPeriod(quotaBucket); !ok { -// Fail("Exprected true, returned: false") -// } -// -// //InputStart time is now -// startTime = time.Now().UTC().Unix() -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// period, err = quotaBucket.GetPeriod() -// if err != nil { -// Fail("no error expected but returned " + err.Error()) -// } -// period.IsCurrentPeriod(quotaBucket) -// if ok := period.IsCurrentPeriod(quotaBucket); !ok { -// Fail("Exprected true, returned: false") -// } -// -// //start Time in period is before now -// startTime = time.Now().UTC().Unix() -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// quotaBucket.SetPeriod(time.Now().UTC().AddDate(0, 0, -1), -// time.Now().UTC().AddDate(0, 1, 0)) -// period, err = quotaBucket.GetPeriod() -// if err != nil { -// Fail("no error expected but returned " + err.Error()) -// } -// period.IsCurrentPeriod(quotaBucket) -// if ok := period.IsCurrentPeriod(quotaBucket); !ok { -// Fail("Exprected true, returned: false") -// } -// -// //start Time in period is now -// startTime = time.Now().UTC().Unix() -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// quotaBucket.SetPeriod(time.Now().UTC(), -// time.Now().UTC().AddDate(0, 1, 0)) -// period, err = quotaBucket.GetPeriod() -// if err != nil { -// Fail("no error expected but returned " + err.Error()) -// } -// period.IsCurrentPeriod(quotaBucket) -// if ok := period.IsCurrentPeriod(quotaBucket); !ok { -// Fail("Exprected true, returned: false") -// } -// -// //end Time in period is after now -// startTime = time.Now().UTC().Unix() -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// quotaBucket.SetPeriod(time.Now().UTC().AddDate(0, 0, -1), -// time.Now().UTC().AddDate(0, 1, 0)) -// period, err = quotaBucket.GetPeriod() -// if err != nil { -// Fail("no error expected but returned " + err.Error()) -// } -// -// period.IsCurrentPeriod(quotaBucket) -// if ok := period.IsCurrentPeriod(quotaBucket); !ok { -// Fail("Exprected true, returned: false") -// } -// -// //start time in period is before end time -// startTime = time.Now().UTC().Unix() -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// quotaBucket.SetPeriod(time.Now().UTC().AddDate(0, 0, -1), -// time.Now().UTC().AddDate(0, 1, 0)) -// period, err = quotaBucket.GetPeriod() -// if err != nil { -// Fail("no error expected but returned " + err.Error()) -// } -// period.IsCurrentPeriod(quotaBucket) -// if ok := period.IsCurrentPeriod(quotaBucket); !ok { -// Fail("Exprected true, returned: false") -// } -// -// }) -// -// It("Test IsCurrentPeriod for calendarType Window InValid TestCase", func() { -// -// edgeOrgID := "sampleOrg" -// id := "sampleID" -// timeUnit := "hour" -// quotaType := "calendar" -// interval := 1 -// maxCount := int64(10) -// weight := int64(1) -// preciseAtSecondsLevel := true -// distributed := true -// synchronous := true -// syncTimeInSec := int64(-1) -// syncMessageCount := int64(-1) -// -// //InputStart time is after now. -// period := NewQuotaPeriod(time.Now().UTC().AddDate(0, 1, 0).Unix(), -// time.Now().UTC().AddDate(0, 1, 0).Unix(), time.Now().AddDate(1, 0, 1).Unix()) -// startTime := time.Now().UTC().AddDate(0, 1, 0).Unix() -// -// quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// if ok := period.IsCurrentPeriod(quotaBucket); ok { -// Fail("Exprected true, returned: false") -// } -// -// //endTime is before start time -// startTime = time.Now().UTC().AddDate(0, -1, 0).Unix() -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// -// quotaBucket.SetPeriod(time.Now().UTC(), time.Now().UTC().AddDate(0, -1, 0)) -// -// if ok := period.IsCurrentPeriod(quotaBucket); ok { -// Fail("Exprected true, returned: false") -// } -// -// //start time in period after now -// quotaBucket.SetPeriod(time.Now().UTC().AddDate(0, 1, 0), time.Now().UTC().AddDate(1, 1, 0)) -// -// if ok := period.IsCurrentPeriod(quotaBucket); ok { -// Fail("Exprected true, returned: false") -// } -// -// //end time in period is before now -// quotaBucket.SetPeriod(time.Now().UTC().AddDate(-1, -1, 0), time.Now().UTC().AddDate(0, -1, 0)) -// -// if ok := period.IsCurrentPeriod(quotaBucket); ok { -// Fail("Exprected true, returned: false") -// } -// -// }) -//}) -// -//var _ = Describe("Test GetPeriod and setCurrentPeriod", func() { -// It("Valid GetPeriod", func() { -// edgeOrgID := "sampleOrg" -// id := "sampleID" -// timeUnit := "hour" -// quotaType := "rollingwindow" -// interval := 1 -// maxCount := int64(10) -// weight := int64(1) -// preciseAtSecondsLevel := true -// distributed := true -// synchronous := true -// syncTimeInSec := int64(-1) -// syncMessageCount := int64(-1) -// -// //InputStart time is before now -// startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() -// quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// quotaBucket.SetPeriod(time.Now().UTC().AddDate(0, 0, -1), time.Now().UTC().AddDate(0, 1, 0)) -// err = quotaBucket.Validate() -// Expect(err).NotTo(HaveOccurred()) -// qPeriod, err := quotaBucket.GetPeriod() -// Expect(err).NotTo(HaveOccurred()) -// -// // check if the rolling window was set properly -// Expect(qPeriod.GetPeriodInputStartTime()).Should(Equal(quotaBucket.GetStartTime())) -// if !qPeriod.GetPeriodEndTime().After(qPeriod.GetPeriodStartTime()) { -// Fail("Rolling Window was not set as expected") -// } -// intervalDuration := qPeriod.GetPeriodEndTime().Sub(qPeriod.GetPeriodStartTime()) -// expectedDuration, err := GetIntervalDurtation(quotaBucket) -// Expect(intervalDuration).Should(Equal(expectedDuration)) -// -// //for non rolling Type window do not setCurrentPeriod as endTime is > time.now. -// quotaType = "calendar" -// pstartTime := time.Now().UTC().AddDate(0, -1, 0) -// pendTime := time.Now().UTC().AddDate(0, 1, 0) -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// quotaBucket.SetPeriod(pstartTime, pendTime) -// qPeriod, err = quotaBucket.GetPeriod() -// Expect(err).NotTo(HaveOccurred()) -// // check if the calendar window was set properly -// Expect(qPeriod.GetPeriodInputStartTime()).Should(Equal(quotaBucket.GetStartTime())) -// if !qPeriod.GetPeriodEndTime().After(qPeriod.GetPeriodStartTime()) { -// Fail("Rolling Window was not set as expected") -// } -// -// //for non rolling Type window setCurrentPeriod as endTime is < time.now. -// quotaType = "calendar" -// pstartTime = time.Now().UTC().AddDate(0, -1, 0) -// pendTime = time.Now().UTC().AddDate(0, -1, 0) -// quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, -// quotaType, preciseAtSecondsLevel, startTime, maxCount, -// weight, distributed, synchronous, syncTimeInSec, syncMessageCount) -// Expect(err).NotTo(HaveOccurred()) -// -// quotaBucket.SetPeriod(pstartTime, pendTime) -// qPeriod, err = quotaBucket.GetPeriod() -// Expect(err).NotTo(HaveOccurred()) -// // check if the calendar window was set properly -// Expect(qPeriod.GetPeriodInputStartTime()).Should(Equal(quotaBucket.GetStartTime())) -// if !qPeriod.GetPeriodEndTime().After(qPeriod.GetPeriodStartTime()) { -// Fail("period for Non Rolling Window Type was not set as expected") -// } -// intervalDuration = qPeriod.GetPeriodEndTime().Sub(qPeriod.GetPeriodStartTime()) -// expectedDuration, err = GetIntervalDurtation(quotaBucket) -// Expect(intervalDuration).Should(Equal(expectedDuration)) -// }) -//}) + +import ( + "github.com/30x/apidQuota/constants" + . "github.com/30x/apidQuota/quotaBucket" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "strings" + "time" +) + +var _ = Describe("Test AcceptedQuotaTimeUnitTypes", func() { + It("testTimeUnit", func() { + if !IsValidTimeUnit("second") { + Fail("Expected true: second is a valid TimeUnit") + } + if !IsValidTimeUnit("minute") { + Fail("Expected true: minute is a valid TimeUnit") + } + if !IsValidTimeUnit("hour") { + Fail("Expected true: hour is a valid TimeUnit") + } + if !IsValidTimeUnit("day") { + Fail("Expected true: day is a valid TimeUnit") + } + if !IsValidTimeUnit("week") { + Fail("Expected true: week is a valid TimeUnit") + } + if !IsValidTimeUnit("month") { + Fail("Expected true: month is a valid TimeUnit") + } + + //invalid type + if IsValidTimeUnit("invalidType") { + Fail("Expected false: invalidType is not a valid TimeUnit") + } + }) +}) + + +var _ = Describe("Test AcceptedQuotaTypes", func() { + It("testTimeUnit", func() { + if !IsValidType("calendar") { + Fail("Expected true: calendar is a valid quotaType") + } + if !IsValidType("rollingwindow") { + Fail("Expected true: rollingwindow is a valid quotaType") + } + if IsValidType("invalidType") { + Fail("Expected false: invalidType is not a valid quotaType") + } + }) +}) + +//Tests for QuotaBucket +var _ = Describe("QuotaBucket", func() { + + //validate all fields set as expected. + //validate period set as expected. + //validate async QuotaBucket is empty. + It("Create with NewQuotaBucket with all valid fields", func() { + edgeOrgID := "sampleOrg" + id := "sampleID" + interval := 1 + timeUnit := "hour" + quotaType := "calendar" + preciseAtSecondsLevel := true + maxCount := int64(10) + weight := int64(1) + distributed := true + synchronous := true + syncTimeInSec := int64(-1) + syncMessageCount := int64(-1) + + //start time before now() + startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() + + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + now := time.Now().UTC() + currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC) + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + + //also check if all the fields are set as expected + getPeriod, err := quotaBucket.GetPeriod() + Expect(err).NotTo(HaveOccurred()) + Expect(getPeriod.GetPeriodInputStartTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodStartTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodEndTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodStartTime().String()).Should(Equal(currentHour.String())) + Expect(getPeriod.GetPeriodEndTime().String()).Should(Equal(currentHour.Add(time.Hour).String())) + + //check if isDistributed and isSynchronous are true. + if !quotaBucket.IsDistrubuted(){ + Fail("expected true, returned true.") + } + if !quotaBucket.IsSynchronous(){ + Fail("expected true, returned true.") + } + + asyncBucket := quotaBucket.GetAsyncQuotaBucket() + if asyncBucket != nil{ + Fail("asyncBucket should be nil for synchronous request.") + } + + //testCase2: synchronous := true and (syncTimeInSec and syncMessageCount) > -1 -> aSyncQuotaBucket is nil. + syncTimeInSec = int64(10) + syncMessageCount = int64(10) + quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + now = time.Now().UTC() + currentHour = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC) + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + //check if isDistributed and isSynchronous are true. + if !quotaBucket.IsDistrubuted(){ + Fail("expected true, returned true.") + } + if !quotaBucket.IsSynchronous(){ + Fail("expected true, returned true.") + } + + asyncBucket = quotaBucket.GetAsyncQuotaBucket() + if asyncBucket != nil{ + Fail("asyncBucket should be nil for synchronous request.") + } + + + }) + + //startTime for quotaBucket after time.Now() + It("Create with NewQuotaBucket with start time after now()", func() { + edgeOrgID := "sampleOrg" + id := "sampleID" + interval := 1 + timeUnit := "hour" + quotaType := "calendar" + preciseAtSecondsLevel := true + maxCount := int64(10) + weight := int64(1) + distributed := true + synchronous := true + syncTimeInSec := int64(-1) + syncMessageCount := int64(-1) + //start time is after now() -> should still set period. + startTime := time.Now().UTC().AddDate(0, 1, 0).Unix() + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + now := time.Now().UTC() + currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC) + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + + getPeriod, err := quotaBucket.GetPeriod() + Expect(err).NotTo(HaveOccurred()) + Expect(getPeriod.GetPeriodInputStartTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodStartTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodEndTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodStartTime().String()).Should(Equal(currentHour.String())) + Expect(getPeriod.GetPeriodEndTime().String()).Should(Equal(currentHour.Add(time.Hour).String())) + + //check if isDistributed and isSynchronous are true. + if !quotaBucket.IsDistrubuted(){ + Fail("expected true, returned false.") + } + if !quotaBucket.IsSynchronous(){ + Fail("expected true, returned false.") + } + + asyncBucket := quotaBucket.GetAsyncQuotaBucket() + if asyncBucket != nil{ + Fail("asyncBucket should be nil for synchronous request.") + } + }) + + //Testcase1 : with syncTimeInSec + //Testcase2 : with syncMessageCount + //Testcase3 : InvalidTestCase - with syncTimeInSec and syncMessageCount + It("Create with NewQuotaBucket for aSyncQuotBucket", func() { + edgeOrgID := "sampleOrg" + id := "sampleID" + interval := 1 + timeUnit := "hour" + quotaType := "calendar" + preciseAtSecondsLevel := true + maxCount := int64(10) + weight := int64(1) + distributed := true + synchronous := false + + //Testcase1 : with syncTimeInSec + syncTimeInSec := int64(10) + syncMessageCount := int64(-1) + //start time is after now() -> should still set period. + startTime := time.Now().UTC().AddDate(0, 1, 0).Unix() + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + now := time.Now().UTC() + currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC) + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + + getPeriod, err := quotaBucket.GetPeriod() + Expect(err).NotTo(HaveOccurred()) + Expect(getPeriod.GetPeriodInputStartTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodStartTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodEndTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodStartTime().String()).Should(Equal(currentHour.String())) + Expect(getPeriod.GetPeriodEndTime().String()).Should(Equal(currentHour.Add(time.Hour).String())) + + //check if isDistributed is true and isSynchronous is false. + if !quotaBucket.IsDistrubuted(){ + Fail("expected true, returned false.") + } + if quotaBucket.IsSynchronous(){ + Fail("expected false, returned true.") + } + + asyncBucket := quotaBucket.GetAsyncQuotaBucket() + if asyncBucket == nil{ + Fail("asyncBucket should be nil for synchronous request.") + } + + //Testcase2 : with syncMessageCount + syncTimeInSec = int64(-1) + syncMessageCount = int64(10) + //start time is after now() -> should still set period. + startTime = time.Now().UTC().AddDate(0, 1, 0).Unix() + quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + now = time.Now().UTC() + currentHour = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC) + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + + getPeriod, err = quotaBucket.GetPeriod() + Expect(err).NotTo(HaveOccurred()) + Expect(getPeriod.GetPeriodInputStartTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodStartTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodEndTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodStartTime().String()).Should(Equal(currentHour.String())) + Expect(getPeriod.GetPeriodEndTime().String()).Should(Equal(currentHour.Add(time.Hour).String())) + + //check if isDistributed is true and isSynchronous is false. + if !quotaBucket.IsDistrubuted(){ + Fail("expected true, returned false.") + } + if quotaBucket.IsSynchronous(){ + Fail("expected false, returned true.") + } + + asyncBucket = quotaBucket.GetAsyncQuotaBucket() + if asyncBucket == nil{ + Fail("asyncBucket should be nil for synchronous request.") + } + + //Testcase3 : InvalidTestCase - with syncTimeInSec and syncMessageCount + syncTimeInSec = int64(10) + syncMessageCount = int64(10) + //start time is after now() -> should still set period. + startTime = time.Now().UTC().AddDate(0, 1, 0).Unix() + quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).To(HaveOccurred()) + + }) + + It("Create with NonDistributed QuotaBucket", func() { + edgeOrgID := "sampleOrg" + id := "sampleID" + interval := 1 + timeUnit := "hour" + quotaType := "calendar" + preciseAtSecondsLevel := true + maxCount := int64(10) + weight := int64(1) + distributed := false + synchronous := false + syncTimeInSec := int64(-1) + syncMessageCount := int64(-1) + + //start time before now() + startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() + + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + now := time.Now().UTC() + currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC) + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + Expect(quotaBucket.IsSynchronous()).Should(BeFalse()) + + //also check if all the fields are set as expected + getPeriod, err := quotaBucket.GetPeriod() + Expect(err).NotTo(HaveOccurred()) + Expect(getPeriod.GetPeriodInputStartTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodStartTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodEndTime().String()).ShouldNot(BeEmpty()) + Expect(getPeriod.GetPeriodStartTime().String()).Should(Equal(currentHour.String())) + Expect(getPeriod.GetPeriodEndTime().String()).Should(Equal(currentHour.Add(time.Hour).String())) + + //check if isDistributed is false and isSynchronous is false. + if quotaBucket.IsDistrubuted(){ + Fail("expected false, returned true.") + } + if quotaBucket.IsSynchronous(){ + Fail("expected false, returned true.") + } + + asyncBucket := quotaBucket.GetAsyncQuotaBucket() + if asyncBucket != nil { + Fail("asyncBucket should be nil for synchronous request.") + } + + synchronous = true + quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).To(HaveOccurred()) + }) + + + + It("Test invalid timeUnitType", func() { + edgeOrgID := "sampleOrg" + id := "sampleID" + timeUnit := "invalidTimeUnit" + quotaType := "calendar" + interval := 1 + maxCount := int64(10) + weight := int64(1) + preciseAtSecondsLevel := true + startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() + distributed := true + synchronous := true + syncTimeInSec := int64(-1) + syncMessageCount := int64(-1) + + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + err = quotaBucket.Validate() + Expect(err).To(HaveOccurred()) + + if !strings.Contains(err.Error(), constants.InvalidQuotaTimeUnitType) { + Fail("expected: " + constants.InvalidQuotaTimeUnitType + "but got: " + err.Error()) + } + + }) + + It("Test invalid quotaType", func() { + edgeOrgID := "sampleOrg" + id := "sampleID" + timeUnit := "hour" + quotaType := "invalidTimeUnit" + interval := 1 + maxCount := int64(10) + weight := int64(1) + preciseAtSecondsLevel := true + startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() + distributed := true + synchronous := true + syncTimeInSec := int64(-1) + syncMessageCount := int64(-1) + + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + err = quotaBucket.Validate() + Expect(err).To(HaveOccurred()) + + if !strings.Contains(err.Error(), constants.InvalidQuotaType) { + Fail("expected: " + constants.InvalidQuotaType + "but got: " + err.Error()) + } + }) +}) + +var _ = Describe("IsCurrentPeriod", func() { + It("Test IsCurrentPeriod for RollingType Window - Valid TestCase", func() { + + edgeOrgID := "sampleOrg" + id := "sampleID" + timeUnit := "hour" + quotaType := "rollingwindow" + interval := 1 + maxCount := int64(10) + weight := int64(1) + preciseAtSecondsLevel := true + distributed := true + synchronous := true + syncTimeInSec := int64(-1) + syncMessageCount := int64(-1) + + //InputStart time is before now + startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() + + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + + period, err := quotaBucket.GetPeriod() + if err != nil { + Fail("no error expected") + } + if ok := period.IsCurrentPeriod(quotaBucket); !ok { + Fail("Exprected true, returned: false") + } + + //InputStart time is now + startTime = time.Now().UTC().Unix() + quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + + period, err = quotaBucket.GetPeriod() + if err != nil { + Fail("no error expected") + } + period.IsCurrentPeriod(quotaBucket) + if ok := period.IsCurrentPeriod(quotaBucket); !ok { + Fail("Exprected true, returned: false") + } + }) + + It("Test IsCurrentPeriod for RollingType Window - InValid TestCase", func() { + + edgeOrgID := "sampleOrg" + id := "sampleID" + timeUnit := "hour" + quotaType := "rollingwindow" + interval := 1 + maxCount := int64(10) + weight := int64(1) + preciseAtSecondsLevel := true + //InputStart time is after now. + startTime := time.Now().UTC().AddDate(0, 1, 0).Unix() + distributed := true + synchronous := true + syncTimeInSec := int64(-1) + syncMessageCount := int64(-1) + + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + + period, err := quotaBucket.GetPeriod() + Expect(err).NotTo(HaveOccurred()) + if ok := period.IsCurrentPeriod(quotaBucket); ok { + Fail("Exprected false, returned: true") + } + + }) + + It("Test IsCurrentPeriod for calendarType Window - Valid TestCases", func() { + + edgeOrgID := "sampleOrg" + id := "sampleID" + timeUnit := "hour" + quotaType := "calendar" + interval := 1 + maxCount := int64(10) + weight := int64(1) + preciseAtSecondsLevel := true + distributed := true + synchronous := true + syncTimeInSec := int64(-1) + syncMessageCount := int64(-1) + + //InputStart time is before now + startTime := time.Now().UTC().UTC().AddDate(-1, -1, 0).Unix() + + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + + period, err := quotaBucket.GetPeriod() + if err != nil { + Fail("no error expected but returned " + err.Error()) + } + if ok := period.IsCurrentPeriod(quotaBucket); !ok { + Fail("Exprected true, returned: false") + } + + //InputStart time is now + startTime = time.Now().UTC().Unix() + quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + + period, err = quotaBucket.GetPeriod() + if err != nil { + Fail("no error expected but returned " + err.Error()) + } + period.IsCurrentPeriod(quotaBucket) + if ok := period.IsCurrentPeriod(quotaBucket); !ok { + Fail("Exprected true, returned: false") + } + + }) + + It("Test IsCurrentPeriod for calendarType Window InValid TestCase", func() { + + edgeOrgID := "sampleOrg" + id := "sampleID" + timeUnit := "hour" + quotaType := "calendar" + interval := 1 + maxCount := int64(10) + weight := int64(1) + preciseAtSecondsLevel := true + distributed := true + synchronous := true + syncTimeInSec := int64(-1) + syncMessageCount := int64(-1) + + //InputStart time is after now. + startTime := time.Now().UTC().AddDate(0, 1, 0).Unix() + + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + + period, err := quotaBucket.GetPeriod() + Expect(err).NotTo(HaveOccurred()) + + if ok := period.IsCurrentPeriod(quotaBucket); ok { + Fail("Exprected false, returned: true") + } + }) +}) + +var _ = Describe("Test GetPeriod and the timeInterval in period set as expected", func() { + It("Valid GetPeriod", func() { + edgeOrgID := "sampleOrg" + id := "sampleID" + timeUnit := "hour" + quotaType := "rollingwindow" + interval := 1 + maxCount := int64(10) + weight := int64(1) + preciseAtSecondsLevel := true + distributed := true + synchronous := true + syncTimeInSec := int64(-1) + syncMessageCount := int64(-1) + + //InputStart time is before now + startTime := time.Now().UTC().AddDate(0, -1, 0).Unix() + quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + + err = quotaBucket.Validate() + Expect(err).NotTo(HaveOccurred()) + qPeriod, err := quotaBucket.GetPeriod() + Expect(err).NotTo(HaveOccurred()) + + // check if the rolling window was set properly + Expect(qPeriod.GetPeriodInputStartTime()).Should(Equal(quotaBucket.GetStartTime())) + if !qPeriod.GetPeriodEndTime().After(qPeriod.GetPeriodStartTime()) { + Fail("Rolling Window was not set as expected") + } + intervalDuration := qPeriod.GetPeriodEndTime().Sub(qPeriod.GetPeriodStartTime()) + expectedDuration, err := GetIntervalDurtation(quotaBucket) + Expect(intervalDuration).Should(Equal(expectedDuration)) + + //for non rolling Type window setCurrentPeriod as endTime is < time.now. + quotaType = "calendar" + quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, + quotaType, preciseAtSecondsLevel, startTime, maxCount, + weight, distributed, synchronous, syncTimeInSec, syncMessageCount) + Expect(err).NotTo(HaveOccurred()) + + qPeriod, err = quotaBucket.GetPeriod() + Expect(err).NotTo(HaveOccurred()) + // check if the calendar window was set properly + Expect(qPeriod.GetPeriodInputStartTime()).Should(Equal(quotaBucket.GetStartTime())) + if !qPeriod.GetPeriodEndTime().After(qPeriod.GetPeriodStartTime()) { + Fail("period for Non Rolling Window Type was not set as expected") + } + intervalDuration = qPeriod.GetPeriodEndTime().Sub(qPeriod.GetPeriodStartTime()) + expectedDuration, err = GetIntervalDurtation(quotaBucket) + Expect(intervalDuration).Should(Equal(expectedDuration)) + }) +})
diff --git a/quotaBucket/quotaCache.go b/quotaBucket/quotaCache.go index 6dbc04a..4afae81 100644 --- a/quotaBucket/quotaCache.go +++ b/quotaBucket/quotaCache.go
@@ -4,6 +4,7 @@ "github.com/30x/apidQuota/constants" "sync" "time" + "errors" ) var quotaCachelock = sync.RWMutex{} @@ -53,6 +54,9 @@ if qBucketCache.qBucket.Distributed && !qBucketCache.qBucket.IsSynchronous() { aSyncBucket := qBucketCache.qBucket.GetAsyncQuotaBucket() + if aSyncBucket == nil { + return errors.New(constants.AsyncQuotaBucketEmpty + " : aSyncQuotaBucket to increment cannot be empty.") + } qticker, err := aSyncBucket.getAsyncQTicker() if err != nil { return err
diff --git a/quotaBucket/quotaDescriptorType.go b/quotaBucket/quotaDescriptorType.go index d910c0e..c436286 100644 --- a/quotaBucket/quotaDescriptorType.go +++ b/quotaBucket/quotaDescriptorType.go
@@ -124,7 +124,7 @@ currentStart = currentEnd.AddDate(0, (-1)*qb.Interval, 0) return currentEnd.Sub(currentStart), nil default: - return time.Duration(0), errors.New(constants.InvalidQuotaBucketType + " : ignoring unrecognized quotaType : " + quotaType) + return time.Duration(0), errors.New(constants.InvalidQuotaType + " : ignoring unrecognized quotaType : " + quotaType) } default:
diff --git a/services/counterServiceHelper.go b/services/counterServiceHelper.go index 21fea87..5a21ce8 100644 --- a/services/counterServiceHelper.go +++ b/services/counterServiceHelper.go
@@ -25,6 +25,13 @@ Timeout: time.Duration(60 * time.Second), } +var token string + +func addApigeeSyncTokenToHeader(req *http.Request) { + token = globalVariables.Config.GetString(constants.ApigeeSyncBearerToken) + req.Header.Set("Authorization", "Bearer "+token) +} + func GetCount(orgID string, quotaKey string, startTimeInt int64, endTimeInt int64) (int64, error) { return IncrementAndGetCount(orgID, quotaKey, 0, startTimeInt, endTimeInt) @@ -34,7 +41,6 @@ headers := http.Header{} headers.Set("Accept", "application/json") headers.Set("Content-Type", "application/json") - method := "POST" if globalVariables.CounterServiceURL == "" { @@ -67,6 +73,7 @@ Body: ioutil.NopCloser(bytes.NewReader(reqBodyBytes)), ContentLength: int64(contentLength), } + addApigeeSyncTokenToHeader(request) resp, err := client.Do(request)