Skip to content

Commit

Permalink
Merge pull request #39 from aertje/get_queue
Browse files Browse the repository at this point in the history
Add get queue functionality and tests
  • Loading branch information
aertje authored Apr 27, 2021
2 parents 37639f6 + aefceb8 commit f08f159
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
7 changes: 5 additions & 2 deletions emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ func (s *Server) ListQueues(ctx context.Context, in *tasks.ListQueuesRequest) (*

// GetQueue returns the requested queue
func (s *Server) GetQueue(ctx context.Context, in *tasks.GetQueueRequest) (*tasks.Queue, error) {
queue, _ := s.fetchQueue(in.GetName())
queue, ok := s.fetchQueue(in.GetName())

// TODO: handle not found
// Cloud responds with the same error message whether the queue was recently deleted or never existed
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.")
}

return queue.state, nil
}
Expand Down
58 changes: 58 additions & 0 deletions emulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"google.golang.org/api/option"
taskspb "google.golang.org/genproto/googleapis/cloud/tasks/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
grpcCodes "google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
grpcStatus "google.golang.org/grpc/status"
)

Expand Down Expand Up @@ -132,6 +134,62 @@ func TestCreateTaskRejectsInvalidName(t *testing.T) {
}
}

func TestGetQueueExists(t *testing.T) {
serv, client := setUp(t)
defer tearDown(t, serv)

createdQueue := createTestQueue(t, client)

getQueueRequest := taskspb.GetQueueRequest{
Name: createdQueue.GetName(),
}

gettedQueue, err := client.GetQueue(context.Background(), &getQueueRequest)

assert.NoError(t, err)
assert.Equal(t, createdQueue.GetName(), gettedQueue.GetName())
}

func TestGetQueueNeverExisted(t *testing.T) {
serv, client := setUp(t)
defer tearDown(t, serv)

getQueueRequest := taskspb.GetQueueRequest{
Name: "hello_q",
}

gettedQueue, err := client.GetQueue(context.Background(), &getQueueRequest)

assert.Nil(t, gettedQueue)
st, _ := status.FromError(err)
assert.Equal(t, codes.NotFound, st.Code())
}

func TestGetQueuePreviouslyExisted(t *testing.T) {
serv, client := setUp(t)
defer tearDown(t, serv)

createdQueue := createTestQueue(t, client)

deleteQueueRequest := taskspb.DeleteQueueRequest{
Name: createdQueue.GetName(),
}

err := client.DeleteQueue(context.Background(), &deleteQueueRequest)

assert.NoError(t, err)

getQueueRequest := taskspb.GetQueueRequest{
Name: createdQueue.GetName(),
}

gettedQueue, err := client.GetQueue(context.Background(), &getQueueRequest)

assert.Nil(t, gettedQueue)
st, _ := status.FromError(err)
assert.Equal(t, codes.NotFound, st.Code())
}

func TestSuccessTaskExecution(t *testing.T) {
serv, client := setUp(t)
defer tearDown(t, serv)
Expand Down

0 comments on commit f08f159

Please sign in to comment.