-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
131 lines (131 loc) · 4.49 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// This package wraps the google cloud pubsub logic into two seperate part.
// A consumer package and a publisher package.
//
// The Package provides testing support, so all you have to do is focuse on the business values,
// without any fuss of emulated environment and other stuffs like that.
//
// Dependencies
//
// go get -u cloud.google.com/go/pubsub
//
// Install
//
// go get -u github.com/adamluzsi/GoogleCloudPubsub
//
// Consumer Usage
//
// First of all, you need a Handler struct that will implement the business logic for the Gcloud pubsub subscription consuming:
//
// type ExampleHandler struct {
// // you can use use fields to store messages if you want for example bulk processing
// // messages []consumer.Message
// }
//
// // HandleMessage method will be called after a message had beed fetched from the pubsub.
// // return error will Nack the message
// func (eh *ExampleHandler) HandleMessage(msg consumer.Message) error {
// // eh.messages = append(eh.messages, msg)
// // single element processing can be implemented here
// return nil
// }
//
// // Finish method will be called before all messages should be acked
// // With this method, you can do bulk actions after the HandleMessage Collected all the elements
// func (eh *ExampleHandler) Finish() error {
// defer eh.wg.Done()
// // heavy bulk actions can be implemented here
// return nil
// }
//
// // The return value should be the consumer.Handler interface,
// // not the actual struct pointer
// func NewExampleHandler() consumer.Handler {
// return &ExampleHandler{}
// }
//
//
// Now with your new fancy struct and with it's constructor function,
// you can begin to use the Consumer
//
// ctx := context.Background()
// c := consumer.New(ctx, "example-subscription-name", NewExampleHandler)
//
// If you want to specify further options for the consumer, you can do so with option setters.
//
// ctx := context.Background()
// cons := consumer.New(ctx, "example", NewExampleHandler,
// // you can configure the new consumer to use given amount of BatchSize
// // This is the amount that will be passed for the HandleMessage method for a single Handler object
// consumer.SetBatchSizeTo(amount),
//
// // This will set the Google Pubsub Message Iterators MaxExtensionDuration
// consumer.SetMaxExtensionDurationTo(10*time.Minute),
//
// // This will configure the consumer to how manny parallel worker should pull from the subscription
// consumer.SetWorkersCountTo(runtime.NumCPU()))
//
//
// Testing
//
// When You test your application, Before the Consumer is being initialized, you should turn on Mock mod.
// When Mock mod enabled, not the original but a Mock consumer will be created when the New method called.
// It's behavior is alike, but remove the Dependency to use Google Pubsub Emulated Host,
// and increase the speed for your tests.
// Make even the Benchmarking more valuable
//
// func TestConsumerMockingAllPerfect(t *testing.T) {
// consumer.TurnMockModOn()
// defer consumer.TurnMockModOff()
//
// // consumer creation is the same, and not required to be happen here,
// // this is just an example , that it should be created after the mock mod enabled
// ctx := context.Background()
// c := consumer.New(ctx, "example-subscription-name", NewExampleHandler)
// c.Start()
// defer c.Stop()
//
// // And this is how you Send Messages to the Mock Consumer
// consumer.MockMessageFeeder["example-subscription-name"] <- []byte(`Hello World!`)
//
// // super complex business logic testing here
//
// }
//
// Publisher
//
// Usage
// Now with your new fancy struct and with it's constructor function,
// you can begin to use the Consumer
//
// import "github.com/adamluzsi/GoogleCloudPubsub/publisher"
// //...
//
// ctx := context.Background()
// p := publisher.New(ctx, "my-example-topic-name")
// p.Publish([]byte(`hello world!`))
// p.Publish([]byte(`hello`), []byte(`world`))
//
// Testing
//
// Publisher also support mock mod
//
// func TestMockedPublishing(t *testing.T) {
//
// publisher.TurnMockModOn()
// defer publisher.TurnMockModOff()
// ctx := context.Background()
//
// p := publisher.New(ctx, TopicName)
// p.Publish([]byte(`hello world!`))
// p.Publish([]byte(`hello`), []byte(`world`))
//
// datas := make([][]byte, 0, 3)
//
// for i := 0; i < 3; i++ {
// data := <- publisher.MockMessageReceiver[TopicName]
// datas = append(datas, data)
// }
//
// }
//
package GoogleCloudPubsub