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

[Tavern][bugfix] Fixed issue with transactional client #106

Merged
merged 1 commit into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 12 additions & 2 deletions tavern/graphql/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,21 @@ func TestCreateJob(t *testing.T) {
func newCreateJobTest(gqlClient *client.Client, sessionIDs []int, input ent.CreateJobInput, checks ...func(t *testing.T, id int, err error)) func(t *testing.T) {
return func(t *testing.T) {
// Define the mutatation for testing, taking the input as a variable
mut := `mutation newCreateJobTest($sessionIDs: [ID!]!, $input: CreateJobInput!) { createJob(sessionIDs:$sessionIDs, input:$input) { id } }`
mut := `mutation newCreateJobTest($sessionIDs: [ID!]!, $input: CreateJobInput!) { createJob(sessionIDs:$sessionIDs, input:$input) {
id
tasks {
id
}
} }`

// Make our request to the GraphQL API
var resp struct {
CreateJob struct{ ID string }
CreateJob struct {
ID string
Tasks []struct {
ID string
} `json:"tasks"`
}
}
err := gqlClient.Post(mut, &resp,
client.Var("sessionIDs", sessionIDs),
Expand Down
22 changes: 19 additions & 3 deletions tavern/graphql/mutation.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func (r *mutationResolver) CreateJob(ctx context.Context, sessionIDs []int, inpu
return nil, rollback(tx, fmt.Errorf("failed to commit transaction: %w", err))
}

// 9. Load the job with our non transactional client (cannot use transaction after commit)
job, err = r.client.Job.Get(ctx, job.ID)
if err != nil {
return nil, fmt.Errorf("failed to load created job: %w", err)
}

return job, nil
}

Expand Down Expand Up @@ -167,23 +173,33 @@ func (r *mutationResolver) ClaimTasks(ctx context.Context, input models.ClaimTas

// 7. Update all ClaimedAt timestamps to claim tasks
// ** Note: If one fails to update, we roll back the transaction and return the error
result := make([]*ent.Task, 0, len(tasks))
taskIDs := make([]int, 0, len(tasks))
for _, t := range tasks {
_, err := client.Task.UpdateOne(t).
SetClaimedAt(time.Now()).
Save(ctx)
if err != nil {
return nil, rollback(tx, fmt.Errorf("failed to update task %d: %w", t.ID, err))
}
result = append(result, t)
taskIDs = append(taskIDs, t.ID)
}

// 8. Commit the transaction
if err := tx.Commit(); err != nil {
return nil, rollback(tx, fmt.Errorf("failed to commit transaction: %w", err))
}

// 9. Return claimed tasks
// 9. Load the tasks with our non transactional client (cannot use transaction after commit)
result := make([]*ent.Task, 0, len(taskIDs))
for _, taskID := range taskIDs {
updatedTask, err := r.client.Task.Get(ctx, taskID)
if err != nil {
return nil, fmt.Errorf("failed to load updated task (but they were still updated) %d: %w", taskID, err)
}
result = append(result, updatedTask)
}

// 10. Return claimed tasks
return result, nil
}

Expand Down
14 changes: 11 additions & 3 deletions tavern/graphql/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,22 @@ func TestSessionMutations(t *testing.T) {
mut := `
mutation newClaimTasksTest($input: ClaimTasksInput!) {
claimTasks(input: $input) {
id
id
job {
id
}
}
}`
// Create a closure to execute the mutation
claimTasks := func(input map[string]any) ([]int, error) {
// Make our request to the GraphQL API
var resp struct {
ClaimTasks []struct{ ID string }
ClaimTasks []struct {
ID string
Job struct {
ID string
} `json:"job"`
}
}
err := gqlClient.Post(mut, &resp, client.Var("input", input))
if err != nil {
Expand Down Expand Up @@ -163,7 +171,7 @@ mutation newClaimTasksTest($input: ClaimTasksInput!) {
mut := `
mutation newSubmitTaskResultTest($input: SubmitTaskResultInput!) {
submitTaskResult(input: $input) {
id
id
}
}`
// Create a closure to execute the mutation
Expand Down