Skip to content

Commit 8b88100

Browse files
Merge pull request #1240 from commercetools/BMU-2448_fix_flaky
Fix flaky test
2 parents 0e296e0 + 835cf46 commit 8b88100

File tree

4 files changed

+149
-176
lines changed

4 files changed

+149
-176
lines changed

src/benchmark/java/com/commercetools/sync/benchmark/ProductTypeSyncBenchmark.java

Lines changed: 29 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import static com.commercetools.sync.benchmark.BenchmarkUtils.*;
44
import static com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat;
5+
import static com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts;
56
import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.ATTRIBUTE_DEFINITION_DRAFT_1;
6-
import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.deleteProductTypes;
7+
import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes;
78
import static com.commercetools.sync.integration.commons.utils.TestClientUtils.CTP_TARGET_CLIENT;
89
import static java.lang.String.format;
910
import static java.util.Collections.singletonList;
@@ -14,7 +15,6 @@
1415
import com.commercetools.api.models.product_type.ProductType;
1516
import com.commercetools.api.models.product_type.ProductTypeDraft;
1617
import com.commercetools.api.models.product_type.ProductTypeDraftBuilder;
17-
import com.commercetools.api.models.product_type.ProductTypePagedQueryResponse;
1818
import com.commercetools.api.models.product_type.ProductTypeUpdateAction;
1919
import com.commercetools.sync.commons.exceptions.SyncException;
2020
import com.commercetools.sync.commons.utils.QuadConsumer;
@@ -23,11 +23,11 @@
2323
import com.commercetools.sync.producttypes.ProductTypeSyncOptions;
2424
import com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder;
2525
import com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics;
26-
import io.vrap.rmf.base.client.ApiHttpResponse;
2726
import java.io.IOException;
2827
import java.util.ArrayList;
2928
import java.util.List;
3029
import java.util.Optional;
30+
import java.util.UUID;
3131
import java.util.concurrent.CompletableFuture;
3232
import java.util.concurrent.CompletionStage;
3333
import java.util.stream.Collectors;
@@ -50,13 +50,17 @@ class ProductTypeSyncBenchmark {
5050

5151
@AfterAll
5252
static void tearDown() {
53-
deleteProductTypes(CTP_TARGET_CLIENT);
53+
deleteAllProducts(CTP_TARGET_CLIENT);
54+
removeAttributeReferencesAndDeleteProductTypes(CTP_TARGET_CLIENT);
5455
}
5556

5657
@BeforeEach
5758
void setupTest() {
5859
clearSyncTestCollections();
59-
deleteProductTypes(CTP_TARGET_CLIENT);
60+
// Delete products first because they reference product types
61+
deleteAllProducts(CTP_TARGET_CLIENT);
62+
// Remove attribute references between product types before deleting them
63+
removeAttributeReferencesAndDeleteProductTypes(CTP_TARGET_CLIENT);
6064
productTypeSyncOptions = buildSyncOptions();
6165
}
6266

@@ -90,9 +94,12 @@ private void clearSyncTestCollections() {
9094

9195
@Test
9296
void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {
97+
// Generate unique prefix for this test run to avoid collisions
98+
final String testRunPrefix = "create_" + UUID.randomUUID().toString().substring(0, 8);
99+
93100
// preparation
94101
final List<ProductTypeDraft> productTypeDrafts =
95-
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
102+
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST, testRunPrefix);
96103
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
97104

98105
// benchmark
@@ -110,20 +117,7 @@ void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {
110117
PRODUCT_TYPE_BENCHMARKS_CREATE_ACTION_THRESHOLD))
111118
.isLessThan(PRODUCT_TYPE_BENCHMARKS_CREATE_ACTION_THRESHOLD);
112119

113-
// Assert actual state of CTP project (total number of existing product types)
114-
final Integer totalNumberOfProductTypes =
115-
CTP_TARGET_CLIENT
116-
.productTypes()
117-
.get()
118-
.execute()
119-
.thenApply(ApiHttpResponse::getBody)
120-
.thenApply(ProductTypePagedQueryResponse::getTotal)
121-
.thenApply(Long::intValue)
122-
.toCompletableFuture()
123-
.join();
124-
125-
assertThat(totalNumberOfProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);
126-
120+
// Assert sync statistics - all should be created since we use unique keys
127121
assertThat(syncStatistics)
128122
.hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, NUMBER_OF_RESOURCE_UNDER_TEST, 0, 0);
129123
assertThat(errorCallBackExceptions).isEmpty();
@@ -136,9 +130,12 @@ void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {
136130

137131
@Test
138132
void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {
133+
// Generate unique prefix for this test run to avoid collisions
134+
final String testRunPrefix = "update_" + UUID.randomUUID().toString().substring(0, 8);
135+
139136
// preparation
140137
final List<ProductTypeDraft> productTypeDrafts =
141-
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
138+
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST, testRunPrefix);
142139
// Create drafts to target project with different attribute definition name
143140
CompletableFuture.allOf(
144141
productTypeDrafts.stream()
@@ -166,35 +163,7 @@ void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {
166163
PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD))
167164
.isLessThan(PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD);
168165

169-
// Assert actual state of CTP project (number of updated product types)
170-
final Long totalNumberOfUpdatedProductTypes =
171-
CTP_TARGET_CLIENT
172-
.productTypes()
173-
.get()
174-
.withWhere("attributes(name=:name)")
175-
.withPredicateVar("name", "attr_name_1")
176-
.execute()
177-
.thenApply(ApiHttpResponse::getBody)
178-
.thenApply(ProductTypePagedQueryResponse::getTotal)
179-
.toCompletableFuture()
180-
.join();
181-
182-
assertThat(totalNumberOfUpdatedProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);
183-
184-
// Assert actual state of CTP project (total number of existing product types)
185-
final Long totalNumberOfProductTypes =
186-
CTP_TARGET_CLIENT
187-
.productTypes()
188-
.get()
189-
.execute()
190-
.thenApply(ApiHttpResponse::getBody)
191-
.thenApply(ProductTypePagedQueryResponse::getTotal)
192-
.toCompletableFuture()
193-
.join();
194-
195-
assertThat(totalNumberOfProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);
196-
197-
// Assert statistics
166+
// Assert statistics - all should be updated since we created them first with modified names
198167
assertThat(syncStatistics)
199168
.hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, 0, NUMBER_OF_RESOURCE_UNDER_TEST, 0);
200169

