package msgsvc import ( "context" "time" ) // This file defines a set of interfaces that wrap the actual pub sub implementation // This enables much easier unit testing of msgsvc public functions, without // depending on the actual pub sub implementation // Each type from pubsub that we require should have an associated interface // defined in this file. Methods associated with these interfaces should always // return an interface defined in here or a stdlib type. // msgsvc.go should never directly depend on pubsub types. // Once an interface is defined in this file, an associated struct should be // created in pubsub.go, which wraps the associated pubsub type and implements // the interface defined here. Only pubsub.go should depend on the pubsub library // If access to fields on pubsub types is required, a getter/setter should be // added to the interface definition here, and implemented in the wrapper struct // in pubsub.go, e.g. the SetOrdering method on topicInt interface // Interfaces should be named `Itfc`, and the associated // wrapper struct should be named , e.g. for the pubsub // type `Client` the interface should be `clientInt` and the struct should be // `client` // struct wrappers in pubsub.go should be as simple as possible as only msgsvc.go // is unit tested, wrappers in pubsub.go should depend on integration testing to // verify their behaviour type publishResultItfc interface { Get(ctx context.Context) (serverID string, err error) } type topicItfc interface { Publish(ctx context.Context, msg messageItfc) publishResultItfc SetOrdering(bool) ID() string Stop() } type subscriptionItfc interface { Receive(context.Context, func(ctx context.Context, msg messageItfc)) error Delete(context.Context) error } type subscriptionInProjecter interface { SubscriptionInProject(id string, projectID string) subscriptionItfc } type topicInProjecter interface { TopicInProject(id string, projectID string) topicItfc } type clientItfc interface { subscriptionInProjecter topicInProjecter CreateSubscription(context.Context, string, subscriptionCfg) (subscriptionItfc, error) } type messageItfc interface { Ack() Nack() ID() string Data() []byte Attributes() map[string]string OrderingKey() string SetOrderingKey(key string) } type subscriptionCfg struct { topicName string projectID string retentionDuration time.Duration expirationPolicy time.Duration filter string }