Skip to content

Commit 88eb125

Browse files
committed
Cleans up / resets the PubSub topic and subscription between test cases
1 parent 2fd7fe2 commit 88eb125

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

fleetspeak/src/server/spanner/spanner_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121
"time"
2222

23+
"cloud.google.com/go/pubsub"
2324
"cloud.google.com/go/spanner"
2425

2526
log "github.com/golang/glog"
@@ -43,6 +44,34 @@ func (e *spannerTestEnv) Create() error {
4344
func (e *spannerTestEnv) Clean() (db.Store, error) {
4445
ctx, fin := context.WithTimeout(context.Background(), 30*time.Second)
4546
defer fin()
47+
log.Info("Resetting PubSub Topic & Subscription")
48+
client, err := pubsub.NewClient(ctx, e.projectId)
49+
if err != nil {
50+
return nil, err
51+
}
52+
defer client.Close()
53+
sub := client.Subscription(e.pubsubSubscription)
54+
if sub != nil {
55+
if err = sub.Delete(ctx); err != nil {
56+
return nil, err
57+
}
58+
}
59+
top := client.Topic(e.pubsubTopic)
60+
if top != nil {
61+
if err = top.Delete(ctx); err != nil {
62+
return nil, err
63+
}
64+
}
65+
top, err = client.CreateTopic(ctx, e.pubsubTopic)
66+
if err != nil {
67+
return nil, err
68+
}
69+
sub, err = client.CreateSubscription(ctx, e.pubsubSubscription, pubsub.SubscriptionConfig{
70+
Topic: top,
71+
})
72+
if err != nil {
73+
return nil, err
74+
}
4675
log.Infof("Using database: %v", e.database)
4776
s, err := MakeDatastore(e.projectId, e.instance, e.database, e.pubsubTopic, e.pubsubSubscription)
4877
if err != nil {

0 commit comments

Comments
 (0)