| 1 |
// Copyright 2019 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 pubsub_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"gocloud.dev/pubsub" |
| 24 |
"gocloud.dev/pubsub/driver" |
| 25 |
) |
| 26 |
|
| 27 |
type funcTopic struct { |
| 28 |
driver.Topic |
| 29 |
sendBatch func(ctx context.Context, ms []*driver.Message) error |
| 30 |
closed bool |
| 31 |
} |
| 32 |
|
| 33 |
func (t *funcTopic) SendBatch(ctx context.Context, ms []*driver.Message) error { |
| 34 |
return t.sendBatch(ctx, ms) |
| 35 |
} |
| 36 |
|
| 37 |
func (t *funcTopic) IsRetryable(error) bool { return false } |
| 38 |
func (t *funcTopic) Close() error { |
| 39 |
t.closed = true |
| 40 |
return nil |
| 41 |
} |
| 42 |
|
| 43 |
func TestTopicShutdownCanBeCanceledEvenWithHangingSend(t *testing.T) { |
| 44 |
dt := &funcTopic{ |
| 45 |
sendBatch: func(ctx context.Context, ms []*driver.Message) error { |
| 46 |
<-ctx.Done() |
| 47 |
return ctx.Err() |
| 48 |
}, |
| 49 |
} |
| 50 |
topic := pubsub.NewTopic(dt, nil) |
| 51 |
|
| 52 |
errs := make(chan error, 1) |
| 53 |
|
| 54 |
go func() { |
| 55 |
m := &pubsub.Message{} |
| 56 |
if err := topic.Send(context.Background(), m); err == nil { |
| 57 |
errs <- errors.New("nil err from Send, expected context cancellation error") |
| 58 |
} |
| 59 |
}() |
| 60 |
|
| 61 |
done := make(chan struct{}) |
| 62 |
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond) |
| 63 |
defer cancel() |
| 64 |
go func() { |
| 65 |
topic.Shutdown(ctx) |
| 66 |
close(done) |
| 67 |
}() |
| 68 |
|
| 69 |
// Now cancel the context being used by topic.Shutdown. |
| 70 |
cancel() |
| 71 |
|
| 72 |
// It shouldn't take too long before topic.Shutdown stops. |
| 73 |
tooLong := 5 * time.Second |
| 74 |
select { |
| 75 |
case <-done: |
| 76 |
case <-time.After(tooLong): |
| 77 |
t.Fatalf("waited too long(%v) for Shutdown(ctx) to run", tooLong) |
| 78 |
case err := <-errs: |
| 79 |
t.Fatalf("got error from goroutine: %v", err) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestTopicCloseIsCalled(t *testing.T) { |
| 84 |
ctx := context.Background() |
| 85 |
dt := &funcTopic{} |
| 86 |
topic := pubsub.NewTopic(dt, nil) |
| 87 |
topic.Shutdown(ctx) |
| 88 |
if !dt.closed { |
| 89 |
t.Error("want Topic.Close to have been called") |
| 90 |
} |
| 91 |
} |