1
// Copyright 2014 Google Inc. All Rights Reserved.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
24
"google.golang.org/cloud/internal/testutil"
27
func TestAll(t *testing.T) {
28
ctx := testutil.Context(ScopePubSub, ScopeCloudPlatform)
30
topic := fmt.Sprintf("topic-%d", now.Unix())
31
subscription := fmt.Sprintf("subscription-%d", now.Unix())
33
if err := CreateTopic(ctx, topic); err != nil {
34
t.Errorf("CreateTopic error: %v", err)
37
if err := CreateSub(ctx, subscription, topic, time.Duration(0), ""); err != nil {
38
t.Errorf("CreateSub error: %v", err)
41
exists, err := TopicExists(ctx, topic)
43
t.Errorf("TopicExists error: %v", err)
46
t.Errorf("topic %s should exist, but it doesn't", topic)
49
exists, err = SubExists(ctx, subscription)
51
t.Errorf("SubExists error: %v", err)
54
t.Errorf("subscription %s should exist, but it doesn't", subscription)
58
msgs := make([]*Message, max)
59
expectedMsgs := make(map[string]bool, max)
60
for i := 0; i < max; i++ {
61
text := fmt.Sprintf("a message with an index %d", i)
62
labels := make(map[string]string)
68
expectedMsgs[text] = false
71
ids, err := Publish(ctx, topic, msgs...)
73
t.Errorf("Publish (1) error: %v", err)
76
t.Errorf("unexpected number of message IDs received; %d, want %d", len(ids), max)
78
expectedIDs := make(map[string]bool, max)
79
for _, id := range ids {
80
expectedIDs[id] = false
83
received, err := PullWait(ctx, subscription, max)
85
t.Errorf("PullWait error: %v", err)
87
if len(received) != max {
88
t.Errorf("unexpected number of messages received; %d, want %d", len(received), max)
90
for _, msg := range received {
91
expectedMsgs[string(msg.Data)] = true
92
expectedIDs[msg.ID] = true
93
if msg.Labels["foo"] != "bar" {
94
t.Errorf("message label foo is expected to be 'bar', found '%s'", msg.Labels["foo"])
97
for msg, found := range expectedMsgs {
99
t.Errorf("message '%s' should be received", msg)
102
for id, found := range expectedIDs {
104
t.Errorf("message with the message id '%s' should be received", id)
108
err = DeleteSub(ctx, subscription)
110
t.Errorf("DeleteSub error: %v", err)
113
err = DeleteTopic(ctx, topic)
115
t.Errorf("DeleteTopic error: %v", err)