1 package msgsvc 2 3 import ( 4 "context" 5 "time" 6 ) 7 8 // This file defines a set of interfaces that wrap the actual pub sub implementation 9 // This enables much easier unit testing of msgsvc public functions, without 10 // depending on the actual pub sub implementation 11 12 // Each type from pubsub that we require should have an associated interface 13 // defined in this file. Methods associated with these interfaces should always 14 // return an interface defined in here or a stdlib type. 15 // msgsvc.go should never directly depend on pubsub types. 16 // Once an interface is defined in this file, an associated struct should be 17 // created in pubsub.go, which wraps the associated pubsub type and implements 18 // the interface defined here. Only pubsub.go should depend on the pubsub library 19 // If access to fields on pubsub types is required, a getter/setter should be 20 // added to the interface definition here, and implemented in the wrapper struct 21 // in pubsub.go, e.g. the SetOrdering method on topicInt interface 22 // Interfaces should be named `<unexported pubsub type>Itfc`, and the associated 23 // wrapper struct should be named <unexported pubsub type>, e.g. for the pubsub 24 // type `Client` the interface should be `clientInt` and the struct should be 25 // `client` 26 27 // struct wrappers in pubsub.go should be as simple as possible as only msgsvc.go 28 // is unit tested, wrappers in pubsub.go should depend on integration testing to 29 // verify their behaviour 30 31 type publishResultItfc interface { 32 Get(ctx context.Context) (serverID string, err error) 33 } 34 35 type topicItfc interface { 36 Publish(ctx context.Context, msg messageItfc) publishResultItfc 37 SetOrdering(bool) 38 ID() string 39 Stop() 40 } 41 42 type subscriptionItfc interface { 43 Receive(context.Context, func(ctx context.Context, msg messageItfc)) error 44 Delete(context.Context) error 45 } 46 47 type subscriptionInProjecter interface { 48 SubscriptionInProject(id string, projectID string) subscriptionItfc 49 } 50 51 type topicInProjecter interface { 52 TopicInProject(id string, projectID string) topicItfc 53 } 54 55 type clientItfc interface { 56 subscriptionInProjecter 57 topicInProjecter 58 CreateSubscription(context.Context, string, subscriptionCfg) (subscriptionItfc, error) 59 } 60 61 type messageItfc interface { 62 Ack() 63 Nack() 64 ID() string 65 Data() []byte 66 Attributes() map[string]string 67 OrderingKey() string 68 SetOrderingKey(key string) 69 } 70 71 type subscriptionCfg struct { 72 topicName string 73 projectID string 74 retentionDuration time.Duration 75 expirationPolicy time.Duration 76 filter string 77 } 78