77 gcPubSub "cloud.google.com/go/pubsub"
88 "cloud.google.com/go/pubsub/pstest"
99 "github.com/stretchr/testify/assert"
10+ "go.uber.org/mock/gomock"
1011 "google.golang.org/api/option"
1112 "google.golang.org/grpc"
1213 "google.golang.org/grpc/credentials/insecure"
@@ -35,10 +36,13 @@ func TestGoogleClient_New_Error(t *testing.T) {
3536 g * googleClient
3637 )
3738
39+ ctrl := gomock .NewController (t )
40+ defer ctrl .Finish ()
41+
3842 out := testutil .StderrOutputForFunc (func () {
3943 logger := testutil .NewMockLogger (testutil .ERRORLOG )
4044
41- g = New (Config {}, logger )
45+ g = New (Config {}, logger , NewMockMetrics ( ctrl ) )
4246 })
4347
4448 assert .Nil (t , g )
@@ -49,6 +53,11 @@ func TestGoogleClient_Publish_Success(t *testing.T) {
4953 client := getGoogleClient (t )
5054 defer client .Close ()
5155
56+ ctrl := gomock .NewController (t )
57+ defer ctrl .Finish ()
58+
59+ mockMetrics := NewMockMetrics (ctrl )
60+
5261 topic := "test-topic"
5362 message := []byte ("test message" )
5463 expectedLog := "published google message test message on topic test-topic\n "
@@ -61,8 +70,12 @@ func TestGoogleClient_Publish_Success(t *testing.T) {
6170 ProjectID : "test" ,
6271 SubscriptionName : "sub" ,
6372 },
73+ metrics : mockMetrics ,
6474 }
6575
76+ mockMetrics .EXPECT ().IncrementCounter (context .Background (), "app_pubsub_publish_total_count" , "topic" , topic )
77+ mockMetrics .EXPECT ().IncrementCounter (context .Background (), "app_pubsub_publish_success_count" , "topic" , topic )
78+
6679 err := g .Publish (context .Background (), topic , message )
6780
6881 assert .Nil (t , err )
@@ -72,16 +85,23 @@ func TestGoogleClient_Publish_Success(t *testing.T) {
7285}
7386
7487func TestGoogleClient_PublishTopic_Error (t * testing.T ) {
88+ ctrl := gomock .NewController (t )
89+ defer ctrl .Finish ()
90+
91+ mockMetrics := NewMockMetrics (ctrl )
92+
7593 g := & googleClient {client : getGoogleClient (t ), Config : Config {
7694 ProjectID : "test" ,
7795 SubscriptionName : "sub" ,
78- }}
96+ }, metrics : mockMetrics }
7997 defer g .client .Close ()
8098
8199 ctx , cancel := context .WithCancel (context .Background ())
82100
83101 cancel ()
84102
103+ mockMetrics .EXPECT ().IncrementCounter (ctx , "app_pubsub_publish_total_count" , "topic" , "test-topic" )
104+
85105 err := g .Publish (ctx , "test-topic" , []byte ("" ))
86106 if assert .Error (t , err ) {
87107 assert .Contains (t , err .Error (), "context canceled" )
0 commit comments