From e7a514dd9d6844642164f918501a10fa2bc929a7 Mon Sep 17 00:00:00 2001 From: Aert van de Hulsbeek Date: Mon, 24 Oct 2022 22:18:27 +1100 Subject: [PATCH] #55 Fix sigsegv on ListTasks when queue is already deleted (#74) --- emulator.go | 5 ++++- emulator_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/emulator.go b/emulator.go index 26674bc..a9a432e 100644 --- a/emulator.go +++ b/emulator.go @@ -226,7 +226,10 @@ func (s *Server) TestIamPermissions(ctx context.Context, in *v1.TestIamPermissio // ListTasks lists the tasks in the specified queue func (s *Server) ListTasks(ctx context.Context, in *tasks.ListTasksRequest) (*tasks.ListTasksResponse, error) { // TODO: Implement pageing of some sort - queue, _ := s.fetchQueue(in.GetParent()) + queue, ok := s.fetchQueue(in.GetParent()) + if !ok || queue == nil { + return nil, status.Errorf(codes.NotFound, "Queue does not exist. If you just created the queue, wait at least a minute for the queue to initialize.") + } var taskStates []*tasks.Task diff --git a/emulator_test.go b/emulator_test.go index 0958a2a..4e2403b 100644 --- a/emulator_test.go +++ b/emulator_test.go @@ -388,6 +388,57 @@ func TestPurgeQueueOptionallyPerformsHardReset(t *testing.T) { ) } +func TestListTasks(t *testing.T) { + serv, client := setUp(t, ServerOptions{}) + defer tearDown(t, serv) + + srv, _ := startTestServer() + defer srv.Shutdown(context.Background()) + + createdQueue := createTestQueue(t, client) + + createTaskRequest := taskspb.CreateTaskRequest{ + Parent: createdQueue.GetName(), + Task: &taskspb.Task{ + Name: createdQueue.GetName() + "/tasks/my-test-task", + MessageType: &taskspb.Task_HttpRequest{ + HttpRequest: &taskspb.HttpRequest{ + Url: "http://localhost:5000/success", + }, + }, + }, + } + + createdTask, err := client.CreateTask(context.Background(), &createTaskRequest) + require.NoError(t, err) + + listTasksRequest := taskspb.ListTasksRequest{ + Parent: createdQueue.GetName(), + } + + tasksIterator := client.ListTasks(context.Background(), &listTasksRequest) + assert.NoError(t, err) + + listedTask, err := tasksIterator.Next() + assert.NoError(t, err) + assert.Equal(t, listedTask.GetName(), createdTask.GetName()) + _, err = tasksIterator.Next() + assert.EqualError(t, err, "no more items in iterator") + + deleteQueueRequest := taskspb.DeleteQueueRequest{ + Name: createdQueue.GetName(), + } + err = client.DeleteQueue(context.Background(), &deleteQueueRequest) + require.NoError(t, err) + + tasksIterator = client.ListTasks(context.Background(), &listTasksRequest) + assert.NoError(t, err) + + listedTask, err = tasksIterator.Next() + assertIsGrpcError(t, "^Queue does not exist", grpcCodes.NotFound, err) + assert.Nil(t, listedTask) +} + func TestSuccessTaskExecution(t *testing.T) { serv, client := setUp(t, ServerOptions{}) defer tearDown(t, serv)