diff --git a/gax/test/test-application/src/index.ts b/gax/test/test-application/src/index.ts index f4500170f..51bdd7a47 100644 --- a/gax/test/test-application/src/index.ts +++ b/gax/test/test-application/src/index.ts @@ -81,8 +81,6 @@ async function testShowcase() { const restClient = new EchoClient(restClientOpts); const restClientCompat = new EchoClient(restClientOptsCompat); - await testResetRetriesToZero(grpcSequenceClientWithServerStreamingRetries); - // assuming gRPC server is started locally await testEcho(grpcClient); await testEchoError(grpcClient); @@ -151,10 +149,13 @@ async function testShowcase() { ); await testErrorMaxRetries0(grpcSequenceClientWithServerStreamingRetries); + await testServerStreamingRetriesImmediatelywithRetryOptions( grpcSequenceClientWithServerStreamingRetries ); + await testResetRetriesToZero(grpcSequenceClientWithServerStreamingRetries); + // ensure legacy tests pass with streaming retries client await testEcho(grpcClientWithServerStreamingRetries); await testEchoError(grpcClientWithServerStreamingRetries); @@ -510,33 +511,28 @@ async function testServerStreamingRetryOptions(client: SequenceServiceClient) { ); const response = await client.createStreamingSequence(request); - await new Promise((resolve, reject) => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings - ); - attemptStream.on('data', (response: {content: string}) => { - finalData.push(response.content); - }); - attemptStream.on('error', (error: GoogleError) => { - // should not reach this - reject(error); - }); - attemptStream.on('end', () => { - attemptStream.end(); - resolve(); - }); - }).then(() => { + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('data', (response: {content: string}) => { + finalData.push(response.content); + }); + attemptStream.on('error', () => { + throw new Error('testServerStreamingRetryOptions problem'); + }); + attemptStream.on('end', () => { assert.equal( finalData.join(' '), 'This is testing the brand new and shiny StreamingSequence server 3' ); + attemptStream.end(); }); } @@ -571,32 +567,28 @@ async function testServerStreamingRetrieswithRetryOptions( ); const response = await client.createStreamingSequence(request); - await new Promise((resolve, reject) => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings - ); - attemptStream.on('data', (response: {content: string}) => { - finalData.push(response.content); - }); - attemptStream.on('error', error => { - reject(error); - }); - attemptStream.on('end', () => { - attemptStream.end(); - resolve(); - }); - }).then(() => { + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('data', (response: {content: string}) => { + finalData.push(response.content); + }); + attemptStream.on('error', () => { + throw new Error('testServerStreamingRetrieswithRetryOptions problem'); + }); + attemptStream.on('end', () => { assert.equal( finalData.join(' '), 'This This is This is testing the brand new and shiny StreamingSequence server 3' ); + attemptStream.end(); }); } @@ -635,32 +627,27 @@ async function testServerStreamingRetriesWithShouldRetryFn( ); const response = await client.createStreamingSequence(request); - await new Promise((resolve, reject) => { - const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; - - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings - ); - attemptStream.on('data', (response: {content: string}) => { - finalData.push(response.content); - }); - attemptStream.on('error', error => { - // should not reach this - reject(error); - }); - attemptStream.on('end', () => { - attemptStream.end(); - resolve(); - }); - }).then(() => { + const sequence = response[0]; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; + + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('data', (response: {content: string}) => { + finalData.push(response.content); + }); + attemptStream.on('error', () => { + throw new Error('testServerStreamingRetriesWithShouldRetryFn problem'); + }); + attemptStream.on('end', () => { assert.equal( finalData.join(' '), 'This This is This is testing the brand new and shiny StreamingSequence server 3' ); + attemptStream.end(); }); } @@ -696,32 +683,30 @@ async function testServerStreamingRetrieswithRetryRequestOptions( ); const response = await client.createStreamingSequence(request); - await new Promise((resolve, reject) => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('data', (response: {content: string}) => { + finalData.push(response.content); + }); + attemptStream.on('error', () => { + throw new Error( + 'testServerStreamingRetrieswithRetryRequestOptions problem' ); - attemptStream.on('data', (response: {content: string}) => { - finalData.push(response.content); - }); - attemptStream.on('error', error => { - reject(error); - }); - attemptStream.on('end', () => { - attemptStream.end(); - resolve(); - }); - }).then(() => { + }); + attemptStream.on('end', () => { assert.equal( finalData.join(' '), 'This This is This is testing the brand new and shiny StreamingSequence server 3' ); + attemptStream.end(); }); } @@ -773,32 +758,28 @@ async function testResetRetriesToZero(client: SequenceServiceClient) { 'This is testing the brand new and shiny StreamingSequence server 3' ); const response = await client.createStreamingSequence(request); - await new Promise((resolve, reject) => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings - ); - attemptStream.on('data', (response: {content: string}) => { - finalData.push(response.content); - }); - attemptStream.on('error', error => { - reject(error); - }); - attemptStream.on('end', () => { - attemptStream.end(); - resolve(); - }); - }).then(() => { + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('data', (response: {content: string}) => { + finalData.push(response.content); + }); + attemptStream.on('error', () => { + throw new Error('testResetRetriesToZero should not receive an error event'); + }); + attemptStream.on('end', () => { assert.deepStrictEqual( finalData.join(' '), 'This This is This is testing This is testing the This is testing the brand' ); + attemptStream.end(); }); } @@ -836,37 +817,33 @@ async function testShouldFailOnThirdError(client: SequenceServiceClient) { 'This is testing the brand new and shiny StreamingSequence server 3' ); const response = await client.createStreamingSequence(request); - await new Promise((resolve, reject) => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('data', () => { + throw new Error('The stream should not receive any data'); + }); + attemptStream.on('error', (error: GoogleError) => { + assert.strictEqual(error.code, 4); + assert.strictEqual( + error.message, + 'Exceeded maximum number of retries before any response was received' ); - attemptStream.on('data', () => { - reject(new GoogleError('The stream should not receive any data')); - }); - attemptStream.on('error', (error: GoogleError) => { - try { - assert.strictEqual(error.code, 4); - assert.strictEqual( - error.message, - 'Exceeded maximum number of retries before any response was received' - ); - resolve(); - } catch (assertionError: unknown) { - reject(assertionError); - } - }); - attemptStream.on('end', () => { - reject( - new GoogleError('The stream should not end before it receives an error') - ); - }); + }); + attemptStream.on('end', () => { + throw new Error( + 'testShouldFailOnThirdError finished before it received an error' + ); + }); + attemptStream.on('close', () => { + attemptStream.end(); }); } @@ -915,32 +892,30 @@ async function testServerStreamingRetrieswithRetryRequestOptionsResumptionStrate 'This is testing the brand new and shiny StreamingSequence server 3' ); const response = await client.createStreamingSequence(request); - await new Promise((resolve, reject) => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('data', (response: {content: string}) => { + finalData.push(response.content); + }); + attemptStream.on('error', () => { + throw new Error( + 'testServerStreamingRetrieswithRetryRequestOptionsResumptionStrategy problem' ); - attemptStream.on('data', (response: {content: string}) => { - finalData.push(response.content); - }); - attemptStream.on('error', error => { - reject(error); - }); - attemptStream.on('end', () => { - attemptStream.end(); - resolve(); - }); - }).then(() => { + }); + attemptStream.on('end', () => { assert.deepStrictEqual( finalData.join(' '), 'This new and new and shiny StreamingSequence server 3' ); + attemptStream.end(); }); } @@ -985,23 +960,28 @@ async function testServerStreamingRetrieswithRetryRequestOptionsErrorsOnBadResum 'This is testing the brand new and shiny StreamingSequence server 3' ); const response = await client.createStreamingSequence(request); - await new Promise(resolve => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings - ); + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); - attemptStream.on('error', (e: GoogleError) => { - assert.strictEqual(e.code, 3); - assert.match(e.note!, /not classified as transient/); - resolve(); - }); + attemptStream.on('error', (e: GoogleError) => { + assert.strictEqual(e.code, 3); + assert.match(e.note!, /not classified as transient/); + }); + attemptStream.on('close', () => { + attemptStream.end(); + }); + attemptStream.on('end', () => { + throw new Error( + 'testServerStreamingRetrieswithRetryRequestOptionsErrorsOnBadResumptionStrategy ended cleanly and did not error' + ); }); } @@ -1035,22 +1015,27 @@ async function testServerStreamingThrowsClassifiedTransientErrorNote( ); const response = await client.createStreamingSequence(request); - await new Promise(resolve => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('error', (e: GoogleError) => { + assert.strictEqual(e.code, 14); + assert.match(e.note!, /not classified as transient/); + }); + attemptStream.on('close', () => { + attemptStream.end(); + }); + attemptStream.on('end', () => { + throw new Error( + 'testServerStreamingThrowsClassifiedTransientErrorNote ended cleanly without an error' ); - attemptStream.on('error', (e: GoogleError) => { - assert.strictEqual(e.code, 14); - assert.match(e.note!, /not classified as transient/); - resolve(); - }); }); } @@ -1084,22 +1069,27 @@ async function testServerStreamingRetriesAndThrowsClassifiedTransientErrorNote( ); const response = await client.createStreamingSequence(request); - await new Promise(resolve => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('error', (e: GoogleError) => { + assert.strictEqual(e.code, 4); + assert.match(e.note!, /not classified as transient/); + }); + attemptStream.on('close', () => { + attemptStream.end(); + }); + attemptStream.on('end', () => { + throw new Error( + 'testServerStreamingRetriesAndThrowsClassifiedTransientErrorNote ended cleanly without throwing an error' ); - attemptStream.on('error', (e: GoogleError) => { - assert.strictEqual(e.code, 4); - assert.match(e.note!, /not classified as transient/); - resolve(); - }); }); } @@ -1133,25 +1123,30 @@ async function testServerStreamingThrowsCannotSetTotalTimeoutMillisMaxRetries( ); const response = await client.createStreamingSequence(request); - await new Promise(resolve => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('error', (e: GoogleError) => { + assert.strictEqual(e.code, 3); + assert.match( + e.message, + /Cannot set both totalTimeoutMillis and maxRetries/ + ); + }); + attemptStream.on('close', () => { + attemptStream.end(); + }); + attemptStream.on('end', () => { + throw new Error( + 'testServerStreamingThrowsCannotSetTotalTimeoutMillisMaxRetries ended cleanly without an error' ); - attemptStream.on('error', (e: GoogleError) => { - assert.strictEqual(e.code, 3); - assert.match( - e.message, - /Cannot set both totalTimeoutMillis and maxRetries/ - ); - resolve(); - }); }); } @@ -1194,33 +1189,29 @@ async function testErrorMaxRetries0(client: SequenceServiceClient) { 'This is testing the brand new and shiny StreamingSequence server 3' ); const response = await client.createStreamingSequence(request); - await new Promise((resolve, reject) => { - const sequence = response[0]; - - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings + const sequence = response[0]; + + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('data', () => { + throw new Error('The stream should not receive any data'); + }); + attemptStream.on('error', (error: GoogleError) => { + assert.strictEqual(error.code, 4); + assert.strictEqual(error.note, 'Max retries is set to zero.'); + }); + attemptStream.on('end', () => { + throw new Error( + 'testErrorMaxRetries0 should not end before it receives an error' ); - attemptStream.on('data', () => { - reject(new GoogleError('The stream should not receive any data')); - }); - attemptStream.on('error', (error: GoogleError) => { - try { - assert.strictEqual(error.code, 4); - assert.strictEqual(error.note, 'Max retries is set to zero.'); - resolve(); - } catch (assertionError: unknown) { - reject(assertionError); - } - }); - attemptStream.on('end', () => { - reject( - new GoogleError('The stream should not end before it receives an error') - ); - }); + }); + attemptStream.on('close', () => { + attemptStream.end(); }); } // a streaming call that retries two times and finishes successfully @@ -1256,33 +1247,30 @@ async function testServerStreamingRetriesImmediatelywithRetryOptions( ); const response = await client.createStreamingSequence(request); - await new Promise((resolve, reject) => { - const sequence = response[0]; + const sequence = response[0]; - const attemptRequest = - new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); - attemptRequest.name = sequence.name!; + const attemptRequest = + new protos.google.showcase.v1beta1.AttemptStreamingSequenceRequest(); + attemptRequest.name = sequence.name!; - const attemptStream = client.attemptStreamingSequence( - attemptRequest, - settings + const attemptStream = client.attemptStreamingSequence( + attemptRequest, + settings + ); + attemptStream.on('data', (response: {content: string}) => { + finalData.push(response.content); + }); + attemptStream.on('error', () => { + throw new Error( + 'testServerStreamingRetriesImmediatelywithRetryOptions stream had an error' ); - attemptStream.on('data', (response: {content: string}) => { - finalData.push(response.content); - }); - attemptStream.on('error', error => { - reject(error); - }); - attemptStream.on('end', () => { - attemptStream.end(); - - resolve(); - }); - }).then(() => { + }); + attemptStream.on('end', () => { assert.equal( finalData.join(' '), 'This is This is testing the brand new and shiny StreamingSequence server 3' ); + attemptStream.end(); }); }