Skip to content

Commit ba4dca6

Browse files
committed
Add some delays to prevent rate limiting
1 parent 401bb32 commit ba4dca6

File tree

2 files changed

+196
-88
lines changed

2 files changed

+196
-88
lines changed

tests/integration/catalog.serial.test.ts

Lines changed: 167 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const MAX_CATALOG_PAGE_SIZE = 100;
55
const MAX_RETRIES_CATALOG = 5;
66
const MAX_TIMEOUT = 120;
77

8+
const sleep = (ms: number) => new Promise((resolve) => {
9+
const timer = setTimeout(resolve, ms);
10+
timer.unref();
11+
});
12+
813
async function deleteAllCatalogObjects(client: SquareClient): Promise<Square.BatchDeleteCatalogObjectsResponse> {
914
const catalogObjectsResp = await client.catalog.list();
1015
const objectIds: string[] = [];
@@ -36,10 +41,31 @@ describe("Catalog API", () => {
3641
let catalogModifierListId: string;
3742
let catalogModifierId: string;
3843
let catalogTaxId: string;
39-
jest.setTimeout(120_000);
44+
45+
beforeAll(async () => {
46+
jest.setTimeout(240_000); // Set timeout for all tests
47+
}, 240_000);
48+
49+
afterAll(async () => {
50+
// Cleanup any remaining objects
51+
try {
52+
await deleteAllCatalogObjects(client);
53+
// Create a promise that resolves after cleanup delay
54+
await new Promise((resolve) => {
55+
const timer = setTimeout(() => {
56+
resolve(true);
57+
}, 2000);
58+
// Unref the timer so it doesn't keep the process alive
59+
timer.unref();
60+
});
61+
} catch (error) {
62+
console.warn('Cleanup failed:', error);
63+
}
64+
}, 240_000);
4065

4166
it("should bulk create and iterate through paginated catalog objects", async () => {
4267
await deleteAllCatalogObjects(client);
68+
await sleep(2000); // Wait after deletion
4369

4470
// Setup: Create 100 catalog objects with 1 CatalogItemVariation each
4571
const catalogObjects = Array(200)
@@ -63,6 +89,7 @@ describe("Catalog API", () => {
6389
);
6490

6591
expect(createCatalogObjectsResp.objects).toHaveLength(200);
92+
await sleep(2000); // Wait after bulk creation
6693

6794
// List all catalog objects
6895
let numberOfPages = 0;
@@ -71,77 +98,125 @@ describe("Catalog API", () => {
7198
expect(catalogObjectsResp.data).toHaveLength(MAX_CATALOG_PAGE_SIZE);
7299

73100
while (catalogObjectsResp.hasNextPage()) {
101+
await sleep(1000); // Wait between page requests
74102
catalogObjectsResp = await catalogObjectsResp.getNextPage();
75103
numberOfPages++;
76104
}
77105

78106
expect(numberOfPages).toBeGreaterThan(1);
107+
await sleep(2000); // Wait before cleanup
79108

80109
const deleteCatalogObjectsResp = await deleteAllCatalogObjects(client);
81110
expect(deleteCatalogObjectsResp.deletedObjectIds).toHaveLength(200);
82-
});
111+
}, 240_000);
83112

84113
it("should upload catalog image", async () => {
85-
// Setup: Load a test image file
86-
const imageFile = await getTestFile();
87-
88-
// Setup: Create a catalog object to associate the image with
89-
const catalogObject = createTestCatalogItem();
90-
const createCatalogResp = await client.catalog.batchUpsert(
91-
{
92-
idempotencyKey: newTestUuid(),
93-
batches: [
114+
// Add retry logic for the image upload
115+
const maxRetries = 5; // Increased from 3 to 5
116+
let lastError = null;
117+
118+
for (let attempt = 0; attempt < maxRetries; attempt++) {
119+
try {
120+
// If this isn't the first attempt, wait before retrying
121+
if (attempt > 0) {
122+
console.log(`Attempt ${attempt + 1} for image upload...`);
123+
// Increase wait time between retries exponentially
124+
await sleep(Math.pow(2, attempt) * 5000);
125+
}
126+
127+
console.log(`Starting image upload attempt ${attempt + 1}`);
128+
129+
// Setup: Load a test image file
130+
const imageFile = await getTestFile();
131+
console.log('Test file loaded');
132+
133+
// Setup: Create a catalog object to associate the image with
134+
const catalogObject = createTestCatalogItem();
135+
136+
// Add delay before catalog creation
137+
await sleep(3000);
138+
console.log('Creating catalog object...');
139+
140+
const createCatalogResp = await client.catalog.batchUpsert(
94141
{
95-
objects: [catalogObject],
142+
idempotencyKey: newTestUuid(),
143+
batches: [
144+
{
145+
objects: [catalogObject],
146+
},
147+
],
96148
},
97-
],
98-
},
99-
{
100-
maxRetries: MAX_RETRIES_CATALOG,
101-
timeoutInSeconds: MAX_TIMEOUT,
102-
},
103-
);
149+
{
150+
maxRetries: MAX_RETRIES_CATALOG,
151+
timeoutInSeconds: MAX_TIMEOUT,
152+
},
153+
);
104154

105-
expect(createCatalogResp.objects).toHaveLength(1);
106-
const createdCatalogObject = createCatalogResp.objects?.[0];
107-
expect(createdCatalogObject).toBeDefined();
155+
console.log('Catalog object created');
156+
expect(createCatalogResp.objects).toHaveLength(1);
157+
const createdCatalogObject = createCatalogResp.objects?.[0];
158+
expect(createdCatalogObject).toBeDefined();
108159

109-
// Create a new catalog image
110-
const imageName = `Test Image ${newTestUuid()}`;
111-
const createCatalogImageResp = await client.catalog.images.create(
112-
{
113-
imageFile,
114-
request: {
115-
idempotencyKey: newTestUuid(),
116-
objectId: createdCatalogObject!.id,
117-
image: {
118-
type: "IMAGE",
119-
id: newTestSquareTempId(),
120-
imageData: {
121-
name: imageName,
160+
// Add delay before image upload
161+
await sleep(5000);
162+
console.log('Uploading image...');
163+
164+
// Create a new catalog image
165+
const imageName = `Test Image ${newTestUuid()}`;
166+
const createCatalogImageResp = await client.catalog.images.create(
167+
{
168+
imageFile,
169+
request: {
170+
idempotencyKey: newTestUuid(),
171+
objectId: createdCatalogObject!.id,
172+
image: {
173+
type: "IMAGE",
174+
id: newTestSquareTempId(),
175+
imageData: {
176+
name: imageName,
177+
},
178+
},
122179
},
123180
},
124-
},
125-
},
126-
{
127-
maxRetries: MAX_RETRIES_CATALOG,
128-
timeoutInSeconds: MAX_TIMEOUT,
129-
},
130-
);
181+
{
182+
maxRetries: MAX_RETRIES_CATALOG,
183+
timeoutInSeconds: 180, // Increased timeout for image upload
184+
},
185+
);
131186

132-
expect(createCatalogImageResp.image).toBeDefined();
187+
console.log('Image uploaded successfully');
188+
expect(createCatalogImageResp.image).toBeDefined();
133189

134-
// Cleanup: Delete the created catalog object and image
135-
await client.catalog.batchDelete(
136-
{
137-
objectIds: [createdCatalogObject!.id!, createCatalogImageResp.image!.id!],
138-
},
139-
{
140-
maxRetries: MAX_RETRIES_CATALOG,
141-
timeoutInSeconds: MAX_TIMEOUT,
142-
},
143-
);
144-
});
190+
// Add delay before cleanup
191+
await sleep(3000);
192+
console.log('Starting cleanup...');
193+
194+
// Cleanup: Delete the created catalog object and image
195+
await client.catalog.batchDelete(
196+
{
197+
objectIds: [createdCatalogObject!.id!, createCatalogImageResp.image!.id!],
198+
},
199+
{
200+
maxRetries: MAX_RETRIES_CATALOG,
201+
timeoutInSeconds: MAX_TIMEOUT,
202+
},
203+
);
204+
205+
console.log('Cleanup completed');
206+
// If we get here, the test succeeded, so break out of retry loop
207+
return;
208+
209+
} catch (error) {
210+
lastError = error;
211+
console.log(`Attempt ${attempt + 1} failed with error:`, error);
212+
// If this was the last attempt, the error will be thrown below
213+
}
214+
}
215+
216+
// If we get here, all retries failed
217+
console.log('All image upload attempts failed');
218+
throw lastError;
219+
}, 240_000);
145220

146221
it("should test upsert catalog object", async () => {
147222
const coffee = createTestCatalogItem({
@@ -152,6 +227,8 @@ describe("Catalog API", () => {
152227
variationName: "Colombian Fair Trade",
153228
});
154229

230+
await sleep(2000); // Wait before upsert
231+
155232
const response = await client.catalog.object.upsert(
156233
{
157234
object: coffee,
@@ -174,30 +251,36 @@ describe("Catalog API", () => {
174251
});
175252

176253
it("should test catalog info", async () => {
254+
await sleep(2000); // Wait before info request
177255
const response = await client.catalog.info();
178256
expect(response).toBeDefined();
179-
});
257+
}, 240_000);
180258

181259
it("should test list catalog", async () => {
260+
await sleep(2000); // Wait before list request
182261
const response = await client.catalog.list();
183262
expect(response).toBeDefined();
184-
});
263+
}, 240_000);
185264

186265
it("should test search catalog objects", async () => {
266+
await sleep(2000); // Wait before search
187267
const response = await client.catalog.search({
188268
limit: 1,
189269
});
190270
expect(response).toBeDefined();
191-
});
271+
}, 240_000);
192272

193273
it("should test search catalog items", async () => {
274+
await sleep(2000); // Wait before search items
194275
const response = await client.catalog.searchItems({
195276
limit: 1,
196277
});
197278
expect(response).toBeDefined();
198-
});
279+
}, 240_000);
199280

200281
it("should test batch upsert catalog objects", async () => {
282+
await sleep(2000); // Wait before batch upsert
283+
201284
const modifier: Square.CatalogObject = {
202285
type: "MODIFIER",
203286
id: "#temp-modifier-id",
@@ -256,9 +339,11 @@ describe("Catalog API", () => {
256339
if (clientObjectId === "#temp-modifier-id") catalogModifierId = objectId!;
257340
if (clientObjectId === "#temp-modifier-list-id") catalogModifierListId = objectId!;
258341
});
259-
});
342+
}, 240_000);
260343

261344
it("should test retrieve catalog object", async () => {
345+
await sleep(2000); // Wait before test start
346+
262347
// First create a catalog object
263348
const coffee = createTestCatalogItem();
264349
const createResp = await client.catalog.object.upsert(
@@ -272,13 +357,17 @@ describe("Catalog API", () => {
272357
},
273358
);
274359

360+
await sleep(2000); // Wait before retrieve
361+
275362
// Then retrieve it
276363
const response = await client.catalog.object.get({
277364
objectId: createResp.catalogObject!.id!,
278365
});
279366
expect(response.object).toBeDefined();
280367
expect(response.object!.id).toBe(createResp.catalogObject!.id);
281368

369+
await sleep(2000); // Wait before cleanup
370+
282371
// Cleanup
283372
await client.catalog.object.delete(
284373
{
@@ -289,9 +378,11 @@ describe("Catalog API", () => {
289378
timeoutInSeconds: MAX_TIMEOUT,
290379
},
291380
);
292-
});
381+
}, 240_000);
293382

294383
it("should test batch retrieve catalog objects", async () => {
384+
await sleep(2000); // Wait before batch retrieve
385+
295386
// Use the IDs created in the batch upsert test
296387
const response = await client.catalog.batchGet({
297388
objectIds: [catalogModifierId, catalogModifierListId, catalogTaxId],
@@ -302,9 +393,11 @@ describe("Catalog API", () => {
302393
expect(response.objects!.map((obj) => obj.id)).toEqual(
303394
expect.arrayContaining([catalogModifierId, catalogModifierListId, catalogTaxId]),
304395
);
305-
});
396+
}, 240_000);
306397

307398
it("should test update item taxes", async () => {
399+
await sleep(2000); // Wait before test start
400+
308401
// First create a test item
309402
const item = createTestCatalogItem();
310403
const createResp = await client.catalog.object.upsert(
@@ -318,6 +411,8 @@ describe("Catalog API", () => {
318411
},
319412
);
320413

414+
await sleep(2000); // Wait before update
415+
321416
const response = await client.catalog.updateItemTaxes(
322417
{
323418
itemIds: [createResp.catalogObject!.id!],
@@ -331,6 +426,8 @@ describe("Catalog API", () => {
331426

332427
expect(response.updatedAt).toBeDefined();
333428

429+
await sleep(2000); // Wait before cleanup
430+
334431
// Cleanup
335432
await client.catalog.object.delete(
336433
{
@@ -341,9 +438,11 @@ describe("Catalog API", () => {
341438
timeoutInSeconds: MAX_TIMEOUT,
342439
},
343440
);
344-
});
441+
}, 240_000);
345442

346443
it("should test update item modifier lists", async () => {
444+
await sleep(2000); // Wait before test start
445+
347446
// First create a test item
348447
const item = createTestCatalogItem();
349448
const createResp = await client.catalog.object.upsert(
@@ -357,6 +456,8 @@ describe("Catalog API", () => {
357456
},
358457
);
359458

459+
await sleep(2000); // Wait before update
460+
360461
const response = await client.catalog.updateItemModifierLists(
361462
{
362463
itemIds: [createResp.catalogObject!.id!],
@@ -370,6 +471,8 @@ describe("Catalog API", () => {
370471

371472
expect(response.updatedAt).toBeDefined();
372473

474+
await sleep(2000); // Wait before cleanup
475+
373476
// Cleanup
374477
await client.catalog.object.delete(
375478
{
@@ -380,5 +483,5 @@ describe("Catalog API", () => {
380483
timeoutInSeconds: MAX_TIMEOUT,
381484
},
382485
);
383-
});
384-
});
486+
}, 240_000);
487+
});

0 commit comments

Comments
 (0)