summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaciej Wereski <m.wereski@partner.samsung.com>2017-10-04 20:07:07 +0200
committerMaciej Wereski <m.wereski@partner.samsung.com>2017-10-12 18:44:25 +0200
commit9e0e786b0a4bd6ef41a01d3c3d43dfd39c9f207f (patch)
tree2f7d869d4d3c23246f3c0f197d60b4d2a35bfe86
parent46687cc33e9f3ee7379f456ba92a265478ccf631 (diff)
downloadboruta-9e0e786b0a4bd6ef41a01d3c3d43dfd39c9f207f.tar.gz
boruta-9e0e786b0a4bd6ef41a01d3c3d43dfd39c9f207f.tar.bz2
boruta-9e0e786b0a4bd6ef41a01d3c3d43dfd39c9f207f.zip
Implement UpdateRequest
Change-Id: Ibad4fab53af38f0be6794c1c16394c4bc06dafc9 Signed-off-by: Maciej Wereski <m.wereski@partner.samsung.com> Reviewed-on: https://mcdsrvbld02.digital.local/review/49546 Reviewed-by: Aleksander Mistewicz <a.mistewicz@samsung.com> Tested-by: Aleksander Mistewicz <a.mistewicz@samsung.com>
-rw-r--r--requests/requests.go91
-rw-r--r--requests/requests_test.go148
2 files changed, 96 insertions, 143 deletions
diff --git a/requests/requests.go b/requests/requests.go
index 8561792..3f8d9a2 100644
--- a/requests/requests.go
+++ b/requests/requests.go
@@ -121,78 +121,57 @@ func modificationPossible(state ReqState) bool {
return state == WAIT
}
-// SetRequestPriority is part of implementation of Requests interface. It will
-// change priority for given request ID only if modification of request is
-// possible. NotFoundError, ErrModificationForbidden or ErrPriority (if given
-// priority is out of bounds) may be returned.
-func (reqs *ReqsCollection) SetRequestPriority(reqID ReqID, priority Priority) error {
+// UpdateRequest is part of implementation of Requests interface. It may be used
+// to modify ValidAfter, Deadline or Priority of request. Caller should pass
+// pointer to new ReqInfo struct which has any of these fields set. Zero value
+// means that field shouldn't be changed. All fields that cannot be changed are
+// ignored.
+func (reqs *ReqsCollection) UpdateRequest(src *ReqInfo) error {
+ if src == nil || (src.Priority == Priority(0) &&
+ src.ValidAfter.IsZero() &&
+ src.Deadline.IsZero()) {
+ return nil
+ }
reqs.mutex.Lock()
defer reqs.mutex.Unlock()
- req, ok := reqs.requests[reqID]
+
+ dst, ok := reqs.requests[src.ID]
if !ok {
return NotFoundError("Request")
}
- // TODO(mwereski): Check if user has rights to set given priority.
- if priority < HiPrio || priority > LoPrio {
- return ErrPriority
- }
- if !modificationPossible(req.State) {
+ if !modificationPossible(dst.State) {
return ErrModificationForbidden
}
- if priority == req.Priority {
+ if src.Priority == dst.Priority &&
+ src.ValidAfter.Equal(dst.ValidAfter) &&
+ src.Deadline.Equal(dst.Deadline) {
return nil
}
- reqs.queue.setRequestPriority(req, priority)
- req.Priority = priority
- return nil
-}
-
-// SetRequestValidAfter is part of implementation of Requests interface.
-// It changes date after which request will be sent to worker. Provided time is
-// converted to UTC. Request must exist, must be in WAIT state and given date
-// must be before deadline of request. Otherwise NotFoundError,
-// ErrModificationForbidden or ErrInvalidTimeRange will be returned.
-func (reqs *ReqsCollection) SetRequestValidAfter(reqID ReqID, validAfter time.Time) error {
- reqs.mutex.Lock()
- defer reqs.mutex.Unlock()
- req, ok := reqs.requests[reqID]
- if !ok {
- return NotFoundError("Request")
+ // TODO(mwereski): Check if user has rights to set given priority.
+ if src.Priority != Priority(0) && (src.Priority < HiPrio ||
+ src.Priority > LoPrio) {
+ return ErrPriority
}
- if !modificationPossible(req.State) {
- return ErrModificationForbidden
+ deadline := dst.Deadline
+ if !src.Deadline.IsZero() {
+ if src.Deadline.Before(time.Now().UTC()) {
+ return ErrDeadlineInThePast
+ }
+ deadline = src.Deadline
}
- if validAfter.After(req.Deadline) && !req.Deadline.IsZero() {
+ if (!src.ValidAfter.IsZero()) && !deadline.IsZero() &&
+ src.ValidAfter.After(deadline) {
return ErrInvalidTimeRange
}
- req.ValidAfter = validAfter
- // TODO(mwereski): check if request is ready to go.
- return nil
-}
-// SetRequestDeadline is part of implementation of Requests interface. It changes
-// date before which request must be sent to worker. Provided time is converted
-// to UTC. Request must exist, must be in WAIT state. Given date must be in the
-// future and must be after ValidAfer. In case of not meeting these constrains
-// following errors are returned: NotFoundError, ErrModificationForbidden,
-// ErrDeadlineInThePast and ErrInvalidTimeRange.
-func (reqs *ReqsCollection) SetRequestDeadline(reqID ReqID, deadline time.Time) error {
- reqs.mutex.Lock()
- defer reqs.mutex.Unlock()
- req, ok := reqs.requests[reqID]
- if !ok {
- return NotFoundError("Request")
+ if src.Priority != Priority(0) {
+ reqs.queue.setRequestPriority(dst, src.Priority)
+ dst.Priority = src.Priority
}
- if !modificationPossible(req.State) {
- return ErrModificationForbidden
- }
- if !deadline.IsZero() && deadline.Before(time.Now().UTC()) {
- return ErrDeadlineInThePast
- }
- if !deadline.IsZero() && deadline.Before(req.ValidAfter) {
- return ErrInvalidTimeRange
+ if !src.ValidAfter.IsZero() {
+ dst.ValidAfter = src.ValidAfter
}
- req.Deadline = deadline
+ dst.Deadline = deadline
// TODO(mwereski): check if request is ready to go.
return nil
}
diff --git a/requests/requests_test.go b/requests/requests_test.go
index 368a869..64b6afe 100644
--- a/requests/requests_test.go
+++ b/requests/requests_test.go
@@ -151,109 +151,83 @@ func TestCloseRequest(t *testing.T) {
assert.EqualValues(0, rqueue.queue.length)
}
-func TestSetRequestPriority(t *testing.T) {
+func TestUpdateRequest(t *testing.T) {
assert, rqueue := initTest(t)
- req := requestsTests[0].req
+ tmp := requestsTests[0].req
// Add valid request.
- reqid, err := rqueue.NewRequest(req.Caps, req.Priority, req.Owner, req.ValidAfter, req.Deadline)
- assert.Nil(err)
-
- legalPrio := req.Priority + 1
- illegalPrio := 2 * LoPrio
-
- // Change priority of the request to the same value.
- err = rqueue.SetRequestPriority(reqid, req.Priority)
+ reqid, err := rqueue.NewRequest(tmp.Caps, tmp.Priority, tmp.Owner, tmp.ValidAfter, tmp.Deadline)
assert.Nil(err)
- reqinfo, err := rqueue.GetRequestInfo(reqid)
+ req := rqueue.requests[reqid]
+ reqBefore, err := rqueue.GetRequestInfo(reqid)
assert.Nil(err)
- assert.Equal(req.Priority, reqinfo.Priority)
+ reqUpdate := new(ReqInfo)
+ *reqUpdate = *req
- // Change priority of the request.
- err = rqueue.SetRequestPriority(reqid, legalPrio)
+ // Check noop.
+ err = rqueue.UpdateRequest(nil)
assert.Nil(err)
- reqinfo, err = rqueue.GetRequestInfo(reqid)
+ reqUpdate.ValidAfter = zeroTime
+ reqUpdate.Deadline = zeroTime
+ reqUpdate.Priority = Priority(0)
+ err = rqueue.UpdateRequest(reqUpdate)
assert.Nil(err)
- assert.Equal(legalPrio, reqinfo.Priority)
-
- // Try to change priority of request that doesn't exist.
- err = rqueue.SetRequestPriority(req.ID+1, legalPrio)
+ assert.Equal(req, &reqBefore)
+ // Check request that doesn't exist.
+ *reqUpdate = *req
+ reqUpdate.ID++
+ err = rqueue.UpdateRequest(reqUpdate)
assert.Equal(NotFoundError("Request"), err)
- reqinfo, err = rqueue.GetRequestInfo(reqid)
+ reqUpdate.ID = req.ID
+ // Change Priority only.
+ reqUpdate.Priority = req.Priority - 1
+ err = rqueue.UpdateRequest(reqUpdate)
assert.Nil(err)
- assert.Equal(legalPrio, reqinfo.Priority)
-
- // Try to change priority of request to invalid value.
- err = rqueue.SetRequestPriority(reqid, illegalPrio)
- assert.Equal(ErrPriority, err)
- reqinfo, err = rqueue.GetRequestInfo(reqid)
+ assert.Equal(reqUpdate.Priority, req.Priority)
+ // Change ValidAfter only.
+ reqUpdate.ValidAfter = yesterday
+ err = rqueue.UpdateRequest(reqUpdate)
assert.Nil(err)
- assert.Equal(legalPrio, reqinfo.Priority)
-
- // Try to change priority of request which is in state that forbid changes.
- rqueue.requests[reqid].State = INVALID
- err = rqueue.SetRequestPriority(reqid, legalPrio)
- assert.EqualValues(ErrModificationForbidden, err)
- reqinfo, err = rqueue.GetRequestInfo(reqid)
+ assert.Equal(reqUpdate.ValidAfter, req.ValidAfter)
+ // Change Deadline only.
+ reqUpdate.Deadline = tomorrow.AddDate(0, 0, 1).UTC()
+ err = rqueue.UpdateRequest(reqUpdate)
assert.Nil(err)
- assert.Equal(legalPrio, reqinfo.Priority)
-}
-
-func TestSetRequestValidAfter(t *testing.T) {
- assert, rqueue := initTest(t)
- req := requestsTests[0].req
- reqid, err := rqueue.NewRequest(req.Caps, req.Priority, req.Owner, req.ValidAfter, req.Deadline)
+ assert.Equal(reqUpdate.Deadline, req.Deadline)
+ // Change Priority, ValidAfter and Deadline.
+ reqUpdate.Deadline = tomorrow
+ reqUpdate.ValidAfter = time.Now().Add(time.Hour)
+ reqUpdate.Priority = LoPrio
+ err = rqueue.UpdateRequest(reqUpdate)
assert.Nil(err)
-
- // Set legal ValidAfter value.
- err = rqueue.SetRequestValidAfter(reqid, tomorrow)
+ assert.Equal(reqUpdate, req)
+ // Change values to the same ones that are already set.
+ err = rqueue.UpdateRequest(reqUpdate)
assert.Nil(err)
- assert.Equal(tomorrow, rqueue.requests[reqid].ValidAfter)
-
- // Try to set ValidAfter for non-existent request.
- err = rqueue.SetRequestValidAfter(ReqID(2), tomorrow)
- assert.Equal(NotFoundError("Request"), err)
-
- // Try to set ValidAfter newer then Deadline.
- rqueue.requests[reqid].Deadline = now
- err = rqueue.SetRequestValidAfter(reqid, tomorrow)
- assert.Equal(ErrInvalidTimeRange, err)
-
- // Try to set ValidAfter for request which cannot be modified.
- rqueue.requests[reqid].State = INVALID
- err = rqueue.SetRequestValidAfter(reqid, yesterday)
- assert.EqualValues(ErrModificationForbidden, err)
-}
-
-func TestSetRequestDeadline(t *testing.T) {
- assert, rqueue := initTest(t)
- req := requestsTests[0].req
- reqid, err := rqueue.NewRequest(req.Caps, req.Priority, req.Owner, req.ValidAfter, req.Deadline)
- assert.Nil(err)
-
- // Set legal Deadline value.
- dayAfter := tomorrow.AddDate(0, 0, 1).UTC()
- err = rqueue.SetRequestDeadline(reqid, dayAfter)
- assert.Nil(err)
- assert.Equal(dayAfter, rqueue.requests[reqid].Deadline)
-
- // Try to set Deadline for non-existent request.
- err = rqueue.SetRequestDeadline(ReqID(2), tomorrow)
- assert.Equal(NotFoundError("Request"), err)
-
- // Try to set Deadline in the past.
- err = rqueue.SetRequestDeadline(reqid, yesterday)
+ assert.Equal(reqUpdate, req)
+ // Change Priority to illegal value.
+ reqUpdate.Priority = LoPrio + 1
+ err = rqueue.UpdateRequest(reqUpdate)
+ assert.Equal(ErrPriority, err)
+ reqUpdate.Priority = req.Priority
+ //Change Deadline to illegal value.
+ reqUpdate.Deadline = yesterday
+ err = rqueue.UpdateRequest(reqUpdate)
assert.Equal(ErrDeadlineInThePast, err)
-
- // Try to set Deadline before ValidAfter.
- rqueue.requests[reqid].ValidAfter = dayAfter
- err = rqueue.SetRequestDeadline(reqid, tomorrow)
+ reqUpdate.Deadline = time.Now().Add(time.Minute)
+ err = rqueue.UpdateRequest(reqUpdate)
assert.Equal(ErrInvalidTimeRange, err)
-
- // Try to set Deadline for request which cannot be modified.
- rqueue.requests[reqid].State = INVALID
- err = rqueue.SetRequestDeadline(reqid, tomorrow)
- assert.EqualValues(ErrModificationForbidden, err)
+ // Change ValidAfer to illegal value.
+ reqUpdate.ValidAfter = req.Deadline.Add(time.Hour)
+ err = rqueue.UpdateRequest(reqUpdate)
+ assert.Equal(ErrInvalidTimeRange, err)
+ // Try to change values for other changes.
+ states := [...]ReqState{INVALID, CANCEL, TIMEOUT, DONE, FAILED, INPROGRESS}
+ for _, state := range states {
+ rqueue.requests[reqid].State = state
+ err = rqueue.UpdateRequest(reqUpdate)
+ assert.Equal(ErrModificationForbidden, err)
+ }
}
func TestGetRequestInfo(t *testing.T) {