Debian Codegraph @

pubsub/mempubsub/mem_test.go

raw
1 // Copyright 2018 The Go Cloud Development Kit Authors
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 // https://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 package mempubsub
16
17 import (
18 "context"
19 "testing"
20 "time"
21
22 "gocloud.dev/pubsub"
23 "gocloud.dev/pubsub/driver"
24 )
25
26 func TestReceive(t *testing.T) {
27 ctx := context.Background()
28 topic := &topic{}
29 sub := newSubscription(topic, 3*time.Second)
30 if err := topic.SendBatch(ctx, []*driver.Message{
31 {Body: []byte("a")},
32 {Body: []byte("b")},
33 {Body: []byte("c")},
34 }); err != nil {
35 t.Fatal(err)
36 }
37 now := time.Now()
38 // We should get only two of the published messages.
39 msgs := sub.receiveNoWait(now, 2)
40 if got, want := len(msgs), 2; got != want {
41 t.Fatalf("got %d, want %d", got, want)
42 }
43 // We should get the remaining message.
44 msgs = sub.receiveNoWait(now, 2)
45 if got, want := len(msgs), 1; got != want {
46 t.Fatalf("got %d, want %d", got, want)
47 }
48 // Since all the messages are outstanding, we shouldn't get any.
49 msgs2 := sub.receiveNoWait(now, 10)
50 if got, want := len(msgs2), 0; got != want {
51 t.Fatalf("got %d, want %d", got, want)
52 }
53 // Advance time past expiration, and we should get all the messages again,
54 // since we didn't ack any.
55 now = now.Add(time.Hour)
56 msgs = sub.receiveNoWait(now, 10)
57 if got, want := len(msgs), 3; got != want {
58 t.Fatalf("got %d, want %d", got, want)
59 }
60 // Again, since all the messages are outstanding, we shouldn't get any.
61 msgs2 = sub.receiveNoWait(now, 10)
62 if got, want := len(msgs2), 0; got != want {
63 t.Fatalf("got %d, want %d", got, want)
64 }
65 // Now ack the messages.
66 var ackIDs []driver.AckID
67 for _, m := range msgs {
68 ackIDs = append(ackIDs, m.AckID)
69 }
70 sub.SendAcks(ctx, ackIDs)
71 // They will never be delivered again, even if we wait past the ack deadline.
72 now = now.Add(time.Hour)
73 msgs = sub.receiveNoWait(now, 10)
74 if got, want := len(msgs), 0; got != want {
75 t.Fatalf("got %d, want %d", got, want)
76 }
77 }
78
79 func TestOpenTopicFromURL(t *testing.T) {
80 tests := []struct {
81 URL string
82 WantErr bool
83 }{
84 // OK.
85 {"mem://mytopic", false},
86 // Invalid parameter.
87 {"mem://mytopic?param=value", true},
88 }
89
90 ctx := context.Background()
91 for _, test := range tests {
92 topic, err := pubsub.OpenTopic(ctx, test.URL)
93 if (err != nil) != test.WantErr {
94 t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr)
95 }
96 if topic != nil {
97 topic.Shutdown(ctx)
98 }
99 }
100 }
101
102 func TestOpenSubscriptionFromURL(t *testing.T) {
103 tests := []struct {
104 URL string
105 WantErr bool
106 }{
107 // OK.
108 {"mem://mytopic", false},
109 // OK with ackdeadline
110 {"mem://mytopic?ackdeadline=30s", false},
111 // Invalid ackdeadline
112 {"mem://mytopic?ackdeadline=notaduration", true},
113 // Nonexistent topic.
114 {"mem://nonexistenttopic", true},
115 // Invalid parameter.
116 {"mem://myproject/mysub?param=value", true},
117 }
118
119 ctx := context.Background()
120 pubsub.OpenTopic(ctx, "mem://mytopic")
121 for _, test := range tests {
122 sub, err := pubsub.OpenSubscription(ctx, test.URL)
123 if (err != nil) != test.WantErr {
124 t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr)
125 }
126 if sub != nil {
127 sub.Shutdown(ctx)
128 }
129 }
130 }
131
132 func TestSendNoSubs(t *testing.T) {
133 // It's OK to send a message to a topic with no subscribers.
134 // (But it will log a warning: that is untested.)
135 ctx := context.Background()
136 topic := NewTopic()
137 defer topic.Shutdown(ctx)
138 if err := topic.Send(ctx, &pubsub.Message{Body: []byte("OK")}); err != nil {
139 t.Fatal(err)
140 }
141 }
142
143 /*
144 // This test runs forever, so it's commented out. It's useful for playing
145 // around with batching. Add a Printf in ../pubsub.go that prints the batch size.
146 func TestReceiveBatching(t *testing.T) {
147 ctx := context.Background()
148 topic := NewTopic()
149 defer topic.Shutdown(ctx)
150 sub := NewSubscriptionWithOptions(topic, 10*time.Second, &SubscriptionOptions{ReceiveBatcherOptions: batcher.Options{MaxBatchSize: 10, MaxHandlers: 100}})
151 defer sub.Shutdown(ctx)
152
153 // Receive messages as quickly as possible.
154 go func() {
155 for {
156 msg, _ := sub.Receive(ctx)
157 if msg != nil {
158 msg.Ack()
159 }
160 }
161 }()
162
163 // Send slightly slower than the 250ms Sleep.
164 for {
165 if err := topic.Send(ctx, &pubsub.Message{Body: []byte("a")}); err != nil {
166 t.Fatal(err)
167 }
168 time.Sleep(333 * time.Millisecond)
169 }
170 // With the above parameters, ideally the batch size would settle down to around 2.
171 }
172 */