Skip to content

Commit 831f82c

Browse files
authored
Close resources in GoogleAdsClientTest. (#33)
Eliminates warnings similar to the following when running unit tests. ``` SEVERE: *~*~*~ Channel io.grpc.internal.ManagedChannelImpl-53 for target googleads.googleapis.com:443 was not shutdown properly!!! ~*~*~* Make sure to call shutdown()/shutdownNow() and awaitTermination(). ```
1 parent 75881d8 commit 831f82c

1 file changed

Lines changed: 60 additions & 48 deletions

File tree

google-ads/src/test/java/com/google/ads/googleads/lib/GoogleAdsClientTest.java

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -77,123 +77,123 @@ private GoogleAdsClient createTestGoogleAdsClient() {
7777
}
7878

7979
@Test
80-
public void testGetAccountBudgetProposalServiceClient() {
81-
assertNotNull(createTestGoogleAdsClient().getAccountBudgetProposalServiceClient());
80+
public void testGetAccountBudgetProposalServiceClient() throws Exception {
81+
assertNotNullAndClose(createTestGoogleAdsClient().getAccountBudgetProposalServiceClient());
8282
}
8383

8484
@Test
85-
public void testGetAdGroupAdServiceClient() {
86-
assertNotNull(createTestGoogleAdsClient().getAdGroupAdServiceClient());
85+
public void testGetAdGroupAdServiceClient() throws Exception {
86+
assertNotNullAndClose(createTestGoogleAdsClient().getAdGroupAdServiceClient());
8787
}
8888

8989
@Test
90-
public void testGetAdGroupBidModifierServiceClient() {
91-
assertNotNull(createTestGoogleAdsClient().getAdGroupBidModifierServiceClient());
90+
public void testGetAdGroupBidModifierServiceClient() throws Exception {
91+
assertNotNullAndClose(createTestGoogleAdsClient().getAdGroupBidModifierServiceClient());
9292
}
9393

9494
@Test
95-
public void testGetAdGroupCriterionServiceClient() {
96-
assertNotNull(createTestGoogleAdsClient().getAdGroupCriterionServiceClient());
95+
public void testGetAdGroupCriterionServiceClient() throws Exception {
96+
assertNotNullAndClose(createTestGoogleAdsClient().getAdGroupCriterionServiceClient());
9797
}
9898

9999
@Test
100-
public void testGetAdGroupServiceClient() {
101-
assertNotNull(createTestGoogleAdsClient().getAdGroupServiceClient());
100+
public void testGetAdGroupServiceClient() throws Exception {
101+
assertNotNullAndClose(createTestGoogleAdsClient().getAdGroupServiceClient());
102102
}
103103

104104
@Test
105-
public void testGetBiddingStrategyServiceClient() {
106-
assertNotNull(createTestGoogleAdsClient().getBiddingStrategyServiceClient());
105+
public void testGetBiddingStrategyServiceClient() throws Exception {
106+
assertNotNullAndClose(createTestGoogleAdsClient().getBiddingStrategyServiceClient());
107107
}
108108

109109
@Test
110-
public void testGetBillingSetupServiceClient() {
111-
assertNotNull(createTestGoogleAdsClient().getBillingSetupServiceClient());
110+
public void testGetBillingSetupServiceClient() throws Exception {
111+
assertNotNullAndClose(createTestGoogleAdsClient().getBillingSetupServiceClient());
112112
}
113113

114114
@Test
115-
public void testGetCampaignBidModifierServiceClient() {
116-
assertNotNull(createTestGoogleAdsClient().getCampaignBidModifierServiceClient());
115+
public void testGetCampaignBidModifierServiceClient() throws Exception {
116+
assertNotNullAndClose(createTestGoogleAdsClient().getCampaignBidModifierServiceClient());
117117
}
118118

119119
@Test
120-
public void testGetCampaignBudgetServiceClient() {
121-
assertNotNull(createTestGoogleAdsClient().getCampaignBudgetServiceClient());
120+
public void testGetCampaignBudgetServiceClient() throws Exception {
121+
assertNotNullAndClose(createTestGoogleAdsClient().getCampaignBudgetServiceClient());
122122
}
123123

124124
@Test
125-
public void testGetCampaignCriterionServiceClient() {
126-
assertNotNull(createTestGoogleAdsClient().getCampaignCriterionServiceClient());
125+
public void testGetCampaignCriterionServiceClient() throws Exception {
126+
assertNotNullAndClose(createTestGoogleAdsClient().getCampaignCriterionServiceClient());
127127
}
128128

129129
@Test
130-
public void testGetCampaignGroupServiceClient() {
131-
assertNotNull(createTestGoogleAdsClient().getCampaignGroupServiceClient());
130+
public void testGetCampaignGroupServiceClient() throws Exception {
131+
assertNotNullAndClose(createTestGoogleAdsClient().getCampaignGroupServiceClient());
132132
}
133133

134134
@Test
135-
public void testGetCampaignServiceClient() {
136-
assertNotNull(createTestGoogleAdsClient().getCampaignServiceClient());
135+
public void testGetCampaignServiceClient() throws Exception {
136+
assertNotNullAndClose(createTestGoogleAdsClient().getCampaignServiceClient());
137137
}
138138

139139
@Test
140-
public void testGetCampaignSharedSetServiceClient() {
141-
assertNotNull(createTestGoogleAdsClient().getCampaignSharedSetServiceClient());
140+
public void testGetCampaignSharedSetServiceClient() throws Exception {
141+
assertNotNullAndClose(createTestGoogleAdsClient().getCampaignSharedSetServiceClient());
142142
}
143143

144144
@Test
145-
public void testGetChangeStatusServiceClient() {
146-
assertNotNull(createTestGoogleAdsClient().getChangeStatusServiceClient());
145+
public void testGetChangeStatusServiceClient() throws Exception {
146+
assertNotNullAndClose(createTestGoogleAdsClient().getChangeStatusServiceClient());
147147
}
148148

149149
@Test
150-
public void testGetConversionActionServiceClient() {
151-
assertNotNull(createTestGoogleAdsClient().getConversionActionServiceClient());
150+
public void testGetConversionActionServiceClient() throws Exception {
151+
assertNotNullAndClose(createTestGoogleAdsClient().getConversionActionServiceClient());
152152
}
153153

154154
@Test
155-
public void testGetCustomerServiceClient() {
156-
assertNotNull(createTestGoogleAdsClient().getCustomerServiceClient());
155+
public void testGetCustomerServiceClient() throws Exception {
156+
assertNotNullAndClose(createTestGoogleAdsClient().getCustomerServiceClient());
157157
}
158158

159159
@Test
160-
public void testGetGeoTargetConstantServiceClient() {
161-
assertNotNull(createTestGoogleAdsClient().getGeoTargetConstantServiceClient());
160+
public void testGetGeoTargetConstantServiceClient() throws Exception {
161+
assertNotNullAndClose(createTestGoogleAdsClient().getGeoTargetConstantServiceClient());
162162
}
163163

164164
@Test
165-
public void testGetGoogleAdsFieldServiceClient() {
166-
assertNotNull(createTestGoogleAdsClient().getGoogleAdsFieldServiceClient());
165+
public void testGetGoogleAdsFieldServiceClient() throws Exception {
166+
assertNotNullAndClose(createTestGoogleAdsClient().getGoogleAdsFieldServiceClient());
167167
}
168168

169169
@Test
170-
public void testGetGoogleAdsServiceClient() {
171-
assertNotNull(createTestGoogleAdsClient().getGoogleAdsServiceClient());
170+
public void testGetGoogleAdsServiceClient() throws Exception {
171+
assertNotNullAndClose(createTestGoogleAdsClient().getGoogleAdsServiceClient());
172172
}
173173

174174
@Test
175-
public void testGetKeywordViewServiceClient() {
176-
assertNotNull(createTestGoogleAdsClient().getKeywordViewServiceClient());
175+
public void testGetKeywordViewServiceClient() throws Exception {
176+
assertNotNullAndClose(createTestGoogleAdsClient().getKeywordViewServiceClient());
177177
}
178178

179179
@Test
180-
public void testGetRecommendationServiceClient() {
181-
assertNotNull(createTestGoogleAdsClient().getRecommendationServiceClient());
180+
public void testGetRecommendationServiceClient() throws Exception {
181+
assertNotNullAndClose(createTestGoogleAdsClient().getRecommendationServiceClient());
182182
}
183183

184184
@Test
185-
public void testGetSharedCriterionServiceClient() {
186-
assertNotNull(createTestGoogleAdsClient().getSharedCriterionServiceClient());
185+
public void testGetSharedCriterionServiceClient() throws Exception {
186+
assertNotNullAndClose(createTestGoogleAdsClient().getSharedCriterionServiceClient());
187187
}
188188

189189
@Test
190-
public void testGetSharedSetServiceClient() {
191-
assertNotNull(createTestGoogleAdsClient().getSharedSetServiceClient());
190+
public void testGetSharedSetServiceClient() throws Exception {
191+
assertNotNullAndClose(createTestGoogleAdsClient().getSharedSetServiceClient());
192192
}
193193

194194
@Test
195-
public void testGetVideoServiceClient() {
196-
assertNotNull(createTestGoogleAdsClient().getVideoServiceClient());
195+
public void testGetVideoServiceClient() throws Exception {
196+
assertNotNullAndClose(createTestGoogleAdsClient().getVideoServiceClient());
197197
}
198198

199199
/**
@@ -309,6 +309,18 @@ public void testBuildWithoutPropertiesFile() throws IOException {
309309
assertGoogleAdsClient(client);
310310
}
311311

312+
/**
313+
* Asserts the argument is not null and closes it in a {@code finally} block to clean up
314+
* resources. Useful for tests that instantiate closeable objects such as service clients.
315+
*/
316+
private void assertNotNullAndClose(AutoCloseable closeable) throws Exception {
317+
try {
318+
assertNotNull(closeable);
319+
} finally {
320+
closeable.close();
321+
}
322+
}
323+
312324
/** Asserts that the provided client matches expectations. */
313325
private void assertGoogleAdsClient(GoogleAdsClient client) throws IOException {
314326
assertNotNull("Null client", client);

0 commit comments

Comments
 (0)