@@ -208,9 +177,12 @@ void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {
208177

209178
@Test
210179
void sync_WithSomeExistingProductTypes_ShouldSyncProductTypes() throws IOException {
180+
// Generate unique prefix for this test run to avoid collisions
181+
final String testRunPrefix = "mix_" + UUID.randomUUID().toString().substring(0, 8);
182+
211183
// preparation
212184
final List<ProductTypeDraft> productTypeDrafts =
213-
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
185+
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST, testRunPrefix);
214186
final int halfNumberOfDrafts = productTypeDrafts.size() / 2;
215187
final List<ProductTypeDraft> firstHalf = productTypeDrafts.subList(0, halfNumberOfDrafts);
216188

@@ -242,35 +214,7 @@ void sync_WithSomeExistingProductTypes_ShouldSyncProductTypes() throws IOExcepti
242214
PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD))
243215
.isLessThan(PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD);
244216

245-
// Assert actual state of CTP project (number of updated product types)
246-
final Long totalNumberOfProductTypesWithOldName =
247-
CTP_TARGET_CLIENT
248-
.productTypes()
249-
.get()
250-
.withWhere("attributes(name=:name)")
251-
.withPredicateVar("name", "attr_name_1_old")
252-
.execute()
253-
.thenApply(ApiHttpResponse::getBody)
254-
.thenApply(ProductTypePagedQueryResponse::getTotal)
255-
.toCompletableFuture()
256-
.join();
257-
258-
assertThat(totalNumberOfProductTypesWithOldName).isEqualTo(0);
259-
260-
// Assert actual state of CTP project (total number of existing product types)
261-
final Long totalNumberOfProductTypes =
262-
CTP_TARGET_CLIENT
263-
.productTypes()
264-
.get()
265-
.execute()
266-
.thenApply(ApiHttpResponse::getBody)
267-
.thenApply(ProductTypePagedQueryResponse::getTotal)
268-
.toCompletableFuture()
269-
.join();
270-
271-
assertThat(totalNumberOfProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);
272-
273-
// Assert statistics
217+
// Assert statistics - first half should be updated, second half created
274218
assertThat(syncStatistics)
275219
.hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, halfNumberOfDrafts, halfNumberOfDrafts, 0);
276220

@@ -283,14 +227,15 @@ void sync_WithSomeExistingProductTypes_ShouldSyncProductTypes() throws IOExcepti
283227
}
284228

285229
@Nonnull
286-
private static List<ProductTypeDraft> buildProductTypeDrafts(final int numberOfTypes) {
230+
private static List<ProductTypeDraft> buildProductTypeDrafts(
231+
final int numberOfTypes, @Nonnull final String prefix) {
287232
return IntStream.range(0, numberOfTypes)
288233
.mapToObj(
289234
i ->
290235
ProductTypeDraftBuilder.of()
291-
.key(format("key__%d", i))
292-
.name(format("name__%d", i))
293-
.description(format("description__%d", i))
236+
.key(format("%s_key_%d", prefix, i))
237+
.name(format("%s_name_%d", prefix, i))
238+
.description(format("%s_description_%d", prefix, i))
294239
.attributes(singletonList(ATTRIBUTE_DEFINITION_DRAFT_1))
295240
.build())
296241
.collect(Collectors.toList());

0 commit comments

Comments
 (0)