~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/google.golang.org/cloud/pubsub/integration_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Google Inc. All Rights Reserved.
 
2
//
 
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
 
6
//
 
7
//      http://www.apache.org/licenses/LICENSE-2.0
 
8
//
 
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.
 
14
 
 
15
// +build integration
 
16
 
 
17
package pubsub
 
18
 
 
19
import (
 
20
        "fmt"
 
21
        "testing"
 
22
        "time"
 
23
 
 
24
        "google.golang.org/cloud/internal/testutil"
 
25
)
 
26
 
 
27
func TestAll(t *testing.T) {
 
28
        ctx := testutil.Context(ScopePubSub, ScopeCloudPlatform)
 
29
        now := time.Now()
 
30
        topic := fmt.Sprintf("topic-%d", now.Unix())
 
31
        subscription := fmt.Sprintf("subscription-%d", now.Unix())
 
32
 
 
33
        if err := CreateTopic(ctx, topic); err != nil {
 
34
                t.Errorf("CreateTopic error: %v", err)
 
35
        }
 
36
 
 
37
        if err := CreateSub(ctx, subscription, topic, time.Duration(0), ""); err != nil {
 
38
                t.Errorf("CreateSub error: %v", err)
 
39
        }
 
40
 
 
41
        exists, err := TopicExists(ctx, topic)
 
42
        if err != nil {
 
43
                t.Errorf("TopicExists error: %v", err)
 
44
        }
 
45
        if !exists {
 
46
                t.Errorf("topic %s should exist, but it doesn't", topic)
 
47
        }
 
48
 
 
49
        exists, err = SubExists(ctx, subscription)
 
50
        if err != nil {
 
51
                t.Errorf("SubExists error: %v", err)
 
52
        }
 
53
        if !exists {
 
54
                t.Errorf("subscription %s should exist, but it doesn't", subscription)
 
55
        }
 
56
 
 
57
        max := 10
 
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)
 
63
                labels["foo"] = "bar"
 
64
                msgs[i] = &Message{
 
65
                        Data:   []byte(text),
 
66
                        Labels: labels,
 
67
                }
 
68
                expectedMsgs[text] = false
 
69
        }
 
70
 
 
71
        ids, err := Publish(ctx, topic, msgs...)
 
72
        if err != nil {
 
73
                t.Errorf("Publish (1) error: %v", err)
 
74
        }
 
75
        if len(ids) != max {
 
76
                t.Errorf("unexpected number of message IDs received; %d, want %d", len(ids), max)
 
77
        }
 
78
        expectedIDs := make(map[string]bool, max)
 
79
        for _, id := range ids {
 
80
                expectedIDs[id] = false
 
81
        }
 
82
 
 
83
        received, err := PullWait(ctx, subscription, max)
 
84
        if err != nil {
 
85
                t.Errorf("PullWait error: %v", err)
 
86
        }
 
87
        if len(received) != max {
 
88
                t.Errorf("unexpected number of messages received; %d, want %d", len(received), max)
 
89
        }
 
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"])
 
95
                }
 
96
        }
 
97
        for msg, found := range expectedMsgs {
 
98
                if !found {
 
99
                        t.Errorf("message '%s' should be received", msg)
 
100
                }
 
101
        }
 
102
        for id, found := range expectedIDs {
 
103
                if !found {
 
104
                        t.Errorf("message with the message id '%s' should be received", id)
 
105
                }
 
106
        }
 
107
 
 
108
        err = DeleteSub(ctx, subscription)
 
109
        if err != nil {
 
110
                t.Errorf("DeleteSub error: %v", err)
 
111
        }
 
112
 
 
113
        err = DeleteTopic(ctx, topic)
 
114
        if err != nil {
 
115
                t.Errorf("DeleteTopic error: %v", err)
 
116
        }
 
117
}