Documentation
¶
Overview ¶
Package pubsubtest demonstrates testing pubsub Publish method using interfaces. There are 2 ways to test it.
1. Define an interface that returns an interface and wrap topic with a wrapper that implements the interface. (interface.go and interface_test.go) 2. Implement synchronous version and use it for asynchronous call. (gochan.go and gochan_test.go)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DoSomethingUsingSyncPublisher ¶
func DoSomethingUsingSyncPublisher(ctx context.Context, sp SyncPublisher)
DoSomethingUsingSyncPublisher does something using synchronous publisher.
Example (RealExample) ¶
ctx := context.Background() pubsubClient, err := pubsub.NewClient(ctx, "project-id") if err != nil { log.Fatal(err) return } topic, err := pubsubClient.CreateTopic(context.Background(), "topic-name") st := SyncTopic{topic} DoSomethingUsingSyncPublisher(ctx, st)
Output:
Example (TestUsingStub) ¶
sp := stubSyncPublisher{ ServerID: "myServerID", Err: nil, Delay: 10 * time.Millisecond, } DoSomethingUsingSyncPublisher(context.Background(), sp)
Output: tick tick serverID = myServerID
Types ¶
type MyStruct ¶
type MyStruct struct {
// contains filtered or unexported fields
}
Example (RealExample) ¶
ctx := context.Background() pubsubClient, err := pubsub.NewClient(ctx, "project-id") if err != nil { log.Fatal(err) return } topic, err := pubsubClient.CreateTopic(context.Background(), "topic-name") ms := NewMyStruct(topicWrapper{topic}) ms.DoSomething(ctx)
Output:
Example (TestWithStub) ¶
ready := make(chan struct{}) m := NewMyStruct(stubPublisher{ Result: stubReadyGetter{ ServerID: "myServerID", Err: nil, ReadyC: ready, }, }) go func() { time.Sleep(10 * time.Millisecond) close(ready) }() m.DoSomething(context.Background())
Output: tick tick serverID = myServerID
func NewMyStruct ¶
func NewMyStruct(topic publisher) *MyStruct
func (MyStruct) DoSomething ¶
DoSomething actually does something using asynchronous feature of pubsub publisher.
type SyncPublisher ¶
type SyncPublisher interface {
PublishSync(ctx context.Context, msg *pubsub.Message) (serverID string, err error)
}
SyncPublisher can publish msg synchronously.
type SyncTopic ¶
SyncTopic provides an additional method PublishSync() which is a synchronous version of Publish().
func (SyncTopic) PublishSync ¶
func (st SyncTopic) PublishSync(ctx context.Context, msg *pubsub.Message) (serverID string, err error)
PublishSync publishes to the underlying topic synchronously. Google should've made Publish() synchronous like this because it's easy to call asynchronously using synchronous API and it's easier to test.