Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to handle REJECTED_TIMEDOUT #224

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/app/coroutines/completePromise.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ func CompletePromise(metadata *metadata.Metadata, req *t_api.Request, res func(*
} else {
status := t_api.ForbiddenStatus(p.State)
strict := req.CompletePromise.Strict && p.State != req.CompletePromise.State
timeout := !req.CompletePromise.Strict && req.CompletePromise.State.In(promise.Timedout)
favalos marked this conversation as resolved.
Show resolved Hide resolved

if !strict && p.IdempotencyKeyForComplete.Match(req.CompletePromise.IdempotencyKey) {
if (!strict && p.IdempotencyKeyForComplete.Match(req.CompletePromise.IdempotencyKey)) || timeout {
status = t_api.StatusOK
}

Expand Down
40 changes: 40 additions & 0 deletions internal/app/subsystems/api/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,46 @@ func TestHttpServer(t *testing.T) {
},
status: 201,
},
{
name: "RejectTimeoutPromise",
path: "promises/foo/bar",
method: "PATCH",
headers: map[string]string{
"Idempotency-Key": "bar",
"Strict": "true",
},
body: []byte(`{
"state": "REJECTED_TIMEDOUT",
"value": {
"headers": {"a":"a","b":"b","c":"c"},
"data": "cmVqZWN0"
}
}`),
req: &t_api.Request{
Kind: t_api.CompletePromise,
CompletePromise: &t_api.CompletePromiseRequest{
Id: "foo/bar",
IdempotencyKey: util.ToPointer(idempotency.Key("bar")),
Strict: true,
State: promise.Timedout,
Value: promise.Value{
Headers: map[string]string{"a": "a", "b": "b", "c": "c"},
Data: []byte("reject"),
},
},
},
res: &t_api.Response{
Kind: t_api.CompletePromise,
CompletePromise: &t_api.CompletePromiseResponse{
Status: t_api.StatusPromiseAlreadyTimedOut,
Promise: &promise.Promise{
Id: "foo/bar",
State: promise.Timedout,
},
},
},
status: 403,
},
{
name: "ReadSchedule",
path: "schedules/foo",
Expand Down
2 changes: 1 addition & 1 deletion internal/app/subsystems/api/http/promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (s *server) completePromise(c *gin.Context) {
err error
)

if !body.State.In(promise.Resolved | promise.Rejected | promise.Canceled) {
if !body.State.In(promise.Resolved | promise.Rejected | promise.Canceled | promise.Timedout) {
favalos marked this conversation as resolved.
Show resolved Hide resolved
c.JSON(http.StatusBadRequest, api.HandleValidationError(errors.New("invalid state")))
return
}
Expand Down
2 changes: 1 addition & 1 deletion test/dst/dst.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (d *DST) Run(r *rand.Rand, api api.API, aio aio.AIO, system *system.System,
generator.AddRequest(generator.GenerateCreatePromise)
model.AddResponse(t_api.CreatePromise, model.ValidatCreatePromise)
case t_api.CompletePromise:
generator.AddRequest(generator.GenerateCancelPromise)
generator.AddRequest(generator.GenerateCompletePromise)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

model.AddResponse(t_api.CompletePromise, model.ValidateCompletePromise)

// SCHEDULES
Expand Down
50 changes: 4 additions & 46 deletions test/dst/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dst

import (
"fmt"
"math"
"math/rand" // nosemgrep
"strconv"

Expand Down Expand Up @@ -193,62 +194,19 @@ func (g *Generator) GenerateCreatePromise(r *rand.Rand, t int64) *t_api.Request
}
}

func (g *Generator) GenerateCancelPromise(r *rand.Rand, t int64) *t_api.Request {
func (g *Generator) GenerateCompletePromise(r *rand.Rand, t int64) *t_api.Request {
id := g.idSet[r.Intn(len(g.idSet))]
idempotencyKey := g.idemotencyKeySet[r.Intn(len(g.idemotencyKeySet))]
data := g.dataSet[r.Intn(len(g.dataSet))]
headers := g.headersSet[r.Intn(len(g.headersSet))]
strict := r.Intn(2) == 0
state := promise.State(math.Exp2(float64(r.Intn(4) + 1)))
favalos marked this conversation as resolved.
Show resolved Hide resolved

return &t_api.Request{
Kind: t_api.CompletePromise,
CompletePromise: &t_api.CompletePromiseRequest{
Id: id,
State: promise.Canceled,
Value: promise.Value{
Headers: headers,
Data: data,
},
IdempotencyKey: idempotencyKey,
Strict: strict,
},
}
}

func (g *Generator) GenerateResolvePromise(r *rand.Rand, t int64) *t_api.Request {
id := g.idSet[r.Intn(len(g.idSet))]
idempotencyKey := g.idemotencyKeySet[r.Intn(len(g.idemotencyKeySet))]
data := g.dataSet[r.Intn(len(g.dataSet))]
headers := g.headersSet[r.Intn(len(g.headersSet))]
strict := r.Intn(2) == 0

return &t_api.Request{
Kind: t_api.CompletePromise,
CompletePromise: &t_api.CompletePromiseRequest{
Id: id,
State: promise.Resolved,
Value: promise.Value{
Headers: headers,
Data: data,
},
IdempotencyKey: idempotencyKey,
Strict: strict,
},
}
}

func (g *Generator) GenerateRejectPromise(r *rand.Rand, t int64) *t_api.Request {
id := g.idSet[r.Intn(len(g.idSet))]
idempotencyKey := g.idemotencyKeySet[r.Intn(len(g.idemotencyKeySet))]
data := g.dataSet[r.Intn(len(g.dataSet))]
headers := g.headersSet[r.Intn(len(g.headersSet))]
strict := r.Intn(2) == 0

return &t_api.Request{
Kind: t_api.CompletePromise,
CompletePromise: &t_api.CompletePromiseRequest{
Id: id,
State: promise.Rejected,
State: state,
Value: promise.Value{
Headers: headers,
Data: data,
Expand Down
13 changes: 10 additions & 3 deletions test/dst/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@
switch res.CompletePromise.Status {
case t_api.StatusOK:
if pm.completed() {
if !pm.idempotencyKeyForCompleteMatch(res.CompletePromise.Promise) {
if !(!req.CompletePromise.Strict && req.CompletePromise.State.In(promise.Timedout)) &&
!pm.idempotencyKeyForCompleteMatch(res.CompletePromise.Promise) {
return fmt.Errorf("ikey mismatch (%s, %s)", pm.promise.IdempotencyKeyForComplete, res.CompletePromise.Promise.IdempotencyKeyForComplete)
} else if req.CompletePromise.Strict && pm.promise.State != promise.Canceled {
} else if req.CompletePromise.Strict && pm.promise.State != req.CompletePromise.State {
return fmt.Errorf("unexpected state %s when strict true", pm.promise.State)
}
}
Expand All @@ -268,7 +269,13 @@
pm.promise = res.CompletePromise.Promise
return nil
case t_api.StatusCreated:
if res.CompletePromise.Promise.State != promise.Canceled {
if req.CompletePromise.State.In(promise.Resolved) && res.CompletePromise.Promise.State != promise.Resolved {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if req.CompletePromise.State.In(promise.Resolved) && res.CompletePromise.Promise.State != promise.Resolved {
if req.CompletePromise.State == promise.Resolved && res.CompletePromise.Promise.State != promise.Resolved {

return fmt.Errorf("unexpected state %s after resolve promise", res.CompletePromise.Promise.State)
}

Check warning on line 274 in test/dst/model.go

View check run for this annotation

Codecov / codecov/patch

test/dst/model.go#L273-L274

Added lines #L273 - L274 were not covered by tests
if req.CompletePromise.State.In(promise.Rejected) && res.CompletePromise.Promise.State != promise.Rejected {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if req.CompletePromise.State.In(promise.Rejected) && res.CompletePromise.Promise.State != promise.Rejected {
if req.CompletePromise.State == promise.Rejected && res.CompletePromise.Promise.State != promise.Rejected {

return fmt.Errorf("unexpected state %s after reject promise", res.CompletePromise.Promise.State)
}

Check warning on line 277 in test/dst/model.go

View check run for this annotation

Codecov / codecov/patch

test/dst/model.go#L276-L277

Added lines #L276 - L277 were not covered by tests
if req.CompletePromise.State.In(promise.Canceled) && res.CompletePromise.Promise.State != promise.Canceled {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if req.CompletePromise.State.In(promise.Canceled) && res.CompletePromise.Promise.State != promise.Canceled {
if req.CompletePromise.State == promise.Canceled && res.CompletePromise.Promise.State != promise.Canceled {

return fmt.Errorf("unexpected state %s after cancel promise", res.CompletePromise.Promise.State)
}
if pm.completed() {
Expand Down
Loading